WhaleSpunk

Untitled

Apr 2nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.38 KB | None | 0 0
  1. %{
  2. #define DEBUG 0
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "AST_Tree_Funcs.h"
  7.  
  8. AST_Tree_node *head = NULL;
  9. int yylex(void);
  10. void yyerror(const char *s);
  11. extern int n_linha;
  12. extern int n_coluna;
  13. extern char * yytext;
  14. extern int yyleng;
  15. int parse = 0;
  16. int flag = 0;
  17. int imprimir = 0;
  18.  
  19. extern int n_linha;
  20. extern int n_coluna;
  21. extern char * yytext;
  22. extern int yyleng;
  23. extern int yylineno;
  24.  
  25.  
  26. %}
  27.  
  28. %union{
  29. char *str;
  30. struct AST_Tree *node_struct;
  31. }
  32.  
  33.  
  34. %token <str> ID INTLIT CHRLIT REALLIT
  35. %token RESERVED CHAR ELSE WHILE IF INT SHORT DOUBLE RETURN VOID BITWISEAND BITWISEOR BITWISEXOR AND ASSIGN MUL COMMA DIV EQ GE GT LBRACE LE LPAR LT MINUS MOD NE NOT OR PLUS RBRACE RPAR SEMI
  36. %type <node_struct> FunctionsAndDeclarations FunctionDefinition_FunctionDeclaration_Declaration FunctionDefinition FunctionBody DeclarationsAndStatements FunctionDeclaration FunctionDeclarator ParameterList COMMA_ParameterDeclaration ParameterDeclaration Declaration COMMA_Declarator TypeSpec Declarator Statement Statement_aux Statement_error Expr ExprNew COMMA_Expr
  37.  
  38. %nonassoc "else"
  39. %left COMMA
  40. %right ASSIGN
  41. %left OR
  42. %left AND
  43. %left BITWISEOR BITWISEXOR BITWISEAND
  44. %left EQ NE
  45. %left LT GT LE GE
  46. %left MINUS PLUS
  47. %left MUL DIV MOD
  48. %nonassoc "add" "minus"
  49. %right NOT
  50. %left LPAR RPAR LBRACE RBRACE
  51. %nonassoc ELSE
  52.  
  53. %%
  54.  
  55. FunctionsAndDeclarations: FunctionDefinition {$$ = head = insertNode("Program"); insert_child($$, $1);}
  56. | FunctionDeclaration {$$ = head = insertNode("Program"); insert_child($$, $1);}
  57. | Declaration {$$ = head = insertNode("Program"); insert_child($$, $1);}
  58. | FunctionDefinition FunctionDefinition_FunctionDeclaration_Declaration {;}
  59. | FunctionDeclaration FunctionDefinition_FunctionDeclaration_Declaration {;}
  60. | Declaration FunctionDefinition_FunctionDeclaration_Declaration {;}
  61. ;
  62.  
  63. FunctionDefinition_FunctionDeclaration_Declaration: FunctionDefinition_FunctionDeclaration_Declaration FunctionDefinition {;}
  64. | FunctionDefinition_FunctionDeclaration_Declaration FunctionDeclaration {;}
  65. | FunctionDefinition_FunctionDeclaration_Declaration Declaration {;}
  66. | FunctionDefinition {;}
  67. | FunctionDeclaration {;}
  68. | Declaration {;}
  69. ;
  70.  
  71. FunctionDefinition: TypeSpec FunctionDeclarator FunctionBody {;}
  72.  
  73. FunctionBody: LBRACE RBRACE {;}
  74. | LBRACE DeclarationsAndStatements RBRACE {;}
  75. ;
  76.  
  77. DeclarationsAndStatements: Statement DeclarationsAndStatements {;}
  78. | Declaration DeclarationsAndStatements {;}
  79. | Statement {;}
  80. | Declaration {;}
  81. ;
  82.  
  83. FunctionDeclaration: TypeSpec FunctionDeclarator SEMI {;}
  84. ;
  85.  
  86. FunctionDeclarator: ID LPAR ParameterList RPAR {;}
  87. ;
  88.  
  89. ParameterList: ParameterDeclaration {;}
  90. | ParameterDeclaration COMMA_ParameterDeclaration {;}
  91. ;
  92. COMMA_ParameterDeclaration: COMMA ParameterDeclaration {;}
  93. | COMMA_ParameterDeclaration COMMA ParameterDeclaration {;}
  94. ;
  95. ParameterDeclaration: TypeSpec {;}
  96. | TypeSpec ID {;}
  97. ;
  98.  
  99. Declaration: TypeSpec Declarator SEMI {;}
  100. | TypeSpec Declarator COMMA_Declarator SEMI {;}
  101. | error SEMI {;}
  102. ;
  103. COMMA_Declarator: COMMA Declarator {;}
  104. | COMMA_Declarator COMMA Declarator {;}
  105. ;
  106. TypeSpec: CHAR {;}
  107. | INT {;}
  108. | VOID {;}
  109. | SHORT {;}
  110. | DOUBLE {;}
  111. ;
  112.  
  113. Declarator: ID {;}
  114. | ID ASSIGN Expr {;}
  115. ;
  116.  
  117.  
  118. Statement: Expr SEMI {;}
  119. | SEMI {;}
  120. | LBRACE Statement_aux RBRACE {;}
  121. | LBRACE RBRACE {;}
  122. | LBRACE error RBRACE {;}
  123. | IF LPAR Expr RPAR Statement_error %prec "else" {;}
  124. | IF LPAR Expr RPAR Statement_error ELSE Statement_error {;}
  125. | WHILE LPAR Expr RPAR Statement {;}
  126. | RETURN Expr SEMI {;}
  127. | RETURN SEMI {;}
  128. ;
  129.  
  130. Statement_aux: Statement_error {;}
  131. | Statement_aux Statement_error {;}
  132. ;
  133. Statement_error: error SEMI {;}
  134. | Statement {;}
  135. ;
  136.  
  137. Expr: ExprNew {;}
  138. | Expr COMMA ExprNew {;}
  139. ;
  140.  
  141. ExprNew: ExprNew ASSIGN ExprNew {;}
  142. | ExprNew PLUS ExprNew {;}
  143. | ExprNew MINUS ExprNew {;}
  144. | ExprNew MUL ExprNew {;}
  145. | ExprNew DIV ExprNew {;}
  146. | ExprNew MOD ExprNew {;}
  147. | ExprNew OR ExprNew {;}
  148. | ExprNew AND ExprNew {;}
  149. | ExprNew BITWISEAND ExprNew {;}
  150. | ExprNew BITWISEOR ExprNew {;}
  151. | ExprNew BITWISEXOR ExprNew {;}
  152. | ExprNew EQ ExprNew {;}
  153. | ExprNew NE ExprNew {;}
  154. | ExprNew LE ExprNew {;}
  155. | ExprNew GE ExprNew {;}
  156. | ExprNew LT ExprNew {;}
  157. | ExprNew GT ExprNew {;}
  158. | PLUS ExprNew %prec "add" {;}
  159. | MINUS ExprNew %prec "minus" {;}
  160. | NOT ExprNew {;}
  161. | ID LPAR RPAR {;}
  162. | ID LPAR ExprNew RPAR {;}
  163. | ID LPAR ExprNew COMMA_Expr RPAR {;}
  164. | ID {;}
  165. | INTLIT {;}
  166. | CHRLIT {;}
  167. | REALLIT {;}
  168. | LPAR Expr RPAR {;}
  169. | ID LPAR error RPAR {;}
  170. | LPAR error RPAR {;}
  171. ;
  172.  
  173. COMMA_Expr: COMMA ExprNew {;}
  174. | COMMA_Expr COMMA ExprNew {;}
  175. ;
  176.  
  177. %%
  178.  
  179. void yyerror(const char* s){
  180. if(flag ==0){
  181. flag = 1;
  182. }
  183. printf("Line %d, col %d: %s: %s\n",yylineno,(int)(n_coluna - strlen(yytext)),s,yytext);
  184. }
  185.  
  186.  
  187. int main(int argc, char *argv[])
  188. {
  189. int imprimir_arvore = 0;
  190. if(argc>1){
  191. if(strcmp(argv[1],"-l")==0){
  192. imprimir=1;
  193. yylex();
  194.  
  195. }else if(flag ==0 && strcmp(argv[1],"-t")==0){
  196. parse = 1;
  197. imprimir_arvore = 1;
  198. yyparse();
  199. }
  200. }
  201. else{
  202. parse = 1;
  203. yyparse();
  204. }
  205. printTree(head, 0, 0);
  206.  
  207.  
  208. return 0;
  209. }
Add Comment
Please, Sign In to add comment