Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.8k views
in Technique[技术] by (71.8m points)

flex lexer - why my lexical analyzer can not recognize numbers and ids and operators

my lexical analyzer in flex can not recognize numbers and ids and operators ,only keywords were recognized where is my mistake? this is my code:

%{
#include<stdio.h>
%}

Nums  [0-9]
LowerCase  [a-z]
UpperCase  [A-Z]
Letters  LowerCase|UpperCase|[_]
Id  {Letters}({Letters}|{Nums})*
operators  +|-||*
%%
"if" {printf("if keyword founded 
");}
"then" {printf("then keyword founded 
");}
"else" {printf("else keyword founded 
");}
Operators {printf(" operator founded 
");}
Id {printf(" id founded ");}
%%
int main (void)
{ yylex(); return(0);}
int yywrap(void)
{ return 1;}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To use a named definition, it ust be enclosed in {}. So your Letters rule should be

Letters   {LowerCase}|{UpperCase}|[_]

... as it is, it matches the literal inputs LowerCase and UpperCase. Similarly in your rules, you want

{Operators}  ...
{Id}  ...

as what you have will match the literal input strings Operators and Id


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...