Drawing Parse Tree With Context Free Gramar
Prerequisite: LR Parser.
The LR parser is an efficient bottom-up syntax analysis technique that can be used for a large class of context-free grammar. This technique is also called LR(0) parsing.
L stands for the left to right scanning
R stands for rightmost derivation in reverse
0 stands for no. of input symbols of lookahead.
Augmented grammar :
If G is a grammar with starting symbol S, then G' (augmented grammar for G) is a grammar with a new starting symbol S' and productions S-> .S' . The purpose of this new starting production is to indicate to the parser when it should stop parsing. The ' . ' before S indicates the left side of ' . ' has been read by a compiler and the right side of ' . ' is yet to be read by a compiler.
Steps for constructing the LR parsing table :
- Writing augmented grammar
- LR(0) collection of items to be found
- Defining 2 functions: goto(list of terminals) and action(list of non-terminals) in the parsing table.
Q. Construct an LR 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 [2nd production]
A–>.b [3rd production]
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.
The terminals of this grammar are {a,b}
The non-terminals of this grammar are {S,A}
RULE – if any nonterminal has ' . ' preceding it, we have to write all its production and add ' . ' preceding each of its-production.
RULE – 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 accepted state.
S is seen by the compiler - Io goes to I2 when ' . ' of 1st production is shifted towards the right (S->A.A) . A is seen by the compiler
- I0 goes to I3 when ' . ' of the 2nd production is shifted towards the right (A->a.A) . a is seen by the compiler.
- I0 goes to I4 when ' . ' of the 3rd production is shifted towards the right (A->b.) . b is seen by the compiler.
- I2 goes to I5 when ' . ' of 1st production is shifted towards the right (S->AA.) . A is seen by the compiler
- I2 goes to I4 when ' . ' of 3rd production is shifted towards the right (A->b.) . b is seen by the compiler.
- I2 goes to I3 when ' . ' of the 2nd production is shifted towards the right (A->a.A) . a is seen by the compiler.
- I3 goes to I4 when ' . ' of the 3rd production is shifted towards the right (A->b.) . b is seen by the compiler.
- I3 goes to I6 when ' . ' of 2nd production is shifted towards the right (A->aA.) . A is seen by the compiler
- I3 goes to I3 when ' . ' of the 2nd production is shifted towards the right (A->a.A) . a is seen by the compiler.
STEP3 – defining 2 functions: goto[list of terminals] and action[list of non-terminals] in the parsing table
- $ is by default a nonterminal that takes the accepting state.
- 0,1,2,3,4,5,6 denotes I0,I1,I2,I3,I4,I5,I6
- I0 gives A in I2, so 2 is added to the A column and 0 rows.
- I0 gives S in I1, so 1 is added to the S column and 1 row.
- similarly, 5 is written in A column and 2nd row, 6 is written in A column and 3 rows.
- I0 gives an in I3 to .so S3(shift 3) is added to a column and 0 rows.
- I0 gives b in I4, so S4(shift 4) is added to the b column and 0 rows.
- Similarly, S3(shift 3) is added on a column and 2,3 rows, S4(shift 4) is added on b column and 2,3 rows.
- I4 is reduced state as ' . ' is at the end. I4 is the 3rd production of grammar. So write r3(reduce 3) in terminals.
- I5 is reduced state as ' . ' is at the end. I5 is the 1st production of grammar. So write r1(reduce 1) in terminals.
- I6 is reduced state as ' . ' is at the end. I6 is the 2nd production of grammar. So write r2(reduce 2) in terminals.
As each cell has only 1 value in it, hence, the given grammar is LR(0).
Source: https://www.geeksforgeeks.org/problem-on-lr0-parser/
0 Response to "Drawing Parse Tree With Context Free Gramar"
Post a Comment