Shift-Reduce Table Longest Sequence at The Top of Stack Matching The RHS of A Rule
Shift-Reduce Table Longest Sequence at The Top of Stack Matching The RHS of A Rule
Shift-Reduce Table Longest Sequence at The Top of Stack Matching The RHS of A Rule
Bottom-up parsing methods that follow the idea of shift-reduce parsers Several avors: operator, simple, and weak precedence. In this course, only weak precedence Main dierences with respect to LR parsers:
I I
There is no explicit state associated to the parser (and thus no state pushed on the stack) The decision of whether to shift or reduce is taken based solely on the symbol on the top of the stack and the next input symbol (and stored in a shift-reduce table) In case of reduction, the handle is the longest sequence at the top of stack matching the RHS of a rule
Syntax analysis
194
a1
ai
an
output
Shift-reduce table
terminals and $ terminals, nonterminals and $
Shift/Reduce/Error
Syntax analysis
modier) (A
195
Syntax analysis
196
E E T T F F
!E +T !T !T F !F ! (E ) ! id
S S S R R R R S R R
S S S R R S
Syntax analysis
197
Example of parsing
Stack $ $id $F $T $E $E + $E + id $E + F $E + T $E + T $E + T id $E + T F $E + T $E Input id $ id $ id $ id $ id $ id $ id $ id $ id $ id $ $ $ $ $ Action Shift Reduce Reduce Reduce Shift Shift Reduce Reduce Shift Shift Reduce Reduce Reduce Accept
by F ! id by T ! F by E ! T by F ! id by T ! F by F ! id by T ! T F by E ! E + T
Syntax analysis
198
We dene the (weak precedence) relations l and m between symbols of the grammar (terminals or nonterminals)
I I
X l Y if XY appears in the RHS of a rule or if X precedes a reducible word whose leftmost symbol is Y X m Y if X is the rightmost symbol of a reducible word and Y the symbol immediately following that word
Shift when Xm l a, reduce when Xm m a Reducing changes the precedence relation only at the top of the stack (there is thus no need to shift backward)
Syntax analysis
199
is in R , and B ) X and
)a
Syntax analysis
200
We only need to consider the pairs X l Y with Y a nonterminal that were added in
E E T T F F
!E +T !T !T F !F ! (E ) ! id
Step 3.1
Step 3.2
Step 3.3
Syntax analysis
202
We only need to consider the pairs X m Y with X a nonterminal that were added in
E E T T F F
!E +T !T !T F !F ! (E ) ! id
Step 3.1
Step 3.2
Step 3.3
Syntax analysis
204
Syntax analysis
205
S S S R R R R S R R
S S S R R S
E ! E + T and E ! T but we dont have + l E (see slide 202) T ! T F and T ! F but we dont have l T (see slide 202)
Syntax analysis
206
Removing rules
Removing rules of the form A ! is not di cult For each rule with A in the RHS, add a set of new rules consisting of the dierent combinations of A replaced or not with . Example: S ! AbA|B
B ! b |c A ! is transformed into S
B ! b |c
! AbA|Ab |bA|b |B
Syntax analysis
207
Construction of a weak precedence parser Eliminate ambiguity (or not, see later) Eliminate productions with and ensure that there are no two productions with identical RHS Construct the shift/reduce table Check that there are no conict during the construction Check condition 4 of slide 205
Syntax analysis
208
Because the ambiguous grammar is much more natural and the corresponding unambiguous one can be very complex Using an ambiguous grammar may eliminate unnecessary reductions
Syntax analysis
E ! E + E |E E |(E )|id
(Dragonbook)
Syntax analysis 210
Disambiguation
Example: Parsing of id + id id will give the conguration (0E 1 + 4E 7, id$) We can choose:
I I
Panic mode Phrase-level recovery Introduce specic productions for errors Global error repair
Syntax analysis
212
Panic-mode recovery
In case of syntax error within a phrase, skip until the next synchronizing token is found (e.g., semicolon, right parenthesis) and then resume parsing In LR parsing:
I I I
Scan down the stack until a state s with a goto on a particular nonterminal A is found Discard zero or more input symbols until a symbol a is found that can follow A Stack the state GOTO (s , A) and resume normal parsing
Syntax analysis
213
Phrase-level recovery
Examine each error entry in the parsing table and decide on an appropriate recovery procedure based on the most likely programmer error. Examples in LR parsing: E ! E + E |E E |(E )|id
I
id + id : is unexpected after a +: report a missing operand error, push an arbitrary number on the stack and go to the appropriate next state id + id ) + id : Report a unbalanced right parenthesis error and remove the right parenthesis from the input
Syntax analysis
214
Syntax analysis
215
Recursive descent: let each parsing function return the sub-trees for the parts of the input they parse Table-driven: each nonterminal on the stack points to its node in the partially built syntax tree. When the nonterminal is replaced by one of its RHS, nodes for the symbols on the RHS are added as children to the nonterminal node
Syntax analysis
216
in which tokens are grouped ea often represented inname a parse token such as <id,1>. The id is short for identifier. The value 1 is
he token <=>. In reality it is probably mapped to a pair, whose second hat there are many different identifiers so we need the second component, mbol =. I Each stack element points to a subtree of the syntax tree n <id,2> n <+>. e right. I When performing a reduce, a new syntax tree is built with g and is discussed further in subsequent chapters. It is mapped to e something. On the one hand there is only one 3 so root we could just use the nonterminal at the and the popped-o stack elements ammar containing rules as can be a difference between how such this should be printed (e.g., in an error hases) and how it should be stored (fixed vs. float vs double). Perhaps the le where an entry for "this kind of 3" is stored. Another possibility is to <;>.
the as children
Note:
I I
In C, most blanks are non-significant. rlly removed during scanning. simplied abstract syntax tree
In practice, the concrete syntax tree is not built but rather an Depending on the complexity of the compiler, the syntax tree might
rs, and the various symbols and punctuation without using recursion
evenalso notthe behierarchical constructed ression (expr). Note decomposition in the figure on the right.
represented in a parse
d the syntax tree with operators as interior nodes and rator. The syntax tree on the right corresponds to the parse
epresents ansuch assignment expression not an assignment statement. In C an containing rules as railing semicolon. That is, in C (unlike in Algol) the semicolon is a statement Syntax analysis
217
Easier to implement (recursively), enough for most standard programming languages Need to modify the grammar sometimes strongly, less general than bottom-up parsers Used in most hand-written compilers More general, less strict rules on the grammar, SLR(1) powerful enough for most standard programming languages More di cult to implement, less easy to maintain (add new rules, etc.) Used in most parser generators like Yacc or Bison (but JavaCC is top-down)
Bottom-up:
I I I
Syntax analysis
218
The choice of a parsing technique is left open for the project but we ask you to implement the parser by yourself (Yacc, bison or other parser generators are forbidden) Weak precedence parsing was the recommended method in previous implementations of this course Motivate your choice in your report and explain any transformation you had to apply to your grammar to make it t the parsers constraints To avoid mistakes, you should build the parsing tables by program
Syntax analysis
219