Deeply-recursive qi grammars (parsers) with synthesized and inherited attributes

强颜欢笑 提交于 2019-12-05 19:03:15

I had the same problem when implementing an SQL parser in qi. I had separate grammars for statements, fields, tables, etc, but fields and tables, for instance, could be statements too sometimes (subqueries). In my case, I solved the problem like this:

The top grammar (the one that you always start parsing with) held shared pointers to all sub grammars. The constructor of the top grammar would create instances of all sub grammars and pass them instances of each other as weak pointers. That way there is only one instance of each grammar created. There are no circular shared_ptr thanks to weak_ptr. So basically, the top grammar owns all sub grammars and just passes pointers around to each other.

So in your case, I would create a TopGrammar class, have it own FooGrammar and BarGrammar. Once they're constructed, pass them pointers to each other. And in TopGrammar's start rule just invoke FooGrammar.

Since TopGrammar owns all subgrammars, you're sure that their lifespan is sufficient.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!