help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: two yyparse() functions in the same program


From: Aaron Jackson
Subject: Re: two yyparse() functions in the same program
Date: Sat, 2 Aug 2008 11:30:51 -0400

On Aug 2, 2008, at 8:23 AM, george smith wrote:

Hello,
I am building a compiler where i need a parser for the front end and another
one for the back end. So i will need two parsers in the same program
but there will be a conflict between the function names as with the global variables. Is there any way to change the names to resolve any conficts?
Thanks in advance
George.

This may not be the best way to do things (maybe somebody can comment as to why), but you can write the .y file to accommodate both parsers. That way you don't have to worry about name conflicts. For example in your .y file you can do something like

...
int lexState;
...
%token FRONTSTART BACKSTART
...
parser: FRONTSTART frontparser
    |      BACKSTART backparser
    |      error
    ;

...
lexState = FRONTSTART
yyparse();
...
lexState = BACKSTART
yyparse();
...

Assuming you use flex, you can start your .l file with something like

...
extern int lexState;
...
%x FRONTSTATE BACKSTATE
...
if (YY_START != lexState) {
 if (lexState == FRONTSTART)
  BEGIN(FRONTSTATE);
 else if (lexState == BACKSTART)
  BEGIN(BACKSTATE)
 else
  yyterminate();
 return(lexState);
}
...

I did something like this to get three parsers into one .y file. My parsers are small and everything works as it should. However, as I said above, I don't know if this is the best way to do things, but this implementation makes sense to me.

Aaron




reply via email to

[Prev in Thread] Current Thread [Next in Thread]