[text] 2nd

Viewer

  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. %}
  5.  
  6. %token NUMBER
  7. %left '+' '-'
  8. %left '*' '/'
  9.  
  10. %%
  11.  
  12. input: 
  13.      | input expr '\n' { printf("Postfix Notation: %s\n", $2); }
  14.      ;
  15.  
  16. expr: NUMBER { $$ = $1; }
  17.     | expr '+' expr { asprintf(&$$, "%s %s +", $1, $3); free($1); free($3); }
  18.     | expr '-' expr { asprintf(&$$, "%s %s -", $1, $3); free($1); free($3); }
  19.     | expr '*' expr { asprintf(&$$, "%s %s *", $1, $3); free($1); free($3); }
  20.     | expr '/' expr { asprintf(&$$, "%s %s /", $1, $3); free($1); free($3); }
  21.     ;
  22.  
  23. %%
  24.  
  25. int main() {
  26.     yyparse();
  27.     return 0;
  28. }
  29.  
  30. int yyerror(char *s) {
  31.     printf("Error: %s\n", s);
  32.     return 0;
  33. }

Editor

You can edit this paste and save as new:


File Description
  • 2nd
  • Paste Code
  • 28 Feb-2024
  • 668 Bytes
You can Share it: