Advertisement
Madeyedexter_ZHCET

parser.y

Nov 14th, 2013
1,645
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #define YYSTYPE char*
  5.  
  6. extern FILE* yyin;
  7. extern int lineno;
  8. int isConditional=0;
  9. int tupleIndex=0;
  10. int temporaryGenerated=0;
  11. int lineCount=100;
  12. //Structure to hold code generated
  13. struct quadruple
  14. {
  15. int line;
  16. char *op;
  17. char *arg1;
  18. char *arg2;
  19. char *result;
  20. } tuple[1000];
  21. //Temporary variable generater
  22. char* newTemp();
  23. char* newTemp()
  24. {
  25. temporaryGenerated++;
  26. char *temp=(char *)malloc(sizeof(2*10));
  27. sprintf(temp,"t%d",temporaryGenerated);
  28. return temp;
  29. }
  30. //code Printer
  31. void printCode();
  32. void printCode()
  33. {
  34. int i=0;
  35. for(;i<=tupleIndex-1;i++)
  36. {
  37. printf("%d:\t%s=%s%s%s\n",tuple[i].line,tuple[i].result,tuple[i].arg1,tuple[i].op,tuple[i].arg2);
  38. }
  39. }
  40. //code generator
  41. void codeGenerator(char*,char*,char*,char*);
  42. void codeGenerator(char*op,char*arg1,char*arg2,char*result)
  43. {
  44. if(!isConditional)
  45. {
  46. tuple[tupleIndex].line=lineCount;
  47. tuple[tupleIndex].op=op;
  48. tuple[tupleIndex].arg1=arg1;
  49. tuple[tupleIndex].arg2=arg2;
  50. tuple[tupleIndex].result=result;
  51. tupleIndex++;
  52. lineCount++;
  53. }
  54.  
  55.  
  56. }
  57.  
  58. %}
  59.  
  60. %token NUMBER
  61. %token ID
  62. %token PLUS
  63. %token MINUS
  64. %token TIMES
  65. %token DIVIDE
  66. %token INT
  67. %token FLOAT
  68. %token COMMA
  69. %token ASSIGN
  70. %token OR
  71. %token AND
  72. %token NOT
  73. %token RELOP
  74. %token TRUE
  75. %token FALSE
  76. %token LP
  77. %token RP
  78. %token LB
  79. %token RB
  80. %token IF
  81. %token ELSE
  82. %token WHILE
  83. %token DELIMITOR
  84. %token E_O_F
  85.  
  86. %left PLUS MINUS
  87. %left TIMES DIVIDE
  88. %left OR
  89. %left AND
  90. %right NOT
  91.  
  92. %start Program
  93.  
  94. %%
  95.  
  96. /*Reduction Rule for the whole Program*/
  97. Program: Declaration Statements E_O_F {
  98. printf("Parsing Successful. The three address code is: \n"); printCode();}
  99. ;
  100.  
  101. /*Reduction rules for Declarations at the beginning of the program*/
  102. Declaration: Declaration Type Identifier DELIMITOR {}
  103. |
  104. ;
  105. Type: INT {}
  106. | FLOAT {}
  107. ;
  108. Identifier: ID COMMA Identifier {}
  109. | ID {}
  110. ;
  111.  
  112. /*Reduction rule for the set of statements*/
  113. Statements: Statements Statement {}
  114. |
  115. ;
  116.  
  117.  
  118. /*Reduction rule for arithmetic assignment statements*/
  119. Statement: ID ASSIGN AExp DELIMITOR {codeGenerator("",$3,"",$1);}
  120. ;
  121. AExp: ID {}
  122. | NUMBER {}
  123. | AExp PLUS AExp {$$=newTemp(); codeGenerator($2,$1,$3,$$);}
  124. | AExp MINUS AExp {$$=newTemp(); codeGenerator($2,$1,$3,$$); }
  125. | AExp TIMES AExp {$$=newTemp(); codeGenerator($2,$1,$3,$$); }
  126. | AExp DIVIDE AExp {$$=newTemp(); codeGenerator($2,$1,$3,$$); }
  127. | LP AExp RP {$$=$2;}
  128. ;
  129.  
  130. /*Reduction rule for boolean and boolean assignment expressions*/
  131. Statement: ID ASSIGN BExp DELIMITOR {codeGenerator("",$3,"",$1);}
  132. ;
  133. BExp: BExp OR BExp {$$=newTemp(); codeGenerator($2,$1,$3,$$);}
  134. | BExp AND BExp {$$=newTemp(); codeGenerator($2,$1,$3,$$);}
  135. | NOT BExp {$$=newTemp(); codeGenerator($1,$1,"",$$);}
  136. | LP BExp RP {$$=$2;}
  137. | AExp RELOP AExp {$$=newTemp(); codeGenerator($2,$1,$3,$$); }
  138. | TRUE
  139. | FALSE
  140. ;
  141.  
  142. /*Reduction rule for if/if-else statements*/
  143. Statement: IF LP BExp RP LB Statements RB {printf("if %s goto ___\n",$3);printf("goto ___\n");}
  144. | IF LP BExp RP LB Statements RB ELSE LB Statements RB {printf("if %s goto __\n",$3);
  145. printf("goto__\n");}
  146. ;
  147.  
  148. /*Reduction rule for while loop statement*/
  149. Statement: WHILE LP BExp RP LB Statements RB {}
  150. ;
  151.  
  152. %%
  153.  
  154. int yyerror(char *s) {
  155. printf("%s\n", s);
  156. }
  157.  
  158. int main()
  159. {
  160.  
  161. FILE* fp=fopen("/home/madeyedexter/Desktop/CompilerTools/CParser/test.txt","r");
  162. yyin=fp;
  163. if (yyparse()==0)
  164. fprintf(stderr, "Successful parsing.\n");
  165. else
  166. {
  167. fprintf(stderr, "Error found.\n");
  168. printf("%d\n",lineno);
  169. }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement