Search code examples
linuxflex-lexerlexcompiler-construction

I am getting error as unrecognized rule in this lex program


%{
#include<stdio.h>
int vowel=0;
%}

%%

[aeiouAEIOU]
{
vowel++;
}

%%

int main()
{
  char str[200];    
  gets(str);
  yylex();
  printf("Vowels:",vowel);
}

Solution

  • If that is the way you wrote the program, then the error is correct. In Flex, the action for a rule must start on the same line as the pattern.

    From the flex manual:

    5.2 Format of the Rules Section

    The rules section of the flex input contains a series of rules of the form:

     pattern   action
    

    where the pattern must be unindented and the action must begin on the same line.

    As written, you have supplied { as a pattern. That's not a valid pattern, and so flex complains.