Advertisement
Guest User

Simple Calculator Bison

a guest
Mar 26th, 2018
369
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. %{
  2. #include<stdio.h>
  3. #include<ctype.h>
  4. void yyerror(char const*s)
  5. {
  6. fprintf(stderr,"%s\n",s);
  7. }
  8. %}
  9. %token DIGIT
  10. %%
  11. line : expr '\n' { printf ("%d\n" , $1 ) ; }
  12. expr : expr '+' term { $$ = $1 + $3 ; }
  13. | term
  14. ;
  15. term : term '*' factor { $$ = $1 * $3 ; }
  16. | factor
  17. ;
  18. factor : '(' expr ')' { $$ = $2 ; }
  19. | DIGIT
  20. ;
  21. %%
  22. yylex( ){
  23. int c;
  24. c = getchar( ) ;
  25. if (isdigit(c ) ) {
  26. yylval= c - '0';
  27. return DIGIT ;
  28. }
  29. return c ;
  30. }
  31. int yyparse(void);
  32. int main ( ) {
  33. return yyparse( ) ;
  34. }
Advertisement
RAW Paste Data Copied
Advertisement