为什么我们在编译器中需要 Look Ahead symbol 和 read ahead symbol

why we need both Look Ahead symbol and read ahead symbol in Compiler

好吧,我正在阅读一些关于在编译器中解析的常见概念..我遇到了 look ahead 并阅读了 ahead symbol 我搜索并阅读了它们,但我被困住了为什么我们需要它们?将不胜感激任何建议

Lookahead symbol: when node being considered in parse tree is for a terminal, and the terminal matches lookahead symbol,then we advance in both parse and input

read aheadsymbol: lexical analyzer may need to read some character before it can decide on the token to be returned

其中一个是关于解析的,它指的是词法扫描器要生成的下一个 token。另一个不太正式,是关于词法分析的,指的是输入流中的下一个字符。哪个是哪个应该分清楚了。

请注意,虽然大多数解析器只需要一个前瞻标记,但词法分析必须回溯的情况并不少见,这相当于检查几个未使用的输入字符。

希望我答对了你的问题。

考虑 C.

它有几个以相同方式开头的标点符号:

  • ++++=
  • ----=->
  • <<=<<<<=
  • ...

为了在您看到第一个 +-< 时弄清楚它是哪个,您需要向前看输入中的一个字符(然后<<= 可能还有一个)。

类似的事情可能发生在更高的层次上:

{
  ident1 ident2;
  ident3;
  ident4:;
}

此处ident1ident3ident4可以开始声明、表达式或标签。你无法立即分辨出是哪一个。您可以查阅现有的声明以查看 ident1ident3 是否已知(作为类型或 variable/function/enumeration),但它仍然不明确,因为冒号可能跟在后面,如果是,它是一个标签,因为允许对标签和类型/variable/function/enumeration 使用相同的标识符(这两个名称空间不相交),例如:

{
  typedef int ident1;
  ident1 ident2; // same as int ident2
  int ident3 = 0;
  ident3; // unused expression of value 0
  ident1:; // unused label
  ident2:; // unused label
  ident3:; // unused label
}

因此,您可能非常需要向前看一个字符或令牌(或 "unread" 一个)来处理此类情况。