GATE GUIDE

Parsing Questions in GATE CSE: LL(1), SLR, LALR and CLR

By MD ANISH AHAMADUpdated 1 Oct 202610 min read

Parsing questions in GATE CSE are mechanical. You are handed three to six productions and asked for a set, a count, or a class name. Marks are lost on bookkeeping: an epsilon that should not be in a FOLLOW set, a goto that should have pointed back at an existing state, the accept item counted as a reduce. This guide works the full procedure on one tiny grammar.

In this guide
  1. Key takeaways
  2. Where parsing sits in the paper
  3. One grammar to work on
  4. FIRST and FOLLOW, mechanically
  5. The LL(1) test in one line
  6. Left recursion, common prefixes and left factoring
  7. LR(0) items and how to count them
  8. Shift-reduce, reduce-reduce, and what each parser buys
  9. The dangling-else grammar
  10. Answering "which of these is LL(1) or SLR(1)" fast
  11. Budgeting time on exam day

Key takeaways

Where parsing sits in the paper

On the third-party analyst compilation this guide uses, Compiler Design marks per paper from 2009 to 2026 run 2, 4, 3, 4, 3, 6, 4, 5, 3, 4, 6, 4, 8, 4, 5, 8, 5, 5. That is an average of about 4.6 marks, a minimum of 2 and a maximum of 8. Split the record in half and the 2009-2017 average is about 3.8 against about 5.4 for 2018-2026, so the subject has roughly doubled from its 2009-2013 level of 2 to 4 marks. These figures are unofficial and approximate: analysts disagree by two or three marks because borderline questions get filed under Theory of Computation instead.

Inside that budget, parsing is the largest block. LR and LL parsing, FIRST and FOLLOW, and syntax-directed translation are the recurring items, and data-flow analysis has joined them since 2021. Compiler Design and Digital Logic are the most formula-stable subjects in the paper, which is why they return the best marks per hour. The rest of the subject map is in Compiler Design important topics, and classification questions overlap with Theory of Computation important topics. The Section 7 syllabus wording is unchanged for 2027, but check the current notification rather than any copied syllabus.

One grammar to work on

Everything below uses this grammar, with SS as the start symbol.

S→A B cA→a∣εB→b∣ε\begin{aligned} S &\to A\,B\,c \\ A &\to a \mid \varepsilon \\ B &\to b \mid \varepsilon \end{aligned}

It is nullable in two places on purpose, because the nullable case is where the errors live.

FIRST and FOLLOW, mechanically

FIRST of a string is what can start it. For A→X1X2…XnA \to X_1 X_2 \dots X_n, add FIRST(X1)∖{ε}\text{FIRST}(X_1) \setminus \{\varepsilon\}; move to X2X_2 only if X1X_1 is nullable; if every XiX_i is nullable, add ε\varepsilon. FOLLOW of a non-terminal is what can stand immediately after it. Seed FOLLOW of the start symbol with the end marker $. For every occurrence A→αBβA \to \alpha B \beta, add FIRST(β)∖{ε}\text{FIRST}(\beta) \setminus \{\varepsilon\} to FOLLOW(B)\text{FOLLOW}(B), and add FOLLOW(A)\text{FOLLOW}(A) to FOLLOW(B)\text{FOLLOW}(B) when β\beta is empty or nullable. Repeat until no set grows.

Symbol FIRST FOLLOW Reason
SS {a,b,c}\{a, b, c\} {$}\{\$\} AA nullable, so BB contributes bb; BB nullable, so cc enters
AA {a,ε}\{a, \varepsilon\} {b,c}\{b, c\} FIRST(Bc)\text{FIRST}(Bc) is bb, and BB is nullable so cc joins
BB {b,ε}\{b, \varepsilon\} {c}\{c\} only cc can stand after BB

Two rules cover almost every mistake made here. Epsilon belongs in FIRST sets and never in a FOLLOW set, because FOLLOW holds terminals that actually appear to the right, and epsilon is the absence of a terminal rather than one. And $ belongs in FOLLOW of every non-terminal that can end a sentence, not only the start symbol.

The LL(1) test in one line

A grammar is LL(1) when, for every non-terminal AA and every pair of distinct productions A→αA \to \alpha and A→βA \to \beta:

It is a pairwise test, so a non-terminal with four alternatives needs (42)=6\binom{4}{2} = 6 checks, and the grammar fails the moment one pair fails.

Filling the table is the same test written out. Production A→αA \to \alpha goes into cell M[A,t]M[A, t] for every terminal t∈FIRST(α)t \in \text{FIRST}(\alpha), and also into M[A,t]M[A, t] for every t∈FOLLOW(A)t \in \text{FOLLOW}(A) when α⇒∗ε\alpha \Rightarrow^* \varepsilon. Any cell holding two productions is a conflict.

For the grammar above: S→ABcS \to ABc goes under aa, bb and cc; A→aA \to a under aa; A→εA \to \varepsilon under bb and cc; B→bB \to b under bb; B→εB \to \varepsilon under cc. Eight filled cells, none with two entries, so the grammar is LL(1). The FIRST sets of A→aA \to a and A→εA \to \varepsilon being different was not enough on its own. What decided it was that a∉FOLLOW(A)a \notin \text{FOLLOW}(A).

Left recursion, common prefixes and left factoring

A predictive parser picks a production from one lookahead token and then commits. Two grammar shapes make that impossible.

Left recursion. With E→E+T∣TE \to E + T \mid T, FIRST(E+T)\text{FIRST}(E + T) contains everything in FIRST(E)\text{FIRST}(E), so the alternatives can never be distinguished. Immediate left recursion is removed by rewriting A→Aα∣βA \to A\alpha \mid \beta as A→βA′A \to \beta A' and A′→αA′∣εA' \to \alpha A' \mid \varepsilon. Indirect left recursion, where AA derives BB which derives AA, needs the same trick after substituting in a fixed non-terminal order.

Common prefix. With S→aSb∣abS \to aSb \mid ab, seeing aa tells you nothing about which alternative to take. Left factoring pulls the prefix out: S→aXS \to aX with X→Sb∣bX \to Sb \mid b.

One warning appears as an option almost every year: these two transformations change the grammar, not the language, and neither removes ambiguity. An ambiguous grammar stays outside every LL and LR class.

LR(0) items and how to count them

An item is a production with a dot somewhere in the right side. A production with kk symbols on the right gives k+1k + 1 items; an epsilon production gives exactly one item, A→⋅A \to \cdot; the augmented production S′→SS' \to S gives two.

For our grammar: 4 items from S→ABcS \to ABc, 2 from A→aA \to a, 1 from A→εA \to \varepsilon, 2 from B→bB \to b, 1 from B→εB \to \varepsilon, plus 2 for the augmented production:

4+2+1+2+1+2=124 + 2 + 1 + 2 + 1 + 2 = 12

Twelve items in all.

Building the canonical collection is closure and goto, repeated. Closure adds B→⋅γB \to \cdot\gamma for every non-terminal BB sitting just after a dot. Goto on XX shifts the dot past XX in every item that has XX after the dot, then closes the result. The commonest counting error is creating a fresh state for a goto that lands on a set you already have, so check each new set against the existing ones before numbering it.

Our grammar gives seven states. The start state closes to four items, one of which is the reduce item A→⋅A \to \cdot, while the same state also shifts on aa. In LR(0) terms that is a shift-reduce conflict, so the grammar is not LR(0).

Shift-reduce, reduce-reduce, and what each parser buys

A shift-reduce conflict is a state that holds a complete item A→α⋅A \to \alpha\cdot and also an item with a terminal after the dot: the parser can reduce or read one more token. A reduce-reduce conflict is a state holding two complete items: the parser knows it must reduce but not by which production. Lookahead is what separates them.

Parser Lookahead used for a reduce Number of states What it resolves
LR(0) none, reduce on every terminal canonical LR(0) collection nothing
SLR(1) FOLLOW(A)\text{FOLLOW}(A), the same set in every state same as LR(0) conflicts where the rival terminal lies outside FOLLOW(A)\text{FOLLOW}(A)
LALR(1) merged LR(1) lookaheads, per state same as LR(0) and SLR(1) almost everything CLR does
CLR(1) the exact LR(1) lookahead carried in each item at least as many as LALR(1) everything one token of lookahead can

The containment stated in exam options is LL(1)⊆SLR(1)⊆LALR(1)⊆CLR(1)\text{LL}(1) \subseteq \text{SLR}(1) \subseteq \text{LALR}(1) \subseteq \text{CLR}(1), with LR(0)⊆SLR(1)\text{LR}(0) \subseteq \text{SLR}(1) as well. Return to our grammar and SLR settles it: the reduce A→εA \to \varepsilon is allowed only on FOLLOW(A)={b,c}\text{FOLLOW}(A) = \{b, c\}, the competing shift is on aa, and the sets are disjoint. The same holds for BB. So the grammar is SLR(1) but not LR(0), which is exactly the shape of the standard classification question.

Now the LALR fact tested as a true-or-false option again and again. Merging two CLR states with the same core leaves the shift actions untouched, because identical cores have identical symbols after the dots; only the reduce lookahead sets are unioned. So merging can turn two reduces that were separated by disjoint lookaheads into one state with overlapping ones, creating a reduce-reduce conflict, and it can never create a shift-reduce conflict. Two consequences follow: LALR never has fewer states than SLR, and a grammar can be CLR(1) without being LALR(1).

The dangling-else grammar

S→iSeS∣iS∣aS \to iSeS \mid iS \mid a, with ii for if and ee for else, is the standard ambiguous example and is worth knowing cold. Top-down, the two alternatives share the prefix iSiS; after left factoring you get S→iSX∣aS \to iSX \mid a with X→eS∣εX \to eS \mid \varepsilon, and the cell M[X,e]M[X, e] now holds both productions, because e∈FIRST(eS)e \in \text{FIRST}(eS) and also e∈FOLLOW(X)e \in \text{FOLLOW}(X). Bottom-up, the same ambiguity is a shift-reduce conflict on ee, resolved by shifting, which attaches each else to the nearest unmatched if. The grammar itself stays ambiguous and stays outside every LR class.

Answering "which of these is LL(1) or SLR(1)" fast

Run the cheap tests before the expensive ones.

  1. Is the grammar ambiguous? If yes, it is neither LL(1) nor LR(kk) for any kk, and you are done.
  2. Is there left recursion or a common prefix? Either one rules out LL(1) without computing a single set.
  3. Only then compute FIRST and FOLLOW, and only for the non-terminals the options mention.
  4. For LR classification, augment, build the collection, and stop at the first state holding a complete item alongside anything else. Check it against FOLLOW before declaring a conflict.
  5. If the ask is a count, recount the accept item S′→S⋅S' \to S\cdot and the epsilon items deliberately. That is where the errors cluster.

The recurring slips, including those from other subjects, are collected in common mistakes that cost marks, and the item-count and state-count formulas sit in the GATE CSE formula sheet.

Budgeting time on exam day

These questions reward practice rather than thought, which is what you want under a clock. A FIRST or FOLLOW question should take under a minute once the sets are in a column. An LL(1) table question takes about two minutes. A canonical collection for a four-production grammar takes two to three minutes if you draw the states in a fixed layout every time you practise, and much longer if you improvise the layout each attempt.

So drill the layout, not only the theory. Take five small grammars, build the full collection for each on paper, and time yourself. Redo the same five a week later. The Compiler Design chapter of the GATE CSE 2027 book carries a worked bank of such grammars with the canonical collections drawn out state by state and the conflicts marked, which is the part that is hard to get from prose.

Frequently asked questions

How do you compute FIRST and FOLLOW sets quickly?

Work left to right. FIRST of a terminal is itself. For A→X1X2…A \to X_1 X_2 \dots add FIRST(X1)\text{FIRST}(X_1) without ε\varepsilon, and move on to X2X_2 only if X1X_1 is nullable. For FOLLOW, put the end marker in FOLLOW of the start symbol, then for every occurrence of BB in A→αBβA \to \alpha B \beta add FIRST(β)\text{FIRST}(\beta) without ε\varepsilon, plus FOLLOW(A)\text{FOLLOW}(A) when β\beta is nullable or absent. Iterate until nothing changes.

Why can epsilon never appear in a FOLLOW set?

FOLLOW(A)\text{FOLLOW}(A) is the set of terminals that can appear immediately to the right of AA in some sentential form, so every member is a real terminal or the end marker. Epsilon is not a terminal, it is the absence of one. When the part after AA derives ε\varepsilon you do not add ε\varepsilon, you add FOLLOW of the left side instead.

How do you check whether a grammar is LL(1)?

Test every pair of productions of the same non-terminal. For A→αA \to \alpha and A→βA \to \beta, FIRST(α)\text{FIRST}(\alpha) and FIRST(β)\text{FIRST}(\beta) must be disjoint, and if one of them derives ε\varepsilon, the FIRST set of the other must be disjoint from FOLLOW(A)\text{FOLLOW}(A). If every pair passes, no cell of the parse table holds two productions and the grammar is LL(1).

What is the difference between SLR, LALR and CLR parsers?

All three build on the same items. SLR reduces by A→αA \to \alpha on every terminal in FOLLOW(A)\text{FOLLOW}(A), which is coarse. CLR carries an exact lookahead inside each item, which is precise but produces the most states. LALR merges CLR states that share a core, so it has the same state count as SLR with lookaheads that are usually as sharp as CLR.

Can merging states in LALR create a shift-reduce conflict?

No. Merging joins states with identical cores, so the shift actions are identical in both and the union of the lookahead sets only affects reduce entries. A shift-reduce conflict would already have existed in CLR. Merging can, however, union two reduce lookahead sets that were disjoint before and create a reduce-reduce conflict.

How many marks does Compiler Design carry in GATE CSE?

On unofficial analyst compilations the subject averaged about 4.6 marks per paper from 2009 to 2026, with a low of 2 and a high of 8. The average was near 3.8 in the earlier half and near 5.4 from 2018 onward, so the trend is upward. Different analysts differ by two or three marks on borderline questions. Check the current notification for the pattern in force.

Sources

Dates, fees and the syllabus are set by the GATE 2027 organising institute and can change. Always confirm at gate2027.iitm.ac.in.

Keep reading

GATE CSE 2027 book1,020 pages · ₹250 ₹300
Buy now — ₹250