-5

I am trying to define two tokens in flex. First one returns "tINTTYPE", which returns when it sees the string "int" in the input, and other one is "TINTTYPE", which returns when it sees "int matrix" in the input.

Here is the related part of my flx file:

int {yylval.type_id.Type=1;return tINTTYPE;}
int[ \t\n]+matrix {yylval.type_id.Type=2;return tINTMATRIXTYPE;} 
. return yytext[0];

The problem is, when the input is int matrixm=4; the scanner recognizes it as int matrix m=4; and returns tINTMATRIXTYPE, but in reality, we have an integer type named matrixm, and i want it to be recognized as this, i.e it shuld return tINTTYPE. What can i do about this?

Thank you

Lesmana
  • 25,663
  • 9
  • 82
  • 87
yrazlik
  • 10,411
  • 33
  • 99
  • 165

1 Answers1

1
int                       {yylval.type_id.Type=1;return tINTTYPE;}
int[ \t\n]+matrix[ \t\n]+ {yylval.type_id.Type=2;return tINTMATRIXTYPE;} 
. return yytext[0];
Josh
  • 1,574
  • 1
  • 15
  • 36
  • This will misbehave if `int matrix` is followed by non-whitespace, non-identifier character (such as `)`). It would be better to use the pattern `int[ \t\n]matrix/[^A-Za-z0-9_]` which will match any `int matrix` as long as it is not followed by something that would make for a longer indentifier that starts with `matrix` – Chris Dodd Apr 12 '13 at 18:30
  • True. But since we know nothing about his grammar, I was keeping it in the style of his original post. – Josh Apr 12 '13 at 18:35