Académique Documents
Professionnel Documents
Culture Documents
Example 1
Stmt ::= 1 if expr then Stmt else Stmt | 2 while Expr do Stmt | 3 begin Stmts end ::= 6 id
do begin end id ; $
1 4
2 4
3 4 5 6
Parser Example
!
Following slides trace execution of the parser (slide 5) on a token string according to the grammar from slide 4 and the corresponding parse tree Snapshots show parser state at the top of the while loop and just before the if statement at each iteration, together with a summary of the action taken in the if Notice how the leaves on the right side of the parse tree correspond to the stack contents
6
Stack:
Action: Match
Stack:
while
Action: 6 E
::= id
while
while
Action: Match
Mid loop S X: a: E do id S id id
Stack:
while
while id
Action: Match
Mid loop S X: a: E do S do do
Stack:
while id
while id do
Stack:
while id do
while id do
Action: Match
Stack:
while id do
while id do begin
Action: 4 Ss ::= S ; Ss
Stack:
while id do begin
Stack:
while id do begin
begin end
end
Action: 3 S
S begin
Stack:
while id do begin
Action: Match
begin begin
Stack:
Stack:
Ss end
Stack:
Stack:
Action: Match
end end
Stack:
Top of loop S X: a: E S Ss S Ss while id do begin begin " end ; end $ ; end Ss ; Ss end $
31
Stack:
Action: Match
Mid loop S X: a: E S Ss S Ss while id do begin begin " end ; end $ ; end Ss Ss end $
32
; ;
Stack:
Top of loop S X: a: E S Ss S Ss while id do begin begin " end ; end $ end Ss Ss end $
33
Stack:
Mid loop S X: a: E S Ss S Ss while id do begin begin " end ; end $ end Ss end $
34
Ss end
Stack:
Top of loop S X: a: E S Ss S Ss while id do begin begin " end ; " end $ end Ss end $
35
Stack:
Action: Match
end end
Stack: end Ss
Stack:
Ss
Action: Match
Stack:
38
Top of loop S
T P E C C A
a: E S Ss S Ss Ss
X:
Stack:
39