0% found this document useful (0 votes)
40 views

Top Down Parsing

The document discusses top-down parsing and predictive parsing techniques. It explains the basic idea of predictive parsing and how it uses lookahead tokens. It also discusses the LL(1) parsing algorithm and how it works by calculating FIRST sets and repeating for nonterminals until no changes. It notes the strengths of LL(1) parsing, including that it is straightforward to implement and fast.

Uploaded by

shvdo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Top Down Parsing

The document discusses top-down parsing and predictive parsing techniques. It explains the basic idea of predictive parsing and how it uses lookahead tokens. It also discusses the LL(1) parsing algorithm and how it works by calculating FIRST sets and repeating for nonterminals until no changes. It notes the strengths of LL(1) parsing, including that it is straightforward to implement and fast.

Uploaded by

shvdo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 133

Top-Down Parsing

Different Types of Parsing


•Top-Down Parsing: Beginning with the
start symbol, try to guess the productions
to apply to end up at the user's program.
•Bottom-Up Parsing: Beginning with the
user's program, try to apply productions in
reverse to convert the program back into
the start symbol.
Top-Down Parsing Methods
•Breadth first search: inefficient for
memory and time
•Depth first search: inefficient for
time
•Predictive parsing
Predictive Parsing
•Predictive parsers are fast: Many
predictive algorithms can be made
to run in linear time.
•Predictive parsers are weak: Not all
grammars can be accepted by
predictive parsers.
Basic Idea of Predictive Parsing
•Given just the start symbol, how do you know
which productions to use to get to the input
program?
•Idea: Use lookahead tokens. When trying to
decide which production to use, look at some
number of tokens of the input to help make
the decision.
Predictive Parsing
•Increasing the number of lookahead tokens
increases the number of grammars we can
parse, but complicates the parser.
•Decreasing the number of lookahead tokens
decreases the number of grammars we can
parse, but simplifies the parser.
// and so on


Another example
// and so on


The LL(1) Algorithm
Repeat for no terminals until no
changes in the first sets
Next wo deal with
grammars that has epsilon
production(s)
Expanding our grammar so that if and while
statements can have block statements inside.
FIRST Sets with ε
Final Result
The Strengths of LL(1)

• LL(1) is Straightforward
• Can be implemented quickly with a table-driven design
• Can be implemented by recursive descent
• Define a function for each nonterminal
• Have these functions call each other based on the lookahead
token.
• LL(1) is Fast
• Both table-driven LL(1) and recursive-descent-powered LL(1) are
fast.
• Can parse in O(n |G|) time, where n is the length of the string and |
G| is the size of the grammar.

You might also like