lex and yacc help desired

Peter Garst pg at bsg.com
Thu Apr 25 04:11:43 AEST 1991


In article <1991Apr23.164744.25927 at mnemosyne.cs.du.edu> awade at isis.cs.du.edu (allen wade) writes:
>
>I am able to figure out the regular expressions for Lex, but I am confused
>about how Lex and Yacc work together.
>

When you are writing a lex/yacc interface, the work is on the lex side;
the yacc side is hidden in the code generated by yacc.

On the lex side, you generally need to do two things when you have found
a token that you want to pass to yacc: 

1) set the variable "yylval" to the value of the token, and
2) return the macro defining the token number.

For example, if you have recognized an integer and want to pass it to yacc,
you will do this in a lex action:

	yylval	= atoi(yytext);	/* Set yylval to the number found	*/
	return(NUMBER);		/* Tell yacc you found a number		*/

In your grammar file you can then get the value of the number with one of
the $n variables in an action associated with a rule.
The easiest way to make sure NUMBER is defined is to include the scanner
generated by lex in the last part of the yacc grammar file. You can use
"#include "put-your-scanner-name-here"" after the second %% in the yacc file.
The the "%token NUMBER" in the declarations section will define NUMBER for
your scanner.

Debugging yacc grammars and parsers can be difficult and time consuming;
we have a symbolic debugger (and yacc upgrade) which makes it much easier.
See our recent comp.newprod posting, or get in touch for more information.

Peter Garst
pg at bsg.com
Bloomsbury Software Group, PO Box 390018, Mountain View, CA 94039
(415) 964-3486



More information about the Comp.unix.wizards mailing list