lex & yacc - cupla questions
Tim Kuehn
timk at xenitec.on.ca
Tue Jul 31 06:21:19 AEST 1990
(Dave Astels) writes:
>woodward at 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 at 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
------------------------------------------------------------------------------
More information about the Comp.lang.c
mailing list