Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!mephisto!bloom-beacon!eru!luth!sunic!mcsun!hp4nl!sci.kun.nl!atcmpe!ronald From: ronald@atcmp.nl (Ronald Pikkert) Newsgroups: comp.unix.wizards Subject: Re: yacc & lex - cupla questions Message-ID: <640@atcmpe.atcmp.nl> Date: 27 Jul 90 08:46:01 GMT References: <1990Jul26.175831.1216@uicbert.eecs.uic.edu> Distribution: comp Organization: AT Computing, Nijmegen, The Netherlands Lines: 88 From article <1990Jul26.175831.1216@uicbert.eecs.uic.edu>, by woodward@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@atcmp.nl @ AT Computing b.v. Tel: 080 - 566880 Toernooiveld 6525 ED Nijmegen