语法分析
top-down parsing
Recursive descent Parsing
a general form of top-down parsing
may require backtracing to find the correct production to be applied.
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
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
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.
左递归
Left Recursion cause duplicate entries in the LL(1) parsing table
Predictive Parsing– Error Recovery
语法器如果查到了表中的一个空项,就说明遇到语法错误了
方法一:直接报错
用户体验不好,能不能只编译一次就找出程序的全部错误
方法二:插入正确的 token,假装无事发生
Errors can be recovered by deleting, replacing, or inserting tokens
可能会导致错误不断发生,不断插入 token,陷入死循环
删除/跳过 token 会更安全,因为不会无限进行
方法三:跳到下一个 FOLLOW 里的符号
skipp tokens until a token in the FOLLOW set is reached