Professional Documents
Culture Documents
Exercises in LR Parsing: LALR
Exercises in LR Parsing: LALR
1. Draw the LALR(1) DFA for the following grammar: 0 IfStmt:- IfStmt 1 IfStmt :- if logexp then Block ElseForm 2 Block :- stmt 3 ElseForm :- else Block 4 ElseForm :-
2. How does the DFA above differ from the DFA produced using the LR(1) technique?
3. How does the DFA above differ from the DFA produced using the SLR(1) technique?
S1 IfStmt :- if . logexp then Block ElseForm {$} S3 IfStmt :- if logexp . then Block ElseForm {$} S4 IfStmt :- if logexp then . Block ElseForm {$} Block :- . stmt {else $} S6
Block then logexp
S2
IfStmt
Sacc
stmt
IfStmt :- if logexp then Block . ElseForm {$} ElseForm :- . else Block {$} ElseForm :- . {$} S8
ElseForm
stmt else
2. How does the DFA above differ from the DFA produced using the LR(1) technique? One less state, as state 5 and state 9 of the LR(1) DFA can merge.
3. How does the DFA above differ from the DFA produced using the SLR(1) technique? Exactly the same states and closures, but the LALR DFA shows lookahead items. 2