help-bison
[Top][All Lists]
Advanced

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

Re: Reading parser stack


From: Mike Aubury
Subject: Re: Reading parser stack
Date: Wed, 29 Apr 2009 13:11:50 +0100
User-agent: KMail/1.11.1 (Linux/2.6.22-gentoo-r9; KDE/4.2.1; i686; ; )

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-bison


-- 
Mike Aubury

http://www.aubit.com/
Aubit Computing Ltd is registered in England and Wales, Number: 3112827
Registered Address : Clayton House,59 Piccadilly,Manchester,M1 2AQ






reply via email to

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