I have an application where I already have a parser for one sort of grammar and I need to add a second different grammar for another purpose.
我有一个应用程序,我已经有一种语法的解析器,我需要为另一个目的添加第二个不同的语法。
Is it possible to have more than one?
是否有可能有多个?
And if so how do you get another entry point?
如果是这样,你如何获得另一个切入点?
Thanks
david allan finch
大卫艾伦芬奇
4 个解决方案
#1
I think you can to this by using the --name-prefix
option to Bison, and the --prefix
option to Flex.
我认为你可以通过使用Bison的--name-prefix选项和Flex的--prefix选项来实现。
In both cases they allow you to replace the default "yy
" prefix used on the functions generated with a prefix of your own choice.
在这两种情况下,它们都允许您替换使用您自己选择的前缀生成的函数上使用的默认“yy”前缀。
#2
Yes, it's possible.
是的,这是可能的。
yacc should have a -p flag, where you can specify a different prefix instead of the default "yy".
yacc应该有一个-p标志,您可以在其中指定不同的前缀而不是默认的“yy”。
The file generated by lex conatins only one symbol used outside: yylex. You can compile with -Dyylex=mySecondLex, for example.
lex生成的文件只包含一个在外面使用的符号:yylex。例如,您可以使用-Dyylex = mySecondLex进行编译。
#3
Not a direct answer, but you may want to consider using a more generic approach such as GoldParser which allows you to use a generic engine to process the LALR and DFA tabled generated from your grammars.
不是直接的答案,但您可能需要考虑使用更通用的方法,例如GoldParser,它允许您使用通用引擎来处理从您的语法生成的LALR和DFA表。
This way around, you can use any number of different grammars in the same application, and changing the grammar does not create new source code but rather just new table files (which could be included as embedded resource or similar).
这样,您可以在同一个应用程序中使用任意数量的不同语法,并且更改语法不会创建新的源代码,而只会创建新的表文件(可以包含为嵌入式资源或类似文件)。
#4
Standard lex and yacc have no way of doing that. Flex and Bison do, as do some other implementations. Since you said (in a comment) that you aren't using flex and bison, which versions of lex and yacc are you using?
标准的lex和yacc无法做到这一点。 Flex和Bison就像其他一些实现一样。既然你(在评论中)说你没有使用flex和bison,你使用的是lex和yacc的哪个版本?
#1
I think you can to this by using the --name-prefix
option to Bison, and the --prefix
option to Flex.
我认为你可以通过使用Bison的--name-prefix选项和Flex的--prefix选项来实现。
In both cases they allow you to replace the default "yy
" prefix used on the functions generated with a prefix of your own choice.
在这两种情况下,它们都允许您替换使用您自己选择的前缀生成的函数上使用的默认“yy”前缀。
#2
Yes, it's possible.
是的,这是可能的。
yacc should have a -p flag, where you can specify a different prefix instead of the default "yy".
yacc应该有一个-p标志,您可以在其中指定不同的前缀而不是默认的“yy”。
The file generated by lex conatins only one symbol used outside: yylex. You can compile with -Dyylex=mySecondLex, for example.
lex生成的文件只包含一个在外面使用的符号:yylex。例如,您可以使用-Dyylex = mySecondLex进行编译。
#3
Not a direct answer, but you may want to consider using a more generic approach such as GoldParser which allows you to use a generic engine to process the LALR and DFA tabled generated from your grammars.
不是直接的答案,但您可能需要考虑使用更通用的方法,例如GoldParser,它允许您使用通用引擎来处理从您的语法生成的LALR和DFA表。
This way around, you can use any number of different grammars in the same application, and changing the grammar does not create new source code but rather just new table files (which could be included as embedded resource or similar).
这样,您可以在同一个应用程序中使用任意数量的不同语法,并且更改语法不会创建新的源代码,而只会创建新的表文件(可以包含为嵌入式资源或类似文件)。
#4
Standard lex and yacc have no way of doing that. Flex and Bison do, as do some other implementations. Since you said (in a comment) that you aren't using flex and bison, which versions of lex and yacc are you using?
标准的lex和yacc无法做到这一点。 Flex和Bison就像其他一些实现一样。既然你(在评论中)说你没有使用flex和bison,你使用的是lex和yacc的哪个版本?