Multiple flex / bison parsers

What is the best way to handle multiple Flex / Bison parsers within a project?

I wrote a parser, and now I need a second one in the same project. So far, in the third section of parser1.y I have inserted the main(..) method and called yyparse from there.

What I want to get consists of two different parsers ( parser1.y and parser2.y ) and can use them from an external function (let's say main in main.cpp ).

What precautions should I use to export yyparse functions outside of .y files and how should I handle two parsers?

PS. I use g ++ for compilation, but not for C ++ versions of Flex and Bison, and I would like to save them that way (avoiding encapsulation of the parser inside the object).

+6
lex bison yacc parser-generator
source share
5 answers

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

Similarly, Flex provides the option '-p zz' to prefix the characters 'zz' instead of 'yy'. It uses "-p" for performance reporting. "It is unfortunate that they are not consistent with each other.

+7
source share

In addition to Leffler's answer, I would like to suggest a different approach:

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

Note that after overriding the default yy prefix, you can access the internal names through BOTH the original yy* and your redefined PREFIX* , although obviously, for external names you MUST use your PREFIX* to access them.

+10
source share

If you use Bison 3.0 or higher, look at %define api.prefix {foo_} , which replaces all the yy and yy prefixes with foo_ and foo_ .

See the documentation for several parsers .

There were no curly braces between Bison 2.6 and 3.0: %define api.prefix foo_ .

+5
source share

The api.prefix variable no longer works for me (it creates a compilation error)

 %define api.prefix {PREFIX} 

I had to use the following syntax

 %name-prefix="PREFIX" 
0
source share

In addition to what has already been said, if you use "% define api.prefix {PREFIX}", it will also rename yytext && yyparse to PREFIXtext and PREFIXparse. Do not forget {} around the prefix!
The same applies to the %% prefix = "PREFIX" prefix in lex, your lexer will be renamed to PREFIXlex.

-one
source share

All Articles