Análisis sintáctico
Introducido
5:58
Teoría de la computación y compiladores
This course asks what machines can decide, and how text becomes meaning. The first three units are theory: automata and regular languages, context-free…
Narración en inglés · subtítulos en inglés y 中文 que puedes cambiar
En esta lección
| English | Significado |
|---|---|
| parser/ˈpɑːsə/ | the part of a compiler that checks tokens against the grammar and builds a parse tree |
| parse tree/pɑːs triː/ | A tree recording how grammar productions generate the structure of a string. |
| LR parser/ˌel ˈɑː ˈpɑːsə/ | a bottom-up parser that reads input left to right and builds the tree from the leaves up by shifting and reducing |
| left-recursive/left rɪˈkɜːsɪv/ | describes a grammar rule whose right side starts with the rule's own symbol, such as E → E + T |
| Left factoring/left ˈfæktərɪŋ/ | |
| empty string/ˈempti strɪŋ/ | |
| lookahead/ˈlʊkəhed/ | the next input token that the parser looks at, without consuming it, to choose a rule |
| shifts/ʃɪfts/ | moves the next input token onto the parser's stack |
| reduces/rɪˈdjuːsɪz/ | replaces symbols on top of the stack that match a rule's right side with the rule's left side |
Tabla más pila
- Los lenguajes libres de contexto requieren un parser; análisis LL(1): un símbolo de anticipación, una tabla de (no terminal, terminal) → producción, una pila. Los conjuntos FIRST/FOLLOW llenan la tabla; los conflictos rechazan la gramática.


La traza, exactamente
- Gramática $E \to T E'$, $E' \to {+} T E' \mid \varepsilon$, $T \to \mathbf{id}$; entrada
id + id: pila $E$; tabla $(E, \mathbf{id})$; coincidencia $\mathbf{id}$; tabla $(E', +)$; coincidencia $+$, coincidencia $\mathbf{id}$; tabla $(E', \$$) $\to \varepsilon$; aceptación — ocho instantáneas de la pila. - Una derivación izquierda es exactamente una disciplina de pila: apilar el lado derecho de la producción, consumir terminales mediante coincidencia; la determinismo de la tabla proviene de la disjuntividad de FIRST/FOLLOW.
Left recursion is removed by —
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
The loop becomes a tail; associativity moves to the AST build.
An LL(1) parser can use $E \to E + T \mid T$ as written.
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
Expanding $E$ pushes $E$ again without consuming input, so the stack grows for ever. Rewrite it as $E \to T E'$, $E' \to {+} T E' \mid \varepsilon$ first.
Eliminar recursión a la izquierda
- $E \to E + T \mid T$ se transforma en $E \to T E'$, $E' \to {+} T E' \mid \varepsilon$ — el bucle se convierte en cola. Es mecánico, pero intercambia la asociatividad izquierda en el árbol: el compilador reasocia al construir el AST.
- Analizar con una gramática recursiva a la izquierda hace que la pila se bucle indefinidamente; un conflicto FIRST/FOLLOW significa que un símbolo de anticipación no puede elegir.
$A \to ab \mid ac$ is LL(1).
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
Both alternatives begin with $a$; left factoring fixes it.
Esta tarjeta es nueva, por eso aparece en inglés hasta que su traducción esté lista.
FIRST sets
- FIRST($X$) is the set of tokens that can begin a string made from $X$. If $X$ can produce the empty string 空串, add $\varepsilon$.
- For our grammar ($F \to (E) \mid \text{id}$): FIRST($F$) $= \{(, \text{id}\}$. $T$ and $E$ both start with $F$, so FIRST($T$) = FIRST($E$) $= \{(, \text{id}\}$. FIRST($E'$) $= \{+, \varepsilon\}$ and FIRST($T'$) $= \{*, \varepsilon\}$.
How many terminals are in FIRST($F$) for $F \to (E) \mid \mathbf{id}$?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
The open bracket and id.
Esta tarjeta es nueva, por eso aparece en inglés hasta que su traducción esté lista.
FOLLOW sets
- FOLLOW($A$) is the set of tokens that can come right after $A$. The parser 语法分析器 needs it when $A$ should produce $\varepsilon$: it uses $A \to \varepsilon$ only if the next token can legally follow $A$.
- Three rules. (1) Put the end marker $ in FOLLOW of the start symbol. (2) For $B \to \alpha A \beta$, add FIRST($\beta$) without $\varepsilon$ to FOLLOW($A$). (3) If $\beta$ can be empty (or is missing), also add FOLLOW($B$). Applied to the grammar:
| Nonterminal | FOLLOW |
|---|---|
| $E$, $E'$ | ) $ |
| $T$, $T'$ | + ) $ |
| $F$ | * + ) $ |
For the expression grammar above, FOLLOW($T'$) is:
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
A term ends where a plus, a closing bracket or the end can follow.
Esta tarjeta es nueva, por eso aparece en inglés hasta que su traducción esté lista.
Building the LL(1) table
- For each rule $A \to \alpha$: put it in row $A$ under every token in FIRST($\alpha$). If $\alpha$ can be empty, also put it under every token in FOLLOW($A$). An empty cell means a syntax error.
id + * ( ) $
E TE' TE'
E' +TE' ε ε
T FT' FT'
T' ε *FT' ε ε
F id (E)
- Each cell holds at most one rule, so the grammar is LL(1): one token of lookahead 向前看符号 always picks the rule. Two rules in one cell is a conflict, and then the grammar is not LL(1).
The LL(1) table is filled from —
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
FIRST chooses expansions; FOLLOW handles the empty production.
A FIRST/FOLLOW conflict means —
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
The grammar is not LL(1): table cell holds two productions.
Esta tarjeta es nueva, por eso aparece en inglés hasta que su traducción esté lista.
Worked example: parsing id + id
- Start with $E\,\$$ on the stack (top on the left). If the top is a rule symbol, replace it using the table. If it is a token that matches the input, remove both.
| Stack | Input | Action |
|---|---|---|
| $E\,\$$ | id + id $ | $E \to TE'$ |
| $TE'\,\$$ | id + id $ | $T \to FT'$ |
| $FT'E'\,\$$ | id + id $ | $F \to$ id |
| id $T'E'\,\$$ | id + id $ | match id |
| $T'E'\,\$$ | + id $ | $T' \to \varepsilon$ (+ is in FOLLOW($T'$)) |
| $E'\,\$$ | + id $ | $E' \to +TE'$ |
| $+TE'\,\$$ | + id $ | match + |
| $TE'\,\$$ | id $ | $T \to FT'$, then $F \to$ id, match id |
| $T'E'\,\$$ | $ | $T' \to \varepsilon$, then $E' \to \varepsilon$ |
| $ | $ | accept |
- Row 5 is the key moment: $T'$ meets
+. FIRST($T'$) has no+, but FOLLOW($T'$) does, so the parser 语法分析器 chooses $T' \to \varepsilon$. This is why FOLLOW sets are needed.
Put the steps of an LL(1) parse in order.
Dificultad 3/5 · 200 monedas + un pequeño bono en el primer intento de hoy
Steps 2 to 4 repeat until the stack is empty; an empty table cell, or a terminal that does not match, is a syntax error at that position.
The stack top is $T'$ and the next token is ). Which rule does the table give?
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
) is not in FIRST($T'$), but it is in FOLLOW($T'$) = {+, ), end marker}, so the parser uses $T' \to \varepsilon$ and the ) is matched later, higher up the stack.
Esta tarjeta es nueva, por eso aparece en inglés hasta que su traducción esté lista.
Bottom-up: shift and reduce
- An LR parser LR分析器 shifts 移进 tokens onto a stack and reduces 归约 when the top of the stack matches the right side of a rule.
- With $E \to E + T \mid T$, $T \to \text{id}$ and input
id + id: shift id; reduce to $T$; reduce to $E$; shift +; shift id; reduce to $T$; reduce $E + T$ to $E$; accept. - LR parsers handle left recursion directly, so the grammar needs no rewriting, and they accept more grammars than LL(1).
- Tools such as yacc and bison build these tables. The dangling
elseis a shift–reduce conflict: the parser shifts, so eachelsepairs with the nearestif.
An LR parser can use the left-recursive rule E → E + T without rewriting it.
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
Bottom-up parsing reduces E + T only after seeing it, so left recursion causes no loop.
The dangling-else conflict is usually resolved by —
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
Shifting keeps the inner if on the stack to take the else.
Esta tarjeta es nueva, por eso aparece en inglés hasta que su traducción esté lista.
Marks that slip away
- Ignoring a table conflict. Two rules in one cell mean the grammar is not LL(1); rewrite it or use an LR parser LR分析器.
- Forgetting FOLLOW for ε-rules. $A \to \varepsilon$ goes under the tokens in FOLLOW($A$), not under FIRST.
- Losing associativity in the rewrite. After removing left recursion,
a - b - cmust still be grouped as(a - b) - cwhen the syntax tree is built.
Esta tarjeta es nueva, por eso aparece en inglés hasta que su traducción esté lista.
Recap
- Top-down (LL) predicts rules from the start symbol; bottom-up (LR) shifts tokens and reduces 归约 them to rules.
- LL(1) needs no left recursion and no common prefixes; FIRST and FOLLOW sets fill its table.
- A cell with two rules is a conflict: the grammar is not LL(1).
¿Qué salió mal? Solo el equipo del sitio ve esta información.