[HD] EECS4302 F26 - 2026-11-26 (Thursday) - Lecture 21
Overview
Jackie Wang explains how to prepare context-free grammars for top-down parsing: eliminate direct and indirect left recursion, remove epsilon productions while preserving the intended language, and use FIRST sets to reduce backtracking. A worked grammar orders Expression, Term, and Factor as variables 1, 2, and 3, then applies the algorithm’s two phases before calculating FIRST sets for arithmetic-expression productions.
Key takeaways
- For A → Aα | β, remove direct left recursion by rewriting it as A → βA′ and A′ → αA′ | ε; this preserves the pattern of zero or more α suffixes after β.
- The indirect-recursion phase checks Ai → Ajγ only for earlier variables j < i, while the direct-recursion phase checks whether Ai produces itself as the leftmost symbol.
- A nullable variable can derive ε directly or through a production whose entire right-hand side is nullable; for k nullable positions, there are 2^k keep-or-omit combinations before deciding whether to retain ε.
- Removing ε productions requires adding alternatives for combinations of omitted nullable symbols, not merely deleting ε rules; repeated symbols can make different combinations produce duplicate alternatives.
- FIRST sets support predictive parsing by identifying possible leading terminals; when a production prefix is nullable, FIRST must also account for later symbols in that production.
- In the arithmetic grammar, FIRST(Factor), FIRST(Term), and FIRST(Expression) all contain the opening parenthesis, num, and name because Factor is not nullable and begins each larger expression production.
Chapters
- The project report template is on e-class, and students are encouraged to review it before asking questions at office hours or the next Tuesday meeting.
- The TA will use the final report to evaluate the compiler submission.
- Milestone Two was released that morning; submissions are due Monday at noon so the TA can prepare feedback for Tuesday's in-class meeting.
- Milestone One was feedback-oriented, and Jackie Wang said everyone received full marks.
- Top-down parsing simulates a leftmost derivation, so a production such as A → Aα can repeatedly expand the leftmost variable and loop indefinitely.
- The course approach is to transform the grammar to remove left recursion rather than redesign the parser to handle it.
- The algorithm must address both direct recursion and indirect recursion through other variables.
- For the example, the variable order is Expression = 1, Term = 2, and Factor = 3, so n = 3.
- For each outer-loop index i, phase one considers j from 1 through i − 1, and phase two checks for direct left recursion at i.
- The ordering can be chosen freely unless an assignment or exam specifies one; the ordering can change the resulting grammar.
- The algorithm processes variable labels, not a special category of start variable.
- At i = 1, phase one has an empty j range, while phase two checks Expression for a production beginning with Expression.
- For Expression → Expression + Term | Term, the recursive suffix is α = + Term and the nonrecursive alternative is β = Term.
- The transformation replaces A → Aα | β with A → βA′ and A′ → αA′ | ε.
- The resulting Expression productions start with Term and use Expression′ to represent repeated + Term suffixes.
- At i = 2, phase one checks whether Term produces Expression followed by a suffix; the example grammar has no such production, so no substitution is made.
- Phase two finds Term → Term * Factor | Factor, a direct left-recursive pattern.
- Here α = * Factor and β = Factor, so Term is rewritten with a Term′ helper and an ε alternative.
- The transformation represents repeated multiplication suffixes without making Term left recursive.
- At i = 3, phase one checks Factor → Expression … and Factor → Term …; neither strict pattern occurs in the example.
- Phase two finds no Factor → Factor … production, so Factor requires no change.
- Term → Factor is not considered during the i = 2 pass because Factor has label 3, outside the j values 1 through i − 1.
- Changing the variable order may produce a different transformed grammar, while the algorithm is intended to eliminate left recursion.
- The direct-recursion transformation introduces ε productions in helper variables such as Expression′ and Term′.
- The lecture next targets grammars without ε productions to simplify tracing the top-down parsing algorithm.
- An ε production cannot simply be deleted: other productions must be expanded to preserve the intended language.
- A variable is nullable if it can derive ε, either directly or because every symbol on the right-hand side of one of its productions is nullable.
- In A → B1 B2 B3, if B1, B2, and B3 are nullable, then A is nullable as well.
- For three nullable positions, enumerate 2³ = 8 keep-or-omit combinations, including the all-omitted ε case.
- When ε is not in the target language, omit the all-null combination but retain the other seven alternatives.
- In the first example, A and B are directly nullable, and S is nullable because S → AB can derive ε when both variables do.
- If the target language excludes ε, S → AB expands to S → A | B | AB.
- For repeated nullable positions such as AA, multiple keep-or-omit choices can produce the same right-hand side, so duplicate alternatives need not be repeated.
- A second example includes a nonnullable C in S → ACB; since C cannot derive ε, S cannot derive ε through that production.
- If a parser chooses the wrong Term′ production, it may build a branch expecting multiplication when the next input token is division, then discard that branch and backtrack.
- Looking at the next input symbol can rule out a production whose first possible token does not match.
- The proposed preprocessing computes the FIRST set for each grammar symbol so a parser can choose more selectively.
- The lecture points students to the course site's top-down parsing trace exercise to see backtracking and ε handling in action.
- FIRST(α) is described as the set of terminals that can appear first in a string derived from α.
- For a terminal such as num, plus, or multiply, its FIRST set is the singleton containing that terminal.
- For a variable, inspect its productions and collect terminals that can begin a derived string.
- If an early symbol in a production is nullable, the first terminal may instead come from a later symbol.
- For the expression grammar, Factor can begin with an opening parenthesis, num, or name, so those terminals belong to FIRST(Factor).
- Term′ can begin with multiply or divide and also has an ε alternative; Expression′ can begin with plus or minus and also has an ε alternative.
- Because Factor is not nullable, FIRST(Term) equals FIRST(Factor); because Term is not nullable, FIRST(Expression) equals FIRST(Term).
- These relationships let the parser use the next token to distinguish arithmetic-expression alternatives.
- When a production begins with a nullable variable, FIRST must include terminals from that variable and may also include terminals from the following symbol.
- In the closing example, a nullable Expression′ means FIRST of the surrounding production must include both Expression′'s possible leading terminals and Factor's FIRST set.
- For a longer right-hand side, continue across successive nullable symbols until reaching a nonnullable symbol or the end.
- The set-comprehension definition captures the general idea, while a systematic algorithm is needed to compute FIRST sets reliably.
Transcript excerpt
0:05 Okay. So uh your project report template has been on the e-class. Please take a look. I think uh uh maybe if you got any questions about it, either drop by my office hour or maybe when we got your project milestone meeting next Tuesday during the class, you can also ask me. I think we have time. Okay, we can definitely go over some sections. You definitely want to get prepared as soon as you can for about the project report. That's definitely something the TA has to rely on in order to evaluate your compiler in the final submission. Okay? And for project project milestone two, uh the guy was just released uh earlier this morning. So please take a look. Okay? The expectation will be just the same. So we're going to have some talk and also have some uh feedback given to you uh on the spot. The submission, the same. Please make
0:53 sure by Monday noon, make sure you submit so the TA can actually get prepared for Tuesday and Tuesday during the class. All right? The same. Okay? Nothing surprising. Okay? Question. >> project milestone once >> Yeah, I will try to post it uh maybe tomorrow, but I think since you guys were there, so maybe try to recall some details from there, but I'll definitely I got some limited notes as well on my side. I'll post them as well. Yeah. I think for milestone one, uh I think everybody got full marks. So that one you don't need to worry. Okay? It's not really meant to have any penalty on you. It's we just give you feedback. Okay? All right. So uh we're going to continue uh with removing the left recursion. Let's now again revisit about the big picture very
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.