Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Top down parsing
1. TOP DOWN PARSING
MADE BY : PRANKIT MISHRA(141CC00007)
UNDER THE GUIDANCE OF DR. AMIT DAS(FACULTY INCHARGE)
5/7/2017Prankit Mishra, ICFAI Tech School
1
2. CONTENT
Introduction
Programming Language Applications
Relationship between Parser Types
Recursive Descent
The Parsing Tables & Parsing Program
Predictive Parser
Building a Predictive Parse Table
For First
For Follow
LL(1) Grammars
Model of a Non-Recursive Predictive
Parser
Non-Recursive Predictive Parsing
Parsing Table M for Grammar
5/7/2017Prankit Mishra, ICFAI Tech School
2
3. Introduction
In computer science, top-down parsing is a parsing strategy where one first looks at the
highest level of the parse tree and works down the parse tree by using the rewriting rules of
a formal grammar.
LL parsers are a type of parser that uses a top-down parsing strategy.
Top-down parsing is a strategy of analysing unknown data relationships by hypothesizing
general parse tree structures and then considering whether the known fundamental
structures are compatible with the hypothesis. It occurs in the analysis of both
natural languages and computer languages.
5/7/2017Prankit Mishra, ICFAI Tech School
3
4. Programming Language Application
A compiler parses input from a programming language to assembly language or an internal
representation by matching the incoming symbols to production rules.
Production rules are commonly defined using Backus-Naur form.
An LL parser is a type of parser that does top-down parsing by applying each production
rule to the incoming symbols, working from the left-most symbol yielded on a production
rule and then proceeding to the next production rule for each non-terminal symbol
encountered.
In this way the parsing starts on the Left of the result side (right side) of the production rule
and evaluates non-terminals from the Left first and, thus, proceeds down the parse tree for
each new non-terminal before continuing to the next symbol for a production rule.
5/7/2017Prankit Mishra, ICFAI Tech School
4
6. Recursive Descent
Recursive descent parsers simply try to build a top-down parse tree.
It would be better if we always knew the correct action to take.
It would be better if we could avoid recursive procedure calls during parsing.
5/7/2017Prankit Mishra, ICFAI Tech School
6
7. The Parsing Table & Parsing Program
The table is a 2D array M[A,a] where A is a nonterminal symbol and a is a terminal or $.
At each step, the parser considers the top-of-stack symbol X and input symbol a:
If both are $, accept
If they are the same (nonterminal), pop X, advance input
If X is a nonterminal, consult M[X,a].
If M[X,a] is “ERROR” call an error recovery routine. Otherwise, if M[X,a] is a production
of the grammar X -> UVW, replace X on the stack with WVU (U on top)
5/7/2017Prankit Mishra, ICFAI Tech School
7
8. Predictive Parsers
A predictive parser always knows which production to use, ( to avoid backtracking )
Example: for the productions
stmt -> if ( expr ) stmt else stmt
| while ( expr ) stmt
| for ( stmt expr stmt ) stmt
A recursive descent parser would always know which production to use, depending on the
input token.
5/7/2017Prankit Mishra, ICFAI Tech School
8
9. Building a predictive parse table
The construction requires two functions:
1. FIRST
2. FOLLOW
5/7/2017Prankit Mishra, ICFAI Tech School
9
10. For First
For a string of grammar symbols α, FIRST(α) is the set of terminals that begin
all possible strings derived from α. If α =*> ε, then ε is also in FIRST(α).
E -> T E’
E’ -> + T E’ | ε
T -> F T’
T’ -> * F T’ | ε
F -> ( E ) | id
FIRST(E) = FIRST (T) = FIRST (F) = {( , id }
FIRST(E’) = {+ , e}
FIRST(T) = {( , id}
FIRST(T’) = { *, e}
FIRST(F) = {( , id }
5/7/2017Prankit Mishra, ICFAI Tech School
10
11. For Follow
FOLLOW(A) for non terminal A is the set of terminals that can appear immediately
to the right of A in some sentential form. If A can be the last symbol in a sentential
form, then $ is also in FOLLOW(A).
E -> T E’
E’ -> + T E’ | ε
T -> F T’
T’ -> * F T’ | ε
F -> ( E ) | id
Follow (E) = { ) , $ }
Follow (E’) = Follow (E)= { ) ,$ }
Follow (T) = { +, Follow (E)}= {+ , ) , $}
Follow (T’) = {+, ) ,$}
Follow ( F) = {*, +, ), $ }
5/7/2017Prankit Mishra, ICFAI Tech School
11
12. LL(1) grammars
In computer science, an LL parser is a top-down parser for a subset of context-free
languages.
It parses the input from Left to right, performing Leftmost derivation of the sentence.
An LL parser is called an LL(k) parser if it uses k tokens of look ahead when parsing a
sentence.
The predictive parser algorithm can be applied to ANY grammar.
5/7/2017Prankit Mishra, ICFAI Tech School
12
13. Model of a non recursive predictive parser
a + b $
X
Y
Z
$
Input buffer
stack
Predictive parsing
program/driver
Parsing Table M
5/7/2017Prankit Mishra, ICFAI Tech School
13
14. NoN recursive Predictive Parsing
If X = a = $, the parser halts and announces successful completion of parsing.
If X = a $, the parser pops X off the stack and advances the input pointer to the next input
symbol.
If X is a nonterminal, the program consults entry M[X, a] of the parsing table M. This entry
will be either an X-production of the grammar or an error entry. If, for example, M[X, a] =
{X UVW}, the parser replaces X on top of the stack by WVU (with U on top).
As output, we shall assume that the parser just prints the production used; any other code
could be executed here. If M[X, a] = error, the parser calls an error recovery routine.
5/7/2017Prankit Mishra, ICFAI Tech School
14