Search code examples
regexflex-lexerlexical-analysis

flex (lexical analyzer) regular expressions - Reusing definitions


I have this working definition:

IDENTIFIER   [a-zA-Z][a-zA-Z0-9]*

I don't want to keep repeating the [a-zA-Z] and [0-9], so I made two new definitions

DIGIT    [0-9]
VALID    [a-zA-Z]

How can I rewrite the IDENTIFIER rule to use the DIGIT and VALID definitions?

I don't know how to do the "second" match, I'm stuck here:

IDENTIFIER {VALID}[{VALID}{DIGIT}]* // This syntax is incorrect

Thanks.

Edit: The entire test program that I'm using: http://pastebin.com/f5b64183f.


Solution

  • It looks like you actually want:

    IDENTIFIER {VALID}({VALID}|{DIGIT})*
    

    [{VALID}{DIGIT}] resolves to [[A-Za-z][0-9]] which is not a legal construct.