Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.56 KB | None | 0 0
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. void yyerror(char *s);
  7. int yylex(void);
  8.  
  9. int flagError = 0;
  10. extern int flag;
  11.  
  12. typedef struct node {
  13. char * type;
  14. struct node * nodeSon;
  15. struct node * nodeBrother;
  16. } node;
  17.  
  18. node * nodeAux=NULL;
  19. node * temp_list=NULL;
  20.  
  21. node * insertNode(char * type, node * nodeSon) {
  22. node * auxNode = (node *)malloc(sizeof(node));
  23. auxNode->type = type;
  24. auxNode->nodeSon = nodeSon;
  25. auxNode->nodeBrother = NULL;
  26.  
  27. return auxNode;
  28. }
  29.  
  30. void printPoints(int n_points) {
  31. int i;
  32. for(i=0; i<n_points; i++)
  33. printf(".");
  34. }
  35.  
  36. void printTree(node * node, int points) {
  37.  
  38. while(node) {
  39.  
  40. if (strcmp(node->type, "NULL")!= 0) {
  41. printPoints(points);
  42. printf("%s\n", node->type);
  43. }
  44.  
  45. printTree(node->nodeSon, points+2);
  46.  
  47. node = node->nodeBrother;
  48. }
  49. }
  50.  
  51. node * insertBrother(node *oldB, node *newB){
  52.  
  53. node *aux = oldB;
  54. while(aux->nodeBrother != NULL){
  55. aux = aux->nodeBrother;
  56. }
  57. aux->nodeBrother = newB;
  58.  
  59. return oldB;
  60. }
  61.  
  62.  
  63. %}
  64.  
  65.  
  66.  
  67.  
  68. %union{
  69. struct node * node;
  70. char * id;
  71. }
  72.  
  73.  
  74. %token <id> ID
  75. %token <id> INTLIT
  76. %token <id> REALLIT
  77. %token <id> STRLIT
  78.  
  79. %type <node> Program
  80. %type <node> Declarations
  81. %type <node> VarDeclaration
  82. %type <node> FuncDeclaration
  83. %type <node> VarSpec
  84. %type <node> VarSpecAux
  85. %type <node> Type
  86. %type <node> FuncBody
  87. %type <node> VarsAndStatements
  88. %type <node> Parameters
  89. %type <node> ParametersAux
  90. %type <node> Statement
  91. %type <node> Expr
  92. %type <node> FuncInvocation
  93.  
  94.  
  95. %token SEMICOLON BLANKID PACKAGE RETURN AND STAR COMMA DIV ASSIGN EQ GE GT LBRACE LE LPAR LSQ LT MINUS MOD NE NOT OR PLUS RBRACE RPAR RSQ ELSE FOR IF VAR INT FLOAT32 BOOL STRING PRINT PARSEINT FUNC CMDARGS RESERVED ESCSEQ
  96.  
  97. %left COMMA
  98. %right ASSIGN
  99. %left OR
  100. %left AND
  101. %left EQ NE
  102. %left LE LT GT GE
  103. %left PLUS MINUS
  104. %left STAR DIV MOD
  105. %right NOT
  106. %left RPAR LPAR
  107.  
  108.  
  109. %%
  110.  
  111. Program:
  112. PACKAGE ID SEMICOLON Declarations {$$ = insertNode("Program", $4); if(flagError==0 && flag ==2)printTree($$, 0);}
  113. ;
  114.  
  115. Declarations: %empty {$$ = insertNode("NULL", NULL);
  116. }
  117. | Declarations VarDeclaration SEMICOLON {$$ = insertBrother($1, $2);
  118. }
  119. | Declarations FuncDeclaration SEMICOLON {nodeAux = insertNode("FuncDecl", $2);
  120. $$ = insertBrother($1, nodeAux);
  121. }
  122. ;
  123.  
  124. VarDeclaration:
  125. VAR VarSpec {$$ = $2;
  126. }
  127. | VAR LPAR VarSpec SEMICOLON RPAR {$$ = $3;
  128. }
  129. ;
  130.  
  131. VarSpec:
  132. ID Type {char strAux[1024];
  133. sprintf(strAux, "Id(%s)", $1);
  134. nodeAux = insertNode(strdup(strAux), NULL);
  135. insertBrother($2, nodeAux);
  136. $$ = insertNode("VarDecl", $2);
  137.  
  138. }
  139. | ID VarSpecAux Type {char strAux[1024];
  140. sprintf(strAux, "Id(%s)", $1);
  141. nodeAux = insertNode(strdup(strAux), NULL);
  142. insertBrother($3, nodeAux);
  143.  
  144. $$ = insertNode("VarDecl", $3);
  145. $$->nodeBrother = $2;
  146.  
  147. temp_list = $$;
  148. while(temp_list) {
  149. temp_list->nodeSon->type = $3->type;
  150. temp_list = temp_list->nodeBrother;
  151. }
  152. }
  153. ;
  154.  
  155. VarSpecAux:
  156. COMMA ID {char strAux[1024];
  157. sprintf(strAux, "Id(%s)", $2);
  158. nodeAux = insertNode(NULL, NULL);
  159. insertBrother(nodeAux, insertNode(strdup(strAux), NULL));
  160.  
  161. $$ = insertNode("VarDecl", nodeAux);
  162. }
  163.  
  164. | VarSpecAux COMMA ID {
  165. char strAux[1024];
  166. sprintf(strAux, "Id(%s)", $3);
  167. nodeAux = insertNode(NULL, NULL);
  168. insertBrother(nodeAux, insertNode(strdup(strAux), NULL));
  169.  
  170. temp_list = $$;
  171. while(temp_list->nodeBrother) {
  172. temp_list = temp_list->nodeBrother;
  173. }
  174. temp_list->nodeBrother = insertNode("VarDecl", nodeAux);
  175. }
  176. ;
  177.  
  178. Type:
  179. INT {$$ = insertNode("Int", NULL);}
  180. | FLOAT32 {$$ = insertNode("Float32", NULL);}
  181. | BOOL {$$ = insertNode("Bool", NULL);}
  182. | STRING {$$ = insertNode("String", NULL);}
  183. ;
  184.  
  185.  
  186. FuncDeclaration:
  187. FUNC ID LPAR RPAR FuncBody {char strAux[1024];
  188. sprintf(strAux, "Id(%s)", $2);
  189. nodeAux = insertNode(strdup(strAux), NULL);
  190. $$ = insertNode("FuncHeader", nodeAux);
  191. insertBrother(nodeAux, insertNode("FuncParams", NULL));
  192.  
  193. insertBrother($$, $5);
  194. }
  195. | FUNC ID LPAR Parameters RPAR FuncBody {char strAux[1024];
  196. sprintf(strAux, "Id(%s)", $2);
  197. nodeAux = insertNode(strdup(strAux), NULL);
  198. $$ = insertNode("FuncHeader", nodeAux);
  199. insertBrother(nodeAux, insertNode("FuncParams", $4));
  200.  
  201. insertBrother($$, $6);
  202. }
  203. | FUNC ID LPAR RPAR Type FuncBody {char strAux[1024];
  204. sprintf(strAux, "Id(%s)", $2);
  205. nodeAux = insertNode(strdup(strAux), NULL);
  206. insertBrother(nodeAux, $5);
  207.  
  208. insertBrother(nodeAux, insertNode("FuncParams", NULL));
  209. $$ = insertNode("FuncHeader", nodeAux);
  210.  
  211. insertBrother($$, $6);
  212. }
  213. | FUNC ID LPAR Parameters RPAR Type FuncBody {char strAux[1024];
  214. sprintf(strAux, "Id(%s)", $2);
  215. nodeAux = insertNode(strdup(strAux), NULL);
  216. insertBrother(nodeAux, $6);
  217.  
  218. insertBrother(nodeAux, insertNode("FuncParams", $4));
  219. $$ = insertNode("FuncHeader", nodeAux);
  220.  
  221. insertBrother($$, $7);
  222. }
  223. ;
  224.  
  225. Parameters:
  226. ID Type {char strAux[1024];
  227. sprintf(strAux, "Id(%s)", $1);
  228. nodeAux = insertBrother($2, insertNode(strdup(strAux), NULL));
  229. $$ = insertNode("ParamDecl", nodeAux);
  230. }
  231. | ID Type ParametersAux {
  232. char strAux[1024];
  233. sprintf(strAux, "Id(%s)", $1);
  234. nodeAux = insertNode(strdup(strAux), NULL);
  235. insertBrother($2, nodeAux);
  236.  
  237. // inserir no inicio da lista
  238. $$ = insertNode("ParamDecl", $2);
  239. $$->nodeBrother = $3;
  240.  
  241.  
  242. }
  243. ;
  244.  
  245. ParametersAux:
  246. COMMA ID Type {char strAux[1024];
  247. sprintf(strAux, "Id(%s)", $2);
  248. nodeAux = insertNode(strdup(strAux), NULL);
  249. insertBrother($3, nodeAux);
  250.  
  251. $$ = insertNode("ParamDecl", $3);
  252. }
  253. | ParametersAux COMMA ID Type {char strAux[1024];
  254. sprintf(strAux, "Id(%s)", $3);
  255. nodeAux = insertNode(strdup(strAux), NULL);
  256. insertBrother($4, nodeAux);
  257.  
  258. nodeAux = insertNode("ParamDecl", $4);
  259.  
  260. temp_list = $$;
  261. while(temp_list->nodeBrother) {
  262. temp_list = temp_list->nodeBrother;
  263. }
  264. temp_list->nodeBrother = nodeAux;
  265. }
  266. ;
  267.  
  268. FuncBody:
  269. LBRACE VarsAndStatements RBRACE {$$ = insertNode("FuncBody", $2);}
  270. ;
  271.  
  272. VarsAndStatements: %empty {$$ = insertNode("NULL", NULL);}
  273. | VarsAndStatements SEMICOLON {$$ = $1;}
  274. | VarsAndStatements VarDeclaration SEMICOLON {$$ = insertBrother($$, $2);}
  275. | VarsAndStatements Statement SEMICOLON {$$ = insertNode("FuncBody", $2);}
  276. ;
  277.  
  278. Statement:
  279. ID ASSIGN Expr {}
  280. | LBRACE StatementAux RBRACE {}
  281. | IF Expr LBRACE StatementAux RBRACE ELSE LBRACE StatementAux RBRACE {}
  282. | IF Expr LBRACE StatementAux RBRACE {}
  283. | FOR LBRACE StatementAux RBRACE {}
  284. | FOR Expr LBRACE StatementAux RBRACE {}
  285. | RETURN {}
  286. | RETURN Expr {}
  287. | FuncInvocation {$$ = $1;}
  288. | ParseArgs {}
  289. | PRINT LPAR Expr RPAR {}
  290. | PRINT LPAR STRLIT RPAR {}
  291. | error {}
  292. ;
  293.  
  294. StatementAux: %empty {}
  295. | Statement SEMICOLON StatementAux {}
  296. ;
  297.  
  298. ParseArgs:
  299. ID COMMA BLANKID ASSIGN PARSEINT LPAR CMDARGS LSQ Expr RSQ RPAR {}
  300. | ID COMMA BLANKID ASSIGN PARSEINT LPAR error RPAR {}
  301. ;
  302.  
  303. FuncInvocation:
  304. ID LPAR RPAR {char strAux[1024];
  305. sprintf(strAux, "Id(%s)", $1);
  306. $$ = insertNode(strdup(strAux), NULL);}
  307. | ID LPAR Expr FuncInvocationAux RPAR {}
  308. | ID LPAR Expr RPAR {}
  309. | ID LPAR error RPAR {}
  310. ;
  311.  
  312. FuncInvocationAux:
  313. COMMA Expr {}
  314. | COMMA Expr FuncInvocationAux {}
  315. ;
  316.  
  317. Expr:
  318. Expr OR Expr {$$ = insertNode("Or", $1);
  319. insertBrother($1, $3);}
  320. | Expr AND Expr {$$ = insertNode("And", $1);
  321. insertBrother($1, $3);}
  322. | Expr LT Expr {$$ = insertNode("Lt", $1);
  323. insertBrother($1, $3);}
  324. | Expr GT Expr {$$ = insertNode("Gt", $1);
  325. insertBrother($1, $3);}
  326. | Expr EQ Expr {$$ = insertNode("Eq", $1);
  327. insertBrother($1, $3);}
  328. | Expr NE Expr {$$ = insertNode("Ne", $1);
  329. insertBrother($1, $3);}
  330. | Expr LE Expr {$$ = insertNode("Le", $1);
  331. insertBrother($1, $3);}
  332. | Expr GE Expr {$$ = insertNode("Ge", $1);
  333. insertBrother($1, $3);}
  334. | Expr PLUS Expr {$$ = insertNode("Add", $1);
  335. insertBrother($1, $3);}
  336. | Expr MINUS Expr {$$ = insertNode("Sub", $1);
  337. insertBrother($1, $3);}
  338. | Expr STAR Expr {$$ = insertNode("Star", $1);
  339. insertBrother($1, $3);}
  340. | Expr DIV Expr {$$ = insertNode("Div", $1);
  341. insertBrother($1, $3);}
  342. | Expr MOD Expr {$$ = insertNode("Mod", $1);
  343. insertBrother($1, $3);}
  344. | NOT Expr {$$ = insertNode("Add", $2);}
  345. | Expr MINUS %prec NOT {$$ = insertNode("Minus", $1);}
  346. | Expr PLUS %prec NOT {$$ = insertNode("Plus", $1);}
  347. | INTLIT {char strAux[1024];
  348. sprintf(strAux, "INTLIT(%s)", $1);
  349. $$ = insertNode(strdup(strAux), NULL);}
  350. | REALLIT {char strAux[1024];
  351. sprintf(strAux, "REALLIT(%s)", $1);
  352. $$ = insertNode(strdup(strAux), NULL);}
  353. | ID {char strAux[1024];
  354. sprintf(strAux, "Id(%s)", $1);
  355. $$ = insertNode(strdup(strAux), NULL);}
  356. | FuncInvocation {$$ = $1;}
  357. | LPAR Expr RPAR {$$ = $2;}
  358. | LPAR error RPAR {$$ = insertNode("NULL", NULL);flagError = 1;}
  359. ;
  360.  
  361. %%
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement