Lexical analysis
引入
5:58
计算理论与编译器⟦number⟧
这门课探讨机器能够判定什么,以及文本如何变成有意义的结构。前三个单元是理论:自动机与正则语言、上下文无关语言,以及可计算性。后三个单元是实践,也就是编译器的各个阶段:词法分析、语法分析,以及语义分析和代码生成。课程中的每条运行轨迹、每张表和每个词法单元的位置都经过测试套件检验。
英文讲解 · 可切换中英文字幕
本课内容
| English | 释义 | 拼音 |
|---|---|---|
| tokens/ˈtəʊkənz/ | 记号 | jì hào |
| lexer/ˈleksə/ | 词法分析器 | cí fǎ fēn xī qì |
| longest match/ˈlɒŋɡɪst mætʃ/ | 最长匹配 | zuì zhǎng pǐ pèi |
| minimises/ˈmɪnɪmaɪzɪz/ | 最小化 | zuì xiǎo huà |
| keyword table/ˈkiːwɜːd ˈteɪbl/ | 关键字表 | guān jiàn zì biǎo |
字符输入,词素输出
- 每个词素类对应一个正则表达式;词法分析器的 DFA 在源代码上执行贪婪最长匹配,并输出 (类型, 词素, 位置)。空白与注释被丢弃;位置保留用于错误定位。


The lexer emits —
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Type for the parser, lexeme for messages, position for errors.
为何采用最长匹配
- 正则表达式
if、ident、num在前缀处重叠:贪婪最长匹配将ifx解析为单个 IDENT,而非if+x— 平局由优先级裁决。处理不当会导致所有以if开头的变量解析失败。 - 课程行
count = a1 + b2;词法分析为 IDENT(count) ASSIGN IDENT(a1) PLUS IDENT(b2) SEMI,分别位于列号 1、7、9、12、14、16。
The input ifx lexes as —
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Greedy longest match: the ident regex permits the longer lexeme.
Put the tokens of count = a1 + b2; in the order the lexer emits them.
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
Whitespace is discarded and each lexeme takes the longest match, so a1 is one IDENT, not IDENT(a) then NUM(1).
错误策略是规范的一部分
12abc被贪婪地词法分析为 NUM(12),随后是 IDENT(abc):这在某些语言中合法,在另一些语言中则是错误。我们的系统会报告该错误——错误策略是词法分析器规范的一部分,而非事后补救措施。- 丢弃位置信息会导致后续所有诊断质量下降:位置信息从词法分析器流经解析器,最终传递到语义错误中。
The course line count = a1 + b2; lexes to — tokens
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
IDENT ASSIGN IDENT PLUS IDENT SEMI - six.
这是一张新卡片,翻译完成前会显示英文。
How a lexer is built
- A lexer generator such as flex turns each regular expression into an NFA (Thompson's construction), joins them, makes a DFA with the subset construction, and then minimises 最小化 it by merging states that no input can tell apart. The result needs one table lookup per character.
- Keywords are cheaper as lookups: lex every word as an identifier, then check a keyword table 关键字表. That keeps the DFA small and makes adding a keyword a one-line change.
x1 = x1 * 10;gives $6$ tokens: IDENT ASSIGN IDENT TIMES NUM SEMI.
How many tokens does total = total + rate * 12; produce?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
IDENT ASSIGN IDENT PLUS IDENT TIMES NUM SEMI: 8 tokens.
The usual way to recognise keywords is —
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
One identifier rule plus a lookup keeps the DFA small.
这是一张新卡片,翻译完成前会显示英文。
A regular expression for real numbers
[0-9]+(\.[0-9]+)?([eE][+-]?[0-9]+)?reads digits, an optional fraction and an optional exponent. It accepts42,2.5and2.5E-3.- It rejects
3.(a fraction needs a digit) and.5(the number must start with a digit). Writing these cases down is the specification; the regular expression only encodes it.
Which string does [0-9]+(\.[0-9]+)?([eE][+-]?[0-9]+)? accept?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
The others lack a digit where the regex requires one.
这是一张新卡片,翻译完成前会显示英文。
Worked example: count the tokens
while (i <= 10) i += 2;lexes to WHILE LPAREN IDENT(i) LE NUM(10) RPAREN IDENT(i) PLUSEQ NUM(2) SEMI: $\mathbf{10}$ tokens.- Longest match makes
<=one token, not<then=, and+=one token, not+then=. Spaces separate tokens but are not tokens.
How many tokens does if (x >= 3) y -= 1; contain?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
IF ( x >= 3 ) y -= 1 ;
With longest match, <= lexes as two tokens, < and =.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
The longest token that matches wins: LE.
这是一张新卡片,翻译完成前会显示英文。
Marks that slip away
- Shortest match.
ifxmust lex as one IDENT, not the keywordiffollowed byx. - Dropping positions. Every later error message needs the line and column the lexer saw.
- Asking the lexer to count. Balanced brackets are the parser's job; a lexer built from regular expressions cannot match nesting.
这是一张新卡片,翻译完成前会显示英文。
Recap
- Token classes are regular expressions; the lexer emits (type, lexeme, position).
- Longest match, then priority:
| Input | Tokens |
|---|---|
ifx |
IDENT, not if + x |
<= |
LE, not < + = |
+= |
PLUSEQ, not + + = |
12abc |
NUM(12), IDENT(abc): our lexer reports it |
- The error policy is part of the specification; positions feed every later error message.
出了什么问题?只有网站团队能看到这些信息。