yacc 如何根据语法规则生成句法解析器?
我已经了解词法分析的工作原理,
但不知道句法分析是如何完成的,
尽管原则上它们两者应该相似(唯一的区别在于 它们的输入符号、字符或标记的类型。),
但生成的解析器代码有很大不同。
尤其是yy_action,yy_lookahead
,词法分析中没有这样的东西......
I've understood how lexical analysis works,
but no idea how the syntactic analysis is done,
though in principle they two should similar(The only difference lies in the
type of their input symbols, characters or tokens.) ,
but the generated parser code is greatly different.
Especially the yy_action,yy_lookahead
,there's no such thing in lexical analysis...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
用于生成词法分析器的语法一般是常规语法,而用于生成句法分析器的语法通常是上下文无关语法。尽管它们表面上看起来很相似,但它们的特征和功能却截然不同。常规语法可以通过确定性有限自动机来识别,其构造和制作相对简单快速地。上下文无关语法的识别器构建起来更具挑战性,通常解析器生成器工具只会为上下文无关语法的子集构建解析器。例如,yacc 使用 下推自动机为上下文无关语法构造解析器,这些语法也是 LALR(1) 语法。
有关解析的更多信息,我强烈推荐 解析技术,其中介绍了解析的所有细微差别都极其详细(但描述得很好!)。
The grammars used to generate lexical analyzers generally are regular grammars, while the grammars used to generated syntatic analyzers generally are context-free grammars. Although they might look the same at the surface, they have very different characteristics and capabilities. Regular grammars can be recognized by deterministic finite automatons, which are relatively simple to construct and make fast. Context-free grammars are more challenging to build a recognizer for and usually a parser generator tool will construct a parser for only a subset of context-free grammars. For example, yacc constructs parsers for context-free grammars that are also LALR(1) grammars using push-down automata.
For more information on parsing, I would highly recommend Parsing Techniques, which walks through all the nuances of parsing in excruciating (but well described!) detail.