0

I'm trying to use GNU flex to write a lexical analyzer. Is it possible to have multiple definitions for the same name in Flex? Like this:

/* DEFINITIONS */
FRUITS "APPLE" | "ORANGE" | "LEMON"

Also, what can we do and not do with string constants in flex's regular expressions? Can we use symbols such as $ and *? Thank you!

2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by L7Sqr
0

Yes.
In general, in a lex/flex file you would provide the tokenizer rules. Something like:

%%
// ...
%%

"apple"    { return APPLE; }
"pear"     { return PEAR; }
"berry"    { return BERRY; }

%%

// ...

Then, in the yacc/bison file you would set up the grammar rule similar to:

fruit :
    APPLE { /* do apple stuff */ }
    |
    PEAR { /* do pear stuff */ }
    |
    BERRY { /* do berry stuff */ }

While you can probably combine these in the lexical analyzer step it is usually better to handle each of your tokens individually in the grammar. This way, if you need to do something special for any of the tokens you have a distinct location to do it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.