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!

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.