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
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
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.
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.
Rules
In the figure, I0 consists of augmented grammar.
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
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
In LALR parsing table construction , we merge these similar states.
Below is the LALR parsing table.
Now we have to remove the unwanted rows
The final LALR table looks like the below.
26 videos|66 docs|30 tests
|
1. What is LALR parsing in computer science engineering? |
2. How does LALR parsing work? |
3. What are the advantages of LALR parsing? |
4. What are some limitations of LALR parsing? |
5. How is LALR parsing different from other parsing techniques? |
|
Explore Courses for Computer Science Engineering (CSE) exam
|