fork download
  1. %{
  2. #define NUM 300
  3. #define ID 301
  4. #define STRING 302
  5.  
  6. union {
  7. int ival;
  8. char name [30];
  9. char str [80];
  10. } yylval;
  11.  
  12. #include <string.h>
  13.  
  14. extern int atoi (const char *);
  15. %}
  16.  
  17. %option noyywrap
  18.  
  19. /* exclusive start condition -- deals with C++ style comments */
  20. %x COMMENT
  21.  
  22. %%
  23.  
  24. [0-9]+ { yylval.ival = atoi (yytext); return NUM; }
  25.  
  26. eps* [1-5] (eps u . [0-9]) credits
  27.  
  28. 3.5
  29.  
  30. [a-zA-Z][a-zA-Z0-9]* { strcpy (yylval.name, yytext); return ID; }
  31.  
  32. \"([^\\"\n]|\\.)*\" { strcpy (yylval.str, yytext); return STRING; }
  33.  
  34. [\n\t\r ]+ /* skip white space */
  35.  
  36. "//" { BEGIN (COMMENT); }
  37.  
  38. <COMMENT>.+ /* skip comment */
  39. <COMMENT>\n { /* end of comment --> resume normal processing */
  40. BEGIN (0); }
  41.  
  42. . { fprintf (stderr, "unrecognized token %c\n", yytext[0]); }
  43.  
  44. %%
  45.  
  46. int main (int argc, char **argv)
  47. {
  48. int token;
  49.  
  50. if (argc != 2) {
  51. fprintf(stderr, "Usage: <run file> <input file>\n");
  52. exit (1);
  53. }
  54.  
  55. yyin = fopen (argv[1], "r");
  56.  
  57. while ((token = yylex ()) != 0)
  58. switch (token) {
  59. case NUM: printf("NUMBER : %d\n", yylval.ival);
  60. break;
  61. case ID: printf ("ID : %s\n", yylval.name);
  62. break;
  63. case STRING: printf ("STRING: %s\n", yylval.str);
  64. break;
  65. default: fprintf (stderr, "error ... \n"); exit (1);
  66. }
  67. fclose (yyin);
  68. exit (0);
  69. }
  70.  
  71.  
Success #stdin #stdout #stderr 0.03s 6980KB
stdin
10006 “Intro to CompSci” 5 B.Sc. Software
10117 “Data Structures” 5 B.Sc. Software
40120 “Databases” 3.5 B.Sc. Management Elective
60165 “Autonomous vehicles” M.Sc. Mechanical elective
stdout
Standard output is empty
stderr
ERROR: /home/h7uyHq/prog:2:1: Syntax error: Operator expected
ERROR: /home/h7uyHq/prog:70:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? EOF: exit