fork download
  1. Parser.l
  2. %{
  3. #include<stdio.h>
  4. #include "y.tab.h"
  5. %}
  6. %%
  7. [0-9]+ {yylval.dval=atof(yytext);
  8. return DIGIT;
  9. }
  10. \n|. return yytext[0];
  11. %%
  12. parser.y
  13. %{
  14. #include<stdio.h>
  15. %}
  16. %union
  17. {
  18. double dval;
  19. }
  20. %token <dval> DIGIT
  21. %type <dval> expr
  22. %type <dval> term
  23. %type <dval> factor
  24. %%
  25. line: expr '\n' {
  26. printf("%g\n",$1);
  27. }
  28. ;
  29. expr: expr '+' term {$$=$1 + $3 ;}
  30. | term
  31.  
  32. ;
  33. term: term '*' factor {$$=$1 * $3 ;}
  34. | factor
  35. ;
  36. factor: '(' expr ')' {$$=$2 ;}
  37. | DIGIT
  38. ;
  39. %%
  40. int main()
  41. {
  42. yyparse();
  43. }
  44. yyerror(char *s)
  45. {
  46. printf("%s",s);
  47. printf("\n Type mismatch");
  48. }
Success #stdin #stdout #stderr 0.03s 6896KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/OJ3oWp/prog:2:2: Syntax error: Operator expected
ERROR: /home/OJ3oWp/prog:48:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit