help-bison
[Top][All Lists]
Advanced

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

Re: Semantic values being stuffed up


From: Hans Aberg
Subject: Re: Semantic values being stuffed up
Date: Thu, 4 Jul 2002 11:05:23 +0200

Reply-to: address@hidden

At 15:44 +1200 2002/07/04, David Mitchell wrote:
>I am trying to allow a user to declare constants using something along
>the lines of:
>
>FOO EQU 1
>
>I have a rule:
>
>const_decl: CONST EQU IMMED { add_constant($1, $3); }
>
>My problem is that the value of $1 here is 'FOO EQU 1', and $3 is '1'.

A very frequent problem users have in this list is that they use a program
like Flex to generate the lexer, which only produces a pointer to the
identified text in a buffer but does not duplicate it. This Flex generated
lexer temporarily inserts a '\0' in the buffer to make it look like a
C-string.

Thus, in your example, after "FOO" has been identified, you get a pointer
to "F" with a '\0' in the place of the first " ". You then save this
pointer as your $1. Then the lexer puts back the " ". After the third call,
when "1" is identified, the lexer has put in a '\0' after the "1". You save
the pointer to "1" as your $3. But then, if you were lucky and the lexer
didn't alter the buffer, the first pointer points at the C-string "FOO EQU
1".

So you must duplicate the strings before put saving a pointer. Under C, use
strdup or something. Under C++, I use the std::string class, so I do not
have to worry about cleanup.

For help about Flex, use the
  Help-flex mailing list
  address@hidden
  http://mail.gnu.org/mailman/listinfo/help-flex

  Hans Aberg





reply via email to

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