Syntax Analysis: EECS 483 - Lecture 4 University of Michigan Monday, September 17, 2006
Syntax Analysis: EECS 483 - Lecture 4 University of Michigan Monday, September 17, 2006
Announcements/Reading
Reading - Ch 4
4.1 4.3 for today
-2-
if
==
if
==
b
=
0
a
-3-
Parsing Analogy
Syntax analysis for natural languages
Recognize whether a sentence is grammatically correct
Identify the function of each word
sentence
subject
verb
indirect object
gave
him
object
noun phrase
article
noun
the
book
...
Context-Free Grammars
Consist of 4 components:
SaSa
ST
TbTb
T
CFG - Example
1 non-terminal: S
2 terminals: (, )
Start symbol: S
2 productions
More on CFGs
A Parser
Context free
grammar, G
Parser
Token stream, s
(from lexer)
Yes, if s in L(G)
No, otherwise
Error messages
- 10 -
RE is a Subset of CFG
Can inductively build a grammar for each RE
a
R1 R2
R1 | R2
R1*
S
Sa
S S1 S2
S S1 | S2
S S1 S |
Where
G1 = grammar for R1, with start symbol S1
G2 = grammar for R2, with start symbol S2
- 11 -
Grammar
SE+S|E
E number | (S)
Expanded
SE+S
SE
E number
E (S)
4 productions
2 non-terminals (S,E)
4 terminals: (, ), +, number
start symbol: S
- 12 -
Constructing a Derivation
Example
SE+S
(S + E) + E (E + S + E) + E
- 13 -
Class Problem
SE+S|E
E number | (S)
Derive: (1 + 2 + (3 + 4)) + 5
- 14 -
Parse Tree
S
E
( S )
E + S
1 E + S
2
( S )
E + S
3
E
4
- 15 -
( S )
E + S
+
+
1 E + S
2
+
2
+
3
( S )
E + S
3
E
Derivation Order
Rightmost derivation
Same, but find rightmost non-terminal
E+SE+E+S
- 17 -
derive: (1 + 2 + (3 + 4)) + 5
S E + S (S)+S (E+S) + S (1+S)+S (1+E+S)+S
(1+2+S)+S (1+2+E)+S (1+2+(S))+S (1+2+(E+S))+S
(1+2+(3+S))+S (1+2+(3+E))+S (1+2+(3+4))+S
(1+2+(3+4))+E (1+2+(3+4))+5
Now, rightmost derive the same input string
S E+S E+E E+5 (S)+5 (E+S)+5 (E+E+S)+5
(E+E+E)+5 (E+E+(S))+5 (E+E+(E+S))+5
(E+E+(E+E))+5 (E+E+(E+4))+5 (E+E+(3+4))+5
(E+2+(3+4))+5 (1+2+(3+4))+5
Result: Same parse tree: same productions chosen, but in diff order
- 18 -
Class Problem
SE+S|E
E number | (S) | -S
- 19 -
Ambiguous Grammars
(1+2+(3+4))+5
+
2
+
3
- 20 -
An Ambiguous Grammar
Derivation 1: S S+S
1+S 1+S*S 1+2*S
1+2*3
2 leftmost derivations
*
+
1
*
2
1
But, obviously not equal!
- 22 -
3
2
Impact of Ambiguity
+
1
*
2
3
2
=9
=7
- 23 -
Eliminating Ambiguity
S + T
T
T * 3
Precedence enforced by
Introduce distinct non-terminals for each
precedence level
Operators for a given precedence level are
specified as RHS for the production
Higher precedence operators are accessed by
referencing the next-higher precedence nonterminal
- 26 -
Associativity
a + b + c = (a + b) + c
a ^ b ^ c = a ^ (b ^ c)
a < b < c is illegal (thus undefined)
Class Problem
S S + S | S S | S * S | S / S | (S) | -S | S ^ S | num
Enforce the standard arithmetic precedence rules and remove
all ambiguity from the above grammar
Precedence (high to low)
(), unary
^
*, /
+, Associativity
^ = right
rest are left
- 28 -