Flex lexer 上下文相关词法分析器

Flex lexer 上下文相关词法分析器,flex-lexer,Flex Lexer,我在bash的parse.y中看到了以下内容。这意味着词汇分析将依赖于上下文。如何使用flex进行这种上下文深度分析?这种上下文依赖的需求会不会让flex代码变得太凌乱?谢谢 (F) lex提供了上下文相关的词法分析 如果您避免了在词法扫描程序中将解析逻辑复制为手写状态机的诱惑,那么启动条件肯定可以简化上下文相关扫描程序的实现 对于有条件识别的关键字(通常称为“半保留字”)的特殊应用,上下文相关的词汇分析通常不是最佳解决方案。相反,考虑写扫描器总是识别关键字,然后在语法中添加规则,以在不可能的

我在bash的
parse.y
中看到了以下内容。这意味着词汇分析将依赖于上下文。如何使用flex进行这种上下文深度分析?这种上下文依赖的需求会不会让flex代码变得太凌乱?谢谢

(F) lex提供了上下文相关的词法分析

如果您避免了在词法扫描程序中将解析逻辑复制为手写状态机的诱惑,那么启动条件肯定可以简化上下文相关扫描程序的实现

对于有条件识别的关键字(通常称为“半保留字”)的特殊应用,上下文相关的词汇分析通常不是最佳解决方案。相反,考虑写扫描器总是识别关键字,然后在语法中添加规则,以在不可能的上下文中处理单词作为标识符。有关示例,请参见

/* 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 `&'.
*/