简体   繁体   English

为什么不能使用LEX / YACC来解析编译器的C ++?

[英]Why can't LEX/YACC be used to parse C++ for a compiler?

I'm aware there's a reason but I haven't found a good, concise explanation as to why LEX/YACC cannot be used for C++. 我知道这是有原因的,但我没有找到一个好的,简明的解释为什么LEX / YACC不能用于C ++。 I am also interested to know whether LEX/YACC could be used to parse Objective C, or whether that language suffers from the same problem. 我也有兴趣知道是否可以使用LEX / YACC来解析Objective C,或者该语言是否会遇到同样的问题。 (Mind you I mean ObjC, not Obj-C++.) Thanks. (请注意,我的意思是ObjC,而不是Obj-C ++。)谢谢。

It is certainly possible to use lex and yacc to parse c++, but you need a lot of other machinery as well. 当然可以使用lex和yacc来解析c ++,但是你也需要很多其他的机器。 At one time, gcc used a yacc-based parser, but it was replaced with a hand-built recursive descent parser which is believed to be easier to maintain, and which makes generating meaningful syntax errors simpler. 有一段时间,gcc使用了一个基于yacc的解析器,但它被一个手工构建的递归下降解析器所取代,这个解析器被认为更容易维护,这使得生成有意义的语法错误变得更加简单。 clang uses a hand-built recursive descent parser for much the same reason. clang使用手工构建的递归下降解析器的原因大致相同。

Bison can build GLR parsers, which makes it a lot easier to explore alternative parses (necessary for disambiguation rules). Bison可以构建GLR解析器,这使得探索替代解析(消除歧义规则所必需)变得更加容易。 See Ira Baxter's answer to Are GCC and Clang parsers really handwritten? 请参阅Ira Baxter对Are GCC和Clang解析器的回答真的是手写的吗? for some testimony about GLR parsing of C++. 关于C ++ GLR解析的一些见证。

Also see the links in Matthew Slattery's answer to the same question for some background on gcc and clang; 另请参阅Matthew Slattery关于gcc和clang的相关问题的答案中的链接; in particular, a summary of costs and benefits perceived in 2008 for replacing the old yacc parser in gcc is found on the gcc wiki (link copied from Matthew Slattery). 特别是,在gcc wiki (从Matthew Slattery复制的链接)中可以找到2008年用于替换gcc中旧的yacc解析器的成本和收益的摘要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM