首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >上下文相关词法分析器

上下文相关词法分析器
EN

Stack Overflow用户
提问于 2019-02-14 05:12:08
回答 1查看 267关注 0票数 1

我在bash的parse.y中看到了以下内容。这意味着词法分析将依赖于上下文。如何使用flex来做这样的上下文依赖分析?这种依赖于上下文的需求会让flex代码变得太混乱吗?谢谢。

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

代码语言:javascript
复制
/* 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 `&'.
*/
EN

回答 1

Stack Overflow用户

发布于 2019-02-14 11:23:19

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

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54679489

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档