Search code examples
regexlex

How to introduce a Regular Expression abbreviation into a Regular Expression in Lex?


I have the following RE abbreviation:

word  [\x21-\x22\x24-\x2F\x3A-\x3F\x5B-\x5E\x60\x7B\-\x7E]

Now i need to introduce the word in the following Regular Expression:

[@][0-9a-zword][@]

So i could have, for example, the following phrases:

@0@
@r@
@!@

How can i say that it can either be a digit, a char from a to z or that "word" i defined before?


Solution

  • "@"([0-9a-z]|({word}))"@"
    

    you can't expand a defined name into a set-match, as that does not in general make any sense. Use | and parenthesis to get alteratives with proper grouping.

    Be careful with AT&T lex (as opposed to flex) as it just substitutes the definition directly, so as with a C macro, you may need extra parens to get the precedence right.