上下文相关的词法分析器

时间:2019-02-13 21:12:08

标签: flex-lexer

我在bash的parse.y中看到以下内容。这意味着词法分析将取决于上下文。如何使用flex进行这种上下文依赖分析?这种上下文依赖的需求会使Flex代码变得凌乱吗?谢谢。

http://git.savannah.gnu.org/cgit/bash.git/tree/parse.y#n3006

/* Handle special cases of token recognition:
  IN is recognized if the last token was WORD and the token
  before that was FOR or CASE or SELECT.

  DO is recognized if the last token was WORD and the token
  before that was FOR or SELECT.

  ESAC is recognized if the last token caused `esacs_needed_count'
  to be set

  `{' is recognized if the last token as WORD and the token
  before that was FUNCTION, or if we just parsed an arithmetic
  `for' command.

  `}' is recognized if there is an unclosed `{' present.

  `-p' is returned as TIMEOPT if the last read token was TIME.
  `--' is returned as TIMEIGN if the last read token was TIMEOPT.

  ']]' is returned as COND_END if the parser is currently parsing
  a conditional expression ((parser_state & PST_CONDEXPR) != 0)

  `time' is returned as TIME if and only if it is immediately
  preceded by one of `;', `\n', `||', `&&', or `&'.
*/

1 个答案:

答案 0 :(得分:0)

(F)lex提供start conditions以便进行上下文相关的词法分析。

如果避免在词法扫描器中以手写状态机的形式复制解析逻辑的诱惑,那么启动条件当然可以简化上下文相关扫描器的实现。

对于条件识别关键字(通常称为“半保留字”)的特定应用,依赖于上下文的词法分析通常不是最佳解决方案。相反,可以考虑编写扫描程序以始终识别关键字,然后在语法中添加规则以在无法使用关键字的上下文中将单词视为标识符。有关示例,请参见this answer