fork(2) download
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int yylineno = 1;
  6. FILE *yyin;
  7. %}
  8.  
  9. letter [a-zA-Z]
  10. digit [0-9]
  11.  
  12. %%
  13.  
  14. \n { yylineno++; }
  15.  
  16. "#include<"{letter}+(\.{letter}+)?">"
  17. { printf("%d\t%s\tPREPROCESSOR\n", yylineno, yytext); }
  18.  
  19. [ \t]+ ; /* Ignore spaces and tabs */
  20.  
  21. (return|main|for|while|case|switch|if|else)
  22. { printf("%d\t%s\tKEYWORD\n", yylineno, yytext); }
  23.  
  24. (int|float|char|struct)
  25. { printf("%d\t%s\tDATATYPE\n", yylineno, yytext); }
  26.  
  27. "(" { printf("%d\t%s\tBRACKET OPEN\n", yylineno, yytext); }
  28.  
  29. ")" { printf("%d\t%s\tBRACKET CLOSE\n", yylineno, yytext); }
  30.  
  31. "{" { printf("%d\t%s\tBRACE OPEN\n", yylineno, yytext); }
  32.  
  33. "}" { printf("%d\t%s\tBRACE CLOSE\n", yylineno, yytext); }
  34.  
  35. {letter}({letter}|{digit})*
  36. { printf("%d\t%s\tIDENTIFIER\n", yylineno, yytext); }
  37.  
  38. {digit}+
  39. { printf("%d\t%s\tINTEGER\n", yylineno, yytext); }
  40.  
  41. {digit}+"E"(\+|\-)?{digit}+
  42. { printf("%d\t%s\tREAL NUMBER\n", yylineno, yytext); }
  43.  
  44. {digit}+\.{digit}+(E(\+|\-)?{digit}+)?
  45. { printf("%d\t%s\tFLOATING NUMBER\n", yylineno, yytext); }
  46.  
  47. "=" { printf("%d\t%s\tASSIGNMENT\n", yylineno, yytext); }
  48.  
  49. ";" { printf("%d\t%s\tTERMINATOR\n", yylineno, yytext); }
  50.  
  51. \"({letter}|{digit}|[ ])*\"
  52. { printf("%d\t%s\tSTRING CONSTANT\n", yylineno, yytext); }
  53.  
  54. . ; /* Ignore other characters */
  55.  
  56. %%
  57.  
  58. int yywrap()
  59. {
  60. return 1;
  61. }
  62.  
  63. int main(int argc, char *argv[])
  64. {
  65. if(argc < 2)
  66. {
  67. printf("Usage: %s <inputfile>\n", argv[0]);
  68. return 1;
  69. }
  70.  
  71. yyin = fopen(argv[1], "r");
  72.  
  73. if(yyin == NULL)
  74. {
  75. printf("Cannot open file\n");
  76. return 1;
  77. }
  78.  
  79. printf("LINE\tLEXEME\tTOKEN\n");
  80.  
  81. yylex();
  82.  
  83. fclose(yyin);
  84. return 0;
  85. }
Success #stdin #stdout #stderr 0.03s 6816KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/CnFudo/prog:2:1: Syntax error: Operator expected
ERROR: /home/CnFudo/prog:85:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit