/*Lex program that distinguishes keywords, integers, floats, identifiers, operators, and
comments in any simple programming language.*/
%{
enum{INTEGER,FLOAT,IDENTIFIER,OPERATOR,COMMENT};
%}
digit [0-9]
letter[A-Za-z_]
%%
" "|"\t" ;
{digit}+ { return INTEGER; }
{digit}+\.{digit}+ { return FLOAT; }
'+' |
'-' |
'*' |
'/' { return OPERATOR; }
{letter}({letter}|{digit})* { return IDENTIFIER; }
"/*" { return COMMENT;}
%%
int main()
{
int output1;
int main_run = 1;
while(main_run)
{
result = yylex();
switch(output1)
{
case INTEGER: printf("integer");
break;
case FLOAT: printf("float");
break;
case OPERATOR: printf("operator");
break;
case IDENTIFIER:printf("identifier");
break;
case COMMENT: printf("comment");
break;
}
}
return 0;
}
output:
No comments:
Post a Comment