LALR Parsing | Compiler Design - Computer Science Engineering (CSE) PDF Download

Introduction

LALR Parser is lookahead LR parser. It is  the most powerful parser which can handle large classes of grammar. The size of CLR parsing table is quite large as compared to other parsing table. LALR reduces the size of this table.LALR works similar to CLR. The only difference is , it combines the similar states of CLR parsing table  into one single state.
The general syntax becomes  [A->∝.B, a ]
where A->∝.B is production and a is a terminal or right end marker $
LR(1) items=LR(0) items + look ahead

How to add lookahead with the production?


Case 1:

A->∝.BC, a

Suppose this is the 0th production.Now, since ‘ . ‘ precedes B,so we have to write B’s productions as well.

B->.D [1st production]

Suppose this is B’s production. The look ahead of this production is given as- we look at previous production i.e. – 0th production. Whatever is after B, we find FIRST(of that value) , that is the lookahead of 1st production. So, here in 0th production, after B, C is there. Assume FIRST(C)=d, then 1st production become.

B->.D, d

Case 2:

Now if the 0th production was like this,

A->∝.B, a

Here,we can see there’s nothing after B. So the lookahead of 0th production will be the lookahead of 1st production. ie-

B->.D, a

Case 3:

Assume a production A->a|b

A->a,$ [0th production]

A->b,$ [1st production]

Here, the 1st production is a part of the previous production, so the lookahead will be the same as that of its previous production.

Steps for constructing the LALR parsing table

  • Writing augmented grammar
  • LR(1) collection of items to be found
  • Defining 2 functions: goto[list of terminals] and action[list of non-terminals] in the LALR parsing table

Example: Construct CLR parsing table for the given context free grammar

S-->AA    

A-->aA|b

Solution:

Step 1: Find augmented grammar
The augmented grammar of the given grammar is:

S'-->.S ,$   [0th production]    

S-->.AA ,$ [1st production]    

A-->.aA ,a|b [2nd production]      

A-->.b ,a|b [3rd production]

Let’s apply the rule of lookahead to the above productions.

  • The initial look ahead is always $
  • Now,the 1st production came into existence because of ‘ . ‘ before ‘S’ in 0th production.There is nothing after ‘S’, so the lookahead of 0th production will be the lookahead of 1st production. i.e. :  S–>.AA ,$
  • Now,the 2nd production came into existence because of ‘ . ‘ before ‘A’ in the 1st production.
  • After ‘A’, there’s  ‘A’. So, FIRST(A) is a,b. Therefore, the lookahead of the 2nd production becomes a|b.
  • Now,the 3rd production is a part of the 2nd production.So, the look ahead will be the same.

Step 2: Find LR(0) collection of items

Below is the figure showing the LR(0) collection of items. We will understand everything one by one.

LALR Parsing | Compiler Design - Computer Science Engineering (CSE)

  • The terminals of this grammar are {a,b}
  • The non-terminals of this grammar are {S,A}

Rules

  • If any non-terminal has ‘ . ‘ preceding it, we have to write all its production and add ‘ . ‘ preceding each of its production.
  • from each state to the next state, the ‘ . ‘ shifts to one place to the right.

In the figure, I0 consists of augmented grammar.

  • Io goes to I1 when  ‘ . ‘ of 0th production is shifted towards the right of S(S’->S.). This state is the accept state . S is seen by the compiler. Since I1 is a part of the 0th production, the lookahead is same i.e. $
  • Io goes to I2 when  ‘ . ‘ of 1st production is shifted towards right (S->A.A) . A is seen by the compiler. Since I2 is a part of the 1st production, the lookahead is  same i.e. $.
  • I0 goes to I3 when  ‘ . ‘ of 2nd production is shifted towards the right (A->a.A) . a is seen by the compiler.since I3 is a part of 2nd production, the lookahead is same i.e. a|b.
  • I0 goes to I4 when  ‘ . ‘ of 3rd production is shifted towards right (A->b.) . b is seen by the compiler. Since I4 is a part of 3rd production, the lookahead is same i.e. a|b.
  • I2 goes to I5 when  ‘ . ‘ of 1st production is shifted towards right (S->AA.) . A is seen by the compiler. Since I5 is a part of the 1st production, the lookahead is same i.e. $.
  • I2 goes to I6 when  ‘ . ‘ of 2nd production is shifted towards the right (A->a.A) . A is seen by the compiler. Since I6 is a part of the 2nd production, the lookahead is same i.e. $.
  • I2 goes to I7 when  ‘ . ‘ of 3rd production is shifted towards right (A->b.) . A is seen by the compiler. Since I6 is a part of the 3rd production, the lookahead is same i.e. $.
  • I3 goes to I3 when  ‘ . ‘ of the 2nd production is shifted towards right (A->a.A) . a is seen by the compiler. Since I3 is a part of the 2nd production, the lookahead is same i.e. a|b.
  • I3 goes to I8 when  ‘ . ‘ of 2nd production is shifted towards the right (A->aA.) . A is seen by the compiler. Since I8 is a part of the 2nd production, the lookahead is same i.e. a|b.
  • I6 goes to I9 when  ‘ . ‘ of 2nd production is shifted towards the right (A->aA.) . A is seen by the compiler. Since I9 is a part of the 2nd production, the lookahead is same i.e. $.
  • I6 goes to I6 when  ‘ . ‘ of the 2nd production is shifted towards right (A->a.A) . a is seen by the compiler. Since I6 is a part of the 2nd production, the lookahead is same i.e. $.
  • I6 goes to I7 when  ‘ . ‘ of the 3rd production is shifted towards right (A->b.) . b is seen by the compiler. Since I6 is a part of the 3rd production, the lookahead is same i.e. $.

Step 3:

Defining 2 functions: goto[list of terminals] and action[list of non-terminals] in the parsing table.Below is the CLR parsing table

LALR Parsing | Compiler Design - Computer Science Engineering (CSE)

Once we make a CLR parsing table, we can easily make a LALR parsing table from it.

In the step2 diagram, we can see that 

  • I3 and I6 are similar except their lookaheads.
  • I4 and I7 are similar except their lookaheads.
  • I8 and I9 are similar except their lookaheads.

In LALR parsing table construction , we merge these similar states. 

  • Wherever there is 3 or 6, make it  36(combined form)
  • Wherever there is 4 or 7, make it  47(combined form)
  • Wherever there is 8 or 9, make it  89(combined form)

Below is the LALR parsing table.

LALR Parsing | Compiler Design - Computer Science Engineering (CSE)Now we have to remove the unwanted rows

  • As we can see, 36 row has same data twice, so we delete 1 row.
  • We combine two 47 row into one by combining each value in the single 47 row.
  • We combine two 89 row into one by combining each value in the single 89 row.

The final LALR table looks like the below.

LALR Parsing | Compiler Design - Computer Science Engineering (CSE)

The document LALR Parsing | Compiler Design - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Compiler Design.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
26 videos|66 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on LALR Parsing - Compiler Design - Computer Science Engineering (CSE)

1. What is LALR parsing in computer science engineering?
Ans. LALR (Look-Ahead Left-to-Right) parsing is a parsing technique used in computer science engineering to construct a bottom-up parsing table based on a context-free grammar. It is an extension of the LR(0) parsing method and is commonly used to parse programming languages.
2. How does LALR parsing work?
Ans. LALR parsing works by constructing a parsing table that represents the grammar rules of a context-free grammar. It uses a bottom-up approach, where the parser tries to reduce the input string to the start symbol of the grammar. The parser uses a stack to keep track of the symbols and their states during the parsing process.
3. What are the advantages of LALR parsing?
Ans. LALR parsing has several advantages, including: - Efficiency: LALR parsers are efficient in terms of time and space complexity. They can handle large grammars and parse input strings quickly. - Error recovery: LALR parsers can recover from syntax errors in the input string by using error productions in the grammar. This helps in providing meaningful error messages to the user. - Language expressiveness: LALR parsing can handle a wide range of context-free grammars, including those with ambiguous or left-recursive productions. This makes it suitable for parsing complex programming languages.
4. What are some limitations of LALR parsing?
Ans. While LALR parsing has many advantages, it also has some limitations, such as: - Conflicts: LALR parsers may encounter shift-reduce or reduce-reduce conflicts when constructing the parsing table. These conflicts occur when the grammar is ambiguous or has overlapping productions. Resolving these conflicts can be challenging. - Table size: The size of the parsing table generated by LALR parsing can be large, especially for grammars with a large number of terminals and non-terminals. This can result in increased memory usage. - Lack of lookahead: LALR parsing has limited lookahead capabilities, which means that it may not be able to handle certain grammars that require more lookahead information for parsing.
5. How is LALR parsing different from other parsing techniques?
Ans. LALR parsing is a variation of LR(0) parsing and is closely related to other parsing techniques like SLR and LR(1) parsing. The main differences between LALR parsing and other techniques lie in the size of the parsing table and the ability to handle certain types of grammars. LALR parsing strikes a balance between the simplicity of SLR parsing and the power of LR(1) parsing by handling a larger class of grammars while keeping the parsing table size manageable.
26 videos|66 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Exam

,

Important questions

,

past year papers

,

Semester Notes

,

LALR Parsing | Compiler Design - Computer Science Engineering (CSE)

,

video lectures

,

Summary

,

shortcuts and tricks

,

Previous Year Questions with Solutions

,

Viva Questions

,

LALR Parsing | Compiler Design - Computer Science Engineering (CSE)

,

LALR Parsing | Compiler Design - Computer Science Engineering (CSE)

,

practice quizzes

,

ppt

,

MCQs

,

pdf

,

Objective type Questions

,

Free

,

mock tests for examination

,

study material

,

Sample Paper

,

Extra Questions

;