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. 1
  22. 2
  23. 3
  24. 4
  25. 5
  26. 6
  27. 7
  28. 8
  29. 9
  30. 10
  31. 11
  32. 12
  33. 13
  34. 14
  35. 15
  36. 16
  37. 17
  38. 18
  39. 19
  40. 20
  41. 21
  42. 22
  43. 23
  44. {rel} {
  45. printf("%s\t%i\tRelational Operator, \t",yytext,no);
  46. if(strcmp(yytext,"<=")==0){printf("LE");}
  47. if(strcmp(yytext,"<")==0){printf("LT");}
  48. if(strcmp(yytext,">=")==0){printf("LE");}
  49. if(strcmp(yytext,">")==0){printf("LT");}
  50. printf("\n");
  51. }
  52. "+"|"-"|"*"|"/" {printf("%s\t%i\tArithmetic opeator, ",yytext,no);
  53. if(strcmp(yytext,"+")==0){printf("ADD");}
  54. if(strcmp(yytext,"-")==0){printf("SUB");}
  55. if(strcmp(yytext,"/")==0){printf("DIV");}
  56. if(strcmp(yytext,"*")==0){printf("MUL");}
  57. printf("\n");
  58. }
  59. "=" {printf("%s\t%i\tAssignment operator, EQ\n",yytext,no);}
  60. {key} {printf("%s\t%i\tkeyword\n",yytext,no);}
  61. {ident} { printf("%s\t%i\tIdentifier\n",yytext,no);}
  62. "\n" {no++;}
  63. . {}
  64. %%
  65. int main()
  66. {
  67. yyin=fopen("input.txt","r");
  68. printf("Lexeme\tLine\tToken\n");
  69. yylex();
  70. }
Success #stdin #stdout #stderr 0.03s 6904KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/Wy5znK/prog:2:1: Syntax error: Operator expected
ERROR: /home/Wy5znK/prog:70:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit