Compi Desi CHP 03 p2
Compi Desi CHP 03 p2
Compiler Design
1
CHAPTER 3
Syntax Analysis
Part 2
2
Bottom-Up Parser
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 image given below depicts the bottom-up parsers available.
Shift-Reduce Parsing
Shift-reduce parsing uses two unique steps for bottom-up parsing.
Shift step: The shift step refers to the advancement of the input pointer to the next input
symbol, which is called the shifted symbol. This symbol is pushed onto the stack. The
shifted symbol is treated as a single node of the parse tree.
Reduce step: When the parser finds a complete grammar rule (RHS) and replaces it to
(LHS), it is known as reduce-step. This occurs when the top of the stack contains a handle.
To reduce, a POP function is performed on the stack which pops off the handle and replaces it
with LHS non-terminal symbol.
LR Parser
The LR parser is a non-recursive, shift-reduce, bottom-up parser.
It uses a wide class of context-free grammar which makes it the most efficient
syntax analysis technique.
LR parsers are also known as LR(k) parsers, where,
Disadvantage:
a considerable amount of input is skipped without checking it for additional
errors
…Compile-time errors
Syntactic phase errors:
These errors are detected during the syntax analysis phase.
Disadvantage:
its difficult to maintain.
…Compile-time errors
4. Global Correction
The parser examines the whole program and tries to find out the closest
match for it which is error free.
The closest match program has less number of insertions, deletions and
changes of tokens to recover from erroneous input.
Due to high time and space complexity, this method is not implemented
practically.
End of Chapter
3
17