L5 Top Down Parsing

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 33

Syntax analysis – Top Down

Parsar
Introduction

• Syntax analysis or parsing recognizes the syntactic structure of a programming language and transforms a
string of tokens into a tree of tokens.
• Top-down parsers are simple to construct.
• Parsers are also used in natural language applications

• This process of analyzing the syntax of the language is done by a module in the compiler called parser.
• Context Free Grammars (CFGs) are used to define standard syntax rules for the language.
• This process of verifying whether an input string matches the grammar of the language is called parsing.
• The output of a parser is a parse tree for the set of tokens produced by the
lexical analyzer.
• In practice, there are a number of tasks that are carried out during syntax
analysis,
such as collecting information about various tokens and
storing into symbol table,
performing type checking and semantic checking, and
generating intermediate code.
These tasks will be discussed in detail in syntax-directed translations
Error handling in Parsing
Error Recover Strategies
Types of Parsers

• General form of top-down parsing, called recursive descent Parsing


• Predictive Parsing- a special form of recursive Descent Parsing
Top Down Parser (TDP)

the primary problem in design is if a nonterminal has more than one


alternative, then there
should be some mechanism to decide the right choice for expansion.

BACKTRACKING
Bottom Up Parser(BUP)

• The task of a bottom up parser at any time is to identify a substring that matches with the right hand side.
Replace that string by the left hand side nonterminal.
• The main difficulty in bottom-up parsing is identifying the substring that is called
handle.
Bottom-up parsing can be described as “detect the handle” and “reduce the handle.”
The main difficulty is detecting the handle.
Predictive Parsers

• A predictive parser is capable of predicting the right choice for expanding a nonterminal.

Given a grammar and input string, to design a predictive parser, there is a restriction on
the grammar.
• The restriction is: The grammar should be free of left recursion and should
be left factored.
• A predictive parser tries to predict which production produces the least chances of a backtracking and infinite looping.
• predictive parsing relies on information about what first symbols can be generated from a production.
• If the first symbols of a production can be a nonterminal, then the nonterminal has to be expanded till we get a
set of terminals.
• Types of predictive parsers.
1. Recursive descent parser
2. Nonrecursive descent parser
The simplest is the recursive descent parser.
Recursive Descent Parser
• Recursive descent parsing is writing recursive procedures for each nonterminal.
• This is a top-down process in which the parser attempts to verify whether the syntax of the input
string is correct as it is read from left to right.
• Our recursive descent parsers will read one character at a time and advance the input pointer when the proper
match occurs. The routine presented in the following figures accomplishes this matching and reading process.
• Recursive descent parser actually performs a depth-first search of the derivation tree for the string being
parsed; hence, the name “descent.”
• It uses a collection of recursive procedures; hence, the name “recursive descent.
Example
Nonrecursive Descent Parser – LL(1) Grammar
Working
Error case
LL(1) Parsing
FIRST and FOLLOW
• The construction of both top-down and bottom-up parsers is aided by two functions, FIRST and FOLLOW,
associated with a grammar G.
• During topdown parsing, FIRST and FOLLOW allow us to choose which production to apply, based on the
next input symbol.
• During panic-mode error recovery, sets of tokens produced by FOLLOW can be used as synchronizing
tokens.
Follow(A)
FIRST(X)
Example
Construction of Predictive Parsing Tables
LL(1) Grammar

You might also like