fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #define MAX_IDENTIFIER_LENGTH 50
  5. %}
  6. %option noyywrap
  7. %option yylineno
  8. void addtosymboltable(const char*)
  9. %%
  10. [\t] /*ignore whitespace*/
  11. \n /*ignore newline*/
  12. \/\*([^*]|\*+[^*/])*\*+\/ /*ignore comments*/
  13. [0-9]+ {printf("Constant:%s \n",yytext);}
  14. = {printf("%s is an Assignment Operator\n",yytext);}
  15. \* |
  16. \+ |
  17. \- {printf("%s is a Operator\n",yytext);}
  18. [a-zA-Z][a-zA-Z0-9]* {printf("identifier:%s\n",yytext);addtosymboltable(yytext);}
  19. . {printf("Invalid token:%s \n",yytext);}
  20. %%
  21. typedef struct
  22. {
  23. char name[MAX_IDENTIFIER_LENGTH];
  24. } Symbol;
  25. Symbol symbol_table[100];
  26. int symbol_count=0;
  27. void addtosymboltable(const char * identifier){
  28. if(symbol_count < 100)
  29. {
  30. strncpy(symbol_table[symbol_count].name,identifier,MAX_IDENTIFIER_LENGTH-1);
  31. symbol_table[symbol_count].name[MAX_IDENTIFIER_LENGTH-1]='\0';
  32.  
  33. symbol_count++;
  34. printf("Identifier %s is entered in the symbol table\n",identifier);
  35. }
  36. else
  37. {
  38. printf("Symbol table is full.Cannot add more identifier.\n");
  39. exit(0);
  40. }
  41. }
  42. int main()
  43. {
  44. yylex();
  45. return 1;
  46. }
Success #stdin #stdout #stderr 0.03s 6996KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/pEcA6C/prog:2:1: Syntax error: Operator expected
ERROR: /home/pEcA6C/prog:46:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit