Difference between revisions of "Introduction to Syntax/Exercise 1"

From Wiki**3

< Introduction to Syntax
(Solution)
(Solution)
Line 16: Line 16:
 
1. The terminal symbols are all the symbols not defined by a rule: '''<tt>or</tt>''', '''<tt>and</tt>''', '''<tt>not</tt>''', '''<tt>(</tt>''', '''<tt>)</tt>''', '''<tt>true</tt>''', '''<tt>false</tt>'''.
 
1. The terminal symbols are all the symbols not defined by a rule: '''<tt>or</tt>''', '''<tt>and</tt>''', '''<tt>not</tt>''', '''<tt>(</tt>''', '''<tt>)</tt>''', '''<tt>true</tt>''', '''<tt>false</tt>'''.
  
2. The following trees are possible for '''true and false and true''' (an analogous analysis could be done for '''or'''). Terminals are in blue and non-terminals 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.
  
 
<graph>
 
<graph>
Line 75: Line 75:
  
  
3. The following grammar selects left associativity for the binary operators ('''and''' and '''or'''):
+
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'''):
  
 
  bexpr  -> bexpr or bterm | bterm
 
  bexpr  -> bexpr or bterm | bterm

Revision as of 15:37, 3 April 2010

The Problem

Consider the following grammar:

 bexpr   -> bexpr or bexpr | bterm
 bterm   -> bterm and bterm | bfactor
 bfactor -> not bfactor | ( bexpr ) | true | false
  1. Identify the terminal and non-terminal symbols of the grammar.
  2. Show that the grammar is ambiguous by deriving two different trees for the same input sequence.
  3. Write a non-ambiguous grammar for the same language.
  4. Build the tree corresponding to the analysis of the following input sequence: not ( true or false and true )

Solution

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):

bexpr   -> bexpr or bterm | bterm
bterm   -> bterm and bfactor | bfactor
bfactor -> not bfactor | ( bexpr ) | true | false

4. The following analysis considers the grammar defined in 3 (again, terminals are in blue and non-terminals in black).