Advertisement
Guest User

Untitled

a guest
Apr 16th, 2013
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. %{
  2. #include <stdio.h>
  3. #include "attr.h"
  4. int yylex();
  5. void yyerror(char * s);
  6. struct Stack *st;
  7. struct Stack *ids;
  8. #include "symtab.h"
  9. %}
  10.  
  11. %union {tokentype token;
  12. char *type;
  13. struct Stack *idstack;
  14. int iconst;
  15. node *attr;
  16. }
  17.  
  18. %token PROG PERIOD VAR ARRAY RANGE OF
  19. %token INT BOOLEAN WRITELN THEN ELSE IF
  20. %token BEG END ASG
  21. %token EQ NEQ LT LEQ OR EXOR AND NOT TRUE FALSE
  22. %token WHILE DO FOR
  23. %token <token> ID ICONST
  24. %type <attr> stype type
  25. %type <idstack> IDlist
  26. %type <iconst> integer_constant
  27.  
  28. %start program
  29.  
  30. %nonassoc EQ NEQ LT LEQ
  31. %left '+' '-' OR EXOR
  32. %left '*' AND
  33. %right NOT
  34.  
  35. %nonassoc THEN
  36. %nonassoc ELSE
  37.  
  38. %%
  39. program : PROG ID ';' block PERIOD { printf("\n\n Done with compiling program %s\n", $2.str); }
  40. ;
  41.  
  42. block : variables cmpdstmt
  43. ;
  44.  
  45. variables : VAR vardcls
  46. | /* epsilon */
  47. ;
  48.  
  49. vardcls : vardcls vardcl ';'
  50. | vardcl ';'
  51. | error ';' { yyerror("***Error: illegal variable declaration\n"); }
  52. ;
  53.  
  54. vardcl : IDlist ':' type { STInsertList(st, ids, $3->type, level); }
  55. ;
  56.  
  57. IDlist : IDlist ',' ID { SPushT(ids, $3); }
  58. | ID { SPushT(ids, $1); }
  59. ;
  60.  
  61. type : ARRAY '[' integer_constant RANGE integer_constant ']' OF stype { $$ = $8 }
  62. | stype { $$ = $1 }
  63. ;
  64.  
  65. stype : INT { $$ = "INT" }
  66. | BOOLEAN { $$ = "BOOLEAN" }
  67. ;
  68.  
  69. stmtlist : stmtlist ';' stmt
  70. | stmt
  71. | error ';' { yyerror("***Error: illegal statement\n");}
  72. ;
  73.  
  74. stmt : ifstmt
  75. | astmt
  76. | wstmt
  77. | fstmt
  78. | cmpdstmt
  79. | writestmt
  80. ;
  81.  
  82. wstmt : WHILE condexp DO stmt
  83. ;
  84.  
  85. fstmt : FOR ID ASG constant ',' constant DO astmt
  86. ;
  87.  
  88. ifstmt : ifhead THEN stmt ELSE stmt
  89. | ifhead THEN stmt
  90. ;
  91.  
  92. ifhead : IF condexp
  93. ;
  94.  
  95. cmpdstmt : BEG stmtlist END
  96. ;
  97.  
  98. writestmt : WRITELN '(' exp ')'
  99. ;
  100.  
  101. astmt : lvalue ASG exp
  102.  
  103. exp : rvalue { }
  104. | exp '+' exp { }
  105. | exp '-' exp { }
  106. | exp '*' exp { }
  107. | exp AND exp { }
  108. | exp OR exp { }
  109. | exp EXOR exp { }
  110. | NOT exp { }
  111. | '(' exp ')' { }
  112. | constant { }
  113. | error { yyerror("***Error: illegal expression\n"); }
  114. ;
  115.  
  116. condexp : exp NEQ exp { }
  117. | exp EQ exp { }
  118. | exp LT exp { }
  119. | exp LEQ exp { }
  120. | ID { }
  121. | boolean_constant { }
  122. | error { yyerror("***Error: illegal conditional expression\n"); }
  123. ;
  124.  
  125. lvalue : ID
  126. | ID '[' exp ']'
  127. ;
  128.  
  129. rvalue : ID
  130. | ID '[' exp ']'
  131. ;
  132.  
  133. constant : integer_constant
  134. | boolean_constant
  135. ;
  136.  
  137. integer_constant: ICONST { $$ = atoi($1) }
  138. ;
  139.  
  140. boolean_constant: TRUE
  141. | FALSE
  142. ;
  143.  
  144. %%
  145. struct StackTokenNode{
  146. tokentype data;
  147. void *next;
  148. int rc;
  149. };
  150. typedef struct StackTokenNode stNode;
  151. int SPushT(sPtr s, tokentype data){
  152. stNode *new = malloc(sizeof(sNode));
  153. new->data = data;
  154. new->next = NULL;
  155. new->rc = 0;
  156. if (s->size == 0){
  157. s->top = new;
  158. s->bottom = new;
  159. s->size++;
  160. return 0;
  161. }
  162. new->next = s->top;
  163. s->top = new;
  164. s->size++;
  165. return 0;
  166. }
  167. void yyerror(char* s) {
  168. fprintf(stderr,"%s\n",s);
  169. }
  170. int main() {
  171. printf("\n CS415 Front-End Compiler\n");
  172. printf(" Project 2, Spring 2013\n\n");
  173. printf("1\t");
  174. st = STCreate();
  175. ids = SCreate(CompareStrings);
  176. level = 0;
  177. yyparse();
  178. vNode *vn = SPop(st);
  179. while (vn != NULL){
  180. printf("%s %s %d", vn->id, vn->type, vn->level);
  181. vn = SPop(st);
  182. }
  183. return 1;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement