written 8.4 years ago by | • modified 4.7 years ago |
written 8.4 years ago by | • modified 8.4 years ago |
Predictive Parsing
- Predictive parser is a recursive descent parser, which has the capability to predict which production is to be used to replace the input string.
- The predictive parser does not suffer from backtracking.
- To accomplish its tasks, the predictive parser uses a look-ahead pointer, which points to the next input symbols.
- To make the parser back-tracking free, the predictive parser puts some constraints on the grammar and accepts only a class of grammar known as LL(k) grammar.
-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
- Predictive parsing uses a stack and a parsing table to parse the input and generate a parse tree.
- Both the stack and the input contains an end symbol \$to denote that the stack is empty and the input is consumed.
- The parser refers to the parsing table to take any decision on the input and stack element combination.
The goal of predictive parsing is to construct a top-down parser that never backtracks. To do so, we must transform a grammar in two ways:
i. eliminate left recursion, and
ii. Perform left factoring.
Steps for designing Predictive Parser:
- Make the grammar suitable for top-down parser. By performing the elimination of left recursion. And by performing left factoring.
- Find the FIRST and FOLLOW of the variables
- Design predictive parser table
- Write predictive parsing algorithm
Give some examples.
Example:
S -> F
S -> (S-F)
F -> a
Above grammar can be written as,
S -> F | (S-F)
F -> a
To solve the above grammar using LL(1) parser we have to follow three steps :-
Step 1 : Remove Left and Right recursion.
Above grammar does not contain any left or right recursion hence we follow step 2 directly.
Step 2 : Finding first & follow
First (S) = S -> F
$\hspace{1 cm}$ First (S) is First (F)
$\hspace{1 cm}$ First (F) = a
$\hspace{1 cm}$ First (S) = {a}
Now,
S -> (S-F)
$\hspace{1 cm}$ First (S) = ( Since, ( is terminal symbol.
$\hspace{1 cm}$ First (S) = { a, ( }
$\hspace{1 cm}$ First (F) = {a}
Now, Lets find follow of each variable
$\hspace{1 cm}$ Follow (F) = { \$, - }
$\hspace{1 cm}$ Follow (F) = { }
$\hspace{1 cm}$ S -> F
Here, F is not followed by any variable or symbol. (In follow of F we put follow of S )
Therefore, Follow (F) ={ S, - }
Again,
S -> (S-F)
Therefore, Follow (F) = { \$, - , ) }
Follow (S) = { \$, - }
Follow (F) = { \$, - , ) }
Step 3:- Generating Parse Table.
( | - | ) | a | \$ | |
---|---|---|---|---|---|
S | S -> (S-F) | S -> F | |||
F | F -> a |
Step 4 :-
Stack | Input | Action |
---|---|---|
\$S | (a-a)\$ | |
\$)F-SC | (a-a)\$ | S -> (S-F) |
\$)F-S | a-a)\$ | S -> F |
\$)F-a | a-a)\$ | F -> a , Pop a |
\$)F- | -a)\$ | Pop - |
\$)F | a)\$ | |
\$)a | a)\$ | F -> a , Pop a |
\$) | )\$ | Pop ) |
\$ | \$ | |
Accepted |
Therefore, (a-a) is Accepted.