[HD] EECS4302 F26 - 2026-11-24 (Tuesday) - Lecture 20
Overview
Jackie Wang explains why a top-down parser that follows leftmost derivations can loop forever on left-recursive grammars, using the expression grammar and input `a + a * a` as examples. The lecture distinguishes direct from indirect left recursion, traces an equivalent right-recursive grammar, and introduces a two-phase algorithm that substitutes productions to prevent indirect recursion before rewriting direct recursion, potentially adding epsilon productions.
Key takeaways
- A top-down parser that follows leftmost derivations can loop on `expression -> expression + term` because it repeatedly expands the leftmost `expression` without consuming an input token.
- To detect left recursion, follow only the leftmost variable in each production: a self-loop is direct recursion, while a multi-variable cycle such as `A -> B -> C -> A` is indirect recursion.
- A grammar can preserve the language of arithmetic expressions while avoiding parser loops by using helper variables such as `expression prime` and epsilon productions.
- The left-recursion removal algorithm uses indexed variables and nested loops: first it substitutes earlier-variable productions to prevent indirect recursion, then it rewrites direct recursion.
- The standard direct-recursion rewrite `Ai -> Ai α | β` becomes `Ai -> β Ai′` and `Ai′ -> α Ai′ | ε`, preserving one base case followed by any number of repetitions.
- Assignment 3 assumes input grammars have no left recursion, while the general transformation discussed in lecture assumes no input epsilon productions and may introduce epsilon productions in its output.
Chapters
- The project report template is available early, so teams can begin drafting and raise questions before the Milestone 2 meeting.
- The in-person Milestone 2 meeting is tentatively planned for Tuesday next week, pending confirmation of the TA's availability.
- An optional programming-test makeup is expected next Thursday, with the same coverage and format as the original test; Jackie Wang plans to confirm details in class.
- The example uses an expression grammar and the input tokens `a`, `+`, `a`, `*`, `a`, followed by an end-of-input marker.
- Initialization creates a root for the start variable `expression`, sets the focus to that root, and initializes a stack with a null value.
- When expanding `expression -> expression + term`, the parser creates three children, pushes `term` and `+` in reverse order, then focuses on the leftmost child.
- The parser tracks which productions have been tried separately for each `expression` node, rather than sharing rule-visit state across the entire parse tree.
- Choosing `expression -> expression + term` again creates a new leftmost `expression` child and directs the focus to it.
- The same production remains available at the new child, setting up another identical expansion.
- A third expansion of `expression -> expression + term` again places `expression` at the leftmost focus, so the parser can repeat without consuming input.
- The valid input `a + a * a` and the parsing algorithm are not the cause of the failure; the left-recursive grammar is.
- A top-down parser following leftmost derivations cannot make progress when the leftmost variable immediately derives itself.
- The lecture identifies productions such as `expression -> expression + term` as left recursive because the leftmost variable on the right-hand side is the left-hand-side variable.
- A left-recursive grammar can cause an infinite loop in this leftmost, top-down parsing process.
- One solution is to change the parser to handle left recursion; the lecture instead focuses on transforming the grammar into a non-left-recursive form.
- Direct left recursion includes rules such as `expression -> expression + term` and `term -> term * factor`.
- Dependency diagrams follow the leftmost variable: a self-loop indicates direct recursion, while a cycle such as `A -> B`, `B -> C`, `C -> A` indicates indirect recursion.
- The relevant dependency is specifically through the leftmost variable, not every variable appearing anywhere on a production's right-hand side.
- Jackie Wang contrasts two grammars that generate the same language but differ in whether their recursion causes the top-down parser to loop.
- The non-left-recursive version uses helper variables such as `expression prime` and `term prime`, with epsilon productions to represent the end of repetition.
- The class begins a manual parse-tree exercise to verify that the transformed grammar can produce `a + a * a`.
- The parse begins with `expression -> term expression prime`, then expands `term -> factor term prime` and `factor -> a` for the first token.
- The first `term prime` uses epsilon because the first `a` is followed by `+`, not multiplication.
- The `expression prime` expands to `+ term expression prime`; that term produces `a * a` through `term prime -> * factor term prime`, followed by epsilon productions.
- Reading the leaves yields `a + a * a`; the same grammar can also be practiced through parse trees, leftmost derivations, or rightmost derivations.
- The class discusses whether ANTLR handles left-recursive grammars internally, possibly by preprocessing them before parsing.
- For Assignment 3, the supplied grammars are guaranteed to contain no left recursion, so students do not need to implement its removal.
- A tracing video on the lecture site demonstrates the top-down algorithm with the right-recursive grammar.
- The grammar-transformation algorithm takes a grammar as input and produces a grammar as output; an input without left recursion may remain unchanged.
- The input grammar is assumed to have no epsilon productions.
- The transformed grammar may introduce epsilon productions while removing left recursion, with epsilon removal presented as a later simplification step.
- The algorithm assigns indices `1` through `n` to the grammar's variables, stored in a linear collection such as an array or `ArrayList`.
- For each `i`, the inner index `j` ranges from `1` to `i - 1`; with `n = 4`, the successive `j` ranges are empty, `1`, `1–2`, and `1–3`.
- The variable order is not required to begin with the grammar's start variable; changing the order changes processing order, not the goal of eliminating recursion.
- The algorithm has two phases: preemptively break paths that could form indirect left recursion, then remove direct left recursion.
- For a production `Ai -> Aj γ`, where `Aj` is the leftmost variable and `i` differs from `j`, substitute each alternative `Aj -> δk`.
- The resulting alternatives are `Ai -> δ1 γ | δ2 γ | ... | δn γ`, replacing the leading `Aj` with each of its possible right-hand sides.
- For rules `Ai -> Ai α | β`, repeated left-recursive expansion produces one `β` followed by zero or more copies of `α`.
- The equivalent right-recursive form is `Ai -> β Ai′` and `Ai′ -> α Ai′ | ε`, which places the base case first and makes repetition terminate through epsilon.
- The transformation relies on recognizing the generated-string pattern rather than memorizing the symbolic rewrite.
- Phase one prevents indirect cycles through production substitution; phase two converts direct left recursion into right recursion using a helper variable.
- Jackie Wang advises understanding how each symbolic pattern transforms the grammar; the algorithm itself will be provided on the exam.
- The next class will apply the procedure to a concrete grammar, and the makeup-test details will be confirmed then.
Transcript excerpt
0:05 Okay. So, some important announcement to make. Um, first of all, your project template, I kind of released that early. So, please take a look. We can definitely start writing a report early uh for your team. That's definitely something you can do. But if you got any questions about the report, do let me know or we can speak about it during the milestone 2 meeting. The pattern is going to be the same. I'll give you the guide uh before the Thursday class but there'll be no surprise since submission procedure. So, we're gonna have an inerson meeting. Uh, it's going to be tenatively Tuesday next week, but I just need to confirm the availability of the TA, but most likely you can assume you can mark that in pencil on your calendar. And then, okay, so this is something uh hopefully that will help optional I think for
0:52 programming test for those of you who didn't do as well makeup test. Okay, so the same coverage and I'm not going to make it more difficult, just the same. All right. So, I think I will find a time uh most likely uh next uh Thursday. All right. I will confirm. Okay. So, I'm going to confirm in class on Thursday uh this week. So, you got about a week to really do it. So, there's no uh additional coverage. There's no additional question. So, you can expect the same format. Okay. So uh but just to be fair to your fellow student who did well in the first time but you can still get an A for sure you know for this test okay it's fair game okay but it's optional so if you actually
1:40 don't feel like you want to attend it you just don't come right but I think
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.