SHARE
TWEET

Untitled

a guest Jun 20th, 2019 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. %{
  2. extern int yylval;
  3. extern int sym[];
  4. extern int yyerror(char *s);
  5. #include "y.tab.h"
  6. %}
  7.  
  8. %%
  9.  
  10. [a-z]   {
  11.            yylval = *yytext - 'a';
  12.            return VARIABLE;
  13.         }
  14.  
  15. [0-9]+  {
  16.            yylval = atoi(yytext);
  17.            return INTEGER;
  18.         }
  19.  
  20. [(),]   { return *yytext; }
  21.  
  22. ":="    return ASSIGN;
  23. "+"     return PLUS;
  24. "-"     return MINUS;
  25. "/"     return DIVIDE;
  26. "%"     return MODULO;
  27. "*"     return TIMES;
  28. "<"     return SMAS;
  29. "<="    return SMGAS;
  30. "=="    return IS;
  31. "!="    return NOTIS;
  32. ">="    return BGGAS;
  33. ">"     return BGAS;
  34.  
  35. "min"   return MIN;
  36. "max"   return MAX;
  37.  
  38. "n"    return NEWLINE;
  39.  
  40. [ t]   ;
  41.  
  42. .       yyerror("Invalid character");
  43.  
  44. %%
  45.  
  46. int yywrap() {
  47.  return 1;
  48. }
  49.      
  50. %{
  51. #include <stdio.h>
  52. int sym[26];
  53. int yylex();
  54. int yyerror(char *s);
  55. %}
  56.  
  57. %token VARIABLE ASSIGN INTEGER NEWLINE
  58. %left PLUS
  59. %left TIMES
  60. %left MINUS
  61. %left DIVIDE
  62. %left MODULO
  63. %left UMINUS
  64. %left UPLUS
  65. %left SMAS SMGAS IS NOTIS BGAS BGGAS
  66. %left MIN MAX
  67.  
  68. %%
  69.  
  70. program: program statement
  71.        |
  72.        ;
  73.  
  74. statement: expr NEWLINE
  75.              { printf("%dn", $1); }
  76.          | VARIABLE ASSIGN expr NEWLINE
  77.              { sym[$1] = $3; }
  78.          ;
  79.  
  80. expr: INTEGER            { $$ = $1; }
  81.       | VARIABLE         { $$ = sym[$1]; }
  82.       | expr PLUS expr   { $$ = $1 + $3; }
  83.       | expr TIMES expr  { $$ = $1 * $3; }
  84.       | expr MINUS expr  { $$ = $1 - $3; }
  85.       | expr DIVIDE expr { $$ = $1 / $3; }
  86.       | expr MODULO expr { $$ = $1 % $3; }
  87.       | '(' expr ')'     { $$ = $2;      }
  88.  
  89.       | MINUS expr %prec UMINUS { $$ = -$2; }
  90.       | PLUS expr %prec UPLUS { $$ = $2; }
  91.  
  92.       | expr SMAS  expr   { $$ = $1 < $3;  }
  93.       | expr SMGAS expr   { $$ = $1 <= $3; }
  94.       | expr IS    expr   { $$ = $1 == $3; }
  95.       | expr NOTIS expr   { $$ = $1 != $3; }
  96.       | expr BGGAS expr   { $$ = $1 >= $3; }
  97.       | expr BGAS  expr   { $$ = $1 > $3;  }
  98.  
  99.       | MIN '(' expr ',' expr ')' { if ($3 < $5){ $$ = $3; } else if ($3 > $5){ $$ = $5; }; }
  100.       | MAX '(' expr ',' expr ')' { if ($3 > $5){ $$ = $3; } else if ($3 < $5){ $$ = $5; }; }
  101.  
  102.       ;
  103.  
  104. %%
  105.  
  106. int yyerror(char *s) {
  107.   fprintf(stderr, "%sn", s);
  108.   return 0;
  109. }
  110.  
  111. int main() {
  112.   yyparse();
  113.   return 0;
  114. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top