I am trying to understand what is meant by "deterministic" in expressions such as "deterministic context-free grammar". (There are more deterministic "things" in this field). I would appreciate an example more then the most elaborate explanation! If possible.
My primary source of confusion is from not being able to tell how this property of a grammar is different from (non-)ambiguity.
The closest I got to finding what it means is this quote from the paper by D. Knuth On the Translation of Languages from Left to Right:
Ginsburg and Greibach (1965) have defined the notion of a deterministic language; we show in Section V that these are precisely the languages for which there exists an L R ( k ) grammar
which becomes circular as soon you get to the Section V, because there it says that what LR(k) parser can parse is the deterministic language...
Below is an example that I could find to help me understand what "ambigous" means, please take a look:
onewartwoearewe
Which can be parsed as one war two ear ewe or o new art woe are we - if a grammar allows that (say it has all the words I just listed).
What would I need to do to make this example language (non-)deterministic? (I could, for example, remove the word o from the grammar, to make the grammar not ambiguous).
Is the above language deterministic?
PS. The example is from the book Godel, Esher, Bach: Eternal Golden Braid.
Let's say, we define the grammar for the example language like so:
S -> A 'we' | A 'ewe'
A -> B | BA
B -> 'o' | 'new' | 'art' | 'woe' | 'are' | 'one' | 'war' | 'two' | 'ear'
By the argument about having to parse the whole string, does this grammar make the language non-deterministic?
let explode s =
let rec exp i l =
if i < 0 then l else exp (i - 1) (s.[i] :: l) in
exp (String.length s - 1) [];;
let rec woe_parser s =
match s with
| 'w' :: 'e' :: [] -> true
| 'e' :: 'w' :: 'e' :: [] -> true
| 'o' :: x -> woe_parser x
| 'n' :: 'e' :: 'w' :: x -> woe_parser x
| 'a' :: 'r' :: 't' :: x -> woe_parser x
| 'w' :: 'o' :: 'e' :: x -> woe_parser x
| 'a' :: 'r' :: 'e' :: x -> woe_parser x
(* this line will trigger an error, because it creates
ambiguous grammar *)
| 'o' :: 'n' :: 'e' :: x -> woe_parser x
| 'w' :: 'a' :: 'r' :: x -> woe_parser x
| 't' :: 'w' :: 'o' :: x -> woe_parser x
| 'e' :: 'a' :: 'r' :: x -> woe_parser x
| _ -> false;;
woe_parser (explode "onewartwoearewe");;
- : bool = true
| Label | Pattern |
|---------+--------------|
| rule-01 | S -> A 'we' |
| rule-02 | S -> A 'ewe' |
| rule-03 | A -> B |
| rule-04 | A -> BA |
| rule-05 | B -> 'o' |
| rule-06 | B -> 'new' |
| rule-07 | B -> 'art' |
| rule-08 | B -> 'woe' |
| rule-09 | B -> 'are' |
| rule-10 | B -> 'one' |
| rule-11 | B -> 'war' |
| rule-12 | B -> 'two' |
| rule-13 | B -> 'ear' |
#+TBLFM: @2$1..@>$1='(format "rule-%02d" (1- @#));L
Generating =onewartwoearewe=
First way to generate:
| Input | Rule | Product |
|-------------------+---------+-------------------|
| '' | rule-01 | A'we' |
| A'we' | rule-04 | BA'we' |
| BA'we' | rule-05 | 'o'A'we' |
| 'o'A'we' | rule-04 | 'o'BA'we' |
| 'o'BA'we' | rule-06 | 'onew'A'we' |
| 'onew'A'we' | rule-04 | 'onew'BA'we' |
| 'onew'BA'we' | rule-07 | 'onewart'A'we' |
| 'onewart'A'we' | rule-04 | 'onewart'BA'we' |
| 'onewart'BA'we' | rule-08 | 'onewartwoe'A'we' |
| 'onewartwoe'A'we' | rule-03 | 'onewartwoe'B'we' |
| 'onewartwoe'B'we' | rule-09 | 'onewartwoearewe' |
|-------------------+---------+-------------------|
| | | 'onewartwoearewe' |
Second way to generate:
| Input | Rule | Product |
|-------------------+---------+-------------------|
| '' | rule-02 | A'ewe' |
| A'ewe' | rule-04 | BA'ewe' |
| BA'ewe' | rule-10 | 'one'A'ewe' |
| 'one'A'ewe' | rule-04 | 'one'BA'ewe' |
| 'one'BA'ewe' | rule-11 | 'onewar'A'ewe' |
| 'onewar'A'ewe' | rule-04 | 'onewar'BA'ewe' |
| 'onewar'BA'ewe' | rule-12 | 'onewartwo'A'ewe' |
| 'onewartwo'A'ewe' | rule-03 | 'onewartwo'B'ewe' |
| 'onewartwo'B'ewe' | rule-13 | 'onewartwoearewe' |
|-------------------+---------+-------------------|
| | | 'onewartwoearewe' |