help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Reading parser stack


From: Philip Herron
Subject: Re: Reading parser stack
Date: Wed, 29 Apr 2009 13:31:11 +0100
User-agent: Thunderbird 2.0.0.21 (X11/20090409)

Mike Aubury wrote:
> Thats because you've not set up $2 properly..
>
> Normally - you'd have something like : 
>
>       %type <str> C 
>
> in the top section of your .y file.
> along with something like : 
>
>       %union {
>               char str[2000];
>       }
>
> Now - you may be using 'int's etc - so you can add more types for different 
> rules in the union and set the %type as required etc.
>
> Now - in the 'lex' file - you need something like : 
>
>       [a-zA-Z] {
>         strcpy(yylval.str, yytext);
>         return C;
>       }
>
>
> And you should be fine. 
> I think that just using the pointers the way you were (yylval) will cause 
> problems anyway unless you start strdup'ing to stop the same bit of memory 
> being overriden. This then causes problems with potential memory leaks etc..
>
>
>
> If you want to pass stuff back up again - you can copy things into that 'str' 
> - or create a structure to hold the information you need if its not a string.
>
> eg : 
>
>
> %union {
>               char str[2000];
> }
> %type <str> A
> %type <str> C
>
> %%
>
> A: B C D {
>       sprintf($$,"The identifier was: %s", $2);
>   }
>
>  | B E D {
>        sprintf($$,"It wasn't an identifier...");
>  }
> ;
>
>
> The 'old school' way of doing this was not using the %type -but specifying it 
> in the rules - so the following has the same effect (but isn't recommended 
> for 
> new code) : 
>
> %union {
>               char str[2000];
> }
>
> %%
>
> A: B C D {
>       sprintf($<str>$,"The identifier was: %s", $<str>2);
>   }
>
>  | B E D {
>        sprintf($<str>$,"It wasn't an identifier...");
>  }
> ;
>
>
>
>
> HTH
>
> On Wednesday 29 April 2009 13:00:24 Mark Redd wrote:
>   
>> Thank you for you fast answer. I've already tried this way but it doesn't
>> work fine. I'm newbie, so I think I don't know something useful.
>>
>> If I use a rule like that you wrote, Bison shows this message
>> "The identifier was: (null)".
>>
>> I've red something about yylval, so if I type in Lex file:
>> yylval=yytext,
>> and so the same your rule shows me
>> "The identifier was: snoopy*charlie", and not only "snoopy".
>>
>> What do you thing about this behaviour?
>>
>>
>> 2009/4/29 Mike Aubury <address@hidden>
>>
>>     
>>> Something like :
>>>
>>>
>>> A: B C D {
>>>       printf("The identifier was: %s", $2);
>>>  }
>>>
>>> | B E D {
>>>
>>>       printf("It wasn't an identifier...");
>>> }
>>> ;
>>>
>>>
>>>
>>>
>>>
>>> 2009/4/29 Mark Redd <address@hidden>
>>>
>>>       
>>>> Hello everybody,
>>>> I would like to receive an hint about reading parser stack.
>>>>
>>>> Suppose my (fantasy) bison grammar is this:
>>>>
>>>> %start A
>>>>
>>>> A : B C D | B E D
>>>>
>>>> B: ID
>>>> C: '*'
>>>> D:  IDENTIFIER
>>>> E: '-'
>>>>
>>>> where IDENTIFIER has been defined like [a-zA-Z0-9]+ using flex.
>>>>
>>>> How can I print "the identifier was: %s" when C matches (but not when E
>>>> does
>>>> it) without rewriting the grammar?
>>>> In particular, I need to know what I have to write in
>>>> C: '*'        { printf("The identifier was: %s", ?????); }
>>>>
>>>> Note that it isn't my very problem, but it's a model of this and so I'd
>>>> like
>>>> a general answer. So I can't modify my grammar and I don't want to read
>>>> only
>>>> the last token in parser, but sometimes I'm interested in reading the
>>>> last n
>>>> tokens.
>>>>
>>>> Thank you!
>>>> Mark Redd
>>>> _______________________________________________
>>>> address@hidden http://lists.gnu.org/mailman/listinfo/help-biso

Hey

Here is a basic way of doing it

In your lexer: you could do all your lexing but when returning do
something like this:

yylval= (int)strdup(yytext);
return(foo);

(I know thats true'ly awful but its ok for a quick test... maby :P)
Then in your grammar you shoud do:

#define YYSTYPE char *

And you should be able to print it properly.

like

myrule: bla foo bla ';'
            {
               printf("foo: %s\n", $2);
            }
             ;

Though this is just a quick way of doing it. Later on you should use a
union to define all your types because this leads to alot of warnings in
gcc but its ok because you know exactly what your returning etc..

Hope this helps.. Flex and Bison are confusing at the start but it gets
simpler. When writing grammars best piece of advice i think is, play
around with it as much as possible at the start. But make sure you keep
it as abstract as possible! As in try not to hardcode in what the syntax
should be. etc...

Whats what semantic analysis is meant to do.

-Phil




reply via email to

[Prev in Thread] Current Thread [Next in Thread]