Search code examples
compilationflex-lexerlexical-analysislexical

Defining a Template in Flex


I want to define a "KEYER" in flex, which is a "KEY" in "[]". A "KEY" is starting with a letter and a string of letters, numbers and the following characters: "~_'?$. -". I defind:

keyChar         ([a-zA-z0-9~_'?$. \-])
letter          ([a-zA-Z])
key             ({letter}{keyChar}+)
keyer           ("["{key}"]") 

and:

<*>{keyer}        print("KEYER");

Somehow the input:

[keyer1] [keyer2] [keyer 3]

is read as one KEYER and not three of them. what did I do wrong?


Solution

  • You wrote A-z instead of A-Z in the pattern for keyChar. [A-z] includes the characters between Z and a, which include brackets.

    On the whole, it is better to avoid range expressions when not necessary. I would have written:

    keyChar         ([[:alnum:]~_'?$. -])
    key             ([[:alpha:]]{keyChar}+)
    keyer           ("["{key}"]")