yacc & lex - cupla questions

Ronald Pikkert ronald at atcmp.nl
Fri Jul 27 18:46:01 AEST 1990


>From article <1990Jul26.175831.1216 at uicbert.eecs.uic.edu>, by woodward at uicbert.eecs.uic.edu:
<> ---------------------------------------------------------------------
<> 1.)  how does one redefine the i/o in a yacc/lex piece of code? 

The code which is generated in fact defaults to yyin and yyout and these
are assigned stdin and stdout respectively.
Reassigning them can be done, without hacking, as shown in the example
using an auxillary function outside of the yylex() body.

<> 
<> ---------------------------------------------------------------------
<> 2.)  how can one get the automagically-defined #defines, which can
<> normally be created from yacc with the -d flag, to come out when you
<> use a makefile?  

Just like make will use the CFLAGS variable for cc, it will use
LFLAGS for lex and YFLAGS for yacc. So YFLAGS= -d in your makefile
will do.


<> ---------------------------------------------------------------------
<> 3.)  
<> the problem lies in the fact that each of A B and C represent
<> three calls to lex, and if i pass back a pointer to yytext[] from lex, 
<> i only retain the value of the last token in the sequence

You will have to copy the scanned pattern into some dynamically
allocated memory. 



/------------------------ example lex scanner  -----------------------------/
%{
       /* this c-code will be inserted before the yylex() c-kode */

       char *strdupl();

       /* call this function before the first call to yylex() */
       initlex(some_file_pointer)
       FILE *some_file_pointer;
       {
	       yyin = some_file_pointer;
       }

%}

constant		[0-9][0-9]*

%%

%{

         /*	initial c-kode that will be executed on every call of yylex() */

%}


{constant}	{ 
			yylval.xxxxxx = strdupl(yytext);
			return(CONSTANT); 
		}

.		{	
			return(yytext[0]);
		}

%%

/*
	use malloc to save any scanned code for use in yacc program
*/
char *strdupl(s)
char *s;
{
	char *hlp;

	hlp = (char *)malloc(strlen(s)+1);
	strcpy(hlp, s);
	return(hlp);
}
/------------------------ end example -----------------------------/


-
Ronald Pikkert                 E-mail: ronald at atcmp.nl
@ AT Computing b.v.            Tel:    080 - 566880
Toernooiveld
6525 ED  Nijmegen



More information about the Comp.unix.wizards mailing list