views:

204

answers:

4

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

+5  A: 

I think you can to this by using the --name-prefix option to Bison, and the --prefix option to Flex.

In both cases they allow you to replace the default "yy" prefix used on the functions generated with a prefix of your own choice.

Alnitak
I am using lex and yacc but this is good to know if we change to the freeware versions. Thanks dave
David Allan Finch
ah, but you did mention them in your tags...
Alnitak
True. If they only way to get it to work is to use flex and bison. We will have up change to use them.
David Allan Finch
Oops. I have just check the makefiles - and found we are using flex and bison.
David Allan Finch
+1  A: 

Yes, it's possible.

yacc should have a -p flag, where you can specify a different prefix instead of the default "yy".

The file generated by lex conatins only one symbol used outside: yylex. You can compile with -Dyylex=mySecondLex, for example.

Ingo
I need to check it but that is just what I need. Thanks dave.
David Allan Finch
I hope your version of yacc has this flag. Otherwise, you can switch to bison, it's not really different.
Ingo
Aren't their symbols like yytext and so on (yywrap?) that are also used outside?
Jonathan Leffler
+1  A: 

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.

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).

Lucero
We are not in a position to change system at the mo but may be one to look into in the future. Thanks dave.
David Allan Finch
+1  A: 

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?

T.E.D.
POSIX requires yacc to support symbol prefix changes with '-p prefix' (in place of 'yy'). Interestingly, it doesn't require that for lex; I assume that in practice many people hand-code the lexical analyzer and so it wasn't so critical.
Jonathan Leffler