[comp.lang.c] how does one pass args to yyparse and yylex?

dean@cs.mcgill.ca (Dean NEWTON) (01/26/91)

Two questions regarding yacc and lex:

1) How does one pass args to yyparse and yylex from a C program?

2) How can one declare local variables in yyparse and yylex?

I have managed to do both of these by using a sed script in my makefile,
but it's ugly.  There has to be a better way.

Thanks,

Kaveh Kardan
Taarna Systems
Montreal, Quebec, Canada
(posting from a friend's account)

martin@mwtech.UUCP (Martin Weitzel) (02/01/91)

In article <1991Jan25.172631.15595@cs.mcgill.ca> dean@cs.mcgill.ca (Dean NEWTON) writes:
>Two questions regarding yacc and lex:
>
>1) How does one pass args to yyparse and yylex from a C program?

If you are the only user on your development, i.e. you use a
workstation, you may be able to change the scanner/parser sceletton
(you'll find them in "/usr/lib/lex/ncform" and "/usr/lib/yaccpar")

>2) How can one declare local variables in yyparse and yylex?

This is only feasable for lex. Right at the start of the second part
(i.e. after the first %%-line) you can place arbitrary C-source between
two lines containing %{ and %}. This source makes it right into yylex,
at the point AFTER its own local declarations and BEFORE its first statement.
Hence you can declare your own local variables there AND you can put
some statements here which are executed once every time yylex() is
called. (I sometimes use this to save and reset start conditions.)

For yacc I have still to find an easy way. (Good designed programs/grammars
may be able to avoid the need for many locals by making clever use of the
value stack.)
-- 
Martin Weitzel, email: martin@mwtech.UUCP, voice: 49-(0)6151-6 56 83

bliss@sp64.csrd.uiuc.edu (Brian Bliss) (02/02/91)

 to declare a parameter to yyparse(), you can use a define:

 #define yyparse()	myparse(x)  <type> x;

 This makes x a parameter to yyparse().

 You can do the same with lex.