This is a presentation on LALR parser. This presentation was created by 6th sem CSE student.
LALR parser is basically used to creating the LR parsing table. LALR parser is used because it is more powerful than SLR and the tables generated by LALR consumes less memory and disk space than CLR parser.
2. Table Of Content
o Introduction To LALR Parser
o LALR Table Construction Method
o Examples – Related To Grammer, First, CLR, etc.
o Computed LR( 0 ) Items
o GOTO Graph
o Canonical Parsing Table
o LALR Parsing Table
2
3. What is LALR Parser
o LALR stands for (look ahead LR) parser.
o LALR parser starts with the idea of building an LR parsing table
o Tables generated by LALR parser are smaller in size as compared to
that of Canonical LR ( CLR ) and Simple LR ( SLR ) techniques.
o LALR parsers are slightly less powerful than LR parsers, but still more
powerful than SLR parsers.
o LALR is used by YACC and other parser generators because of its
effectiveness.
3
4. LALR TableConstruction Idea
o Construct the set of LR (1) items.
o Merge the sets with common core together in CLR table.
o If any problem arises then grammar is not LALR.
4
5. Example
Grammar:
1. S’ -> S
2. S -> CC
3. C -> cC
4. C -> d
5
First : Where F Means First
F( S’ ) -> F( S ) -> F( C ) -> { c,d }
F ( C ) -> { c, d }
-> c/d Look Ahead Symbol
Note :: CC are two different items.
Note :: Non-terminals denoted by upper-case letters, terminals denoted by
lower-case letters
S’ -> .S,$ :: This (a rule with a dot in it) is called an item, it indicates what is in
the stack [ left side of . ] and what is to be expected on input [ right side of . ]
Anything after ( , ) comma is termed as look ahead.
6. Computed LR( 0 )& LR( 1 )Items6
I5: S -> CC., $
I6: C -> c.C, $
C -> .cC, $
C -> .d, $
I7: C -> d., $
I8: C -> cC., c /d
I9: C -> cC., $
I0 : S’ -> .S, $
S -> .CC, $
C -> .c C, c /d
C -> .d, c /d
I1: S’ -> S., $
I2: S -> C.C, $
C -> .c C, $
C -> .d, $
I3: C -> c. C, c /d
C -> .Cc, c /d
C -> .d, c /d
14: C -> d., c / d
Grammar:
1. S’ -> S
2. S -> CC
3. C -> cC
4. C -> d
Grammar:
1. S’ -> S
2. S -> CC
3. C -> cC/d
First ( c/d )
Note : I0 consist of LR ( 0 ).
Items while rest are LR ( 1 )
items.
Its always suggested not
to work beyond State 2 by
hand as they are
compicated and should be
calculated using standard
tools.
8. 8 Canonical ParsingTable
States
c d $ S C
0 S3 S4 1 2
1 acc
2 S6 S7 5
3 S3 S4 8
4 R4 R4
5 R2
6 S6 S7 9
7 R4
8 R3 R3
9 R3
Actions GOTO
9. 9 LALR Parser
Actions GOTO
Merge the Cores:
What is core ?
A core is a set of LR (0) (SLR) items for the grammar.
o 3 & 6
o 4 & 7
o 8 & 9
10. 10 LALR Parsing Table
Actions
Start
c d $ S C
0 S36 S47 1 2
1 acc
2 S36 S47 5
36 S36 S47 89
47 R47 R47 R47
5 R2
89 R36 R36 R36
GOTOAction
11. Conflicts In LALR Parser
o LALR Parser cannot introduce shift/reduce conflicts.
o Such conflicts arises when the look ahead is same as the token on which
we can shift.
o They depend on the core of the item but we merge only those rows
which have common cores.
o The only way by which this conflict can arise in LALR is when the
conflict is already their in the LR(1).
11
12. 11 Refrence
Actions
GOTOActionUsed to generate First of Given Grammer
http://hackingoff.com/compilers/predict-first-follow-set
IITKGP notes for making presentation
http://www.facweb.iitkgp.ernet.in/~niloy/COURSE/
Compilers Principles Techniques and Tools (2nd Edition) - BOOK