written 8.4 years ago by | • modified 4.7 years ago |
Generate three address code for following code,
While (a-b) do
If (cod) then
x=y+2
else
x=y-2
written 8.4 years ago by | • modified 4.7 years ago |
While (a-b) do
If (cod) then
x=y+2
else
x=y-2
written 8.4 years ago by | • modified 8.4 years ago |
Recursive decent parsing
Back Tracking
To understand this, take the following example of CFG:
$S → r X d | rZd \\ X → oa | ea \\ Z → ai$
For an input string: read, a top-down parser, will behave like this:
It will start with S from the production rules and will match its yield to the left-most letter of the input, i.e. ‘r’.
The very production of S (S → rXd) matches with it. So the top-down parser advances to the next input letter (i.e. ‘e’).
Predictive Parsing
Predictive parser consists of following components:
Input Buffer
It consists \$ to indicated end of input.
Stack
It consist \$ to indicate end of stack.
Driver Routine
It is a function that drives the Parser.
Parser Table
It determines the actions to be carried out by the parser
The parser refers to the parsing table to take any decision on the input and stack element combination.
Steps for designing Predictive Parser:
Example
S-> A
A-> aB| Ad
B->bBC|f
C→g
Step 1:
A Ad/aB
LR
A → aBA’
A’ → dA’|€
S → A
B → bBC|f
C → g
Step 2
FIRST
(S) | {a} |
---|---|
(A) | {a} |
(A’) | {d, €} |
(B) | (b,f) |
(C) | {g} |
FOLLOW
(S) | {\$} |
---|---|
(A) | {\$} |
(A’) | {\$} |
(B) | {d,g,\$} |
(C) | {d,g,\$} |
Step 3
a | b | d | g | f | \$ | |
---|---|---|---|---|---|---|
S | S→A | |||||
A | A→aBA’ | |||||
A’ | A’→dA’ ,€ | A’→€ | ||||
B | B→bBC | B,f | ||||
C | C→g |
Step 4
Repeat forever
{Lest ‘X’ be the stack top symbol
And ‘a’ be the input symbol
If X=a=\$ then accept and break
else if X=a=\$
then
i.Pop X from stack
ii. Remove a from input
Else
If M[X,a] = x → y1,y2…….yk
Then
1. Pop X from stack
2. Push yk,yk+1…….y2,y1 on the stack
Else
ERROR()
}