依赖上下文的词法分析器

问题描述 投票:0回答:1

我在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 `&'.
*/
flex-lexer
1个回答
1
投票

(F)lex提供start conditions以允许依赖于上下文的词法分析。

如果您避免将解析逻辑重现为词法扫描程序中的手写状态机,那么启动条件肯定可以简化依赖于上下文的扫描程序的实现。

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

© www.soinside.com 2019 - 2024. All rights reserved.