(One intermediate revision by the same user not shown) | |||
Line 18: | Line 18: | ||
2. The following two trees are possible for '''true and false and true''' (an analogous analysis could be done for '''or'''). The fact that more than one tree can be found (independently of the method used), is a signal that the grammar is ambiguous. In the case shown, the ambiguity manifests itself in the associativity of the '''and''' operator (similar finding can be shown for '''or''' -- this is left as an exercise). In the trees, terminal symbols (a.k.a. tokens) are in blue and non-terminal symbols are in black. | 2. The following two trees are possible for '''true and false and true''' (an analogous analysis could be done for '''or'''). The fact that more than one tree can be found (independently of the method used), is a signal that the grammar is ambiguous. In the case shown, the ambiguity manifests itself in the associativity of the '''and''' operator (similar finding can be shown for '''or''' -- this is left as an exercise). In the trees, terminal symbols (a.k.a. tokens) are in blue and non-terminal symbols are in black. | ||
− | < | + | <dot-hack> |
graph nfa { | graph nfa { | ||
node [shape=plaintext] | node [shape=plaintext] | ||
Line 72: | Line 72: | ||
26 -- 31 -- 32 -- 33 | 26 -- 31 -- 32 -- 33 | ||
} | } | ||
− | </ | + | </dot-hack> |
Line 83: | Line 83: | ||
4. The following analysis considers the grammar defined in 3 (again, terminals are in blue and non-terminals in black). | 4. The following analysis considers the grammar defined in 3 (again, terminals are in blue and non-terminals in black). | ||
− | < | + | <dot-hack> |
graph nfa { | graph nfa { | ||
node [shape=plaintext] | node [shape=plaintext] | ||
Line 125: | Line 125: | ||
4 -- 19 | 4 -- 19 | ||
} | } | ||
− | </ | + | </dot-hack> |
− | [[category: | + | [[category:Compiladores]] |
− | [[category: | + | [[category:Ensino]] |
Consider the following grammar:
E -> E or E | T T -> T and T | F F -> not F | ( E ) | true | false
1. The terminal symbols are all the symbols not defined by a rule: or, and, not, (, ), true, false.
2. The following two trees are possible for true and false and true (an analogous analysis could be done for or). The fact that more than one tree can be found (independently of the method used), is a signal that the grammar is ambiguous. In the case shown, the ambiguity manifests itself in the associativity of the and operator (similar finding can be shown for or -- this is left as an exercise). In the trees, terminal symbols (a.k.a. tokens) are in blue and non-terminal symbols are in black.
3. To solve the problem found before, a grammar can be written that only accepts one direction for the associativity of the two binary operators present. The following rewritten grammar selects left associativity for the binary operators (and and or):
E -> E or T | T T -> T and F | F F -> not F | ( E ) | true | false
4. The following analysis considers the grammar defined in 3 (again, terminals are in blue and non-terminals in black).