fork download
  1. /* program name is lexp.l */
  2. %{
  3. /* program to recognize a C program */
  4. int COMMENT = 0;
  5. %}
  6.  
  7. identifier [a-zA-Z][a-zA-Z0-9]*
  8.  
  9. %%
  10.  
  11. #.* {
  12. printf("\n%s is a PREPROCESSOR DIRECTIVE", yytext);
  13. }
  14.  
  15. (int|float|char|double|while|for|do|if|break|continue|void|switch|case|long|struct|const|typedef|return|else|goto) {
  16. printf("\n\t%s is a KEYWORD", yytext);
  17. }
  18.  
  19. "/*" {
  20.   COMMENT = 1;
  21. }
  22.  
  23. "*/" {
  24. COMMENT = 0;
  25. }
  26.  
  27. {identifier}\(.*\) {
  28. if (!COMMENT)
  29. printf("\n\nFUNCTION\n\t%s", yytext);
  30. }
  31.  
  32. \{ {
  33. if (!COMMENT)
  34. printf("\n BLOCK BEGINS");
  35. }
  36.  
  37. \} {
  38. if (!COMMENT)
  39. printf("\n BLOCK ENDS");
  40. }
  41.  
  42. {identifier}(\[[0-9]*\])? {
  43. if (!COMMENT)
  44. printf("\n %s IDENTIFIER", yytext);
  45. }
  46.  
  47. \".*\" {
  48. if (!COMMENT)
  49. printf("\n\t%s is a STRING", yytext);
  50. }
  51.  
  52. [0-9]+ {
  53. if (!COMMENT)
  54. printf("\n\t%s is a NUMBER", yytext);
  55. }
  56.  
  57. \)(\;)? {
  58. if (!COMMENT)
  59. printf("\n\t");
  60. ECHO;
  61. printf("\n");
  62. }
  63.  
  64. \( ECHO;
  65.  
  66. = {
  67. if (!COMMENT)
  68. printf("\n\t%s is an ASSIGNMENT OPERATOR", yytext);
  69. }
  70.  
  71. \<=|\>=|\<|==|\> {
  72. if (!COMMENT)
  73. printf("\n\t%s is a RELATIONAL OPERATOR", yytext);
  74. }
  75.  
  76. %%
  77.  
  78. int main(int argc, char **argv) {
  79. if (argc > 1) {
  80. FILE *file;
  81. file = fopen(argv[1], "r");
  82. if (!file) {
  83. printf("could not open %s \n", argv[1]);
  84. exit(0);
  85. }
  86. yyin = file;
  87. }
  88. yylex();
  89. printf("\n\n");
  90. return 0;
  91. }
  92.  
  93. int yywrap() {
  94. return 0;
  95. }
  96.  
Success #stdin #stdout #stderr 0.03s 6948KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/wFrNR6/prog:95:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit