简体   繁体   English

解析时动态切换解析器

[英]Dynamically switch parser while parsing

I'm parsing spice netlists , for which I already have a parser. 我正在解析香料网表 ,对此我已经有一个解析器。 Since I actually use spectre (cadence, integrated electronics), I want to support both simulator languages (they differ, unfortunately). 由于我实际上使用了幽灵(节奏,集成电子),因此我想支持两种模拟器语言(不幸的是,它们是不同的)。 I could use a switch (eg commandline) and use the correct parser from start. 我可以使用一个开关(例如命令行),并从一开始就使用正确的解析器。 However, spectre allows simulator lang=spectre statements, which I would also want to support (and vice versa, of course). 但是,幽灵允许simulator lang=spectre语句,我也想支持该语句(当然,反之亦然)。 How can this be done with boost::spirit? 如何用boost :: spirit完成?

My grammar looks roughly like this: 我的语法大致如下:

line = component_parser             | 
       command_parser               |  
       comment_parser               | 
       subcircuit_parser            | 
       subcircuit_instance_parser;

main = -line % qi::eol >> qi::eoi;

This toplevel structure is fine for both languages, so i need to change the subparsers. 这种顶层结构对两种语言都适用,因此我需要更改子解析器。 A first idea for me would be to have the toplevel parser hold instances (or objects) to the respective parser and to switch on finding the simulator lang statement (with a semantic action). 对我来说,第一个想法是让顶级解析器将实例(或对象)保存到各自的解析器中,并继续查找simulator lang语句(使用语义动作)。 Is this a good approach? 这是一个好方法吗? If not, how else would one do this? 如果没有,那怎么办呢?

You can use qi::lazy ( https://www.boost.org/doc/libs/1_68_0/libs/spirit/doc/html/spirit/qi/reference/auxiliary/lazy.html ). 您可以使用qi::lazyhttps://www.boost.org/doc/libs/1_68_0/libs/spirit/doc/html/spirit/qi/reference/auxiliary/lazy.html )。

There's an idiomatic pattern related to that, known as The Nabialek Trick . 有一个与此相关的惯用模式,称为The Nabialek Trick

I have several answers up on this site that show these various techniques. 我在这个站点上有几个答案,展示了这些不同的技术。

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

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