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