Skip to content

语法分析

:material-circle-edit-outline: 约 299 个字 :material-clock-time-two-outline: 预计阅读时间 1 分钟

CP Part.2 Parsing (I)

top-down parsing

Recursive descent Parsing

a general form of top-down parsing

may require backtracing to find the correct production to be applied.

image-20250326233703487

Predictive parsing - LL(k)

  • a special case of recursive descent parsing
  • no backtracking is required

chooses the correct production by looking ahead at the input a fixed number of symbols (typically only 1)

需要提取左因子(Left Factoring),否则会遇到多个可选项

FIRST & FOLLOW & NULLABLE

image-20250326234556105

image-20250326234607199

image-20250326234546929

image-20250326234638245

image-20250327102312377

LL(1) grammar: if a predictive parsing table constructed this way contains no duplicate entries, the grammar is called LL(1).

Left-to-right parse, left-most derivation, 1 symbol lookahead

image-20250327102442536

Based on this parsing table, we can build a recursive predictive parser

We can also build a non-recursive predictive parser by maintaining a stack explicitly.

image-20250327102905433

左递归

Left Recursion cause duplicate entries in the LL(1) parsing table

image-20250327103133448

Predictive Parsing– Error Recovery

语法器如果查到了表中的一个空项,就说明遇到语法错误了

方法一:直接报错

image-20250329183340551

用户体验不好,能不能只编译一次就找出程序的全部错误

方法二:插入正确的 token,假装无事发生

Errors can be recovered by deleting, replacing, or inserting tokens

image-20250329183514913

可能会导致错误不断发生,不断插入 token,陷入死循环

删除/跳过 token 会更安全,因为不会无限进行

方法三:跳到下一个 FOLLOW 里的符号

skipp tokens until a token in the FOLLOW set is reached

image-20250329183832966