Lexical analysis, parsing and syntax-directed translation
The compiler front end: tokens and lexemes, FIRST and FOLLOW sets, LL(1) parsing, shift-reduce and LR parsers (LR(0), SLR, LALR, CLR) with their conflicts, and evaluating syntax-directed translations with S- and L-attributed definitions.
The lexical analyser (scanner) reads characters and groups them into tokens.
A token is a category, such as id, num, relop or the keyword if.
A lexeme is the actual character sequence matched, such as count or >=.
A pattern is the rule (a regular expression) describing the lexemes of a token.
The scanner is a DFA built from the regular expressions of all tokens. It also removes white space and comments, tracks line numbers, and enters identifiers into the symbol table.
Two rules resolve clashes.
Longest match (maximal munch): take the longest prefix that matches some pattern. So >= is one token, not > then =, and a+++b is read as a ++ + b.
Priority: if two patterns match the same longest lexeme, the one listed first wins. Keywords are listed before id, so if is a keyword but iffy is an identifier.
Counting tokens (a frequent GATE question). Count every identifier, keyword, constant, operator and punctuation mark once. A whole string literal such as "x = %d" is one token. Comments and white space produce no tokens. Multi-character operators (<=, ==, ++, <<, ->) are single tokens.
Lexical errors are only those the scanner can see: an illegal character, an unterminated string or comment, a malformed number. A misspelt keyword like whlie (x) is just an identifier to the scanner; the parser reports the error.
Front end of a compiler: the scanner feeds tokens to the parser, whose productions trigger semantic actions
2. FIRST, FOLLOW and LL(1) parsing
FIRST() is the set of terminals that can begin a string derived from , plus if . For : take FIRST() without ; if is nullable, add FIRST(), and so on; add only if all are nullable.
FOLLOW() is the set of terminals that can appear immediately after in some sentential form. Rules: put in FOLLOW(). For , add FIRST() . If is nullable (or empty), add FOLLOW(). Repeat until nothing changes. FOLLOW never contains .
LL(1) table. For each production : put it in for every FIRST(); if is nullable, also for every FOLLOW(). The grammar is LL(1) iff no cell gets two entries. Equivalently, for every pair :
FIRST() and FIRST() are disjoint, and
if , then FIRST() and FOLLOW() are disjoint.
Preparing a grammar for top-down parsing.
Left recursion makes a top-down parser loop. becomes , .
Left factoring removes a common prefix: becomes , .
A left-recursive or ambiguous grammar is never LL(1). Removing left recursion and left factoring are necessary steps, but they do not guarantee LL(1).
Recursive descent is the hand-written version: one function per non-terminal, choosing a production by the next token.
3. Bottom-up parsing: shift-reduce and LR parsers
A bottom-up parser builds the rightmost derivation in reverse. A shift-reduce parser keeps a stack and does one of four actions: shift the next token, reduce a handle (the right side of a production at the top of the stack, whose reduction is a step of the reverse rightmost derivation), accept, or report error. Every reduction corresponds to one interior node of the parse tree.
LR(0) items are productions with a dot: . The canonical LR(0) collection is built with closure and goto, starting from for the augmented grammar.
Parser
Items
Reduce on
States
LR(0)
LR(0)
every terminal
SLR(1)
LR(0)
FOLLOW()
LALR(1)
LR(1) cores merged
the item's lookaheads
CLR(1)
LR(1)
the item's lookahead
Conflicts.
Shift-reduce: a state has a complete item and an item with in the reduce set.
Reduce-reduce: two complete items whose reduce sets overlap.
Merging CLR states into LALR (same core, union of lookaheads) can create reduce-reduce conflicts, but never a new shift-reduce conflict, because shift actions depend only on the core.
Power. LR(0) ⊂ SLR(1) ⊂ LALR(1) ⊂ CLR(1), each proper, for grammars. Every LL(1) grammar is LR(1), but an LL(1) grammar need not be LALR(1). No ambiguous grammar is LL(1) or LR(). Parser generators such as YACC and Bison build LALR(1) parsers.
Operator precedence parsing works on operator grammars: no -productions and no two adjacent non-terminals on any right side.
Grammar classes accepted by deterministic parsers: each LR box properly contains the one inside it; LL(1) grammars all lie inside LR(1)
4. Syntax-directed translation
A syntax-directed definition (SDD) attaches attributes to grammar symbols and semantic rules to productions. An SDT scheme puts actions inside the right sides, which run when the parser reaches them.
A synthesized attribute of is computed from attributes of its children (and itself): .
An inherited attribute of a symbol is computed from its parent and siblings: in .
Class
Allowed rules
Evaluated by
S-attributed
synthesized attributes only
a bottom-up (LR) parser, at each reduction
L-attributed
synthesized, plus inherited attributes of in that use only inherited attributes of and attributes of
one left-to-right depth-first pass; fits top-down parsing
Every S-attributed SDD is L-attributed. A rule that uses an attribute of a right sibling, such as , breaks the L-attributed condition.
Evaluating a translation (GATE pattern). The grammar fixes precedence and associativity, whatever the operator symbols look like:
An operator generated deeper in the grammar (closer to the terminals) binds tighter.
A left-recursive production () makes the operator left-associative; a right-recursive one makes it right-associative.
Draw the parse tree, then apply the semantic rules bottom-up. Use the rule given, not the usual meaning of the symbol.
Actions that print. In a scheme such as , actions at the end of productions run in postorder, so the output is the postfix form of the input. Actions placed before a symbol run when the parser reaches that point in a top-down parse. To read the output, walk the parse tree depth-first and print each action when you pass it.
Formulas to remember
Left recursion removal
Left factoring
LL(1) condition for A → α | β
Parser power
State counts
Common mistakes
Counting a string literal as several tokens, or counting comments; a literal is one token and comments give none.
Splitting multi-character operators such as >=, ++ or << into two tokens instead of applying longest match.
Putting ε in a FOLLOW set, or forgetting to add $ to FOLLOW of the start symbol.
Using the usual meaning of + and * in a translation question instead of the semantic rule and the precedence fixed by the grammar.
Believing that LALR merging can create shift-reduce conflicts; it can create only reduce-reduce conflicts.
Assuming that removing left recursion and left factoring always makes a grammar LL(1).
Worked examples
Try each step yourself, then reveal the working.
Example 1: Counting tokens
numerical
How many tokens does the C statement int x = a+++b; contain?
Step 1: Apply longest match to `a+++b`.
Try it on paper first.
Example 2: FIRST, FOLLOW and the LL(1) table
numerical
Grammar: , , . Find FIRST and FOLLOW of each non-terminal and decide whether the grammar is LL(1).
Step 1: Find FIRST of A and B.
Try it on paper first.
Example 3: Counting LR(0) states
numerical
How many states does the LR(0) automaton have for , (after adding )?
Step 1: Build $I_0$.
Try it on paper first.
Example 4: Evaluating a syntax-directed definition
numerical
SDD: , , , , . Find for the input .
Step 1: Read precedence from the grammar.
Try it on paper first.
Previous year questions
43 official GATE CSE questions on this topic, newest first.
10 questions: up to 4 official PYQs and original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.
Mark as mastered
Loading your progress…
My revision notes
Notes written by the GATEiit team against the official GATE CSE syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.