390 likes | 593 Views
SC4312. Lecture 4a. 2. Syntax Analysis. Syntax analysis is the process that verifies if a given source program is syntactically correct.A parser is a program that encodes the grammar of the target language.Its output is a parse tree that represents the source program.. SC4312. Lecture 4a. 3. Pars
E N D
1. SC4312 Lecture 4a 1 Parsing 1 SC4312 Compiler Construction
Kwankamol Nongpong
2. SC4312 Lecture 4a 2 Syntax Analysis Syntax analysis is the process that verifies if a given source program is syntactically correct.
A parser is a program that encodes the grammar of the target language.
Its output is a parse tree that represents the source program.
3. SC4312 Lecture 4a 3 Parsing Parsing is the process done by a parser to convert a stream of tokens (from a scanner) to a parse tree.
There are two well-known parsing algorithms that runs in O(n3) time, where n is the length of the input program:
Earleys algorithm
CYK (Cocke-Younger-Kasami)
4. SC4312 Lecture 4a 4 Parsing in Linear Time There are large classes of grammars that we can build parsers that run in linear time.
Grammars for most programming languages fall into these classes.
The two most important classes of grammars are:
LL (Left-to-right, Left-most derivation)
LR (Left-to-right, Right-most derivation)
5. SC4312 Lecture 4a 5 LL vs. LR Grammars LL grammars are simpler and easier to understand.
They can be written by hand or generated from a tool.
LR grammars covers larger set of languages.
They are almost always created by a tool.
Coco/R generates a parser for LL grammars.
6. SC4312 Lecture 4a 6 LL Parsers LL parsers are also called top-down or predictive parsers.
They construct a parse tree from the root down to terminals.
At each step, they predict which production rule will be used to expand the current non-terminal based on the next token of input.
7. SC4312 Lecture 4a 7 LR Parsers LR parsers are also called bottom-up or shift-reduce parsers.
They construct a parse tree from terminals up to the root.
At each step, they try to join a group of terminals (and non-terminals) into a single non-terminal that represent an LHS of a production rule in the grammar.
8. SC4312 Lecture 4a 8 Example: a list of identifiers Consider the following grammar for a comma-separated list of identifiers:
id_list -> id id_list_tail
id_list_tail -> , id id_list_tail
id_list_tail -> ;
These productions are used in a top-down parser.
They can also be used in a bottom-up parser but not as efficient. Why?
9. SC4312 Lecture 4a 9 Figure 2.13: Top-Down