fork download
  1. CALC.L
  2. %{
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. void yyerror(char *);
  6. #include "y.tab.h"
  7. int yylval;
  8. %}
  9. %%
  10. [a-z] {yylval=*yytext='&'; return VARIABLE;}
  11. [0-9]+ {yylval=atoi(yytext); return INTEGER;}
  12. CALC.Y
  13. [\t] ;
  14. %%
  15. int yywrap(void)
  16. {
  17. return 1;
  18. }
  19. %token INTEGER VARIABLE
  20. %left '+' '-'
  21. %left '*' '/'
  22. %{
  23. int yylex(void);
  24. void yyerror(char *);
  25. int sym[26];
  26. %}
  27. %%
  28. PROG:
  29. PROG STMT '\n'
  30. ;
  31. STMT: EXPR {printf("\n %d",$1);}
  32. | VARIABLE '=' EXPR {sym[$1] = $3;}
  33. ;
  34. EXPR: INTEGER
  35. | VARIABLE {$$ = sym[$1];}
  36. | EXPR '+' EXPR {$$ = $1 + $3;}
  37. | '(' EXPR ')' {$$ = $2;}
  38. %%
  39. void yyerror(char *s)
  40. {
  41. printf("\n %s",s);
  42. return;
  43. }
  44. int main(void)
  45. {
  46. printf("\n Enter the Expression:");
  47. yyparse();
  48. return 0;
  49.  
  50.  
Success #stdin #stdout #stderr 0.04s 6780KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/WWLmeX/prog:49:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit