Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.61 KB | None | 0 0
  1. %{
  2. #include <stdio.h>
  3. #include "ast.h"
  4.  
  5. void yyerror(char *s);
  6. int yylex();
  7.  
  8. //node_type* root = NULL;
  9. //node_type* aux_node = NULL;
  10. %}
  11.  
  12. %union{
  13. char* token;
  14. struct node_type* node;
  15. }
  16.  
  17. %token SEMICOLON BLANKID PACKAGE RETURN AND STAR ASSIGN COMMA DIV EQ GT GE LBRACE LE LPAR
  18. %token LSQ LT MINUS MOD NE NOT OR PLUS RBRACE RPAR RSQ ELSE FOR IF VAR INT FLOAT32 BOOL
  19. %token STRING PRINT PARSEINT FUNC CMDARGS
  20.  
  21. %token <token> RESERVED ID INTLIT REALLIT STRLIT
  22.  
  23. %type <node> Program Declarations VarDeclaration VarSpec Type FuncDeclaration Parameters
  24. %type <node> FuncBody VarsAndStatements Statement ParseArgs FuncInvocation Expr
  25. %type <node> auxVS auxPar auxStat auxFI
  26.  
  27. %nonassoc ELSE
  28.  
  29. %left COMMA
  30. %right ASSIGN
  31. %left OR
  32. %left AND
  33. %left EQ NE
  34. %left LT LE GE GT
  35. %left PLUS MINUS
  36. %left STAR DIV MOD
  37. %right NOT
  38.  
  39. %%
  40.  
  41. Program:
  42. PACKAGE ID SEMICOLON Declarations {;}
  43. ;
  44.  
  45. Declarations: /*empty*/ {;}
  46. | Declarations VarDeclaration SEMICOLON {;}
  47. | Declarations FuncDeclaration SEMICOLON {;}
  48. ;
  49.  
  50. VarDeclaration:
  51. VAR VarSpec {;}
  52. | VAR LPAR VarSpec SEMICOLON RPAR {;}
  53. ;
  54.  
  55. VarSpec:
  56. ID auxVS Type {;}
  57. ;
  58.  
  59. auxVS: /*empty*/ {;}
  60. | auxVS COMMA ID {;}
  61. ;
  62.  
  63. Type:
  64. INT {;}
  65. | FLOAT32 {;}
  66. | BOOL {;}
  67. | STRING {;}
  68. ;
  69.  
  70. FuncDeclaration:
  71. FUNC ID LPAR Parameters RPAR Type FuncBody {;}
  72. | FUNC ID LPAR RPAR Type FuncBody {;}
  73. | FUNC ID LPAR Parameters RPAR FuncBody {;}
  74. | FUNC ID LPAR RPAR FuncBody {;}
  75. ;
  76.  
  77. Parameters:
  78. ID Type auxPar {;}
  79. ;
  80.  
  81. auxPar: /*empty*/ {;}
  82. | auxPar COMMA ID Type {;}
  83. ;
  84.  
  85. FuncBody:
  86. LBRACE VarsAndStatements RBRACE {;}
  87. ;
  88.  
  89. VarsAndStatements: {;}
  90. | VarsAndStatements VarDeclaration SEMICOLON {;}
  91. | VarsAndStatements Statement SEMICOLON {;}
  92. | VarsAndStatements SEMICOLON {;}
  93. ;
  94.  
  95. Statement:
  96. error {;}
  97. | ID ASSIGN Expr {;}
  98. | LBRACE auxStat RBRACE {;}
  99. | IF Expr LBRACE auxStat RBRACE ELSE LBRACE auxStat RBRACE {;}
  100. | IF Expr LBRACE auxStat RBRACE {;}
  101. | FOR Expr LBRACE auxStat RBRACE {;}
  102. | FOR LBRACE auxStat RBRACE {;}
  103. | RETURN Expr {;}
  104. | RETURN {;}
  105. | FuncInvocation {;}
  106. | ParseArgs {;}
  107. | PRINT LPAR Expr RPAR {;}
  108. | PRINT LPAR STRLIT RPAR {;}
  109. ;
  110.  
  111. auxStat: /*empty*/ {;}
  112. | auxStat Statement SEMICOLON {;}
  113. ;
  114.  
  115. ParseArgs:
  116. ID COMMA BLANKID ASSIGN PARSEINT LPAR CMDARGS LSQ Expr RSQ RPAR {;}
  117. | ID COMMA BLANKID ASSIGN PARSEINT LPAR error RPAR {;}
  118. ;
  119.  
  120. FuncInvocation:
  121. ID LPAR Expr auxFI RPAR {;}
  122. | ID LPAR RPAR {;}
  123. | ID LPAR error RPAR {;}
  124. ;
  125.  
  126. auxFI: /*empty*/ {;}
  127. | auxFI COMMA Expr {;}
  128. ;
  129.  
  130. Expr:
  131. Expr OR Expr {;}
  132. | Expr AND Expr {;}
  133. | Expr LT Expr {;}
  134. | Expr GT Expr {;}
  135. | Expr EQ Expr {;}
  136. | Expr NE Expr {;}
  137. | Expr LE Expr {;}
  138. | Expr GE Expr {;}
  139. | Expr PLUS Expr {;}
  140. | Expr MINUS Expr {;}
  141. | Expr STAR Expr {;}
  142. | Expr DIV Expr {;}
  143. | Expr MOD Expr {;}
  144. | NOT Expr {;}
  145. | MINUS Expr {;}
  146. | PLUS Expr {;}
  147. | INTLIT {;}
  148. | REALLIT {;}
  149. | ID {;}
  150. | FuncInvocation {;}
  151. | LPAR Expr RPAR {;}
  152. | LPAR error RPAR {;}
  153. ;
  154.  
  155. %%
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement