Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Exercises in LR Parsing: LALR(1)

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?

Solutions LALR(1) Exercises


1. Draw the LALR(1) DFA for the following grammar:

S0 IfStmt:- . IfStmt {$} IfStmt :- . if logexp then Block ElseForm {$}


if

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

IfStmt:- IfStmt . {$}


$

Sacc

stmt

S5 Block :- stmt . {else $}

IfStmt :- if logexp then Block . ElseForm {$} ElseForm :- . else Block {$} ElseForm :- . {$} S8
ElseForm

stmt else

S7 ElseForm :- else . Block {$} Block :- . stmt {$}


Block

IfStmt :- if logexp then Block ElseForm . {$}

S9 ElseForm :- else Block . {$}

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

You might also like