Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %{
- #include <stdio.h>
- #include <stdlib.h>
- #define YYSTYPE char*
- extern FILE* yyin;
- extern int lineno;
- int isConditional=0;
- int tupleIndex=0;
- int temporaryGenerated=0;
- int lineCount=100;
- //Structure to hold code generated
- struct quadruple
- {
- int line;
- char *op;
- char *arg1;
- char *arg2;
- char *result;
- } tuple[1000];
- //Temporary variable generater
- char* newTemp();
- char* newTemp()
- {
- temporaryGenerated++;
- char *temp=(char *)malloc(sizeof(2*10));
- sprintf(temp,"t%d",temporaryGenerated);
- return temp;
- }
- //code Printer
- void printCode();
- void printCode()
- {
- int i=0;
- for(;i<=tupleIndex-1;i++)
- {
- printf("%d:\t%s=%s%s%s\n",tuple[i].line,tuple[i].result,tuple[i].arg1,tuple[i].op,tuple[i].arg2);
- }
- }
- //code generator
- void codeGenerator(char*,char*,char*,char*);
- void codeGenerator(char*op,char*arg1,char*arg2,char*result)
- {
- if(!isConditional)
- {
- tuple[tupleIndex].line=lineCount;
- tuple[tupleIndex].op=op;
- tuple[tupleIndex].arg1=arg1;
- tuple[tupleIndex].arg2=arg2;
- tuple[tupleIndex].result=result;
- tupleIndex++;
- lineCount++;
- }
- }
- %}
- %token NUMBER
- %token ID
- %token PLUS
- %token MINUS
- %token TIMES
- %token DIVIDE
- %token INT
- %token FLOAT
- %token COMMA
- %token ASSIGN
- %token OR
- %token AND
- %token NOT
- %token RELOP
- %token TRUE
- %token FALSE
- %token LP
- %token RP
- %token LB
- %token RB
- %token IF
- %token ELSE
- %token WHILE
- %token DELIMITOR
- %token E_O_F
- %left PLUS MINUS
- %left TIMES DIVIDE
- %left OR
- %left AND
- %right NOT
- %start Program
- %%
- /*Reduction Rule for the whole Program*/
- Program: Declaration Statements E_O_F {
- printf("Parsing Successful. The three address code is: \n"); printCode();}
- ;
- /*Reduction rules for Declarations at the beginning of the program*/
- Declaration: Declaration Type Identifier DELIMITOR {}
- |
- ;
- Type: INT {}
- | FLOAT {}
- ;
- Identifier: ID COMMA Identifier {}
- | ID {}
- ;
- /*Reduction rule for the set of statements*/
- Statements: Statements Statement {}
- |
- ;
- /*Reduction rule for arithmetic assignment statements*/
- Statement: ID ASSIGN AExp DELIMITOR {codeGenerator("",$3,"",$1);}
- ;
- AExp: ID {}
- | NUMBER {}
- | AExp PLUS AExp {$$=newTemp(); codeGenerator($2,$1,$3,$$);}
- | AExp MINUS AExp {$$=newTemp(); codeGenerator($2,$1,$3,$$); }
- | AExp TIMES AExp {$$=newTemp(); codeGenerator($2,$1,$3,$$); }
- | AExp DIVIDE AExp {$$=newTemp(); codeGenerator($2,$1,$3,$$); }
- | LP AExp RP {$$=$2;}
- ;
- /*Reduction rule for boolean and boolean assignment expressions*/
- Statement: ID ASSIGN BExp DELIMITOR {codeGenerator("",$3,"",$1);}
- ;
- BExp: BExp OR BExp {$$=newTemp(); codeGenerator($2,$1,$3,$$);}
- | BExp AND BExp {$$=newTemp(); codeGenerator($2,$1,$3,$$);}
- | NOT BExp {$$=newTemp(); codeGenerator($1,$1,"",$$);}
- | LP BExp RP {$$=$2;}
- | AExp RELOP AExp {$$=newTemp(); codeGenerator($2,$1,$3,$$); }
- | TRUE
- | FALSE
- ;
- /*Reduction rule for if/if-else statements*/
- Statement: IF LP BExp RP LB Statements RB {printf("if %s goto ___\n",$3);printf("goto ___\n");}
- | IF LP BExp RP LB Statements RB ELSE LB Statements RB {printf("if %s goto __\n",$3);
- printf("goto__\n");}
- ;
- /*Reduction rule for while loop statement*/
- Statement: WHILE LP BExp RP LB Statements RB {}
- ;
- %%
- int yyerror(char *s) {
- printf("%s\n", s);
- }
- int main()
- {
- FILE* fp=fopen("/home/madeyedexter/Desktop/CompilerTools/CParser/test.txt","r");
- yyin=fp;
- if (yyparse()==0)
- fprintf(stderr, "Successful parsing.\n");
- else
- {
- fprintf(stderr, "Error found.\n");
- printf("%d\n",lineno);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement