yacc sorrows

Daeshik Kim dkim at wam.umd.edu
Thu Feb 8 14:53:11 AEST 1990


In article <7179 at arcturus> evil at arcturus.UUCP (Wade Guthrie) writes:
>
>	%{
>	#include "y.tab.h"
>	[...]
>	%}
>	[...]
>	%%
>	auto        { return(AUTO); }
>	register    { return(REGISTER); }
>	[...]
>	"->"        { return(ARROW); }
>	";"         { return(SEMICOLON); }
>	.           { return(yytext[0]); }
>
	If you need to pass a value to 'yacc', then do the following:

	somestring	{
			yylval.sval=(char*)malloc(yyleng);
			strcpy(yylval.sval, yytext);
			return(TOKENNAME);
			}

	someinteger	{
			yylval.ival=atoi(yytext);
			return(TOKENNAME);
			}
	...

	Note that "yyval" is the type defined by "%union ....".
	In the above example, in 'yacc' def,
		%union
			{
			int ival;
			char *sval;
			}

	scan over c code from lex.
--
	Daeshik Kim	H: (301) 445-0475/2147 O: (703) 689-5878
	SCHOOL:	dkim at wam.umd.edu, dskim at eng.umd.edu, mz518 at umd5.umd.edu
	WORK:	dkim at daffy.uu.net (uunet!daffy!dkim)



More information about the Comp.lang.c mailing list