Advertisement
Guest User

Untitled

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