Multiple flex/bison parsers

旧巷老猫 提交于 2019-12-05 06:44:35

Note that Bison provides the '-p zz' option to prefix symbols with 'zz' instead of 'yy'.

Similarly, Flex provides the '-P zz' option to prefix symbols with 'zz' instead of 'yy'. It uses '-p' for performance reporting. 'Tis a pity they are not consistent with each other.

In addition to Leffler's answer, I'd like to provide another approach here:

In the .lex file you could use %option prefix="PREFIX", and in the .y file you could use %define api.prefix PREFIX, which does the same thing as passing -p PREFIX to Bison and -P PREFIX to Flex.

Notice after the overriding of the default prefix yy, you can access internal names via BOTH the original yy* and your overridden PREFIX*, while obviously for external names you MUST use your PREFIX* to access them.

If you use Bison 3.0 or better, then have a look at %define api.prefix {foo_}, which replaces all yy and YY prefixes with foo_ and FOO_.

See the Documentation about Multiple Parsers.

Between Bison 2.6 and 3.0, there used to be no braces: %define api.prefix foo_.

Eduardo Cesar

The api.prefix variable is not working for me anymore (it's producing a compilation error)

%define api.prefix {PREFIX}

I had to use the following syntax

%name-prefix="PREFIX"

In addition to what was already stated, if you use a '%define api.prefix {PREFIX}' it will also rename yytext && yyparse to PREFIXtext and PREFIXparse. Don't forget the {} around the prefix !
The same applies to '%option prefix="PREFIX"' in lex, your lexer will be renamed to PREFIXlex.

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