SlideShare a Scribd company logo
CS143 Handout 11 
Summer 2008 July 09, 2008 
LALR Parsing 
Handout written by Maggie Johnson and revised by Julie Zelenski. 
Motivation 
Because a canonical LR(1) parser splits states based on differing lookahead sets, it can 
have many more states than the corresponding SLR(1) or LR(0) parser. Potentially it 
could require splitting a state with just one item into a different state for each subset of 
the possible lookaheads; in a pathological case, this means the entire power set of its 
follow set (which theoretically could contain all terminals—yikes!). It never actually 
gets that bad in practice, but a canonical LR(1) parser for a programming language 
might have an order of magnitude more states than an SLR(1) parser. Is there 
something in between? 
With LALR (lookahead LR) parsing, we attempt to reduce the number of states in an 
LR(1) parser by merging similar states. This reduces the number of states to the same as 
SLR(1), but still retains some of the power of the LR(1) lookaheads. Let’s examine the 
LR(1) configurating sets from an example given in the LR parsing handout. 
S' –> S 
S –> XX 
X –> aX 
X –> b 
I0: S' –> •S, $ 
S –> •XX, $ 
X –> •aX, a/b 
X –> •b, a/b 
I1: S' –> S•, $ 
I2: S –> X•X, $ 
X –> •aX, $ 
X –> •b, $ 
I3: X –> a•X, a/b 
X –> •aX, a/b 
X –> •b, a/b 
I4: X –> b•, a/b 
I5: S –> XX•, $ 
I6: X –> a•X, $ 
X –> •aX, $ 
X –> •b, $ 
I7: X –> b•, $ 
I8: X –> aX•, a/b 
I9: X –> aX•, $ 
Notice that some of the LR(1) states look suspiciously similar. Take I3 and I6 for 
example. These two states are virtually identical—they have the same number of items, 
the core of each item is identical, and they differ only in their lookahead sets. This 
observation may make you wonder if it possible to merge them into one state. The 
same is true of I4 and I7, and I8 and I9. If we did merge, we would end up replacing those 
six states with just these three:
2 
I36: X –> a•X, a/b/$ 
X –> •aX, a/b/$ 
X –> •b, a/b/$ 
I47: X –> b•, a/b/$ 
I89: X –> aX•, a/b/$ 
But isn’t this just SLR(1) all over again? In the above example, yes, since after the 
merging we coincidentally end up with the complete follow sets as the lookahead. This 
is not always the case however. Consider this example: 
S' –> S 
S –> Bbb | aab | bBa 
B –> a 
I0: S' –> •S, $ 
S –> •Bbb, $ 
S –> •aab, $ 
S –> •bBa, $ 
B –> •a, b 
I1: S' –> S•, $ 
I2: S –> B•bb, $ 
I3: S –> a•ab, $ 
B –> a•, b 
.... 
In an SLR(1) parser there is a shift-reduce conflict in state 3 when the next input is 
anything in Follow(B)which includes a and b. In LALR(1), state 3 will shift on a and 
reduce on b. Intuitively, this is because the LALR(1) state "remembers" that we arrived 
at state 3 after seeing an a. Thus we are trying to parse either Bbb or aab. In order for 
that first a to be a valid reduction to B, the next input has to be exactly b since that is the 
only symbol that can follow B in this particular context. Although elsewhere an 
expansion of B can be followed by an a, we consider only the subset of the follow set 
that can appear here, and thus avoid the conflict an SLR(1) parser would have. 
LALR Merge Conflicts 
Can merging states in this way ever introduce new conflicts? A shift-reduce conflict 
cannot exist in a merged set unless the conflict was already present in one of the 
original LR(1) configurating sets. When merging, the two sets must have the same core 
items. If the merged set has a configuration that shifts on a and another that reduces on 
a, both configurations must have been present in the original sets, and at least one of 
those sets had a conflict already. 
Reduce-reduce conflicts, however, are another story. Consider the following grammar: 
S' –> S 
S –> aBc | bCc | aCd | bBd 
B –> e 
C –> e
3 
The LR(1) configurating sets are as follows: 
I0: S' –> •S, $ 
S –> •aBc, $ 
S –> •bCc, $ 
S –> •aCd, $ 
S –> •bBd, $ 
I1: S' –> S•, $ 
I2: S –> a•Bc, $ 
S –> a•Cd, $ 
B –> •e, c 
C –> •e, d 
I3: S –> b•Cc, $ 
S –> b•Bd, $ 
C –> •e, c 
B –> •e, d 
I4: S –> aB•c, $ 
I5: S –> aC•d, $ 
I6: B –> e•, c 
C –> e•, d 
I7: S –> bC•c, $ 
I8: S –> bB•d, $ 
I9: B –> e•, d 
C –> e•, c 
I10: S –> aBc•, $ 
I11: S –> aCd•, $ 
I12: S –> bCc•, $ 
I13: S –> bBd•, $ 
We try to merge I6 and I9 since they have the same core items and they only differ in 
lookahead: 
I69: C –> e, c/d 
B –> e, d/c 
However, this creates a problem. The merged configurating set allows a reduction to 
either B or C when next token is c or d. This is a reduce-reduce conflict and can be an 
unintended consequence of merging LR(1) states. When such a conflict arises in doing a 
merging, we say the grammar is not LALR(1).
4 
LALR Table Construction 
A LALR(1) parsing table is built from the configurating sets in the same way as 
canonical LR(1); the lookaheads determine where to place reduce actions. In fact, if 
there are no mergable states in the configuring sets, the LALR(1) table will be identical 
to the corresponding LR(1) table and we gain nothing. 
In the common case, however, there will be states that can be merged and the LALR 
table will have fewer rows than LR. The LR table for a typical programming language 
may have several thousand rows, which can be merged into just a few hundred for 
LALR. Due to merging, the LALR(1) table seems more similar to the SLR(1) and LR(0) 
tables, all three have the same number of states (rows), but the LALR may have fewer 
reduce actions—some reductions are not valid if we are more precise about the 
lookahead. Thus, some conflicts are avoided because an action cell with conflicting 
actions in SLR(1) or LR(0) table may have a unique entry in an LALR(1) once some 
erroneous reduce actions have been eliminated. 
Brute Force? 
There are two ways to construct LALR(1) parsing tables. The first (and certainly more 
obvious way) is to construct the LR(1) table and merge the sets manually. This is 
sometimes referred as the "brute force" way. If you don’t mind first finding all the 
multitude of states required by the canonical parser, compressing the LR table into the 
LALR version is straightforward. 
1. Construct all canonical LR(1) states. 
2. Merge those states that are identical if the lookaheads are ignored, i.e., two 
states being merged must have the same number of items and the items have 
the same core (i.e., the same productions, differing only in lookahead). The 
lookahead on merged items is the union of the lookahead from the states 
being merged. 
3. The successor function for the new LALR(1) state is the union of the 
successors of the merged states. If the two configurations have the same core, 
then the original successors must have the same core as well, and thus the 
new state has the same successors. 
4. The action and goto entries are constructed from the LALR(1) states as for the 
canonical LR(1) parser. 
Let’s do an example, eh?
5 
Consider the LR(1) table for the grammar given on page 1 of this handout. There are 
nine states. 
State on Action Goto 
top of stack a b $ S X 
0 s3 s4 1 2 
1 Acc 
2 s6 s7 5 
3 s3 s4 8 
4 r3 r3 
5 r1 
6 s6 s7 9 
7 r3 
8 r2 r2 
9 r2 
Looking at the configurating sets, we saw that states 3 and 6 can be merged, so can 4 
and 7, and 8 and 9. Now we build this LALR(1) table with the six remaining states: 
State on Action Goto 
top of stack a b $ S X 
0 S36 s47 1 2 
1 acc 
2 S36 s47 5 
36 S36 s47 89 
47 r3 r3 r3 
5 r1 
89 r2 r2 r2 
The More Clever Approach 
Having to compute the LR(1) configurating sets first means we won’t save any time or 
effort in building an LALR parser. However, the work wasn’t all for naught, because 
when the parser is executing, it can work with the compressed table, thereby saving 
memory. The difference can be an order of magnitude in the number of states. 
However there is a more efficient strategy for building the LALR(1) states called step-by-step 
merging. The idea is that you merge the configurating sets as you go, rather than 
waiting until the end to find the identical ones. Sets of states are constructed as in the 
LR(1) method, but at each point where a new set is spawned, you first check to see 
whether it may be merged with an existing set. This means examining the other states
6 
to see if one with the same core already exists. If so, you merge the new set with the 
existing one, otherwise you add it normally. 
Here is an example of this method in action: 
S' –> S 
S –> V = E 
E –> F | E + F 
F –> V | int | (E) 
V –> id 
Start building the LR(1) collection of configurating sets as you would normally: 
I0: S' –> •S, $ 
S –> •V = E, $ 
V –> •id, = 
I1: S' –> S•, $ 
I2: S' –> V• = E, $ 
I3: V –> id•, = 
I4: S –> V =•E, $ 
E –> •F, $/+ 
E –> •E + F, $/+ 
F –>•V, $/+ 
F –>•int, $/+ 
F –>•(E), $/+ 
V –>•id, $/+ 
I5: S –> V = E•, $ 
E –> E• + F, $/+ 
I6: E –> F•, $/+ 
I7: F–> V•, $/+ 
I8: F–> int•, $/+ 
I9: F–> (•E), $/+ 
E –> •F, )/+ 
E –> •E + F, )/+ 
F –> •V, )/+ 
F –> •int, )/+ 
F –> •(E), )/+ 
V –> •id )/+ 
I10: F–> (E•), $/+ 
E –> E• + F, )/+
When we construct state I11, we get something we’ve seen before: 
I11: E –>F•,)/+ 
It has the same core as I6, so rather than add a new state, we go ahead and merge with 
that one to get: 
I611: E –>F•, $/+/) 
We have a similar situation on state I12 which can be merged with state I7 . The algorithm 
continues like this, merging into existing states where possible and only adding new 
states when necessary. When we finish creating the sets, we construct the table just as in 
LR(1). 
LALR(1) Grammars 
A formal definition of what makes a grammar LALR(1) cannot be easily encapsulated in 
a set of rules, because it needs to look beyond the particulars of a production in isolation 
to consider the other situations where the production appears on the top of the stack and 
what happens when we merge those situations. Instead we state that what makes a 
grammar LALR(1) is the absence of conflicts in its parser. If you build the parser and it 
is conflict-free, it implies the grammar is LALR(1) and vice-versa. 
LALR(1) is a subset of LR(1) and a superset of SLR(1). A grammar that is not LR(1) is 
definitely not LALR(1), since whatever conflict occurred in the original LR(1) parser will 
still be present in the LALR(1). A grammar that is LR(1) may or may not be LALR(1) 
depending on whether merging introduces conflicts. A grammar that is SLR(1) is 
definitely LALR(1). A grammar that is not SLR(1) may or may not be LALR(1) 
depending on whether the more precise lookaheads resolve the SLR(1) conflicts. 
LALR(1) has proven to be the most used variant of the LR family. The weakness of the 
SLR(1) and LR(0) parsers mean they are only capable of handling a small set of 
grammars. The expansive memory needs of LR(1) caused it to languish for several years 
as a theoretically interesting but intractable approach. It was the advent of LALR(1) that 
offered a good balance between the power of the specific lookaheads and table size. The 
popular tools yacc and bison generate LALR(1) parsers and most programming 
language constructs can be described with an LALR(1) grammar (perhaps with a little 
grammar massaging or parser trickery to skirt some isolated issues).
8 
Error Handling 
As in LL(1) parsing tables, we can implement error processing for any of the variations 
of LR parsing by placing appropriate actions in the parse table. Here is a parse table for 
a simple arithmetic expression grammar with error actions inserted into what would 
have been the blank entries in the table. 
E –> E + E | E * E | (E) | id 
State on Action Goto 
top of stack id + * ( ) $ E 
0 s3 e1 e1 s2 e2 e1 1 
1 e3 s4 s5 e3 e2 acc 
2 s3 e1 e1 s2 e2 e1 6 
3 e3 r4 r4 e3 r4 r4 
4 s3 e1 e1 s2 e2 e1 7 
5 s3 e1 e1 s2 e2 e1 8 
6 e3 s4 s5 e3 s9 e4 
7 e3 r1 s5 e3 r1 r1 
8 e3 r2 r2 e3 r2 r2 
9 e3 r3 r3 e3 r3 r3 
Error e1 is called from states 0, 2, 4, 5 when we encounter an operator. All of these states 
expect to see the beginning of an expression, i.e., an id or a left parenthesis. One way to 
fix is for the parser to act as though id was seen in the input and shift state 3 on the stack 
(the successor for id in these states), effectively faking that the necessary token was 
found. The error message printed might be something like "missing operand". Error e2 
is called from states 0, 1, 2, 4, 5 on finding a right parenthesis where we were expecting 
either the beginning of a new expression (or potentially the end of input for state 1). A 
possible fix: remove right parenthesis from the input and discard it. The message printed 
could be "unbalanced right parenthesis." 
Error e3 is called from state 1, 3, 6, 7, 8, 9 on finding id or left parenthesis. What were 
these states expecting? What might be a good fix? How should you report the error to 
the user? Error e4 is called from state 6 on finding $. What is a reasonable fix? What do 
you tell the user? 
Bibliography 
A. Aho, R. Sethi, J. Ullman, Compilers: Principles, Techniques, and Tools. Reading, MA: 
Addison-Wesley, 1986. 
J.P. Bennett, Introduction to Compiling Techniques. Berkshire, England: McGraw-Hill, 
1990. 
K. Loudon, Compiler Construction. Boston, MA: PWS, 1997 
A. Pyster, Compiler Design and Construction. New York, NY: Van Nostrand Reinhold, 1988.

More Related Content

What's hot

Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)
Nitin Mohan Sharma
 
Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4
Iffat Anjum
 
LISP: Introduction to lisp
LISP: Introduction to lispLISP: Introduction to lisp
LISP: Introduction to lisp
DataminingTools Inc
 
Functional Programming Concepts for Imperative Programmers
Functional Programming Concepts for Imperative ProgrammersFunctional Programming Concepts for Imperative Programmers
Functional Programming Concepts for Imperative Programmers
Chris
 
Ch06
Ch06Ch06
Ch06
Hankyo
 
First and follow set
First and follow setFirst and follow set
First and follow set
Dawood Faheem Abbasi
 
Ch03
Ch03Ch03
Ch03
Hankyo
 
Unit 2 application of stack
Unit 2  application of stack Unit 2  application of stack
Unit 2 application of stack
LavanyaJ28
 
Data Structure (Circular Linked List)
Data Structure (Circular Linked List)Data Structure (Circular Linked List)
Data Structure (Circular Linked List)
Adam Mukharil Bachtiar
 
Witchcraft
WitchcraftWitchcraft
Witchcraft
Brooklyn Zelenka
 
U2.linked list
U2.linked listU2.linked list
U2.linked list
Ssankett Negi
 
Stack project
Stack projectStack project
Stack project
Amr Aboelgood
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
Akhil Kaushik
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operation
Senthil Kumar
 
Project on stack Data structure
Project on stack Data structureProject on stack Data structure
Project on stack Data structure
Soham Nanekar
 

What's hot (19)

Ch4b
Ch4bCh4b
Ch4b
 
Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)
 
Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4
 
LISP: Introduction to lisp
LISP: Introduction to lispLISP: Introduction to lisp
LISP: Introduction to lisp
 
Functional Programming Concepts for Imperative Programmers
Functional Programming Concepts for Imperative ProgrammersFunctional Programming Concepts for Imperative Programmers
Functional Programming Concepts for Imperative Programmers
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
 
Ch06
Ch06Ch06
Ch06
 
First and follow set
First and follow setFirst and follow set
First and follow set
 
Ch03
Ch03Ch03
Ch03
 
Team 3
Team 3Team 3
Team 3
 
Unit 2 application of stack
Unit 2  application of stack Unit 2  application of stack
Unit 2 application of stack
 
Data Structure (Circular Linked List)
Data Structure (Circular Linked List)Data Structure (Circular Linked List)
Data Structure (Circular Linked List)
 
Witchcraft
WitchcraftWitchcraft
Witchcraft
 
U2.linked list
U2.linked listU2.linked list
U2.linked list
 
Stack project
Stack projectStack project
Stack project
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operation
 
Project on stack Data structure
Project on stack Data structureProject on stack Data structure
Project on stack Data structure
 

Viewers also liked

Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up Parsing
Gerwin Ocsena
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive Parsing
Tanzeela_Hussain
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
ASHOK KUMAR REDDY
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
Akshaya Arunan
 
Module 11
Module 11Module 11
Module 11
bittudavis
 
Compiler First Set Follow Set Brief
Compiler First Set Follow Set BriefCompiler First Set Follow Set Brief
Compiler First Set Follow Set Briefsallypicnic
 
Top down parsing(sid) (1)
Top down parsing(sid) (1)Top down parsing(sid) (1)
Top down parsing(sid) (1)
Siddhesh Pange
 
First and follow
First and followFirst and follow
First and follow
rvarshneyp
 
Lex and Yacc ppt
Lex and Yacc pptLex and Yacc ppt
Lex and Yacc ppt
pssraikar
 
Ds list of experiments ce
Ds list of experiments ceDs list of experiments ce
Ds list of experiments ceShraddha Patel
 
Flex y Bison
Flex y BisonFlex y Bison
Flex y Bison
Bryant Arellano
 
Yacc topic beyond syllabus
Yacc   topic beyond syllabusYacc   topic beyond syllabus
Yacc topic beyond syllabus
JK Knowledge
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
Antony Alex
 

Viewers also liked (20)

Parsing
ParsingParsing
Parsing
 
Parsing
ParsingParsing
Parsing
 
Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up Parsing
 
Cs419 lec11 bottom-up parsing
Cs419 lec11   bottom-up parsingCs419 lec11   bottom-up parsing
Cs419 lec11 bottom-up parsing
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive Parsing
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
Module 11
Module 11Module 11
Module 11
 
Compiler First Set Follow Set Brief
Compiler First Set Follow Set BriefCompiler First Set Follow Set Brief
Compiler First Set Follow Set Brief
 
Top down parsing(sid) (1)
Top down parsing(sid) (1)Top down parsing(sid) (1)
Top down parsing(sid) (1)
 
First and follow
First and followFirst and follow
First and follow
 
Natural Language Processing
Natural Language ProcessingNatural Language Processing
Natural Language Processing
 
Lex and Yacc ppt
Lex and Yacc pptLex and Yacc ppt
Lex and Yacc ppt
 
Assigment cd
Assigment cdAssigment cd
Assigment cd
 
Ds list of experiments ce
Ds list of experiments ceDs list of experiments ce
Ds list of experiments ce
 
Ch4c
Ch4cCh4c
Ch4c
 
Flex y Bison
Flex y BisonFlex y Bison
Flex y Bison
 
Yacc topic beyond syllabus
Yacc   topic beyond syllabusYacc   topic beyond syllabus
Yacc topic beyond syllabus
 
Cimplementation
CimplementationCimplementation
Cimplementation
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
 

Similar to Parsing example

Compilers section 4.7
Compilers section 4.7Compilers section 4.7
Compilers section 4.7myasir16
 
Data Structure lec#2
Data Structure lec#2Data Structure lec#2
Data Structure lec#2
University of Gujrat, Pakistan
 
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docxAssg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
jane3dyson92312
 
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docxAssg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
festockton
 
Master of Computer Application (MCA) – Semester 4 MC0080
Master of Computer Application (MCA) – Semester 4  MC0080Master of Computer Application (MCA) – Semester 4  MC0080
Master of Computer Application (MCA) – Semester 4 MC0080
Aravind NC
 
Real World Haskell: Lecture 6
Real World Haskell: Lecture 6Real World Haskell: Lecture 6
Real World Haskell: Lecture 6Bryan O'Sullivan
 
LR-Parsing.ppt
LR-Parsing.pptLR-Parsing.ppt
LR-Parsing.ppt
BapanKar2
 
COMPILER DESIGN- Syntax Analysis
COMPILER DESIGN- Syntax AnalysisCOMPILER DESIGN- Syntax Analysis
sameermlr0parser-200701133032.pptx
sameermlr0parser-200701133032.pptxsameermlr0parser-200701133032.pptx
sameermlr0parser-200701133032.pptx
BapanKar2
 
MS SQLSERVER:Joining Databases
MS SQLSERVER:Joining DatabasesMS SQLSERVER:Joining Databases
MS SQLSERVER:Joining Databases
sqlserver content
 
MS Sql Server: Joining Databases
MS Sql Server: Joining DatabasesMS Sql Server: Joining Databases
MS Sql Server: Joining Databases
DataminingTools Inc
 
MS SQL SERVER: Joining Databases
MS SQL SERVER: Joining DatabasesMS SQL SERVER: Joining Databases
MS SQL SERVER: Joining Databases
sqlserver content
 
Chapter Five(2)
Chapter Five(2)Chapter Five(2)
Chapter Five(2)bolovv
 
day 13.pptx
day 13.pptxday 13.pptx
day 13.pptx
codewavecommunity44
 
Excel Tutorials - VLOOKUP and HLOOKUP Functions
Excel Tutorials - VLOOKUP and HLOOKUP FunctionsExcel Tutorials - VLOOKUP and HLOOKUP Functions
Excel Tutorials - VLOOKUP and HLOOKUP Functions
Merve Nur Taş
 
ADS_Lec2_Linked_Allocation
ADS_Lec2_Linked_AllocationADS_Lec2_Linked_Allocation
ADS_Lec2_Linked_Allocation
Hemanth Kumar
 
Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02Getachew Ganfur
 

Similar to Parsing example (20)

Compilers section 4.7
Compilers section 4.7Compilers section 4.7
Compilers section 4.7
 
Data Structure lec#2
Data Structure lec#2Data Structure lec#2
Data Structure lec#2
 
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docxAssg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
 
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docxAssg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
 
Master of Computer Application (MCA) – Semester 4 MC0080
Master of Computer Application (MCA) – Semester 4  MC0080Master of Computer Application (MCA) – Semester 4  MC0080
Master of Computer Application (MCA) – Semester 4 MC0080
 
Real World Haskell: Lecture 6
Real World Haskell: Lecture 6Real World Haskell: Lecture 6
Real World Haskell: Lecture 6
 
LR-Parsing.ppt
LR-Parsing.pptLR-Parsing.ppt
LR-Parsing.ppt
 
COMPILER DESIGN- Syntax Analysis
COMPILER DESIGN- Syntax AnalysisCOMPILER DESIGN- Syntax Analysis
COMPILER DESIGN- Syntax Analysis
 
sameermlr0parser-200701133032.pptx
sameermlr0parser-200701133032.pptxsameermlr0parser-200701133032.pptx
sameermlr0parser-200701133032.pptx
 
MS SQLSERVER:Joining Databases
MS SQLSERVER:Joining DatabasesMS SQLSERVER:Joining Databases
MS SQLSERVER:Joining Databases
 
MS Sql Server: Joining Databases
MS Sql Server: Joining DatabasesMS Sql Server: Joining Databases
MS Sql Server: Joining Databases
 
MS SQL SERVER: Joining Databases
MS SQL SERVER: Joining DatabasesMS SQL SERVER: Joining Databases
MS SQL SERVER: Joining Databases
 
Chapter Five(2)
Chapter Five(2)Chapter Five(2)
Chapter Five(2)
 
MA3696 Lecture 5
MA3696 Lecture 5MA3696 Lecture 5
MA3696 Lecture 5
 
day 13.pptx
day 13.pptxday 13.pptx
day 13.pptx
 
Excel Tutorials - VLOOKUP and HLOOKUP Functions
Excel Tutorials - VLOOKUP and HLOOKUP FunctionsExcel Tutorials - VLOOKUP and HLOOKUP Functions
Excel Tutorials - VLOOKUP and HLOOKUP Functions
 
ADS_Lec2_Linked_Allocation
ADS_Lec2_Linked_AllocationADS_Lec2_Linked_Allocation
ADS_Lec2_Linked_Allocation
 
2ds
2ds2ds
2ds
 
NP-Completes
NP-CompletesNP-Completes
NP-Completes
 
Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02Sienna6bst 120411102353-phpapp02
Sienna6bst 120411102353-phpapp02
 

More from Shraddha Patel

Ch 02 (1)
Ch 02 (1)Ch 02 (1)
Ch 02 (1)
Shraddha Patel
 
Chap 03
Chap 03Chap 03
Ch 02 (1)
Ch 02 (1)Ch 02 (1)
Ch 02 (1)
Shraddha Patel
 
Program to remove Left factoring
Program to remove Left factoringProgram to remove Left factoring
Program to remove Left factoring
Shraddha Patel
 
Practical List COMPILER DESIGN
Practical List COMPILER DESIGNPractical List COMPILER DESIGN
Practical List COMPILER DESIGN
Shraddha Patel
 
operator precedence Parser
operator precedence Parser operator precedence Parser
operator precedence Parser
Shraddha Patel
 
Assigment1
Assigment1Assigment1
Assigment1
Shraddha Patel
 

More from Shraddha Patel (8)

Ch 02 (1)
Ch 02 (1)Ch 02 (1)
Ch 02 (1)
 
Chap 03
Chap 03Chap 03
Chap 03
 
Chap 03
Chap 03Chap 03
Chap 03
 
Ch 02 (1)
Ch 02 (1)Ch 02 (1)
Ch 02 (1)
 
Program to remove Left factoring
Program to remove Left factoringProgram to remove Left factoring
Program to remove Left factoring
 
Practical List COMPILER DESIGN
Practical List COMPILER DESIGNPractical List COMPILER DESIGN
Practical List COMPILER DESIGN
 
operator precedence Parser
operator precedence Parser operator precedence Parser
operator precedence Parser
 
Assigment1
Assigment1Assigment1
Assigment1
 

Recently uploaded

The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
Kartik Tiwari
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
gb193092
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 

Recently uploaded (20)

The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 

Parsing example

  • 1. CS143 Handout 11 Summer 2008 July 09, 2008 LALR Parsing Handout written by Maggie Johnson and revised by Julie Zelenski. Motivation Because a canonical LR(1) parser splits states based on differing lookahead sets, it can have many more states than the corresponding SLR(1) or LR(0) parser. Potentially it could require splitting a state with just one item into a different state for each subset of the possible lookaheads; in a pathological case, this means the entire power set of its follow set (which theoretically could contain all terminals—yikes!). It never actually gets that bad in practice, but a canonical LR(1) parser for a programming language might have an order of magnitude more states than an SLR(1) parser. Is there something in between? With LALR (lookahead LR) parsing, we attempt to reduce the number of states in an LR(1) parser by merging similar states. This reduces the number of states to the same as SLR(1), but still retains some of the power of the LR(1) lookaheads. Let’s examine the LR(1) configurating sets from an example given in the LR parsing handout. S' –> S S –> XX X –> aX X –> b I0: S' –> •S, $ S –> •XX, $ X –> •aX, a/b X –> •b, a/b I1: S' –> S•, $ I2: S –> X•X, $ X –> •aX, $ X –> •b, $ I3: X –> a•X, a/b X –> •aX, a/b X –> •b, a/b I4: X –> b•, a/b I5: S –> XX•, $ I6: X –> a•X, $ X –> •aX, $ X –> •b, $ I7: X –> b•, $ I8: X –> aX•, a/b I9: X –> aX•, $ Notice that some of the LR(1) states look suspiciously similar. Take I3 and I6 for example. These two states are virtually identical—they have the same number of items, the core of each item is identical, and they differ only in their lookahead sets. This observation may make you wonder if it possible to merge them into one state. The same is true of I4 and I7, and I8 and I9. If we did merge, we would end up replacing those six states with just these three:
  • 2. 2 I36: X –> a•X, a/b/$ X –> •aX, a/b/$ X –> •b, a/b/$ I47: X –> b•, a/b/$ I89: X –> aX•, a/b/$ But isn’t this just SLR(1) all over again? In the above example, yes, since after the merging we coincidentally end up with the complete follow sets as the lookahead. This is not always the case however. Consider this example: S' –> S S –> Bbb | aab | bBa B –> a I0: S' –> •S, $ S –> •Bbb, $ S –> •aab, $ S –> •bBa, $ B –> •a, b I1: S' –> S•, $ I2: S –> B•bb, $ I3: S –> a•ab, $ B –> a•, b .... In an SLR(1) parser there is a shift-reduce conflict in state 3 when the next input is anything in Follow(B)which includes a and b. In LALR(1), state 3 will shift on a and reduce on b. Intuitively, this is because the LALR(1) state "remembers" that we arrived at state 3 after seeing an a. Thus we are trying to parse either Bbb or aab. In order for that first a to be a valid reduction to B, the next input has to be exactly b since that is the only symbol that can follow B in this particular context. Although elsewhere an expansion of B can be followed by an a, we consider only the subset of the follow set that can appear here, and thus avoid the conflict an SLR(1) parser would have. LALR Merge Conflicts Can merging states in this way ever introduce new conflicts? A shift-reduce conflict cannot exist in a merged set unless the conflict was already present in one of the original LR(1) configurating sets. When merging, the two sets must have the same core items. If the merged set has a configuration that shifts on a and another that reduces on a, both configurations must have been present in the original sets, and at least one of those sets had a conflict already. Reduce-reduce conflicts, however, are another story. Consider the following grammar: S' –> S S –> aBc | bCc | aCd | bBd B –> e C –> e
  • 3. 3 The LR(1) configurating sets are as follows: I0: S' –> •S, $ S –> •aBc, $ S –> •bCc, $ S –> •aCd, $ S –> •bBd, $ I1: S' –> S•, $ I2: S –> a•Bc, $ S –> a•Cd, $ B –> •e, c C –> •e, d I3: S –> b•Cc, $ S –> b•Bd, $ C –> •e, c B –> •e, d I4: S –> aB•c, $ I5: S –> aC•d, $ I6: B –> e•, c C –> e•, d I7: S –> bC•c, $ I8: S –> bB•d, $ I9: B –> e•, d C –> e•, c I10: S –> aBc•, $ I11: S –> aCd•, $ I12: S –> bCc•, $ I13: S –> bBd•, $ We try to merge I6 and I9 since they have the same core items and they only differ in lookahead: I69: C –> e, c/d B –> e, d/c However, this creates a problem. The merged configurating set allows a reduction to either B or C when next token is c or d. This is a reduce-reduce conflict and can be an unintended consequence of merging LR(1) states. When such a conflict arises in doing a merging, we say the grammar is not LALR(1).
  • 4. 4 LALR Table Construction A LALR(1) parsing table is built from the configurating sets in the same way as canonical LR(1); the lookaheads determine where to place reduce actions. In fact, if there are no mergable states in the configuring sets, the LALR(1) table will be identical to the corresponding LR(1) table and we gain nothing. In the common case, however, there will be states that can be merged and the LALR table will have fewer rows than LR. The LR table for a typical programming language may have several thousand rows, which can be merged into just a few hundred for LALR. Due to merging, the LALR(1) table seems more similar to the SLR(1) and LR(0) tables, all three have the same number of states (rows), but the LALR may have fewer reduce actions—some reductions are not valid if we are more precise about the lookahead. Thus, some conflicts are avoided because an action cell with conflicting actions in SLR(1) or LR(0) table may have a unique entry in an LALR(1) once some erroneous reduce actions have been eliminated. Brute Force? There are two ways to construct LALR(1) parsing tables. The first (and certainly more obvious way) is to construct the LR(1) table and merge the sets manually. This is sometimes referred as the "brute force" way. If you don’t mind first finding all the multitude of states required by the canonical parser, compressing the LR table into the LALR version is straightforward. 1. Construct all canonical LR(1) states. 2. Merge those states that are identical if the lookaheads are ignored, i.e., two states being merged must have the same number of items and the items have the same core (i.e., the same productions, differing only in lookahead). The lookahead on merged items is the union of the lookahead from the states being merged. 3. The successor function for the new LALR(1) state is the union of the successors of the merged states. If the two configurations have the same core, then the original successors must have the same core as well, and thus the new state has the same successors. 4. The action and goto entries are constructed from the LALR(1) states as for the canonical LR(1) parser. Let’s do an example, eh?
  • 5. 5 Consider the LR(1) table for the grammar given on page 1 of this handout. There are nine states. State on Action Goto top of stack a b $ S X 0 s3 s4 1 2 1 Acc 2 s6 s7 5 3 s3 s4 8 4 r3 r3 5 r1 6 s6 s7 9 7 r3 8 r2 r2 9 r2 Looking at the configurating sets, we saw that states 3 and 6 can be merged, so can 4 and 7, and 8 and 9. Now we build this LALR(1) table with the six remaining states: State on Action Goto top of stack a b $ S X 0 S36 s47 1 2 1 acc 2 S36 s47 5 36 S36 s47 89 47 r3 r3 r3 5 r1 89 r2 r2 r2 The More Clever Approach Having to compute the LR(1) configurating sets first means we won’t save any time or effort in building an LALR parser. However, the work wasn’t all for naught, because when the parser is executing, it can work with the compressed table, thereby saving memory. The difference can be an order of magnitude in the number of states. However there is a more efficient strategy for building the LALR(1) states called step-by-step merging. The idea is that you merge the configurating sets as you go, rather than waiting until the end to find the identical ones. Sets of states are constructed as in the LR(1) method, but at each point where a new set is spawned, you first check to see whether it may be merged with an existing set. This means examining the other states
  • 6. 6 to see if one with the same core already exists. If so, you merge the new set with the existing one, otherwise you add it normally. Here is an example of this method in action: S' –> S S –> V = E E –> F | E + F F –> V | int | (E) V –> id Start building the LR(1) collection of configurating sets as you would normally: I0: S' –> •S, $ S –> •V = E, $ V –> •id, = I1: S' –> S•, $ I2: S' –> V• = E, $ I3: V –> id•, = I4: S –> V =•E, $ E –> •F, $/+ E –> •E + F, $/+ F –>•V, $/+ F –>•int, $/+ F –>•(E), $/+ V –>•id, $/+ I5: S –> V = E•, $ E –> E• + F, $/+ I6: E –> F•, $/+ I7: F–> V•, $/+ I8: F–> int•, $/+ I9: F–> (•E), $/+ E –> •F, )/+ E –> •E + F, )/+ F –> •V, )/+ F –> •int, )/+ F –> •(E), )/+ V –> •id )/+ I10: F–> (E•), $/+ E –> E• + F, )/+
  • 7. When we construct state I11, we get something we’ve seen before: I11: E –>F•,)/+ It has the same core as I6, so rather than add a new state, we go ahead and merge with that one to get: I611: E –>F•, $/+/) We have a similar situation on state I12 which can be merged with state I7 . The algorithm continues like this, merging into existing states where possible and only adding new states when necessary. When we finish creating the sets, we construct the table just as in LR(1). LALR(1) Grammars A formal definition of what makes a grammar LALR(1) cannot be easily encapsulated in a set of rules, because it needs to look beyond the particulars of a production in isolation to consider the other situations where the production appears on the top of the stack and what happens when we merge those situations. Instead we state that what makes a grammar LALR(1) is the absence of conflicts in its parser. If you build the parser and it is conflict-free, it implies the grammar is LALR(1) and vice-versa. LALR(1) is a subset of LR(1) and a superset of SLR(1). A grammar that is not LR(1) is definitely not LALR(1), since whatever conflict occurred in the original LR(1) parser will still be present in the LALR(1). A grammar that is LR(1) may or may not be LALR(1) depending on whether merging introduces conflicts. A grammar that is SLR(1) is definitely LALR(1). A grammar that is not SLR(1) may or may not be LALR(1) depending on whether the more precise lookaheads resolve the SLR(1) conflicts. LALR(1) has proven to be the most used variant of the LR family. The weakness of the SLR(1) and LR(0) parsers mean they are only capable of handling a small set of grammars. The expansive memory needs of LR(1) caused it to languish for several years as a theoretically interesting but intractable approach. It was the advent of LALR(1) that offered a good balance between the power of the specific lookaheads and table size. The popular tools yacc and bison generate LALR(1) parsers and most programming language constructs can be described with an LALR(1) grammar (perhaps with a little grammar massaging or parser trickery to skirt some isolated issues).
  • 8. 8 Error Handling As in LL(1) parsing tables, we can implement error processing for any of the variations of LR parsing by placing appropriate actions in the parse table. Here is a parse table for a simple arithmetic expression grammar with error actions inserted into what would have been the blank entries in the table. E –> E + E | E * E | (E) | id State on Action Goto top of stack id + * ( ) $ E 0 s3 e1 e1 s2 e2 e1 1 1 e3 s4 s5 e3 e2 acc 2 s3 e1 e1 s2 e2 e1 6 3 e3 r4 r4 e3 r4 r4 4 s3 e1 e1 s2 e2 e1 7 5 s3 e1 e1 s2 e2 e1 8 6 e3 s4 s5 e3 s9 e4 7 e3 r1 s5 e3 r1 r1 8 e3 r2 r2 e3 r2 r2 9 e3 r3 r3 e3 r3 r3 Error e1 is called from states 0, 2, 4, 5 when we encounter an operator. All of these states expect to see the beginning of an expression, i.e., an id or a left parenthesis. One way to fix is for the parser to act as though id was seen in the input and shift state 3 on the stack (the successor for id in these states), effectively faking that the necessary token was found. The error message printed might be something like "missing operand". Error e2 is called from states 0, 1, 2, 4, 5 on finding a right parenthesis where we were expecting either the beginning of a new expression (or potentially the end of input for state 1). A possible fix: remove right parenthesis from the input and discard it. The message printed could be "unbalanced right parenthesis." Error e3 is called from state 1, 3, 6, 7, 8, 9 on finding id or left parenthesis. What were these states expecting? What might be a good fix? How should you report the error to the user? Error e4 is called from state 6 on finding $. What is a reasonable fix? What do you tell the user? Bibliography A. Aho, R. Sethi, J. Ullman, Compilers: Principles, Techniques, and Tools. Reading, MA: Addison-Wesley, 1986. J.P. Bennett, Introduction to Compiling Techniques. Berkshire, England: McGraw-Hill, 1990. K. Loudon, Compiler Construction. Boston, MA: PWS, 1997 A. Pyster, Compiler Design and Construction. New York, NY: Van Nostrand Reinhold, 1988.