[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference turris::digital_unix

Title:DIGITAL UNIX(FORMERLY KNOWN AS DEC OSF/1)
Notice:Welcome to the Digital UNIX Conference
Moderator:SMURF::DENHAM
Created:Thu Mar 16 1995
Last Modified:Fri Jun 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:10068
Total number of notes:35879

8605.0. "dumb yacc question" by CSC32::S_LEDOUX (Want some cheese with that whine ?) Mon Jan 27 1997 21:56

Hi ppl...  I've been playing with yacc/bison and have a question that I can't
answer by reading the bison www page.

If I have a grammar production that looks like:

yuk	:
	  FOO BAR BAZ  { printf("-%s-%s-%s-\n", $1, $2, $3); }
	;

The terminals FOO, BAR and BAZ have character semantic values and the input
stream is something like "foo bar baz" my output looks like:

-foo bar baz-bar baz-baz-

so it seems that $1 holds the semantic value of the entire production but
the doc says $$ is supposed to do that.  $2 has some middle state and $3
has the last token's value.  what I wanted was $1="foo" $2="bar" and $3="baz".
Its easy enough for me to parse out what I want and do the right things 
with it but surely its not supposed to work this way ???

Can somebody out there rent me a clue ?
Thanks -
Scott
T.RTitleUserPersonal
Name
DateLines
8605.1looks like your lexerSMURF::GAFJerry Feldman, Unix Dev. Environment, DTN:381-2970Tue Jan 28 1997 15:323
    I don't see anything wrong with your grammar file per se. Is your lexer
    properly terminating the string it passes to yacc. All yacc is going to
    do is to save the yylvals in an array. 
8605.2oh ..CSC32::S_LEDOUXWant some cheese with that whine ?Tue Jan 28 1997 23:426
Oh...  Told you it was a dumb question...  Am I to understand then that I
have to cap yytext with a null byte ??  I thought that yylex took care of
that somehow.

thanks -
Scott ;)
8605.3Here's smore help.SMURF::GAFJerry Feldman, Unix Dev. Environment, DTN:381-2970Wed Jan 29 1997 15:0425
    Whose yylex are you using. The lexer is responsible for nul terminating
    yytext. In your example, $1, $2, ... are instances of the _yystype_
    union. Depending on how you define the tokens, $1 will point to the 
    instance of the union that is set by the lexer. 
    For example, in your yacc grammar:
    %union {
    	long	i;
    	unsigned char	*cp;
    };
    
    %token CHAR 
    r:	CHAR
    	={	$$.i = mn0($1.i); }
    
    or:
    %token<cp> CHAR
    	={	$$ = mn0($1); }
    
    yytext is shared between yylex and yacc, but is is static.You would
    need to define your YYSTYPE either with the %union rule if you have
    multiple types, or define YYSTYPE as a pointer to some character array. 
    
    In any case, the lexer does not do it automatically. If you have a lex
    based lexer, yytext will contain the null terminated string.  
    
8605.4workin now...CSC32::S_LEDOUXWant some cheese with that whine ?Wed Jan 29 1997 17:497
I'm using lex.  Actually I got flex & bison on a vms machine.  I missed the
point that capping off yytext is my job.  That makes perfect sense to me now
thanks.  Is is safe to presume that I can change the buffer upto the point
that yylex has parsed without hosing myself ??

Thanks
scott ;)
8605.5in lex it is easy to yhose yourself.SMURF::GAFJerry Feldman, Unix Dev. Environment, DTN:381-2970Thu Jan 30 1997 12:267
    >Is is safe to presume that I can change the buffer upto the point
    >that yylex has parsed without hosing myself ??
    The lexer owns yytext. Generally, before returning a token to yacc, 
    if you are passing a string to yacc, you would copy that string to the
    yylval union. Also note that lex adds the current character to yytext,
    and null terminates yytext. If any of your actions modify yytext, you
    can cause some problems.