help-bison
[Top][All Lists]
Advanced

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

Re: How to get the text corresponding to a bison rule


From: Laurence Finston
Subject: Re: How to get the text corresponding to a bison rule
Date: Fri, 28 Nov 2008 12:28:30 +0100 (CET)

On Fri, 28 Nov 2008, sgaurelius wrote:

> Flex scanner has previously analyzed the string and extracted the tokens.
> However, if I want the text that corresponds to this expression, for example
> the "(1+2)" and not just the tokens, what should I do ? Is there some
> solution ?

Save `yytext' somewhere "persistent".  `yytext' is available in the 
actions of your scanner.  You can save it in a data member of an object 
that you pass as a parameter to `yylex'.  If you use Flex, you can pass a 
parameter of a certain type (I'd have to look up what it's called) and 
declare an object of a type of your choice to be a data member of this 
object.  I use this to carry around information that needs to be available 
within my scanner and parser rules, and outside them.  This is a 
thread-safe approach.

Alternatively, you could pass the strings back to `yyparse' as the 
semantic values of the tokens, _unless_ you need the semantic values to be 
numeric values or something else.  This might be easier, because you 
wouldn't have to keep track of which string is associated with a given 
token.  If you chose this approach, you might have to convert strings to 
numbers in your parser rules, which isn't very difficult.

There are probably other approaches, but these are the ones that occur to 
me off the top of my head.  However, I can't think of any reason why I 
would want to keep the strings.

Laurence Finston




reply via email to

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