fork download
  1. %{
  2. #include <stdio.h>
  3. int no=1;
  4. %}
  5. lite [0-9][0-9]*
  6. inv [0-9][a-zA-Z0-9]*
  7. ident [a-zA-Z][a-zA-Z0-9]*
  8. lite2 \"[a-zA-Z][a-zA-Z0-9]*\"
  9. rel <=|<|>=|>
  10. key int|void|char|if|main|printf
  11. %%
  12. {lite} {printf("%s\t%i\tInteger\n",yytext,no);}
  13. {lite2} { char aa[10],bb[10];
  14. strcpy(aa,yytext);
  15. for(int i=1;i<strlen(aa)-1;i++){
  16. bb[i-1]=aa[i];
  17. }
  18. printf("%s\t%i\tLiteral\n",bb,no);
  19. }
  20. {inv} {printf("Invalid:\t%s\n",yytext);}
  21. {rel} {
  22. printf("%s\t%i\tRelational Operator, \t",yytext,no);
  23. if(strcmp(yytext,"<=")==0){printf("LE");}
  24. if(strcmp(yytext,"<")==0){printf("LT");}
  25. if(strcmp(yytext,">=")==0){printf("LE");}
  26. if(strcmp(yytext,">")==0){printf("LT");}
  27. printf("\n");
  28. }
  29. "+"|"-"|"*"|"/" {printf("%s\t%i\tArithmetic opeator, ",yytext,no);
  30. if(strcmp(yytext,"+")==0){printf("ADD");}
  31. if(strcmp(yytext,"-")==0){printf("SUB");}
  32. if(strcmp(yytext,"/")==0){printf("DIV");}
  33. if(strcmp(yytext,"*")==0){printf("MUL");}
  34. printf("\n");
  35. }
  36. "=" {printf("%s\t%i\tAssignment operator, EQ\n",yytext,no);}
  37. {key} {printf("%s\t%i\tkeyword\n",yytext,no);}
  38. {ident} { printf("%s\t%i\tIdentifier\n",yytext,no);}
  39. "\n" {no++;}
  40. . {}
  41. %%
  42. int main()
  43. {
  44. yyin=fopen("input.txt","r");
  45. printf("Lexeme\tLine\tToken\n");
  46. yylex();
  47. }
Success #stdin #stdout #stderr 0.02s 6944KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/P9q4sb/prog:2:1: Syntax error: Operator expected
ERROR: /home/P9q4sb/prog:47:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit