Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!decwrl!apple!portal!cup.portal.com!chucka From: chucka@cup.portal.com (Charles - Anderson) Newsgroups: comp.unix.wizards Subject: Re: yacc & lex - cupla questions Message-ID: <32114@cup.portal.com> Date: 26 Jul 90 23:02:52 GMT References: <1990Jul26.175831.1216@uicbert.eecs.uic.edu> Organization: The Portal System (TM) Lines: 92 First suggestion is to buy the new book out from O'Reilly and Associates, Inc. "lex & yacc". It answers your questions quite nicely. 1-800-338-6887 I listed a couple of suggestions below. >i have been trying to parse a straightforward stream of bytes using the >c-preprocessors lex & yacc. being a new user of these utilities, i have >a couple of problems for which i'd like to solicit your suggestions: > >--------------------------------------------------------------------- >1.) how does one redefine the i/o in a yacc/lex piece of code? i.e. >the code which is generated defaults to stdin and stdout for input and >output, respectively. i'd like to redefine these defaults w/o having >to hack on the intermediate c-code, since this is a live production >project; i'd like to be able to update and modify the program simply by >saying "make". You can use freopen, or if you wish another file use dup. > >--------------------------------------------------------------------- >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? i.e. suppose i have lex.l and yacc.y lex and yacc >source files, respectively, and i have object files defined in my makefile >called lex.o and yacc.o such that "make" follows default rules to create >these from the aforementioned source files. > Some make utilities have default rules for lex and yacc file ending with .l and .y You can always force make with a dependency ie: prog: prog.c lex.yy.o y.tab.o cc prog.c -o prog lex.yy.o y.tab.o -ly -ll lex.yy.o: lex.yy.c cc lex.yy.c -c ... lex.yy.c: lex.l y.tab.o lex lex.l y.tab.o: y.tab.c cc y.c -c ... y.tab.c: y.y yacc -d y.y This will only compile files that have changed. Solution 2 is to put all the commands under prog: and wholesale do what ever. Solution 3 is to use a shell script and make it a dependency. >--------------------------------------------------------------------- >3.) if i have a yacc construct such as: > >line3 : A B C > { yacc action sequence } > > >which indicates that the construct line3 is composed of the 3 tokens >A B and C, in that order ... > >how can i now assign the values of A, B, and C into local vars of my >choice? 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, in this case C, >when i get to the action sequence in my yacc code. what if i want to >be able to select the EXACT ascii tokens for each of A B and C above in >my yacc code. how do i do that? > The book recommends having a line that gives the file name, parameters etc. Just as if it were a yacc specification. Make it the first line as input and you get your file name. You can have a default or make a fatal error if you do not get your first line. The book talks about redirection, opening multiple files as needed. > >any comments or suggestions would be most heartily appreciated. > >jp woodward >univ of ill at chicago >312-996-0939