Compiler Design · Comparison of Parsers
Classification of Parsers: Top-Down vs Bottom-Up
Overview
A parser, also called a syntax analyzer, is the software used at the syntax analysis phase of a compiler.
Parsers fall into two main classes — top-down and bottom-up — and each class splits further into specific parser types.
Knowing this classification is the starting point for understanding how different parsing techniques build a parse for the input string.
Key concepts
- Parser (syntax analyzer): the software component that performs the syntax analysis phase of the compiler.
- Top-down parser: starts from the start symbol and works down to the input string. It has two sub-types:
- Top-down parser with full backtracking
- Top-down parser without full backtracking
- Backtracking: analyzing all possibilities, and if any possibility turns out to be wrong, going back and choosing the next possibility.
- Top-down parsing without backtracking: generating exactly the string that is required, with no trial-and-error return to earlier choices.
- Bottom-up parser: works from the input string up to the start symbol. It has two sub-types:
- Operator precedence parser
- LR parsers
- SR parser (Shift-Reduce parser): another name for bottom-up parsers. When a stack is used, shift = pushing onto the stack and reduce = popping from the stack.
- LR parser: L = the input string is scanned from left to right; R = the parser uses the reverse of the rightmost derivation. LR parsers are of four types.
- Convention note: different textbooks follow different conventions for this classification; the scheme above may differ from the one in the compiler book by Aho, Ullman, Ravisethi (the "dragon book").
Any convention may be followed, but the scheme above is the one used here.
How it works
- Identify the parser as the syntax analyzer of the compiler.
- Classify it as top-down or bottom-up.
- If top-down, decide whether it uses full backtracking or works without full backtracking.
- If bottom-up (SR parser), decide whether it is an operator precedence parser or an LR parser.
- If LR, note that it scans input left to right and uses the reverse of the rightmost derivation; it comes in four types.
Formulas
Comparison
| Feature | Top-down parser | Bottom-up parser |
|---|---|---|
| Direction of construction | Starts from the start symbol and works down to the input string | Works from the input string up to the start symbol |
| Sub-types (as taught here) | (1) With full backtracking, (2) Without full backtracking | (1) Operator precedence parser, (2) LR parsers |
| Other name | — | SR parser (Shift-Reduce parser) |
| Stack operation meaning | — | Shift = push, Reduce = pop |
| Derivation used | — | Reverse of rightmost derivation (the R in LR) |
Revision summary
- A parser is also called a syntax analyzer and works at the syntax analysis phase.
- Two main classes: top-down and bottom-up.
- Top-down splits into with full backtracking and without full backtracking.
- Backtracking = try a possibility, and on failure go back and try the next possibility;
without backtracking = generate exactly the required string. - Bottom-up splits into operator precedence parser and LR parsers; bottom-up parsers are also called SR (shift-reduce) parsers.
- In a stack-based SR parser: shift = push, reduce = pop.
- In LR: L = scan input left to right, R = reverse of rightmost derivation; LR parsers have four types.
- Textbook conventions for this classification differ (e.g., the dragon book); the scheme above is the one used in this course.
Previous-year exam insights
- LL parsers (recursive descent, predictive) are top-down; LR parsers (SLR, CLR/LR(1), LALR) and operator-precedence/shift-reduce parsers are bottom-up.
Asked inGATE 2024UGC NET 2015ISRO 2013 - Top-down parsers produce a leftmost derivation; bottom-up parsers produce the reverse of a rightmost derivation.
Asked inUGC NET 2018UGC NET 2015 - In LL(k), the first L = scan input left to right, second L = leftmost derivation, k = number of look-ahead symbols;
in LR(k), L = left-to-right scan, R = rightmost derivation in reverse, k = look-ahead.
Asked inUGC NET 2015 - The four LR parser types are SLR(1), canonical LR(1) (CLR), LALR(1), and (in some texts) LR(0); all are bottom-up.
Asked inGATE 2024UGC NET 2015 - Predictive parsers (LL(1)) are top-down and require no backtracking; recursive-descent parsers may use backtracking.
Asked inGATE 2024ISRO 2013 - A shift-reduce parser uses a stack plus input buffer and performs shift, reduce, accept and error actions; it is the model underlying LR parsing.
Asked inISRO 2013
Parser Classification Hierarchy
Drawing the diagram…
Shows how parsers split into top-down and bottom-up classes and their sub-types.