Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 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 * id;
  14. char * type;
  15. struct node * nodeSon;
  16. struct node * nodeBrother;
  17. } node;
  18.  
  19. node * nodeAux;
  20.  
  21. node * insertNode(char * id, char * type, node * nodeSon) {
  22. node * auxNode = (node *)malloc(sizeof(node));
  23. auxNode->type = type;
  24. auxNode->id = id;
  25. auxNode->nodeSon = nodeSon;
  26. auxNode->nodeBrother = NULL;
  27.  
  28. return auxNode;
  29. }
  30.  
  31. void printPoints(int n_points) {
  32. int i;
  33. for(i=0; i<n_points; i++)
  34. printf(".");
  35. }
  36.  
  37. void printTree(node * node, int points) {
  38.  
  39. while(node) {
  40. printPoints(points);
  41. printf("%s\n", node->type);
  42.  
  43. printTree(node->nodeSon, points+2);
  44. node = node->nodeBrother;
  45. }
  46.  
  47. }
  48.  
  49. void linkBrother(node * node1, node * node2) {
  50. node1->nodeBrother = node2;
  51. }
  52.  
  53. %}
  54.  
  55.  
  56.  
  57.  
  58. %union{
  59. struct node * node;
  60. char * id;
  61. }
  62.  
  63.  
  64. %token <id> ID
  65. %token <id> INTLIT
  66. %token <id> REALLIT
  67. %token <id> STRLIT
  68.  
  69. %type <node> Program
  70. %type <node> Declarations
  71. %type <node> VarDeclaration
  72. %type <node> FuncDeclaration
  73. %type <node> VarSpec
  74. %type <node> VarSpecAux
  75. %type <node> Type
  76.  
  77. %token SEMICOLON
  78. %token BLANKID
  79. %token PACKAGE
  80. %token RETURN
  81. %token AND
  82. %token STAR
  83. %token COMMA
  84. %token DIV
  85. %token ASSIGN
  86. %token EQ
  87. %token GE
  88. %token GT
  89. %token LBRACE
  90. %token LE
  91. %token LPAR
  92. %token LSQ
  93. %token LT
  94. %token MINUS
  95. %token MOD
  96. %token NE
  97. %token NOT
  98. %token OR
  99. %token PLUS
  100. %token RBRACE
  101. %token RPAR
  102. %token RSQ
  103. %token ELSE
  104. %token FOR
  105. %token IF
  106. %token VAR
  107. %token INT
  108. %token FLOAT32
  109. %token BOOL
  110. %token STRING
  111. %token PRINT
  112. %token PARSEINT
  113. %token FUNC
  114. %token CMDARGS
  115. %token RESERVED
  116. %token ESCSEQ
  117.  
  118. %left COMMA
  119. %right ASSIGN
  120. %left OR
  121. %left AND
  122. %left EQ NE
  123. %left LE LT GT GE
  124. %left PLUS MINUS
  125. %left STAR DIV MOD
  126. %right NOT
  127. %left RPAR LPAR
  128.  
  129.  
  130. %%
  131.  
  132. Program:
  133. PACKAGE ID SEMICOLON Declarations {$$ = insertNode(NULL, "Program", $4); if(flagError==0)printTree($$, 0);}
  134. ;
  135.  
  136. Declarations: %empty {$$ = insertNode(NULL, "NULL", NULL);}
  137.  
  138.  
  139. | VarDeclaration SEMICOLON Declarations {//printf("%s\n", $1->type);
  140. $$ = insertNode(NULL, "VarDecl", $1);
  141.  
  142. linkBrother($1, $3);
  143. nodeAux = $3;
  144. while (nodeAux->nodeBrother!=NULL) {
  145. printf("type: %s\n", nodeAux->type);
  146. nodeAux = nodeAux->nodeBrother;
  147. }
  148.  
  149. }
  150.  
  151.  
  152.  
  153.  
  154. | FuncDeclaration SEMICOLON Declarations {linkBrother($1, $3); $$ = $1;}
  155. ;
  156.  
  157. VarDeclaration:
  158. VAR VarSpec {$$ = $2;}
  159. | VAR LPAR VarSpec SEMICOLON RPAR {$$ = insertNode(NULL, "VarDecl", $3);}
  160. ;
  161.  
  162. VarSpec:
  163. ID Type {$$ = insertNode($1, "Id", NULL);}
  164. | ID VarSpecAux Type {$$ = insertNode($1, "Id", NULL);}
  165. ;
  166.  
  167. VarSpecAux:
  168. COMMA ID {$$ = insertNode(NULL, "Var", NULL);}
  169. | COMMA ID VarSpecAux {$$ = insertNode(NULL, "Var", NULL);
  170. nodeAux = $$;
  171. while (nodeAux->nodeBrother != NULL) {
  172. nodeAux = nodeAux->nodeBrother;
  173. }
  174. nodeAux->nodeBrother = $3;
  175. }
  176. ;
  177.  
  178. Type:
  179. INT {$$ = insertNode(NULL, "Int", NULL);}
  180. | FLOAT32 {$$ = insertNode(NULL, "Float32", NULL);}
  181. | BOOL {$$ = insertNode(NULL, "Bool", NULL);}
  182. | STRING {$$ = insertNode(NULL, "String", NULL);}
  183. ;
  184.  
  185.  
  186. FuncDeclaration:
  187. FUNC ID LPAR RPAR FuncBody
  188. | FUNC ID LPAR Parameters RPAR FuncBody
  189. | FUNC ID LPAR RPAR Type FuncBody
  190. | FUNC ID LPAR Parameters RPAR Type FuncBody
  191. ;
  192.  
  193. Parameters:
  194. ID Type
  195. | ID Type ParametersAux
  196. ;
  197.  
  198. ParametersAux:
  199. COMMA ID Type
  200. | COMMA ID Type ParametersAux
  201. ;
  202.  
  203. FuncBody:
  204. LBRACE VarsAndStatements RBRACE
  205. ;
  206.  
  207. VarsAndStatements: %empty
  208. | VarsAndStatements SEMICOLON
  209. | VarsAndStatements VarDeclaration SEMICOLON
  210. | VarsAndStatements Statement SEMICOLON
  211. ;
  212.  
  213. Statement:
  214. ID ASSIGN Expr
  215. | LBRACE StatementAux RBRACE
  216. | IF Expr LBRACE StatementAux RBRACE ELSE LBRACE StatementAux RBRACE
  217. | IF Expr LBRACE StatementAux RBRACE
  218. | FOR LBRACE StatementAux RBRACE
  219. | FOR Expr LBRACE StatementAux RBRACE
  220. | RETURN
  221. | RETURN Expr
  222. | FuncInvocation
  223. | ParseArgs
  224. | PRINT LPAR Expr RPAR
  225. | PRINT LPAR STRLIT RPAR
  226. | error {printf("error");}
  227. ;
  228.  
  229. StatementAux: %empty
  230. | Statement SEMICOLON StatementAux
  231. ;
  232.  
  233. ParseArgs:
  234. ID COMMA BLANKID ASSIGN PARSEINT LPAR CMDARGS LSQ Expr RSQ RPAR
  235. | ID COMMA BLANKID ASSIGN PARSEINT LPAR error RPAR
  236. ;
  237.  
  238. FuncInvocation:
  239. ID LPAR RPAR
  240. | ID LPAR Expr FuncInvocationAux RPAR
  241. | ID LPAR Expr RPAR
  242. | ID LPAR error RPAR
  243. ;
  244.  
  245. FuncInvocationAux:
  246. COMMA Expr
  247. | COMMA Expr FuncInvocationAux
  248. ;
  249.  
  250. Expr:
  251. Expr OR Expr
  252. | Expr AND Expr
  253. | Expr LT Expr
  254. | Expr GT Expr
  255. | Expr EQ Expr
  256. | Expr NE Expr
  257. | Expr LE Expr
  258. | Expr GE Expr
  259. | Expr PLUS Expr
  260. | Expr MINUS Expr
  261. | Expr STAR Expr
  262. | Expr DIV Expr
  263. | Expr MOD Expr
  264. | NOT Expr
  265. | MINUS Expr NOT
  266. | PLUS Expr NOT
  267. | INTLIT
  268. | REALLIT
  269. | ID
  270. | FuncInvocation
  271. | LPAR Expr RPAR
  272. | LPAR error RPAR
  273. ;
  274.  
  275. %%
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement