Search code examples
flex-lexerlexer

Flex find substring until character


This is my lexer.l file:

%{
#include "../h/Tokens.h"
%}

%option yylineno

%%

[+-]?([1-9]*\.[0-9]+)([eE][+-]?[0-9])?  return FLOAT;
[+-]?[1-9]([eE][+-]?[0-9])?             return INTEGER;
\"(\\\\|\\\"|[^\"])*\"                  return STRING;
(true|false)                            return BOOLEAN;

(func|val|if|else|while|for)*           return KEYWORD;
[A-Za-z_][A-Za-z_0-9]*                  return IDENTIFIER;

"+"                                     return PLUS;
"-"                                     return MINUS;
"*"                                     return MULTI;
"."                                     return DOT;
","                                     return COMMA;
":"                                     return COLON;
";"                                     return SEMICOLON;

.                                       printf("Unexpected or invalid token: '%s'\n", yytext);

%%

int yywrap(void)
{
    return 1;
}

Now, if my lexer finds an unexpected token, it sends an error for every character. I want it to send an error message for every substring until a whitespace or operator.

Example:

Input:

foo bar baz
~±`≥ hello


Output:

Identifier.
Identifier.
Identifier.
Unexpected or invalid token: '~±`≥'
Identifier.

Is there a way to do this with a regex pattern?

Thanks.


Solution

  • Certainly it is possible to do with a regex. But you can't do it with a regex independent of your other token rules. And it may not be trivial to find a correct regex.

    In this fairly simple example, though, it's reasonably simple, although there is a corner case. Since there are no multicharacter operators, a character cannot start a token unless it is alphabetic, numeric, one of the operators (-+*.,:;) or a double-quote. And therefore any sequence of such characters is an invalid sequence. Also, I think that you really want ignore whitespace characters (based on the example output), even though your question doesn't show any rule which matches whitespace. So on the assumption that you just left out the whitespace rule, which would be something like

    [[:space:]]+                    { /* Ignore whitespace */ }
    

    your regex to match a sequence of illegal characters would be

    [^-+*.,:;[:alnum:][:space:]]+   { fprintf(stderr, "Invalid sequence %s\m", yytext); }
    

    The corner-case is an unterminated string literal; that is, a token which starts with a " but does not include the matching closing quote. Such a token must necessarily extend to the end of the input, and it can easily be matched by using your string pattern, leaving out the final ". (That works because (f)lex always uses the longest matching pattern, so if there is a terminating " the correct string literal will be matched.)

    There are a number of errors in your patterns:

    1. It's almost always a bad idea to match +- at the start of a numeric literal. If you do that, then x+2 will not be correctly analysed; your lexer will return two tokens, an IDENTIFIER and an INTEGER, instead of the correct three tokens (IDENTIFIER, PLUS, INTEGER).

    2. Your FLOAT pattern won't accept numbers starting which contain a 0 before the decimal point, so 0.5 and 10.3 will both fail. Also, you force the exponent to be a single digit, so 1.3E11 won't be matched either. And you force the user to put a digit after the decimal point; most languages accept 3. as equivalent to 3.0. (That last one is not necessarily an error, but it's unconventional.)

    3. Your INTEGER pattern won't accept numbers containing a 0, such as 10. But it will accept scientific notation, which is a little odd; in most languages 3E10 is a floating point constant, not an integer.

    4. Your KEYWORD pattern accepts keywords which are made up of a concatenated series of words, such as forwhilefuncif. You probably didn't intend to put a * at the end of the pattern.

    5. Your string literal pattern allows any sequence of characters other than ", which means a backslash \ will be allowed to match as a single character, even if it is followed by a quote or a backslash. That will result in some string literals not being correctly terminated. For example, given the string literal

       "\\"
      

      (which is a string literal containing a single backslash), the regex will match the initial ", then the \ as a single character, and then the \" sequence, and then whatever follows the string literal until it encounters another quote.

      The error is the result of flex requiring \ to be escaped inside bracket expressions, unlike Posix regular expressions where \ loses special significance inside brackets.

    So that would leave you with something like this:

    %{
    #include "../h/Tokens.h"
    %}
    
    %option yylineno noyywrap
    
    %%
    [[:space:]]+                    /* Ignore whitespace */
    
    (\.[0-9]+|[0-9]+\.[0-9]*)([eE][+-]?[0-9]+)?  {
                                      return FLOAT;
                                    }
    0|[1-9][0-9]*                   return INTEGER;
    true|false                      return BOOLEAN;
    func|val|if|else|while|for      return KEYWORD;
    [A-Za-z_][A-Za-z_0-9]*          return IDENTIFIER;
    
    "+"                             return PLUS;
    "-"                             return MINUS;
    "*"                             return MULTI;
    "."                             return DOT;
    ","                             return COMMA;
    ":"                             return COLON;
    ";"                             return SEMICOLON;
    
    \"(\\\\|\\\"|[^\\"])*\"         return STRING;
    \"(\\\\|\\\"|[^\\"])*           { fprintf(stderr,
                                              "Unterminated string literal\n"); }
    [^-+*.,:;[:alnum:][:space:]]+   { fprintf(stderr,
                                              "Invalid sequence %s\m", yytext); }
    

    (If any of those patterns look mysterious, you might want to review the description of flex patterns in the flex manual.)


    But I have a feeling that you were looking for something different: a way of magically adapting to any change in the token patterns without excess analysis.

    That's possible, too, but I don't know how to do it without code repetition. The basic idea is simple enough: when we encounter an unmatchable character, we just append it to the end of an error token and when we find a valid token, we emit the error message and clear the error token.

    The problem is the "when we find a valid token" part, because that means that we need to insert an action at the beginning of every rule other than the error rule. The easiest way to do that is to use a macro, which at least avoids writing out the code for every action.

    (F)lex does provide us with some useful tools we can build this on. We'll use one of (f)lex's special actions, yymore(), which causes the current match to be appended to the token being built, which is useful to build up the error token.

    In order to know the length of the error token (and therefore to know if there is one), we need an additional variable. Fortunately, (f)lex allows us to define our own local variables inside the scanner. Then we define the macro E_ (whose name was chosen to be short, in order to avoid cluttering the rule actions), which prints the error message, moves yytext over the error token, and resets the error count.

    Putting that together:

    %{
    #include "../h/Tokens.h"
    %}
    
    %option yylineno noyywrap
    
    %%
        int nerrors = 0; /* To keep track of the length of the error token */
        /* This macro must be inserted at the beginning of every rule,
         * except the fallback error rule.
         */
        #define E_                                                       \
          if (nerrors > 0) {                                             \
            fprintf(stderr, "Invalid sequence %.*s\n", nerrors, yytext); \
            yytext += nerrors; yyleng -= nerrors; nerrors = 0;           \
          } else /* Absorb the following semicolon */
    
    [[:space:]]+                    { E_; /* Ignore whitespace */ }
    
    (\.[0-9]+|[0-9]+\.[0-9]*)([eE][+-]?[0-9]+)?  { E_; return FLOAT; }
    0|[1-9][0-9]*                   { E_; return INTEGER; }
    true|false                      { E_; return BOOLEAN; }
    func|val|if|else|while|for      { E_; return KEYWORD; }
    [A-Za-z_][A-Za-z_0-9]*          { E_; return IDENTIFIER; }
    
    "+"                             { E_; return PLUS; }
    "-"                             { E_; return MINUS; }
    "*"                             { E_; return MULTI; }
    "."                             { E_; return DOT; }
    ","                             { E_; return COMMA; }
    ":"                             { E_; return COLON; }
    ";"                             { E_; return SEMICOLON; }
    
    \"(\\\\|\\\"|[^\\"])*\"         { E_; return STRING; }
    \"(\\\\|\\\"|[^\\"])*           { E_; 
                                      fprintf(stderr,
                                              "Unterminated string literal\n"); }
    
    .                               { yymore(); ++nerror; }
    

    That all assumes that we're happy to just produce an error message inside the scanner, and otherwise ignore the erroneous characters. But it may be better to actually return an error indication and let the caller decide how to handle the error. That introduces an extra wrinkle because it requires us to return two tokens in a single action.

    For a simple solution, we use another (f)lex feature, yyless(), which allows us to rescan part or all of the current token. We can use that to remove the error token from the current token, instead of adjusting yytext and yyleng. (yyless will do that adjustment for us.) That means that after an error, the next correct token is scanned twice. That may seem inefficient, but it's probably acceptable because:

    1. Most tokens are short,
    2. There's not really much point in optimising for errors. It's much more useful to optimise processing of correct inputs.

    To accomplish that, we just need a small change to the E_ macro:

        #define E_                                                       \
          if (nerrors > 0) {                                             \
            yyless(nerrors);                                             \
            fprintf(stderr, "Invalid sequence %s\n", yytext);            \
            nerrors = 0;                                                 \
            return BAD_INPUT;                                            \
          } else /* Absorb the following semicolon */