问题 |
答案 |
What is the role of a Syntax Analyser? 开始学习
|
|
[1] read a sequence of tokens returned from the lexical analyser [2] group these tokens into phrases of certain phrase types according to the syntax of the programming language.
|
|
|
What is the output of the Syntax Analysis phase 开始学习
|
|
The output of this phase is an abstract syntax tree—a data structure that represents the syntactic structure of the source program.
|
|
|
Given an input program, the goals of parsing is to? 开始学习
|
|
[1] find all syntax errors and for each, produce an appropriate diagnostic message and recover quickly [2] produce the parse tree for the program for code generation.
|
|
|
What are the 2 categories of parsers? 开始学习
|
|
|
|
|
describe a top down parser 开始学习
|
|
[1] Beginning with the root (the start symbol); [2] Each node is visited before its branches are followed; [3] Branch from a particular node are visited in left-to-right order - a leftmost derivation;
|
|
|
Describe a bottom up parser 开始学习
|
|
[1] Beginning at the leaves of parse tree (terminal symbols) and progressing toward the root. [2] Order is that of the reverse of a rightmost derivation
|
|
|
Consider the following grammar: S → AB A → aA | ε B → b | bB What language does the grammar define? 开始学习
|
|
The grammar defines strings consisting of any number (zero included) of a’s followed by at least one (and possibly more) b’s
|
|
|
What is Recursive-Descent Parsers (RDP) 开始学习
|
|
[1] A RDP consists of a collection of subprograms, many of which are recursive (therefore its name) and produces a parse tree in top-down order. [2] There is a subprogram for each nonterminal in the grammar.
|
|
|
A nonterminal that has more than one RHS requires an initial process to determine which RHS it is to choose, give an example method to find The correct RHS 开始学习
|
|
[1] The next token of input is compared with the first token that can be generated by each RHS until a match is found (e.g., the left parenthesis of the <factor> rule) [2] If no match is found, it is a syntax error.
|
|
|
If a grammar has left recursion, either direct or indirect can it be used? 开始学习
|
|
not directly as it leads to non-terminating recursion
|
|
|
how can you transform left recursive grammar to one that is not? 开始学习
|
|
For each nonterminal, A, Group the A-rules as A → Aα1 | ... | Aαm | β1 | β2 | ... | βn where none of the β’s begins with A Replace the original A-rules with A → β1A’ | β2A’ | ... | βnA’ A’ → α1A’ | α2A’ | ... | αmA’ | ε
|
|
|