Advertisement
Sri27119

cd3 calc yacc

Nov 20th, 2024
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. %{
  2. #include <stdio.h>
  3.  
  4. int yylex(void);
  5. void yyerror(const char *s);
  6.  
  7. int regs[26];
  8. int base;
  9. %}
  10. %start list
  11. %token DIGIT LETTER
  12. %left '|'
  13. %left '&'
  14. %left '+' '-'
  15. %left '*' '/' '%'
  16. %% /* beginning of rules section */
  17. list: /*empty */
  18. |
  19. list stat '\n'
  20. |
  21. list error '\n'
  22. {
  23. printf("errorrrrrr\n");
  24. yyerrok;
  25. }
  26. ;
  27. stat: expr
  28. {
  29. printf("%d\n",$1);
  30. }
  31. |
  32. LETTER '=' expr
  33. {
  34. printf("here\n");
  35. regs[$1] = $3;
  36. }
  37. ;
  38. expr: '(' expr ')'
  39. {
  40. $$ = $2;
  41. }
  42. |
  43. expr '+' expr
  44. {
  45. $$ = $1 + $3;
  46. }
  47. |
  48. expr '-' expr
  49. {
  50. $$ = $1 - $3;
  51. }
  52. |
  53. expr '*' expr
  54. {
  55. $$ = $1 * $3;
  56. }
  57. |
  58. expr '/' expr
  59. {
  60. $$ = $1 / $3;
  61. }
  62. |
  63. expr '%' expr
  64. {
  65. $$ = $1 % $3;
  66. }
  67. |
  68. LETTER
  69. {
  70. $$ = regs[$1];
  71. }
  72. |
  73. number
  74. ;
  75. number: DIGIT
  76. {
  77. //printf("first here!!");
  78. $$ = $1;
  79. base = ($1==0) ? 8 : 10;
  80. } |
  81. number DIGIT
  82. {
  83. printf("%d %d \n", $1, $2);
  84. $$ = base * $1 + $2;
  85. }
  86. ;
  87. %%
  88. int main()
  89. {
  90. return(yyparse());
  91. }
  92. void yyerror(const char *s)
  93. {
  94. fprintf(stderr, "%s\n",s);
  95. }
  96. int yywrap()
  97. {
  98. return(1);
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement