Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!att!watmath!maytag!xenitec!timk From: timk@xenitec.on.ca (Tim Kuehn) Newsgroups: comp.lang.c Subject: Re: lex & yacc - cupla questions Keywords: lex yacc Message-ID: <1990Jul30.202119.2768@xenitec.on.ca> Date: 30 Jul 90 20:21:19 GMT References: <1990Jul26.175545.959@uicbert.eecs.uic.edu> <1990Jul27.185058.6257@aucs.uucp> Reply-To: timk@xenitec.UUCP (Tim Kuehn) Distribution: comp Organization: TDK Consulting Services, Kitchener, ON, Canada Lines: 39 (Dave Astels) writes: >woodward@uicbert.eecs.uic.edu writes: >>3.) if i have a yacc construct such as: >> >>line3 : A B C >> { yacc action sequence } >> >>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. > >return a copy of yytext, not a pointer to it. That still doesn't solve the problem as you don't know *which* rule has fired, and where to store the text value associated with tokens A B C until after you've made three (or more) calls to yylex. However, provision for handling this in provided with the yylval, which can be used with the %union {} declaration in yacc, and assigned in the action associated with the regexp's in lex. In the case where text values associated with these tokens, one would include a char pointer declaration in the %union statement, (say char *cval) copy the text value to a malloc'd space, and assign the pointer to yylval.cval before returning from lex. This will be saved on the value stack yacc maintains as it goes through it's states, and can be retreived by making reference to $$, $1, $2, or $3 (the equivalent variable names for the rule, first, second, and third elements of the rule respectively). Check your yacc/lex manual for more details. ------------------------------------------------------------------------------ Timothy D. Kuehn TDK Consulting Services "Where Quality is Guaranteed" timk@xenitec.on.ca uunet!watmath!maytag!xenitec!timk 119 University Ave. East, Waterloo, Ont., Canada. N2J 2W1 519-888-0766 if no answer 519-742-2036 (w/ans mach) fax: 519-747-0881. Contract services available in Dos/Unix/Xenix - SW & HW. Clipper, Foxbase/Pro, C, Pascal, Fortran, Assembler etc. *Useable* dBase program generator under construction ------------------------------------------------------------------------------