Compiler Design · LL(1) Parsing
LL(1) Parsers: Naming, Components and Structure
Overview
An LL(1) parser is a top-down parser that builds the parse tree from the start symbol down to the input string, the reverse of bottom-up (reduction) parsing, which starts from the string.
Its name encodes how it reads input, how it derives, and how much it looks ahead, and it is built from four standard components.
Key concepts
- LL(1) parser: a top-down parser that builds the parse tree starting from the start symbol and working down to the input string — the reverse of the bottom-up (reduction) process that starts from the string.
- First L (Left to right): the input is scanned from left to right. For an input such as A, B, C, D, the symbols are read from the leftmost symbol toward the rightmost symbol — never from the other side.
- Second L (Leftmost derivation): while deriving the parse tree, a leftmost derivation is used.
- 1 (Lookahead): a lookahead is how many symbols are seen when a decision must be made. In LL(1) only one symbol is taken, so lookahead = 1.
- Parsing algorithm (the LL(1) parser itself): defines what actions should be taken at each step of parsing.
- LL(1) parsing table: a data structure constructed from the given grammar; it drives the parser's decisions.
- Stack: a data structure used for the procedure of parsing.
- Input buffer: holds the input string being parsed.
- End markers: the bottom of the stack always contains . These two dollar symbols are used just to detect when parsing should stop.
How it works
Any general parser has all of the following components:
- Parsing algorithm (the LL(1) parser itself) — defines what actions should be taken at each step of parsing.
- LL(1) parsing table — constructed from the given grammar; it drives the parser's decisions.
- Stack — used for the procedure of parsing; its bottom always contains $.
- Input buffer — holds the input string being parsed; the input string always ends with $.
The two dollar symbols are used just to detect when parsing should stop.
Drawing the diagram…
Comparison
| Part of name | Meaning |
|---|---|
| First L | Scan the input from Left to right |
| Second L | Use Leftmost derivation while building the parse tree |
| 1 | Number of lookahead symbols used to make a parsing decision |
Revision summary
- LL(1) is top-down: the parse tree is built from the start symbol down to the input string, the reverse of bottom-up (reduction) parsing.
- First L = left-to-right scan of input; second L = leftmost derivation; 1 = one lookahead symbol.
- A general parser has four components: parsing algorithm, LL(1) parsing table (built from the grammar), stack, and input buffer.
- The stack bottom holds $ and the input string ends with $; the two dollars signal when to stop parsing.
- Input is never read from the right side toward the left.
Previous-year exam insights
- An LL(1) grammar must be unambiguous, free of left recursion, and free of left factoring conflicts; left recursion must be eliminated and the grammar left-factored before an LL(1) table can be built.
- FIRST(X) is the set of terminals that can begin strings derived from X (plus ε if X derives ε); FOLLOW(A) is the set of terminals that can immediately follow A in some sentential form (plus $ if A can be the last symbol).
- Table construction rule: for each production A → α, place A → α in M[A, a] for every terminal a in FIRST(α); if ε ∈ FIRST(α), place A → α in M[A, b] for every b in FOLLOW(A).
- A grammar is LL(1) iff no table cell contains more than one entry, equivalently for any two productions A → α and A → β, FIRST(α) and FIRST(β) are disjoint, and at most one of them can derive ε, and if β derives ε then FIRST(α) ∩ FOLLOW(A) = ∅.
- LL(1) parsing is deterministic and non-backtracking; it cannot handle left-recursive or ambiguous grammars, and it cannot parse all context-free languages (e.g., it cannot handle grammars requiring more than one lookahead symbol).
- The parser configuration is (stack, input, output);
the initial configuration is stack = , and acceptance occurs when both stack and input contain only $, with the output being the leftmost derivation.
LL(1) Parser Components
Drawing the diagram…
Shows the four standard components of an LL(1) parser and how the input buffer, parsing table and stack feed the parsing algorithm.