fork download
  1. /*Lex code to count total number of tokens */
  2.  
  3. %{
  4. int n = 0 ;
  5. %}
  6.  
  7. // rule section
  8. %%
  9.  
  10. //count number of keywords
  11. "while"|"if"|"else" {n++;printf("\t keywords : %s", yytext);}
  12.  
  13. // count number of keywords
  14. "int"|"float" {n++;printf("\t keywords : %s", yytext);}
  15.  
  16. // count number of identifiers
  17. [a-zA-Z_][a-zA-Z0-9_]* {n++;printf("\t identifier : %s", yytext);}
  18.  
  19. // count number of operators
  20. "<="|"=="|"="|"++"|"-"|"*"|"+" {n++;printf("\t operator : %s", yytext);}
  21.  
  22. // count number of separators
  23. [(){}|, ;] {n++;printf("\t separator : %s", yytext);}
  24.  
  25. // count number of floats
  26. [0-9]*"."[0-9]+ {n++;printf("\t float : %s", yytext);}
  27.  
  28. // count number of integers
  29. [0-9]+ {n++;printf("\t integer : %s", yytext);}
  30.  
  31. . ;
  32. %%
  33.  
  34.  
  35. int main()
  36.  
  37. {
  38.  
  39. yylex();
  40.  
  41. printf("\n total no. of token = %d\n", n);
  42.  
  43. }
  44.  
  45.  
  46.  
  47.  
  48.  
Success #stdin #stdout #stderr 0.02s 6920KB
stdin
a=10 float b=5  >  <
stdout
Standard output is empty
stderr
ERROR: /home/citszt/prog:4:4: Syntax error: Operator expected
ERROR: /home/citszt/prog:47:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit