dylhunn
dylhunn

Reputation: 1432

Flex: trying to generate a C++ lexer using Flex; "unrecognized rule" error

I am trying to generate a lexer using flex. This is my definition file lexer.l:

%{
#include <iostream>

using namespace std;

//#define YY_DECL extern "C" int yylex()
%}

staffType "grand" | "treble" | "bass" | "alto" | "tenor"
upperRomans "I" | "II" | "III" | "IV" | "V" | "VI" | "VII"
lowerRomans "i" | "ii" | "iii" | "iv" | "v" | "vi" | "vii"
quality "dim" | "halfdim" | "aug" | "maj" | "min"

%%

[ \t\n]+ { ; // Ignore arbitrary whitespace. }
{staffType} { cout << "Staff" << endl; }
{upperRomans} { cout << "Upper roman" << endl; }
{lowerRomans} { cout << "Lower roman" << endl; }
"doublebar" { cout << "End of line" << endl; }
. { cout << "Parse error" << endl; }

%%

int main(int, char**) {
    // lex through the input
    yylex();
}

However, after invoking:

flex lexer.l

I get:

lexer.l:18: unrecognized rule
lexer.l:19: unrecognized rule
lexer.l:20: unrecognized rule

My flex version is flex 2.5.35 Apple(flex-31).

What have I done wrong?

Upvotes: 4

Views: 402

Answers (1)

Andre Kampling
Andre Kampling

Reputation: 5630

The problem is about the whitespaces between the different tokens in your pattern. It must be:

[...]
staffType "grand"|"treble"|"bass"|"alto"|"tenor"
upperRomans "I"|"II"|"III"|"IV"|"V"|"VI"|"VII"
lowerRomans "i"|"ii"|"iii"|"iv"|"v"|"vi"|"vii"
quality "dim"|"halfdim"|"aug"|"maj"|"min"
[...]

It's written in the manpage of flex.

PATTERNS
The patterns in the input are written using an extended set of regular expressions. These are:

[...]
r|s either an r or an s

Upvotes: 5

Related Questions