Compilers - CH5
Top-Down Parsing
M.C.C Victor Rodriguez
What is this about ?
PNP FACT
In this chapter, we study the following two forms of top-down parsers:
• Recursive-descent parsers contain a set of mutually recursive procedures that cooperate to parse a string. Code for these procedures can be written directly from a suitable grammar.
• Table-driven LL parsers use a generic LL(k) parsing engine and a parse table that directs the activity of the engine. The entries for the parse table are determined by the particular LL(k) grammar.
top-down parsers
Table-driven LL parsers
Recursive-descent parsers
Fortunately, context-free grammars (CFGs) with certain properties can be used to generate such parsers automatically.
Tools that operate in this fashion are generally called compiler compilers or parser generators. They take a grammar description file as input and attempt to produce a parser for the language defined by the grammar.
The term “compiler compiler” applies because the parser generator is itself a compiler: it accepts a high-level expression of a program (the grammar definition file) and generates an executable form of that program (the parser).
This approach makes parsing one of the easiest and most reliable phases of compiler construction for the following reasons:
When the grammar serves as a language’s definition, parsers can be automatically constructed to perform syntax analysis in a compiler. The automatic construction guarantees that the resulting parser is faithful to the language’s syntactic specification.
When a language is revised, updated, or extended, the associated modifications can be applied to the grammar description to generate a parser for the new language.
Video Time for LL(k) gramars
Exercise ( fix )
and suppose the (input) sentence is aabbcc.
First we try the top-down parsing method.
Suppose we have the monotonic grammar for the language a^n b^n c^n
A -> A α | B
Please eliminate the Left recursion of the following Grammar
A -> A α | β
A -> A α | β
Top Down Parsing
https://www.youtube.com/watch?v=nv9J5Jb7IxM
Example 1
Example 2
Example 1
Full Example
LL1 parsing table
Now … what ?
Assume that you have the input (id) which is valid
stack | inputs | moves |
$E | (id)$ | E->TE’ |
$E’T | (id)$ | T->FT’ |
$E’T’F | (id)$ | F->(E) |
$E’T’)E( | (id)$ | Pop ‘(’ |
$E’T)E | id)$ | E->TE’ |
$E’T’)E’T | id)$ | T->FT’ |
$E’T’)E’T’F | id)$ | F->id |
$E’T’)E’T’id | id)$ | Pop id |
$E’T’)E’T’ | )$ | T’->e |
$E’T’)E’ | )$ | E’->e |
$E’T’) | )$ | Pop ) |
$E’T’ | $ | T’->e |
$E’ | $ | E’->e |
$ | $ | |
Another Full example
First
Follow
Another Full example
The non blank entries in the above table indicate the number of the rule that should be applied, given a nonterminal on top-of-stack and an input symbol as lookahead.
Recursive descent parsers
Summary
Crafting a compiler Chapter 5 until Section 5.3
Complement your summary with:
Videos:
https://www.youtube.com/watch?v=PnOCNxckV48
https://www.youtube.com/watch?v=SH5F-rwWEog
https://www.youtube.com/watch?v=SH5F-rwWEog
Read Chapter 3 of
https://dickgrune.com/Books/PTAPG_1st_Edition/
Extra links
http://faculty.ycp.edu/~dhovemey/fall2012/cs340/lecture/lecture05.html
http://www.csd.uwo.ca/~moreno/CS447/Lectures/Syntax.html/node8.html
http://www.iitg.ac.in/gkd/ma513/sep/sep27/note-0.pdf
Exam will be about these subjects