Compiler Design 5
Compiler Design 5
Syntax Analysis
Bottom-up Parsing
Bottom-up parsing starts from the leaf nodes of a tree and works in upward direction till it
reaches the root node. Here, we start from a sentence and then apply production rules in
reverse manner in order to reach the start symbol. The available bottom-up parsers are given
below:
Botom-up Parsing
Botom-up Parsing
Shift-Reduce
Shift-Reduce
LR
LRparsing
parsing
SLR
SLR Parsing
Parsing LR
LRParsing
Parsing LALR Parsing
1
10/07/2020
Reductions
• At each reduction step, a specific substring
matching the body of a production is replaced by
the nonterminal at the head of that production.
Handle Pruning
2
10/07/2020
3
10/07/2020
While the primary operations are shift and reduce, there are
actually four possible actions a shift-reduce parser can make:
(1) shift, (2) reduce, (3) accept, and (4) error.
▪ Shift: Shift the next input symbol onto the top of the
stack.
Shift- ▪ Reduce: The right end of the string to be reduced
must be at the top of the stack. Locate the left end of
Reduce the string within the stack and decide with what
nonterminal to replace the string.
Parsing ▪ Accept: Announce successful completion of parsing.
▪ Error: Discover a syntax error and call an error
recovery routine.
4
10/07/2020
LR Parser
Why LR Parsers?
LR parsing is attractive for a variety of reasons:
• LR parsers can be constructed to recognize virtually all
programming language constructs for which context-free
grammars can be written. Non- LR context-free grammars
exist, but these can generally be avoided for typical
programming-language constructs.
• The LR-parsing method is the most general nonbacktracking
shift-reduce parsing method known, yet it can be
implemented as efficiently as other, more primitive shift-
reduce methods.
• An LR parser can detect a syntactic error as soon as it is
possible to do so on a left-to-right scan of the input.
10
5
10/07/2020
LL LR
Does a leftmost derivation. Does a rightmost derivation in reverse.
Starts with the root nonterminal on Ends with the root nonterminal on the
the stack. stack.
Ends when the stack is empty. Starts with an empty.
Uses the stack for designating what Uses the stack for designating what is
LL vs LR is still to be expected. already seen.
Builds the parse tree top-down. Builds the parse tree bottom-up.
Continuously pops a nonterminal off Tries to recognize a right-hand side on
the stack and pushes the the stack, pops it, and pushes the
corresponding right hand side. corresponding nonterminal.
Expands the non-terminals. Reduces the non-terminals.
Reads the terminals when it pops Reads the terminals while it pushes
one off the stack. them on the stack.
11
Error Recovery
12
6
10/07/2020
Error Recovery
There are four common error-recovery strategies that can be implemented
in the parser to deal with errors in the code.
Panic mode
When a parser encounters an error anywhere in the statement, it ignores
the rest of the statement by not processing input from erroneous input to
delimiter, such as semi-colon. This is the easiest way of error-recovery
and also, it prevents the parser from developing infinite loops.
Statement mode
When a parser encounters an error, it tries to take corrective measures so
that the rest of inputs of statement allow the parser to parse ahead. For
example, inserting a missing semicolon, replacing comma with a
semicolon etc. Parser designers have to be careful here because one
wrong correction may lead to an infinite loop.
13
Error Recovery
Error productions
Some common errors are known to the compiler designers that may occur
in the code. In addition, the designers can create augmented grammar to
be used, as productions that generate erroneous constructs when these
errors are encountered.
Global correction
The parser considers the program in hand as a whole and tries to figure
out what the program is intended to do and tries to find out a closest
match for it, which is error-free. When an erroneous input (statement) X
is fed, it creates a parse tree for some closest error-free statement Y. This
may allow the parser to make minimal changes in the source code, but
due to the complexity (time and space) of this strategy, it has not been
implemented in practice yet.
14