Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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;}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
3.9k views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...