LALR Parsers
LALR Parsers
Bottom-up Parsing
They operate by building up the structure of the input from the bottom (tokens) to
the top (the start symbol).
Powerful
LALR parsers can handle a wide range of context-free grammars, making them
versatile for various programming language parsing needs.
Parser Generator
LALR parsers are typically generated using LALR parser generators, which take a
context-free grammar as input and produce the parser.
Trade-offs
While powerful, LALR parsers can introduce conflicts (shift-reduce, reduce-reduce)
in some cases, which may require grammar modifications to resolve.
1
LALR parsers essentially combine states of a more powerful LR(1) parser that have
the same core items (productions and their positions). This reduces the number of
states while preserving much of the LR(1) parsing power.
Parsing source code: LALR parsers are used to parse the source code of
programming languages.
Generating error messages: They help identify and report syntax errors during
parsing.
Code optimization: LALR parsers can be used in conjunction with other
compiler stages to optimize code.
Flexibility: They offer flexibility in handling various grammar structures
LALR PARSER
ALGORITHM FOR EASY CONSTRUCTION OF AN LALR TABLE
Input: G'
Output: LALR parsing table functions with action and goto for G'
Method:
1. Construct C = {I0, I1, ..., In} the collection of sets of LR (1) items for G'.
2. For each core present among the set of LR (1) items, find all sets having that core
and replace these sets by the union.
3. Let C' = {J0, J1, ..., Jm} be the resulting sets of LR (1) items. The parsing actions
for state i are constructed from Ji in the same manner as in the construction of the
canonical LR parsing table.
4. If there is a conflict, the grammar is not LALR (1) and the algorithm fails.
5. The goto table is constructed as follows: If J is the union of one or more sets of LR
(1) items, that is, J = I0U I1 U ... U Ik, then the cores of goto (I0, X), goto (I1, X), ...,
goto (Ik, X) are the same, since I0, I1 , ..., Ik all have the same core. Let K be the
union of all sets of items having the same core as goto (I1, X).
6. Then goto (J, X) = K.
Example:-
2
Grammar: Input String: aba
1. S′→S
2. S→AA
3. A→aA
4. A→b
Step 1: Construct the Augmented Grammar (already done above). We add a new
start symbol S′ and a production S′→S to indicate when the parsing should accept the
input.
Step 2: Find the LR(0) Items. LR(0) items represent the possible states of the parser
as it processes the input. A "." indicates the current parsing position.
I0
[S′→⋅S]
[S→⋅AA]
[A→⋅aA]
[A→⋅b]
I1
I2
[S→A⋅A]
[A→⋅aA]
[A→⋅b]
I3
[A→⋅aA]
[A→⋅b]
I4
I5
I6
3
[A→aA⋅] (Reduce by rule 3: A→aA)
Step 3: Construct the GOTO Function. The GOTO function defines the transitions
between the LR(0) item sets based on the input symbol.
GOTO(I0,S)=I1
GOTO(I0,A)=I2
GOTO(I0,a)=I3
GOTO(I0,b)=I4
GOTO(I2,A)=I5
GOTO(I2,a)=I3
GOTO(I2,b)=I4
GOTO(I3,a)=I3
GOTO(I3,b)=I4
GOTO(I3,A)=I6
Step 5: Construct the LALR(1) Parsing Table. The parsing table has two parts:
ACTION and GOTO.
ACTION[state, terminal]: Specifies what action the parser should take given the current
state and the next input terminal. Actions can be:
o Shift (s):* Shift the terminal onto the stack and move to state *.
o Reduce (r):* Reduce the top of the stack using grammar rule *.
o Accept (acc): Parsing is successful.
o Error: Syntax error.
GOTO[state, non-terminal]: Specifies the next state if the parser has just reduced a non-
terminal.
State a b $ S A
0 s3 s4 1 2
1 acc
2 s3 s4 5
3 s3 s4 6
4 r4 r4 r4
5 r2
6 r3 r3 r3
4
Stack Input Action
$0 $aba\$ $ Shift 'a', go to state 3
$0a3 $ba\$ $ Shift 'b', go to state 4
$0a3b4 $a\$ $ Reduce by A→b (r4), GOTO(3, A) = 6
$0a3A6 $a\$ $ Reduce by A→aA (r3), GOTO(0, A) = 2
$0A2 $a\$ $ Shift 'a', go to state 3
$0A2a3 $ Shift '$', go to state 4
$0A2a3b4 $ Reduce by A→b (r4), GOTO(3, A) = 6
$0A2a3A6 $ Reduce by A→aA (r3), GOTO(0, A) = 2
$0A2A5 $ Reduce by S→AA (r2), GOTO(0, S) = 1
$0S1 $ Accept
Therefore, the input string "aba" is successfully parsed by the LALR(1) parser.