Defining lex substitution strings

You can define string macros that the lex program expands when it generates the lexical analyzer.

Define them before the first %% delimiter in the lex specification file. Any line in this section that begins in column 1 and that does not lie between %{ and %} defines a lex substitution string. Substitution string definitions have the following general format:
name                     translation

where name and translation are separated by at least one blank or tab, and the specified name begins with a letter. When the lex program finds the string defined by name enclosed in {} (braces) in the rules part of the specification file, it changes that name to the string defined in translation and deletes the braces.

For example, to define the names D and E, put the following definitions before the first %% delimiter in the specification file:
D           [0-9]
E           [DEde][-+]{D}+
Then, use these names in the rules section of the specification file to make the rules shorter:
{D}+                             printf("integer");
{D}+"."{D}*({E})?                |
{D}*"."{D}+({E})?                |
{D}+{E}                          printf("real");
You can also include the following items in the definitions section:
  • Character set table
  • List of start conditions
  • Changes to size of arrays to accommodate larger source programs