fork download
  1. %{
  2. #include<stdio.h>
  3. int i=0,k=0,n=0,s=0,r=0;
  4. %}
  5.  
  6. letter [A-Za-z]
  7. digit [0-9]
  8. id {letter}({letter}|{digit})*
  9. number {digit}+
  10.  
  11. %%
  12.  
  13. int|char|double|float|case {printf("\n\"%s\" is a keyword",yytext);k++;}
  14. {id} {printf("\n\"%s\" is an identifier",yytext);i++;}
  15. {number} {printf("\n\"%s\" is a number",yytext);n++;}
  16. [<>]=|<>|== {printf("\n\"%s\" is a relational operator",yytext);r++;}
  17. [;{}()\",] {printf("\n\"%s\" is a special symbol",yytext);s++;}
  18.  
  19. %%
  20.  
  21. int yywrap() {
  22. return 1;
  23. }
  24.  
  25. int main(int argc, char *argv[]) {
  26. FILE *fp;
  27. if(argc >= 2) {
  28. fp = fopen(argv[1], "r");
  29. if(!fp) {
  30. printf("File cannot open %s", argv[1]);
  31. return 1;
  32. }
  33. else {
  34. yyin = fp;
  35. yylex();
  36. printf("\nIdentifiers: %d \nKeywords: %d \nRelational Operators: %d \nDigits: %d \nSpecial Symbols: %d\n", i, k, r, n, s);
  37. fclose(fp);
  38. return 0;
  39. }
  40. }
  41. else {
  42. printf("Please provide input file as argument");
  43. return 1;
  44. }
  45. }
  46.  
Success #stdin #stdout #stderr 0.02s 6972KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/cBp2SR/prog:45:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit