[net.unix-wizar] Two parsers in one process

berry (01/19/83)

#N:zinfandel:12400011:000:4172
zinfandel!berry    Jan 17 15:47:00 1983

Since I got 5 requests, and our mail router insists on using
harpo, I am posting this in hopes that it will reach everyone
interested.

Here is the nitty-gritty  on  how  I  made  "siamese  twin"  yacc
parsers  fit into a single process.  The reason I did it this way
was partly historical, and partly because when I merged  the  two
grammars,  yacc's  internal tables overflowed (we were restricted
to PDP-11's then).

The basic strategy is to edit the "dot-c" file produced  by  yacc
for  ONE  of  the  parsers.  Since the secondary parser was for a
feature of our tester language called 'Data Director*', I substi-
tuted  'dd'  for  'yy'.  However, due to the peculiar miscegenous
interface between yacc parsers and lex,  uh,  lexers,  some  'dd'
keywords  needed  to be changed back to 'yy' so that both parsers
could share a single lexer.  (I KNOW it's "lexical analyzer", OK?
"lexer"  is  easier  to type, unambiguous and not too offensive.)
Such things as 'yytext', the pointer to the text  of  the  token,
and  'yylval',  the actual token itself, need to be changed back,
along with a few others.  Consult the 'ex' script 'ddyaccfix' in-
cluded below for details.

Some miscellaneous 'mv's need to be added to the makefile.  I in-
clude a sample (editted) makefile for my project.

Finally, I include a sample (editted  and  abridged)  yacc  spec.
that  illustrates  how  parser one calls parser two.  In my case,
statements beginning with one of a number of keywords  needed  to
be  parsed  by  the  'dd'  parser.  I included rules that matched
those tokens in the primary parser, and in the action  for  those
rules set a flag, call 'ddparse()' and reset the flag.  It's that
simple.

The flag is used by the lexer for  those  few  tokens  that  have
meanings  in  the context of the priary parser different from the
meanings in the context of the secondary parser.  The lex actions
for those tokens look like

        if ( doing_dd ) 
		return FOO;
	else
                return BAR;


I hope this summary helps someone out there.  Feel free  to  send
me  questions or constructive comments.  (or even a better way if
you have one!)

	Berry Kercheval
	Zehntel Inc.
	(decvax!sytek!zehntel!zinfandel!berry)
	(415)932-6900



-------
*Data Director is a trademark of Zehntel Inc.




::::::::
makefile
::::::::

convert: $(OBJ)
	cc $(OBJ) $(LIBES) -o $(OFILE)

convert.c: convert.y
	${YACC} ${YFLAGS} convert.y
	mv y.tab.c convert.c
	mv y.tab.h cy.tab.h
	mv y.output cy.output

convert.o: convert.c
	${CC} ${CFLAGS} -c convert.c

clex.yy.c: convert.l
	${LEX} ${LFLAGS} convert.l
	mv lex.yy.c clex.yy.c

clex.yy.o: clex.yy.c
	${CC} ${CFLAGS} -c clex.yy.c

dd.c: ddyacc.y
	${YACC} ${YFLAGS} ddyacc.y
	mv y.tab.c dd.c
	ex dd.c < ddyaccfix
	mv y.tab.h dy.tab.h
	mv y.output dy.output

dd.o: dd.c
	${CC} ${CFLAGS} -c dd.c

OBJ =   convert.o clex.yy.o main.o yyerror.o stubs.o dd.o cat.o

::::::::::
ddyaccfix
::::::::::
g/yy/s/yy/dd/g
g/ddreject/s/ddreject/yyreject/g
g/dderror/s/dderror/yyerror/g
g/ddsptr/s/ddsptr/yysptr/g
g/ddsbuf/s/ddsbuf/yysbuf/g
g/ddlex/s/ddlex/yylex/g
g/ddchar/s/ddchar/yychar/g
g/ddtext/s/ddtext/yytext/g
g/ddlval/s/ddlval/yylval/g
/YYSTYPE yylval, ddval;/d
i
extern YYSTYPE yylval;
YYSTYPE ddval;

leichter (01/21/83)

Are we the only ones who have received this message >10 times over the last
couple of days, or is the problem general?

This isn't the only message that's been repeating but I noticed it.  zehntel,
are you at it again?
							-- Jerry
						decvax!yale-comix!leichter