Home »
Compiler Design
Classification of Top Down Parser
In this article, we are going to learn about the Classification of Top Down Parser: with backtracking, without backtracking.
Submitted by Anusha Sharma, on March 25, 2018
Top Down Parser is classified into:
- With backtracking
- Without backtracking
Bruteforce Technique
This technique is under top-down parser with backtracking. In this technique whenever a non terminal is expanding the first time and go with the first alternative and compare with the input string if does not matches go with the second alternative and compare with the input string if does, not matches go with the third alternative and so on and continue with all the alternatives if at least one alternative matches the input string then input string is parsed successfully otherwise the string cannot be parsed.
LL(1) Parser or Table Driven Parser
LL(1) has 3 components
1) Input buffer
It is divided into cells and each cell is capable of holding only one symbol. The tape header always pointing any one symbol at a time. The symbol which is pointed by tape header is called Look ahead symbol.
2) Parse stack
It contains the grammar symbols. The grammar symbol is pushed into stack or popped from the stack based on the occurrence of the matching, i.e. if there is no matching between the topmost symbol of stack and look ahead symbol then grammar symbols are pushed into stack if matching occurs between the topmost symbol of stack and look ahead symbol then grammar symbols are popped from the stack.
3) Parse Table
It is a two dimensional array of order m*n, where m=number of non terminals and n=number of terminals+1. It contains all the productions which are used in the parsing to push the grammar symbols into stack.
Parsing Process
- Step 1 - Push the start symbol into the stack.
- Step 2 - Compare the topmost symbol of a stack with the lookahead symbol.
- Step 3 - If matching occurs then tops of the grammar symbol and increment the input pointer and continues the process.
- Step 4 - Output the production which is used for the expanding a nonterminal and continue.
LL(1) Parsing Algorithm
Let x be the grammar symbol or start symbol in the stack and a is the look ahead symbol.
- If x=a=$ then it is successful in parsing.
- If x=a!=$ then pop off and increment the input pointer.
- If x!=a!=$ then m[x,a] has the production x->uvw thus replace by uvw in the reverse order and continue the process.
- Output the production which is used for expanding the non terminal.
LL(1) Grammar
The grammar whose parse table does not contain multiple entries is called LL(1) grammar.
FIRST and FOLLOW are the function used for the construction of parse table.
FIRST
First (alpha) is the set of all terminals that may begin in any sentential form which is derived from following rules:
- If alpha is a terminal then, first(alpha)={alpha}
- If alpha is non terminal and defined by the rule then first(alpha)={epsilon}
- If alpha is a non terminal and defined by non null production i.e. alpha=X1X2X3 then, first(alpha)=first(X1)
- first(alpha)=first(X1)Ufirst(X2)
- first(alpha)= first(X1)Ufirst(X2)Ufirst(X3)U{epsilon}
FOLLOW
Follow(A) is the set of all terminals that may follow to the right of A in any sentential form of the grammar.