LL(1) Parsing: Working Mechanism and Parsing Table Construction for a Given Grammar

LL(1) Parsing: Working Mechanism and Parsing Table Construction for a Given Grammar

Verified Sources
Nov 3, 2026

An LL(1) parser is a deterministic parser that builds a leftmost derivation using a single lookahead symbol. Its core idea is: at each step, look at the current input token (lookahead) and the top stack nonterminal, then consult the predictive parsing table to choose the unique production to apply.

At a high level, predictive parsing works like this:

  1. Maintain a stack holding grammar symbols to be matched/expanded.
  2. Repeatedly compare the stack top with the current input symbol.
  3. If the stack top is a terminal, match it (advance input).
  4. If the stack top is a nonterminal, consult the parsing table entry for (nonterminal, lookahead) to pick the production and replace the nonterminal on the stack with the production’s RHS (pushed in reverse order).

Below is the standard control flow expressed as a state-style diagram.

LL(1) Parsing (Predictive Parsing) Explained

Key definitions you’ll use

  • FIRST set: determines which terminals can start derivations.
  • FOLLOW set: determines what can legally follow a nonterminal in a sentential form.
  • LL(1) condition: for every nonterminal AA and lookahead aa, at most one production is chosen.

These sets are used to fill the parsing table so that M[A,a]M[A,a] is unique when the grammar is LL(1).

Working of an LL(1) Parser (Predictive Parsing Algorithm)

  1. 1
    Step 1

    Push end-marker $ on the stack, then push the start symbol S. Set lookahead a to the first input token.

  2. 2
    Step 2

    Let X be the stack top symbol.

  3. 3
    Step 3

    If X equals the current lookahead a, pop X and advance to the next input token; otherwise reject due to mismatch.

  4. 4
    Step 4

    Consult the table entry M[X,a]. If it contains a production X → α, pop X and push α onto the stack (rightmost symbol first). If the entry is empty, reject.

  5. 5
    Step 5

    Accept when the stack top is andthelookaheadisand the lookahead is (both are at end-marker).

Constructing the LL(1) parsing table for the given grammar

Given:

S→aA∣bBS \rightarrow aA \mid bB A→aA \rightarrow a B→bB \rightarrow b

We will build M[nonterminal,lookahead]M[\text{nonterminal}, \text{lookahead}] using FIRST/FOLLOW-based rules.

Step 1: Compute FIRST sets

  • From A→aA \rightarrow a, we have FIRST(A) = { aa }.
  • From B→bB \rightarrow b, we have FIRST(B) = { bb }.
  • From S→aA∣bBS \rightarrow aA \mid bB:
    • The first terminal of aAaA is aa
    • The first terminal of bBbB is bb

So FIRST(S) = { a,ba, b }.

Step 2: Compute FOLLOW sets (needed only to handle ε-productions)

None of the productions produce ε (empty string). Therefore:

  • There are no ε-derived contributions to table entries via FOLLOW.
  • In standard LL(1) table construction, ε-handling affects entries when a production can derive ε. Since that does not happen here, we can directly place entries using FIRST of the RHS.

We still need the end marker convention for SS:

  • [FOLLOW(S)][FOLLOW(S)] includes [[end-marker](commonly(commonly[$])because) because S$ is the start symbol.
  • AA and BB appear at the end of RHS as aAaA and bBbB, so their FOLLOW sets would include what can follow those positions—but again, ε is absent, so they will not produce extra table entries beyond those already determined by FIRST.

Table construction rules used (LL(1))

For each production A→αA \rightarrow \alpha:

  • For every terminal t∈FIRST(α)t \in FIRST(\alpha), add A→αA \rightarrow \alpha to M[A,t]M[A,t].
  • If ε∈FIRST(α)ε \in FIRST(\alpha), then for every t∈FOLLOW(A)t \in FOLLOW(A) add A→αA \rightarrow \alpha to M[A,t]M[A,t].

Here, εε is not present anywhere, so we only use the first rule.

LL(1) Parsing Table for the Grammar

Terminals appearing in lookahead are: {a,b}\{a,b\} (and [\]$ for acceptance).

Fill entries

  1. For S→aAS \rightarrow aA:

    • FIRST(aA)={a}FIRST(aA)=\{a\}, so M[S,a]=S→aAM[S,a] = S \rightarrow aA
  2. For S→bBS \rightarrow bB:

    • FIRST(bB)={b}FIRST(bB)=\{b\}, so M[S,b]=S→bBM[S,b] = S \rightarrow bB
  3. For A→aA \rightarrow a:

    • FIRST(a)={a}FIRST(a)=\{a\}, so M[A,a]=A→aM[A,a] = A \rightarrow a
  4. For B→bB \rightarrow b:

    • FIRST(b)={b}FIRST(b)=\{b\}, so M[B,b]=B→bM[B,b] = B \rightarrow b

All other M[⋅,⋅]M[\cdot,\cdot] entries are error/empty.

LL(1) Parsing Table Entries (Non-empty Cells)

Each row is a stack nonterminal; each column is the lookahead terminal. Non-empty cells contain the chosen production.

For clarity, here is the parsing table in a conventional grid form:

Nonterminal \ Lookaheadab$
SS→aAS \rightarrow aAS→bBS \rightarrow bB—
AA→aA \rightarrow a——
B—B→bB \rightarrow b—

“—” means: error (no rule for that (nonterminal, lookahead) pair).

Pro Tip: How to predict quickly

Because there are no ε-productions, your LL(1) table here is determined purely by the first terminal of each RHS. That’s why each entry is forced by seeing whether the lookahead is aa or bb.

Warning: Empty table entries mean rejection

If the parser is at a nonterminal XX and the lookahead token is tt, and M[X,t]M[X,t] is empty, LL(1) parsing must reject—there is no backtracking in predictive parsing.

How LL(1) Parsing Chooses Productions

Look at stack top and lookahead

1

Compute (X, a) where X is the stack top and a is the current input token."

Table lookup

2

Find M[X,a]M[X,a] in the LL(1) parsing table."

Apply production

3

If present, replace X with the production RHS (push RHS reversed)."

Terminal matching

4

If stack top is terminal, match it and advance input."

Accept or reject

5

Accept at ($$, $). Reject if a mismatch or empty table entry occurs."

LL(1) Quick Check Deck

1 / 4
Question · Term

What does M[X,a] represent in an LL(1) parser?

Click to reveal
Answer · Definition

The unique production to expand nonterminal X when the current lookahead token is a.

Knowledge Check

Question 1 of 4
Q1Single choice

In LL(1) predictive parsing, what guides the choice of which production to apply for a nonterminal X?