简体   繁体   English

flex 生成的 c++ 解析器稳定吗?

[英]Is flex's generated c++ parser stable?

I all, I'm reading this book and it says that the generation of c++ lexer with flex is a buggy experimental feature.我都在读这本书,它说用 flex 生成 c++ 词法分析器是一个有问题的实验性功能。

The book was published in 2009 so I would like to know if the issue was fixed in the last 10 years.这本书出版于 2009 年,所以我想知道这个问题是否在过去 10 年中得到解决。

You cannot use a split lexer (=tokenizer) and parser for C++, since nested templates brackets are allowed:您不能对 C++ 使用拆分词法分析器 (=tokenizer) 和解析器,因为允许嵌套模板括号:

std::map<int,std::map<int,int>>

This is allowed since C++11 and cannot cleanly be handled with a split lexter/parser approach.这是从 C++11 开始允许的,并且不能用拆分词法分析器/解析器方法干净地处理。 The problem is the last token ( >> ), which a lexer (which has no knowledge about the grammar) would see as a right shift operator.问题是最后一个标记 ( >> ),词法分析器(不了解语法)会将其视为右移运算符。 Repairing this in the grammar is virtually impossible.在语法中修复这个几乎是不可能的。

Using a parsing approach which does not differentiate between characters and tokens fixes this problem.使用不区分字符和标记的解析方法解决了这个问题。 This means not using flex fixes this problem (and many others).这意味着不使用 flex 可以解决这个问题(以及许多其他问题)。

Please note that flex was mainly invented to work around the rather strong limitations of the bison/yacc parsers which can only parse LALR1 languages with a lookahead of one.请注意,flex 主要是为了解决 bison/yacc 解析器的相当强的限制而发明的,这些解析器只能以一种超前的方式解析 LALR1 语言。 The approach is dead since two decades.这种方法已经死了二十年。

Do not use flex/bison/yacc.不要使用 flex/bison/yacc。 Use a recursive descent parser PEG (parsing expression grammar) parser instead, eg PEGTL ( https://github.com/taocpp/PEGTL ).改用递归下降解析器 PEG(解析表达式语法)解析器,例如 PEGTL ( https://github.com/taocpp/PEGTL )。

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

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