Re: Lex/Yacc: Multiple parsers in one file

"Paul Ogilvie" <ogilviep@xs4all.nl>
8 Apr 2006 17:10:23 -0400

          From comp.compilers

Related articles
Lex/Yacc: Multiple parsers in one file dj.cyberdance@gmx.at (Christoph B.) (2006-03-29)
Re: Lex/Yacc: Multiple parsers in one file parcour@gmail.com (Joshua Shinavier) (2006-04-01)
Re: Lex/Yacc: Multiple parsers in one file oliver@first.in-berlin.de (Oliver Bandel) (2006-04-03)
Re: Lex/Yacc: Multiple parsers in one file ogilviep@xs4all.nl (Paul Ogilvie) (2006-04-08)
| List of all articles for this month |
From: "Paul Ogilvie" <ogilviep@xs4all.nl>
Newsgroups: comp.compilers
Date: 8 Apr 2006 17:10:23 -0400
Organization: Compilers Central
Keywords: parse, yacc, comment
Posted-Date: 08 Apr 2006 17:10:23 EDT

"Christoph B." <dj.cyberdance@gmx.at> wrote:


"...Does anyone know where to find a simple example on how to use mulitple
parsers in one file? Or maybe someone can give me instructions or a
short example?"


Reading your message about having "two yacc parsers in one file", it can
mean:
- having two parsers in one file BUT using the same lexical analyzer, or
- having two parsers in one executable AND using DIFFERENT lexical
analyzers.


The first is solved by the first yacc rule being:


do_it: language1 | language2 ;


and then defining language1 and language2 in the same yacc script as two
distinct set of rules.


The second approach I solved by slightly modifying the source of yacc to
generate user-definable global variables and functions. With that approach
'make' calls yacc twice with different input scripts, generating two
c-output files with different names, exporting different yyparse functions
and calling different yylex functions, and using different yylval variables
e.g.:


script1.y --> yscript1.c
- yyparse = int yyparse1(void);
- yylex = extern int yylex1(void);
- yylval = yylval1


script2.y --> yscript2.c
- yyparse = int yyparse2(void);
- yylex = extern int yylex2(void);
- yylval = yylval2


Although the first apporach is often viable, it does not allow modular
development, particularly not by different teams.


Paul Ogilvie,
ogilviep@xs4all.nl
[Why did you modify the yacc source rather than using the -p switch that
does the same thing for you? -John]


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.