[HD] EECS4302 F26 - 2026-12-03 (Thursday) - Lecture 24
Overview
Jackie Wang explains how to compute FIRST and FOLLOW sets for context-free grammars, then combines them into START sets for one-symbol lookahead parsing. The lecture applies fixed-point algorithms and nullable-symbol reasoning to an expression grammar, shows how overlapping START sets expose backtracking and how left factoring can fix a common-prefix conflict, and closes with LL(1) recursive-descent parsing.
Key takeaways
- FIRST for a sequence is computed left to right: collect FIRST sets through nullable components, then stop at the first nonnullable component.
- FOLLOW is computed right to left: a rightmost variable inherits its production's left-hand-side FOLLOW set, while nullable suffixes allow FIRST and FOLLOW information to propagate farther left.
- A nullable production needs FOLLOW information in its START set; otherwise, FIRST alone cannot determine when choosing epsilon is safe.
- Under the lecture's one-symbol lookahead criterion, alternatives are backtrack-free when their START sets are pairwise disjoint.
- Left factoring converts alternatives with a shared prefix into a common-prefix production and a new suffix-selection variable, as in the factor and arguments example.
- LL(1) parsing reads input left to right, builds a leftmost derivation, and uses one lookahead symbol to select recursive-descent branches.
Chapters
- For a terminal such as plus, FIRST contains that terminal itself.
- For a variable, FIRST collects terminals that can begin some string derived from it.
- The definition considers terminal strings in sigma star, while the remainder of a derivation does not affect the leading symbol.
- The getFirst algorithm returns a set of leading terminals for a terminal, variable, epsilon, or EOF key.
- It initializes FIRST(t) to {t} for each terminal and for epsilon and EOF.
- Each grammar variable starts with an empty FIRST set, ready to be populated from productions.
- The FIRST computation repeats until the map cannot grow, using the same fixed-point idea as lazy NFA-to-DFA evaluation.
- For a production A → β₁…βₖ, it adds FIRST sets from left to right, continuing only while earlier components are nullable.
- If every component is nullable, epsilon is added to FIRST(A); a nonnullable component stops further collection.
- Jackie traces the factor productions and shows that a nonnullable first component prevents the algorithm from examining later components.
- Successive productions add the opening parenthesis, number, and name terminals to FIRST(factor).
- The rule order can vary, but iteration must continue until no FIRST set changes.
- The expression-prime alternatives contribute plus, minus, and epsilon to FIRST(expression-prime).
- A hypothetical alternative beginning with term-prime followed by factor demonstrates why later components matter.
- Because term-prime is nullable, FIRST(expression-prime) must also include FIRST(factor).
- The original FIRST operation accepts one terminal or variable; the extension handles a sequence of mixed grammar symbols.
- For β₁…βₙ, include FIRST sets from the left until reaching the first nonnullable component.
- Nullable prefixes allow multiple positions in the sequence to contribute possible leading symbols.
- For term followed by expression-prime, FIRST includes FIRST(term), but term is nonnullable, so the calculation stops there.
- For plus followed by term and expression-prime, the terminal plus blocks consideration of later components.
- The same stopping rule applies to minus, while a sequence consisting only of epsilon contributes epsilon.
- For expression-prime alternatives plus, minus, and epsilon, the epsilon alternative has no terminal lookahead that immediately selects it.
- Choosing epsilon may require later derivation steps and backtracking to determine whether the remaining input can be matched.
- FOLLOW sets add information about what can appear after a derived variable, helping resolve nullable alternatives.
- FOLLOW(V) contains terminals that can appear immediately after a string derived from variable V.
- Its abstract definition considers a derivation from the start variable in which the derived string for V is followed by a terminal string.
- The FOLLOW algorithm assumes FIRST sets have already been computed.
- To compute FOLLOW(V), inspect occurrences of V on production right-hand sides, rather than only looking at left-hand sides.
- A variable at the right edge of a production inherits the FOLLOW set of that production's left-hand-side variable.
- The start variable receives EOF because the complete input is assumed to end at EOF.
- In the expression grammar, expression-prime occurs at the right edge of expression and inherits FOLLOW(expression).
- FOLLOW(expression) includes EOF and the closing parenthesis from the enclosing grammar context.
- Term is followed by expression-prime, so its FOLLOW set includes FIRST(expression-prime), and also FOLLOW(expression) because expression-prime is nullable.
- getFollow initializes every variable's FOLLOW set to empty, then seeds the start variable with EOF.
- It repeats updates while the FOLLOW map changes and terminates when the map reaches a fixed point.
- For each production, the algorithm scans its right-hand side from right to left, unlike the left-to-right FIRST calculation.
- The trailer tracks symbols that may follow the current right-hand-side component during reverse scanning.
- When a component is nullable, its FIRST set can be accumulated into the trailer for the preceding component.
- When a component is nonnullable, its FIRST set replaces the trailer, so the previous trailer does not propagate across it.
- For A → β₁β₂β₃, the rightmost component β₃ inherits FOLLOW(A).
- If β₃ is nonnullable, FOLLOW(β₂) includes FIRST(β₃), but not FOLLOW(β₃).
- If both β₂ and β₃ are nullable, FOLLOW(β₁) can include FIRST(β₂), FIRST(β₃), and FOLLOW(A).
- The example initializes all variable FOLLOW sets as empty and sets FOLLOW(goal) to {EOF}.
- For goal → expression, expression is the sole rightmost component, so FOLLOW(expression) gains EOF.
- For factor → … expression ), the closing parenthesis contributes to FOLLOW(expression); the nonnullable terminal blocks propagation from FOLLOW(factor).
- In expression → term expression-prime, expression-prime inherits FOLLOW(expression), including EOF and a closing parenthesis.
- FOLLOW(term) receives plus and minus from FIRST(expression-prime), and also inherits FOLLOW(expression) because expression-prime is nullable.
- For factor → … term-prime, FOLLOW(factor) includes FIRST(term-prime), multiply and divide, and can inherit FOLLOW(term-prime) because term-prime is nullable.
- START is the lookahead set used to choose a production in a one-symbol lookahead parser.
- For a right-hand side β, START(β) is FIRST(β) when β is nonnullable.
- When β is nullable, START(β) includes both FIRST(β) and FOLLOW of the production's left-hand-side variable.
- A grammar is backtrack-free under this criterion when the START sets of every pair of alternatives for a variable are disjoint.
- The example has multiple factor productions whose START sets contain name, so a name lookahead cannot select one production unambiguously.
- A shared lookahead leaves multiple candidate rules and can force backtracking.
- Left factoring extracts a common prefix α from alternatives αβ₁, αβ₂, through αβₙ.
- The grammar replaces those alternatives with α followed by a new variable that selects among β₁ through βₙ.
- For the factor example, the shared name prefix is extracted and an arguments variable handles the remaining alternatives, including epsilon.
- Left recursion can cause nontermination in a top-down parser, so it must be removed or converted to right recursion.
- Left factoring and one-symbol lookahead address backtracking, but not every context-free language has a backtrack-free grammar.
- Given a grammar, checking the pairwise START-set property is decidable; finding whether a language has some backtrack-free grammar is undecidable.
- A recursive-descent parser is a top-down parser implemented as mutually recursive procedures, often generated by tools such as ANTLR 4.
- LL(1) means reading input Left to right, constructing a Leftmost derivation, with 1 lookahead symbol.
- Parser generators can calculate FIRST, FOLLOW, and START sets automatically, though the lecture emphasizes how to compute them manually.
- The expression-prime procedure chooses the plus or minus alternative when the lookahead is that operator.
- When the lookahead is EOF or a closing parenthesis, it selects the epsilon alternative and succeeds without consuming another symbol.
- If no alternative's START set matches, the recursive-descent procedure returns failure, indicating a syntax error.
Transcript excerpt
0:05 Let's focus on this one first. This is more like the abstract definition for the first set, okay? The alpha here, okay, let's take a look. Whenever you want to do a computation for the first, you can pass an alpha. Alpha can either be a terminal, like a symbol, or it could be a variable. There are two cases. In the case where the alpha is simply a terminal, what will be the first set for terminal? What do you see first in a terminal? Well, just the terminal itself. So that's why it's simply just alpha. That's very straightforward. You can think about that's the base case. What about the recursive case? If you actually got some variable over here,
0:53 basically what you need is to somehow go through some derivation step. Right? You can see that exactly over here. In order for me to figure out the first of certain variable, is going to be all the words. So you can think about the W here, all the terminals or the words. That's why you see W is simply sigma star, right? There's no variable here. For all such W, alpha is able to be derived into something starting with W. And the beta over here is something we don't care. Okay? You can think about W is the starting symbols. And such string can be derived from this
1:41 alpha, which we want to calculate its first set. Okay? That's the definition. And uh yep, that's the abstract definition. That's something you can maybe spend a little bit more time looking into once we have gone through the algorithm. Any part of the definition you are not so sure about, you want me to go over, but I think that's pretty much it. Okay? Just remember the W over here should be all terminals.
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Jackie Wang.