การแยกองค์ประกอบ
แนะนำใหม่ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| 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ɪŋ/ | |
| 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 |
What a parser does
- The lexer turns source code into tokens, for example
id + id * id. The parser 语法分析器 checks that the tokens follow the grammar and builds a parse tree 语法分析树 that shows how they group: here*groups before+. - There are two strategies. A top-down parser starts from the start symbol and predicts which rule to use (LL parsers). A bottom-up parser starts from the tokens and joins them into larger pieces (LR parsers).

Why top-down parsing needs a new grammar
- The natural grammar is $E \to E + T \mid T$. It is left-recursive 左递归: $E$ appears first on its own right side. A top-down parser that expands $E$ pushes $E$ again without reading a token, so it loops for ever.
- The fix turns the loop into a tail. The general rule: $A \to A\alpha \mid \beta$ becomes $A \to \beta A'$ and $A' \to \alpha A' \mid \varepsilon$.
- So $E \to E + T \mid T$ becomes $E \to T E'$, $E' \to {+} T E' \mid \varepsilon$, and in the same way $T \to F T'$, $T' \to {*} F T' \mid \varepsilon$.
Left recursion is removed by —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
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.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
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.
Worked example: left factoring
- $A \to ab \mid ac$: both choices start with $a$, so seeing $a$ next does not tell the parser which rule to use.
- Left factoring 提取左公因子 takes the common start out: $A \to a A'$, $A' \to b \mid c$. Now the parser reads $a$ first, and the next token, $b$ or $c$, picks the rule. Removing left recursion and left factoring are the two standard repairs before building an LL table.
$A \to ab \mid ac$ is LL(1).
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Both alternatives begin with $a$; left factoring fixes it.
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}$?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The open bracket and id.
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:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
A term ends where a plus, a closing bracket or the end can follow.
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 —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
FIRST chooses expansions; FOLLOW handles the empty production.
A FIRST/FOLLOW conflict means —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The grammar is not LL(1): table cell holds two productions.
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.
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
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?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
) 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.
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.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Bottom-up parsing reduces E + T only after seeing it, so left recursion causes no loop.
The dangling-else conflict is usually resolved by —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Shifting keeps the inner if on the stack to take the else.
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.
- 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.
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).
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้