yacc.y 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. %{
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "yacc.tab.h"
  5. #include "symtable.h"
  6. #include "ast_node.h"
  7. #include "parser.h"
  8. #include <unistd.h>
  9. #include "structtable.h"
  10. #include <stdlib.h>
  11. // line number (got from lex.l)
  12. extern int linenum;
  13. // buffer for general use
  14. #define DD_BUFFER_SIZE 1000
  15. char buffer[DD_BUFFER_SIZE];
  16. // error
  17. void yyerror(const char *str)
  18. {
  19. fprintf(stderr,"error: line %d: %s\n", linenum, str);
  20. _exit(-1);
  21. }
  22. // game node, parent of all nodes
  23. struct ast_node *game_node;
  24. // init data, parse, exit
  25. int main(int argc, char *argv[])
  26. {
  27. if (argc <= 1) {
  28. printf("arg 1 should be output file\n");
  29. return -1;
  30. }
  31. // init data
  32. linenum = 1;
  33. symtable_init();
  34. symtable_insert("DD_WIDTH", DD_INTERNAL_WIDTH);
  35. symtable_insert("DD_HEIGHT", DD_INTERNAL_HEIGHT);
  36. game_node = ast_create(AST_GAME, 0);
  37. // init structs
  38. struct struct_entry *temp_entry = malloc(sizeof(struct struct_entry));
  39. temp_entry->name = "dd_world";
  40. struct_insert(temp_entry);
  41. temp_entry = malloc(sizeof(struct struct_entry));
  42. temp_entry->name = "dd_sprite";
  43. struct_insert(temp_entry);
  44. temp_entry = malloc(sizeof(struct struct_entry));
  45. temp_entry->name = "dd_vector2d";
  46. struct_insert(temp_entry);
  47. // parse!
  48. yyparse();
  49. // parse resulting ast tree to a file
  50. parse(argv[1], game_node);
  51. struct_print();
  52. // print debug data and clean everything
  53. symtable_print();
  54. symtable_clean();
  55. ast_print(game_node);
  56. ast_delete(game_node);
  57. // success!
  58. return 0;
  59. }
  60. %}
  61. /* keywords */
  62. %token DD_KEYWORD_INT DD_KEYWORD_FLOAT DD_KEYWORD_STRUCT DD_KEYWORD_OVERRIDE DD_KEYWORD_IF DD_KEYWORD_VOID
  63. /* internal variables */
  64. %token DD_INTERNAL_WIDTH DD_INTERNAL_HEIGHT
  65. /* constants */
  66. %token DD_CONSTANT_SYMBOL DD_CONSTANT_STRING DD_CONSTANT_NUMBER
  67. /* operators */
  68. %token DD_OPERATOR_EQ DD_OPERATOR_LE DD_OPERATOR_GE
  69. /* associativity */
  70. %left '+' '-'
  71. %left '*' '/'
  72. %left '>' '<' DD_OPERATOR_EQ DD_OPERATOR_LE DD_OPERATOR_GE
  73. %%
  74. /* each rule creates a node,
  75. * possibly with children nodes,
  76. * all non-terminals are nodes that can be obtained with ast_pop() (left to right)
  77. */
  78. /* the game itself, contains statements
  79. */
  80. game:
  81. statements {
  82. ast_child_add(game_node, ast_pop());
  83. }
  84. ;
  85. statements:
  86. { ast_push( ast_create(AST_GROUP, 0) ); }
  87. | statements statement {
  88. // add statement to game
  89. //ast_child_add(game_node, ast_pop());
  90. struct ast_node *n;
  91. struct ast_node *c;
  92. c = ast_pop();
  93. n = ast_pop();
  94. ast_child_add(n, c);
  95. ast_push(n);
  96. }
  97. statement:
  98. /* definition statement
  99. */
  100. definition
  101. |
  102. /* define struct
  103. * takes all definitions, and adds them to the struct table
  104. */
  105. DD_KEYWORD_STRUCT DD_CONSTANT_SYMBOL struct_parent '{' definitions '}' ';' {
  106. // get symbol in symbol table
  107. struct entry *e = symtable_entryat($2);
  108. // check if its already defined, if not define it, if yes error
  109. if (e->token == DD_CONSTANT_SYMBOL) {
  110. e->token = DD_KEYWORD_STRUCT;
  111. }
  112. else {
  113. snprintf(buffer, DD_BUFFER_SIZE, "'%s' is already defined", e->lexptr);
  114. yyerror(buffer);
  115. }
  116. // create new struct entry based on symbol table
  117. struct struct_entry se;
  118. se.name = symtable_entryat($2)->lexptr;
  119. se.parent = 0;//struct_lookup(se.name);
  120. // group node has all definitions
  121. struct ast_node *group = ast_pop();
  122. // if parent exists
  123. struct ast_node *parent = ast_pop();
  124. if (parent->node_type == AST_STRUCT_PARENT) {
  125. se.parent = parent->value;
  126. }
  127. else {
  128. se.parent = -1;
  129. }
  130. int struct_index = struct_insert(&se);
  131. // create struct node and add definitions as child
  132. /*
  133. group->node_type = AST_STRUCT;
  134. group->value = struct_index;
  135. */
  136. struct ast_node *n = ast_create(AST_STRUCT, struct_index);
  137. ast_child_add(n, group);
  138. // push to ast tree
  139. ast_push(n);
  140. }
  141. |
  142. /* assign
  143. * change the value of a variable
  144. * error if variable is not defined yet
  145. */
  146. variable '=' expression ';' {
  147. // get symbol in symbol table
  148. struct entry *e = symtable_entryat($1);
  149. // if variable is not defined, error
  150. if (e->token == DD_CONSTANT_SYMBOL && strcmp(e->lexptr, "this") != 0) {
  151. snprintf(buffer, DD_BUFFER_SIZE, "'%s' is not defined", e->lexptr);
  152. yyerror(buffer);
  153. }
  154. // create an 'assign' node
  155. struct ast_node *n = ast_create(AST_ASSIGNMENT, 0);
  156. // with first child the variable to be assigned some value
  157. struct ast_node *c2 = ast_pop();
  158. struct ast_node *c1 = ast_pop();
  159. ast_child_add(n, c1);
  160. // and second child the result (probably an expression)
  161. ast_child_add(n, c2);
  162. // add to game
  163. ast_push(n);
  164. }
  165. |
  166. /* function call */
  167. variable '(' ')' ';' {
  168. struct ast_node *variable = ast_pop();
  169. struct ast_node *n = ast_create(AST_FUNCTION_CALL, 0);
  170. ast_child_add(n, variable);
  171. ast_push(n);
  172. }
  173. |
  174. /* if statement */
  175. DD_KEYWORD_IF '(' expression ')' '{' statements '}' {
  176. struct ast_node *expression;
  177. struct ast_node *statements;
  178. struct ast_node *ifnode = ast_create(AST_IF, 0);
  179. statements = ast_pop();
  180. expression = ast_pop();
  181. ast_child_add(ifnode, expression);
  182. ast_child_add(ifnode, statements);
  183. ast_push(ifnode);
  184. }
  185. ;
  186. definitions:
  187. { ast_push( ast_create(AST_GROUP, 0) ); }
  188. | definitions definition {
  189. struct ast_node *n;
  190. struct ast_node *c;
  191. c = ast_pop();
  192. n = ast_pop();
  193. ast_child_add(n, c);
  194. ast_push(n);
  195. }
  196. ;
  197. definition:
  198. /* definition
  199. * define a new variable
  200. * error if variable is already defined
  201. */
  202. variable_type DD_CONSTANT_SYMBOL ';' {
  203. // get variable type
  204. struct ast_node *vartype = ast_pop();
  205. // get symbol in symbol table
  206. struct entry *e = symtable_entryat($2);
  207. // check if its already defined, if not define it, if yes error
  208. if (e->token == DD_CONSTANT_SYMBOL) {
  209. // variable type is void
  210. if (vartype->node_type == AST_VARTYPE_VOID) {
  211. e->token = DD_KEYWORD_VOID;
  212. }
  213. else
  214. // variable type is an int
  215. if (vartype->node_type == AST_VARTYPE_INT) {
  216. e->token = DD_KEYWORD_INT;
  217. }
  218. else
  219. // variable type is a float
  220. if (vartype->node_type == AST_VARTYPE_FLOAT) {
  221. e->token = DD_KEYWORD_FLOAT;
  222. }
  223. else
  224. // variable type is a struct
  225. if (vartype->node_type == AST_VARTYPE_STRUCT) {
  226. e->token = DD_KEYWORD_STRUCT;
  227. e->value = vartype->value;
  228. }
  229. }
  230. else {
  231. snprintf(buffer, DD_BUFFER_SIZE, "'%s' is already defined", e->lexptr);
  232. yyerror(buffer);
  233. }
  234. // create a definition node
  235. struct ast_node *n = ast_create(AST_DEFINITION, 0);
  236. // that has the to-be defined variable as child
  237. struct ast_node *c = ast_create(AST_IDENTIFIER, $2);
  238. ast_child_add(n, c);
  239. // add it to the game
  240. ast_push(n);
  241. }
  242. |
  243. /* function definition */
  244. variable_type DD_CONSTANT_SYMBOL '(' function_arguments ')' '{' statements '}' ';' {
  245. // get variable type
  246. struct ast_node *statements = ast_pop();
  247. struct ast_node *arguments = ast_pop();
  248. struct ast_node *vartype = ast_pop();
  249. struct ast_node *n = ast_create(AST_FUNCTION_DEFINITION, $2);
  250. ast_child_add(n, arguments);
  251. ast_child_add(n, statements);
  252. ast_push(n);
  253. }
  254. |
  255. DD_KEYWORD_OVERRIDE variable_type DD_CONSTANT_SYMBOL '(' function_arguments ')' '{' statements '}' ';' {
  256. // get variable type
  257. struct ast_node *statements = ast_pop();
  258. struct ast_node *arguments = ast_pop();
  259. struct ast_node *vartype = ast_pop();
  260. struct entry *function = symtable_entryat($3);
  261. function->value = 1;
  262. struct ast_node *n = ast_create(AST_FUNCTION_DEFINITION, $3);
  263. ast_child_add(n, arguments);
  264. ast_child_add(n, statements);
  265. ast_push(n);
  266. }
  267. ;
  268. expression:
  269. expression '>' expression {
  270. int operator = AST_MORE_THAN;
  271. struct ast_node *n = ast_create(operator, 0);
  272. struct ast_node *exp = ast_pop();
  273. struct ast_node *term = ast_pop();
  274. ast_child_add(n, term);
  275. ast_child_add(n, exp);
  276. ast_push(n);
  277. }
  278. |
  279. expression '<' expression {
  280. int operator = AST_LESS_THAN;
  281. struct ast_node *n = ast_create(operator, 0);
  282. struct ast_node *exp = ast_pop();
  283. struct ast_node *term = ast_pop();
  284. ast_child_add(n, term);
  285. ast_child_add(n, exp);
  286. ast_push(n);
  287. }
  288. |
  289. expression DD_OPERATOR_EQ expression {
  290. int operator = AST_EQUAL;
  291. struct ast_node *n = ast_create(operator, 0);
  292. struct ast_node *exp = ast_pop();
  293. struct ast_node *term = ast_pop();
  294. ast_child_add(n, term);
  295. ast_child_add(n, exp);
  296. ast_push(n);
  297. }
  298. |
  299. expression DD_OPERATOR_LE expression {
  300. int operator = AST_LESS_EQUAL;
  301. struct ast_node *n = ast_create(operator, 0);
  302. struct ast_node *exp = ast_pop();
  303. struct ast_node *term = ast_pop();
  304. ast_child_add(n, term);
  305. ast_child_add(n, exp);
  306. ast_push(n);
  307. }
  308. |
  309. expression DD_OPERATOR_GE expression {
  310. int operator = AST_MORE_EQUAL;
  311. struct ast_node *n = ast_create(operator, 0);
  312. struct ast_node *exp = ast_pop();
  313. struct ast_node *term = ast_pop();
  314. ast_child_add(n, term);
  315. ast_child_add(n, exp);
  316. ast_push(n);
  317. }
  318. |
  319. expression '+' expression {
  320. struct ast_node *n = ast_create( AST_ADDITION, 0);
  321. struct ast_node *exp = ast_pop();
  322. struct ast_node *term = ast_pop();
  323. ast_child_add(n, term);
  324. ast_child_add(n, exp);
  325. ast_push(n);
  326. }
  327. |
  328. expression '-' expression {
  329. struct ast_node *n = ast_create( AST_SUBSTRACTION, 0);
  330. struct ast_node *exp = ast_pop();
  331. struct ast_node *term = ast_pop();
  332. ast_child_add(n, term);
  333. ast_child_add(n, exp);
  334. ast_push(n);
  335. }
  336. |
  337. expression '*' expression {
  338. struct ast_node *n = ast_create(AST_MULTIPLICATION, 0);
  339. struct ast_node *exp = ast_pop();
  340. struct ast_node *term = ast_pop();
  341. ast_child_add(n, term);
  342. ast_child_add(n, exp);
  343. ast_push(n);
  344. }
  345. |
  346. expression '/' expression {
  347. struct ast_node *n = ast_create(AST_DIVISION, 0);
  348. struct ast_node *exp = ast_pop();
  349. struct ast_node *term = ast_pop();
  350. ast_child_add(n, term);
  351. ast_child_add(n, exp);
  352. ast_push(n);
  353. }
  354. |
  355. '(' expression ')' {
  356. struct ast_node *n = ast_create(AST_GROUP_EXPRESSIONS, 0);
  357. struct ast_node *child = ast_pop();
  358. ast_child_add(n, child);
  359. ast_push(n);
  360. }
  361. |
  362. factor
  363. ;
  364. factor:
  365. DD_CONSTANT_NUMBER { ast_push( ast_create(AST_NUMBER, $1) ); }
  366. |
  367. variable
  368. |
  369. DD_CONSTANT_STRING { ast_push( ast_create(AST_STRING, $1) ); }
  370. ;
  371. variable_type:
  372. DD_KEYWORD_VOID { $$ = DD_KEYWORD_VOID; ast_push( ast_create(AST_VARTYPE_VOID, 0) ); }
  373. |
  374. DD_KEYWORD_INT { $$ = DD_KEYWORD_INT; ast_push( ast_create(AST_VARTYPE_INT, 0) ); }
  375. |
  376. DD_KEYWORD_FLOAT { $$ = DD_KEYWORD_FLOAT; ast_push( ast_create(AST_VARTYPE_FLOAT, 0) ); }
  377. |
  378. DD_KEYWORD_STRUCT DD_CONSTANT_SYMBOL { $$ = DD_KEYWORD_STRUCT; ast_push( ast_create(AST_VARTYPE_STRUCT, struct_lookup(symtable_entryat($2)->lexptr)) ); }
  379. ;
  380. struct_parent:
  381. { ast_push( ast_create(AST_EMPTY, 0) ); }
  382. |
  383. ':' DD_CONSTANT_SYMBOL { ast_push( ast_create(AST_STRUCT_PARENT, struct_lookup(symtable_entryat($2)->lexptr)) ); }
  384. ;
  385. variable:
  386. DD_CONSTANT_SYMBOL {
  387. ast_push( ast_create(AST_IDENTIFIER, $1 ));
  388. }
  389. variable_additional
  390. ;
  391. variable_additional:
  392. | variable_additional '.' DD_CONSTANT_SYMBOL {
  393. struct ast_node *n = ast_pop();
  394. ast_child_add(n, ast_create(AST_IDENTIFIER, $3));
  395. ast_push(n);
  396. $$ = $3;
  397. }
  398. ;
  399. function_arguments:
  400. { ast_push( ast_create(AST_EMPTY, 0) ); }
  401. | variable_type DD_CONSTANT_SYMBOL function_additional_argument {
  402. struct entry *e = symtable_entryat($2);
  403. e->token = $1;
  404. struct ast_node *n = ast_pop();
  405. ast_pop(); // waste variable_type for now
  406. ast_child_add(n, ast_create(AST_ARGUMENT, $2));
  407. ast_push(n);
  408. }
  409. ;
  410. function_additional_argument:
  411. { ast_push( ast_create(AST_ARGUMENTS, 0) ); }
  412. | ',' variable_type DD_CONSTANT_SYMBOL function_additional_argument {
  413. struct ast_node *n = ast_pop();
  414. ast_pop(); // waste variable_type for now
  415. ast_child_add(n, ast_create(AST_ARGUMENT, $3));
  416. ast_push(n);
  417. }
  418. ;