Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %{
- #include<iostream>
- #include<cstdlib>
- #include<cstring>
- #include<cmath>
- #include "1505097.h"
- extern FILE * yyin;
- extern int yylineno;
- using namespace std;
- string variable_type;
- int yyparse(void);
- int yylex(void);
- FILE *logfile;
- FILE * errorfile;
- FILE * inputfile;
- vector<string> arglist;
- vector<string> arglistID;
- vector<string> asm_arg;
- vector<string> temp_asm_param;
- string arr_var;
- string paramlist[100][2];
- int paramlistlen = 0;
- string ret_type = "no";
- SymbolTable *table;
- int stxerrorno = 0;
- int semerror = 0;
- int labelCount=0;
- int tempCount=0;
- string dataseg = ".DATA\nglobaltemp\tDW\t?\n";
- string PUSHALLREG = "\tPUSH AX\n\tPUSH BX\n\tPUSH CX\n\tPUSH DX\n";
- string POPALLREG = "\tPOP DX\n\tPOP CX\n\tPOP BX\n\tPOP AX\n";
- string return_temp;
- string newLabel()
- {
- string temp;
- temp = "L"+to_string(labelCount);
- labelCount++;
- return temp;
- }
- string newTemp()
- {
- string temp;
- temp = "t"+to_string(tempCount);
- tempCount++;
- return temp;
- }
- string lowerToUPPER(string str)
- {
- int l = str.size();
- for(int i = 0; i < l ; i++)
- {
- if(str[i]>= 'a' && str[i]<= 'z')
- {
- str[i] = str[i] - 'a' + 'A';
- }
- }
- return str;
- }
- void yyerror(char *s)
- {
- fprintf(logfile,"Syntax Error at line %d : %s",yylineno,s);
- stxerrorno++;
- }
- %}
- %union{
- node* symVal;
- }
- %token STRING COMMENT IF FOR WHILE DO BREAK CONTINUE INT FLOAT CHAR DOUBLE VOID RETURN DEFAULT INCOP DECOP ASSIGNOP LPAREN RPAREN LCURL RCURL LTHIRD RTHIRD SEMICOLON COMMA NOT PRINTLN SWITCH CASE
- %token <symVal>ID
- %token <symVal>CONST_INT
- %token <symVal>CONST_FLOAT
- %token <symVal>CONST_CHAR
- %token <symVal>ADDOP
- %token <symVal>MULOP
- %token <symVal>LOGICOP
- %token <symVal>RELOP
- %token <symVal>BITOP
- %type <symVal>start program unit func_declaration func_definition parameter_list expression_statement argument_list arguments compound_statement var_declaration declaration_list statement statements type_specifier expression logic_expression rel_expression simple_expression term unary_expression factor variable
- %nonassoc LOWER_THEN_ELSE
- %nonassoc ELSE
- %%
- start : program
- {
- fprintf(logfile,"At line no: %d start : program\n\n",yylineno);
- fprintf(logfile,"%s\n\n",$1->name.c_str());
- $$ = new node();
- $$->name = $1->name;
- fprintf(logfile,"-----------------------Symbol Table--------------------\n\n");
- table->PrintAllScopeTable();
- $$->code = ".MODEL SMALL\n";
- $$->code+=".STACK 100H\n";
- $$->code += dataseg+".CODE\n"+$1->code+"\n\n";
- fprintf(logfile,"\n%s\n",$$->code.c_str());
- char c;
- FILE *asmOut = fopen("1505097.asm","w");
- if(semerror == 0 && stxerrorno == 0)
- {
- fprintf(asmOut,"%s\n",$$->code.c_str());
- FILE *fp = fopen("println.txt","r");
- while ((c = fgetc(fp)) != EOF){
- fputc(c,logfile);
- fputc(c,asmOut);
- }
- fprintf(logfile,"END MAIN\n\n");
- fprintf(asmOut,"END MAIN\n\n");
- fclose(fp);
- fclose(asmOut);
- }
- }
- ;
- program : program unit
- {
- fprintf(logfile,"At line no: %d program : program unit\n\n",yylineno);
- fprintf(logfile,"%s%s\n\n",$1->name.c_str(),$2->name.c_str());
- $$ = new node();
- $$->name = $1->name+$2->name;
- $$->code = $1->code+$2->code;
- }
- | unit
- {
- fprintf(logfile,"At line no: %d program : unit\n\n",yylineno);
- fprintf(logfile,"%s\n\n",$1->name.c_str());
- $$ = new node();
- $$->name = $1->name;
- $$->code = $1->code;
- }
- ;
- unit : var_declaration
- {
- fprintf(logfile,"At line no: %d unit : var_declaration\n\n",yylineno);
- fprintf(logfile,"%s\n\n",$1->name.c_str());
- $$ = new node();
- $$->name = $1->name;
- $$->code = $1->code;
- }
- | func_declaration
- {
- fprintf(logfile,"At line no: %d unit : func_declaration\n\n",yylineno);
- fprintf(logfile,"%s\n\n",$1->name.c_str());
- $$ = new node();
- $$->name = $1->name;
- $$->code = $1->code;
- }
- | func_definition
- {
- fprintf(logfile,"At line no: %d unit : func_definition\n\n",yylineno);
- fprintf(logfile,"%s\n\n",$1->name.c_str());
- $$ = new node();
- $$->name = $1->name;
- $$->code = $1->code;
- }
- ;
- func_declaration : type_specifier ID LPAREN parameter_list RPAREN SEMICOLON
- {
- fprintf(logfile,"At line no: %d func_declaration : type_specifier ID LPAREN parameter_list RPAREN SEMICOLON\n\n",yylineno);
- fprintf(logfile,"%s %s(%s);\n\n",$1->name.c_str(),$2->name.c_str(),$4->name.c_str());
- $$ = new node();
- $$->name=$1->name+" "+$2->name+"("+$4->name+");\n";
- //return_type = $1->name;
- //cout<<return_type<<endl;
- $2->return_type = $1->name;
- //fprintf(logfile,"----------------\n%s\n\n",$2->return_type.c_str());
- int len = paramlistlen;
- for(int i = 0; i < len; i++)
- {
- $2->param_type.push_back(paramlist[i][0]);
- $2->param_list.push_back(paramlist[i][1]);
- }
- if(!table->Insert($2))
- {
- fprintf(errorfile,"Error at Line %d: Multiple Declaration of %s function\n\n",yylineno,$2->name.c_str());
- semerror++;
- }
- paramlistlen = 0;
- $$->code = $2->code;
- }
- | type_specifier ID LPAREN RPAREN SEMICOLON
- {
- fprintf(logfile,"At line no: %d func_declaration : type_specifier ID LPAREN RPAREN SEMICOLON\n\n",yylineno);
- fprintf(logfile,"%s %s();\n\n",$1->name.c_str(),$2->name.c_str());
- $$ = new node();
- $$->name = $1->name+" "+$2->name+"();\n";
- //return_type = $1->name;
- //cout<<return_type<<endl;
- $2->return_type = $1->name;
- if(!table->Insert($2))
- {
- fprintf(errorfile,"Error at Line %d: Multiple Declaration of %s function\n\n",yylineno,$2->name.c_str());
- semerror++;
- }
- $$->code = $2->code;
- }
- ;
- func_definition : type_specifier ID LPAREN parameter_list RPAREN compound_statement
- {
- fprintf(logfile,"At line no: %d func_definition : type_specifier ID LPAREN parameter_list RPAREN compound_statement\n\n",yylineno);
- fprintf(logfile,"%s %s(%s)\n%s\n\n",$1->name.c_str(),$2->name.c_str(),$4->name.c_str(),$6->name.c_str());
- $$ = new node();
- $$->name = $1->name+" "+$2->name+"("+$4->name+")\n"+$6->name;
- int scopeID;
- if(table->lookUp($2->name)!=NULL)
- {
- node* temp = table->lookUp($2->name);
- int len = paramlistlen;
- //cout<<"pararlist len : "<<paramlistlen<<endl;
- //cout<<"temp->param_list.size() : "<<temp->param_list.size()<<endl;
- int len2 = temp->param_list.size();
- if(temp->Func_defined == true)
- {
- fprintf(errorfile,"Error at Line %d: Multiple Definition of %s function\n\n",yylineno,$2->name.c_str());
- semerror++;
- }
- else if(len!=len2)
- {
- fprintf(errorfile,"Error at Line %d: Argument no mismatch of %s function\n\n",yylineno,$2->name.c_str());
- semerror++;
- }else
- {
- for(int i = 0 ; i < len ; i++)
- {
- if(temp->param_type[i]!=paramlist[i][0])
- {
- fprintf(errorfile,"Error at Line %d: Parameter type mismatch of %s function\n\n",yylineno,$2->name.c_str());
- semerror++;
- break;
- }
- }
- temp->Func_defined = true;
- }
- }
- else
- {
- $2->return_type = $1->name;
- $2->Func_defined = true;
- int len = paramlistlen;
- for(int i = 0; i < len; i++)
- {
- $2->param_type.push_back(paramlist[i][0]);
- $2->param_list.push_back(paramlist[i][1]);
- $2->asm_param.push_back(temp_asm_param[i]);
- }
- table->Insert($2);
- }
- if(ret_type == "yes" && $1->name == "void")
- {
- fprintf(errorfile,"Error at line %d : void function can't return \n\n",yylineno);
- semerror++;
- }
- $$->code = $2->code;
- if($2->name != "main")
- {
- $$->code += lowerToUPPER($2->name)+" PROC\n";
- $$->code += PUSHALLREG+"\n";
- $$->code += $6->code;
- $$->code += POPALLREG+"\n";
- if(ret_type == "yes")
- {
- $$->code+= "\tPOP globaltemp\n";
- $$->code+= $6->code;
- $$->code+="\tPUSH "+$6->address+"\n";
- $$->code+="\tPUSH globaltemp\n";
- }
- $$->code+="\tRET\n\n";
- $$->code+=lowerToUPPER($2->name)+" ENDP\n\n";
- }
- else
- {
- $$->code += "\nMAIN PROC\n";
- $$->code += "\tMOV AX,@DATA\n";
- $$->code += "\tMOV DS,AX\n\n";
- $$->code+= $6->code;
- $$->code+= "\tMOV AX,4CH\n";
- $$->code+= "\tINT 21H\n\n";
- $$->code+= lowerToUPPER($2->name)+" ENDP\n\n";
- }
- ret_type = "no";
- paramlistlen = 0;
- temp_asm_param.clear();
- }
- | type_specifier ID LPAREN RPAREN compound_statement
- {
- fprintf(logfile,"At line no: %d func_definition : type_specifier ID LPAREN RPAREN compound_statement\n\n",yylineno);
- fprintf(logfile,"%s %s()\n%s\n\n",$1->name.c_str(),$2->name.c_str(),$5->name.c_str());
- $$ = new node();
- $$->name = $1->name+" "+$2->name+"()\n"+$5->name;
- //cout<<return_type<<endl;
- node* temp;
- temp = table->lookUp($2->name);
- if(temp==NULL)
- {
- $2->return_type = $1->name;
- $2->Func_defined = true;
- table->Insert($2);
- }
- else
- {
- if(temp->Func_defined == true)
- {
- fprintf(errorfile,"Error at Line %d: Multiple Definition of %s function\n\n",yylineno,$2->name.c_str());
- semerror++;
- }
- else
- {
- temp->Func_defined = true;
- }
- }
- if(ret_type == "yes" && $1->name == "void")
- {
- fprintf(errorfile,"Error at line %d : void function can't return \n\n",yylineno);
- semerror++;
- }
- $$->code = $2->code;
- if($2->name == "main")
- {
- $$->code += "\nMAIN PROC\n";
- $$->code += "\tMOV AX,@DATA\n";
- $$->code += "\tMOV DS,AX\n\n";
- $$->code+= $5->code;
- $$->code+= "\tMOV AX,4CH\n";
- $$->code+= "\tINT 21H\n\n";
- $$->code+= lowerToUPPER($2->name)+" ENDP\n\n";
- }
- else
- {
- $$->code += lowerToUPPER($2->name)+" PROC\n";
- $$->code += PUSHALLREG+"\n";
- $$->code += $5->code;
- $$->code += POPALLREG+"\n";
- if(ret_type == "yes")
- {
- $$->code+= "\tPOP globaltemp\n";
- $$->code+= $5->code;
- $$->code+="\tPUSH "+$5->address+"\n";
- $$->code+="\tPUSH globaltemp\n";
- }
- $$->code+="\tRET\n\n";
- $$->code+=lowerToUPPER($2->name)+" ENDP\n\n";
- }
- ret_type = "no";
- }
- ;
- parameter_list : parameter_list COMMA type_specifier ID
- {
- fprintf(logfile,"At line no: %d parameter_list : parameter_list COMMA type_specifier ID\n\n",yylineno);
- fprintf(logfile,"%s,%s %s\n\n",$1->name.c_str(),$3->name.c_str(),$4->name.c_str());
- $$ = new node();
- $$->name = $1->name+","+$3->name+" "+$4->name;
- $4->var_type = $3->name;
- paramlist[paramlistlen][0] = $3->name;
- paramlist[paramlistlen][1] = $4->name;
- //dataseg+="\t"+paramlist[paramlistlen][1]+to_string(table->getCurrentScopeID()+1)+"\tDW\t?\n";
- paramlistlen++;
- }
- | parameter_list COMMA type_specifier
- {
- fprintf(logfile,"At line no: %d parameter_list : parameter_list COMMA type_specifier\n\n",yylineno);
- fprintf(logfile,"%s,%s\n\n",$1->name.c_str(),$3->name.c_str());
- $$ = new node();
- $$->name = $1->name+","+$3->name;
- }
- | type_specifier ID
- {
- fprintf(logfile,"At line no: %d parameter_list : type_specifier ID\n\n",yylineno);
- fprintf(logfile,"%s %s\n\n",$1->name.c_str(),$2->name.c_str());
- $$ = new node();
- $$->name = $1->name+" "+$2->name;
- $2->var_type = $1->name;
- paramlist[paramlistlen][0] = $1->name;
- paramlist[paramlistlen][1] = $2->name;
- //dataseg+="\t"+paramlist[paramlistlen][1]+to_string(table->getCurrentScopeID()+1)+"\tDW\t?\n";
- paramlistlen++;
- }
- | type_specifier
- {
- fprintf(logfile,"At line no: %d parameter_list : type_specifier\n\n",yylineno);
- fprintf(logfile,"%s\n\n",$1->name.c_str());
- $$ = new node();
- $$->name = $1->name;
- }
- ;
- compound_statement : LCURL {
- table->EnterScope();
- for(int i = 0; i < paramlistlen ; i++)
- {
- node* temp = new node();
- temp->name = paramlist[i][1];
- temp->var_type = paramlist[i][0];
- temp->type = "ID";
- table->Insert(temp);
- }
- for(int i = 0; i < paramlistlen;i++)
- {
- temp_asm_param.push_back(paramlist[i][1]+to_string(table->getCurrentScopeID()));
- }
- for(int i = 0; i < paramlistlen;i++)
- {
- dataseg+="\t"+paramlist[i][1]+to_string(table->getCurrentScopeID())+"\tDW\t?\n";
- }
- } statements {
- fprintf(logfile,"\n\n%d - >\n %s\n\n",yylineno,dataseg.c_str());
- table->PrintAllScopeTable();
- } RCURL {
- table->ExitScope();}
- {
- fprintf(logfile,"At line no: %d compound_statement : LCURL statements RCURL\n\n",yylineno);
- fprintf(logfile,"{\n%s}\n\n",$3->name.c_str());
- $$ = new node();
- $$->name = "{\n"+$3->name+"}\n";
- $$->address = $3->address;
- $$->code = $3->code;
- }
- | LCURL RCURL
- {
- fprintf(logfile,"At line no: %d compound_statement : LCURL RCURL\n\n",yylineno);
- fprintf(logfile,"{}\n\n");
- $$ = new node();
- $$->name = "{}\n";
- }
- ;
- var_declaration : type_specifier declaration_list SEMICOLON
- {
- fprintf(logfile,"At line no: %d var_declaration : type_specifier declaration_list SEMICOLON\n\n",yylineno);
- fprintf(logfile,"%s %s;\n",$1->name.c_str(),$2->name.c_str());
- $$ = new node();
- $$->name = $1->name+" "+$2->name+";";
- }
- ;
- type_specifier : INT
- {
- fprintf(logfile,"At line no: %d type_specifier : INT\n\n",yylineno);
- fprintf(logfile,"int\n\n");
- $$ = new node();
- $$->name = "int"; //cout<< "hello1"<<endl;
- variable_type = "int";
- }
- | FLOAT
- {
- fprintf(logfile,"At line no: %d type_specifier : FLOAT\n\n",yylineno);
- fprintf(logfile,"float\n\n");
- $$ = new node();
- $$->name = "float"; //cout<< "hello2"<<endl;
- variable_type = "float";
- }
- | VOID
- {
- fprintf(logfile,"At line no: %d type_specifier : VOID\n\n",yylineno);
- fprintf(logfile,"void\n\n");
- $$ = new node();
- $$->name = "void"; //cout<< "hello3"<<endl;
- variable_type = "void";
- }
- ;
- declaration_list : declaration_list COMMA ID
- {
- fprintf(logfile,"At line no: %d declaration_list : declaration_list COMMA ID\n\n",yylineno);
- fprintf(logfile,"%s,%s\n\n",$1->name.c_str(),$3->name.c_str());
- $$ = new node();
- $$->name = $1->name +","+ $3->name;
- $$->var_type = variable_type;
- if(variable_type == "void")
- {
- fprintf(errorfile,"Error at Line %d : Variable type can't be void\n\n",yylineno);
- semerror++;
- }
- else
- {
- $3->var_type = variable_type;
- if(!table->Insert($3))
- {
- fprintf(errorfile,"Error at Line %d: Multiple Declaration of %s\n\n",yylineno,$3->name.c_str());
- semerror++;
- }
- }
- //table->PrintCurrentScopeTable();
- dataseg+="\t"+$3->name+to_string(table->GetTableID($3->name))+"\tDW\t?\n";
- $$->arr_type = false;
- }
- | declaration_list COMMA ID {arr_var = variable_type;} LTHIRD CONST_INT RTHIRD
- {
- fprintf(logfile,"At line no: %d declaration_list : declaration_list COMMA ID LTHIRD CONST_INT RTHIRD\n\n",yylineno);
- fprintf(logfile,"%s,%s[%s]\n\n",$1->name.c_str(),$3->name.c_str(),$6->name.c_str());
- $$ = new node();
- $$->name = $1->name +","+ $3->name+"["+$6->name+"]";
- $$->var_type = arr_var;
- //cout<<yylineno<<"-->"<<$6->name<<endl;
- if(arr_var == "void")
- {
- fprintf(errorfile,"Error at Line %d : Variable type can't be void\n\n",yylineno);
- semerror++;
- }
- else
- {
- $3->var_type = arr_var;
- $3->arr_size = atoi($6->name.c_str());
- if(!table->Insert($3))
- {
- fprintf(errorfile,"Error at Line %d: Multiple Declaration of %s\n\n",yylineno,$3->name.c_str());
- semerror++;
- }
- }
- //table->PrintCurrentScopeTable();
- dataseg+="\t"+$3->name+to_string(table->GetTableID($3->name))+"\tDW\t"+$6->name+"\tDUP(?)\n";
- $$->arr_type = true;
- }
- | ID
- {
- fprintf(logfile,"At line no: %d declaration_list : ID\n\n",yylineno);
- fprintf(logfile,"%s\n\n",$1->name.c_str());
- $$ = new node();
- $$->name= $1->name;
- $$->var_type = variable_type;
- $1->type = "ID";
- if(variable_type == "void")
- {
- fprintf(errorfile,"Error at Line %d : Variable type can't be void\n\n",yylineno);
- semerror++;
- }
- else
- {
- $1->var_type = variable_type;
- if(!table->Insert($1))
- {
- fprintf(errorfile,"Error at Line %d: Multiple Declaration of %s\n\n",yylineno,$1->name.c_str());
- semerror++;
- }
- }
- dataseg+="\t"+$1->name+to_string(table->GetTableID($1->name))+"\tDW\t?\n";
- $$->arr_type = false;
- //table->PrintCurrentScopeTable();
- }
- | ID {arr_var = variable_type;} LTHIRD CONST_INT RTHIRD
- {
- fprintf(logfile,"At line no: %d declaration_list : ID LTHIRD CONST_INT RTHIRD\n\n",yylineno);
- fprintf(logfile,"%s[%s]\n\n",$1->name.c_str(),$4->name.c_str());
- $$ = new node();
- $$->name = $1->name+"["+$4->name+"]";
- $$->var_type = arr_var;
- //cout<<yylineno<<"-->"<<$4->name<<endl;
- if(arr_var == "void")
- {
- fprintf(errorfile,"Error at Line %d : Variable type can't be void\n\n",yylineno);
- semerror++;
- }
- else
- {
- //table->PrintAllScopeTable();
- $1->var_type = arr_var;
- $1->arr_size = atoi($4->name.c_str());
- if(!table->Insert($1))
- {
- fprintf(errorfile,"Error404 at Line %d: Multiple Declaration of %s\n\n",yylineno,$1->name.c_str());
- semerror++;
- }
- //table->PrintCurrentScopeTable();
- }
- //table->PrintAllScopeTable();statements
- //$$->code = $1->code;
- //$$->address = $1->address;
- dataseg+="\t"+$1->name+to_string(table->GetTableID($1->name))+"\tDW\t"+$4->name+"\tDUP(?)\n";
- $$->arr_type = true;
- }
- ;
- statements : statement
- {
- fprintf(logfile,"At line no: %d statements : statement\n\n",yylineno);
- fprintf(logfile,"%s\n\n",$1->name.c_str());
- $$ = new node();
- $$->name= $1->name;
- $$->arr_type = $1->arr_type;
- $$->address = $1->address;
- $$->code = $1->code;
- fprintf(logfile,"\n%s\n",$$->code.c_str());
- }
- | statements statement
- {
- fprintf(logfile,"At line no: %d statements : statements statement\n\n",yylineno);
- fprintf(logfile,"%s\n%s\n\n",$1->name.c_str(),$2->name.c_str());
- $$ = new node();
- $$->name= $1->name+"\n"+$2->name;
- $$->arr_type = $1->arr_type;
- $$->address = $2->address;
- $$->code = $1->code+$2->code;
- fprintf(logfile,"\n%s\n",$$->code.c_str());
- }
- ;
- statement : var_declaration
- {
- fprintf(logfile,"At line no: %d statement : var_declaration\n\n",yylineno);
- fprintf(logfile,"%s\n\n",$1->name.c_str());
- $$ = new node();
- $$->name= $1->name;
- $$->code = $1->code;
- $$->address = $1->address;
- $$->arr_type = $1->arr_type;
- }
- | expression_statement
- {
- fprintf(logfile,"At line no: %d statement : expression_statement\n\n",yylineno);
- fprintf(logfile,"%s\n\n",$1->name.c_str());
- $$ = new node();
- $$->name= $1->name;
- $$->arr_type = $1->arr_type;
- $$->address = $1->address;
- $$->code = $1->code;
- fprintf(logfile,"\n%s\n",$$->code.c_str());
- }
- | compound_statement
- {
- fprintf(logfile,"At line no: %d statement : compound_statement\n\n",yylineno);
- fprintf(logfile,"%s\n\n",$1->name.c_str());
- $$ = new node();
- $$->name= $1->name;
- $$->code = $1->code;
- $$->address = $1->address;
- $$->arr_type = $1->arr_type;
- }
- | FOR LPAREN expression_statement expression_statement expression RPAREN statement
- {
- fprintf(logfile,"At line no: %d statement : FOR LPAREN expression_statement expression_statement expression RPAREN statement\n\n",yylineno);
- fprintf(logfile,"for(%s%s%s)%s\n\n",$3->name.c_str(),$4->name.c_str(),$5->name.c_str(),$7->name.c_str());
- $$ = new node();
- $$->name = "for("+$3->name+$4->name+$5->name+")\n"+$7->name;
- string loop = newLabel();
- string skip = newLabel();
- $$->code = $3->code;
- $$->code+= loop+":\n";
- $$->code+=$4->code;
- $$->code+="\tCMP "+$4->address+",0\n";
- $$->code+="\tJE "+skip+"\n";
- $$->code+=$7->code;
- $$->code+=$5->code;
- $$->code+="\tJMP "+loop+"\n";
- $$->code+=skip+":\n";
- $$->address = $3->address;
- $$->arr_type = $3->arr_type;
- }
- | IF LPAREN expression RPAREN statement %prec LOWER_THEN_ELSE
- {
- fprintf(logfile,"At line no: %d statement : IF LPAREN expression RPAREN statement\n\n",yylineno);
- fprintf(logfile,"if(%s)\n%s\n\n",$3->name.c_str(),$5->name.c_str());
- $$ = new node();
- $$->name="if("+$3->name+")\n"+$5->name;
- string endif = newLabel();
- $$->code = $3->code;
- $$->code += "\tMOV AX,"+$3->address+"\n";
- $$->code += "\tCMP AX,1\n";
- $$->code += "\tJNE "+endif+"\n";
- $$->code += $5->code;
- $$->code += endif+": \n";
- $$->address = $3->address;
- $$->arr_type = $3->arr_type;
- }
- | IF LPAREN expression RPAREN statement ELSE statement
- {
- fprintf(logfile,"At line no: %d statement : IF LPAREN expression RPAREN statement ELSE statement\n\n",yylineno);
- fprintf(logfile,"if(%s)\n%s else \n%s\n\n",$3->name.c_str(),$5->name.c_str(),$7->name.c_str());
- $$ = new node();
- $$->name = "if("+$3->name+")\n"+$5->name+"else\n"+$7->name;
- string elselabel = newLabel();
- string endifelse = newLabel();
- $$->code = $3->code;
- $$->code += "\tMOV AX,"+$3->address+"\n";
- $$->code += "\tCMP AX,1\n";
- $$->code += "\tJNE "+elselabel+"\n";
- $$->code += $5->code;
- $$->code += "\tJMP "+endifelse+"\n";
- $$->code += elselabel+":\n";
- $$->code += $7->code;
- $$->code += endifelse+":\n";
- $$->address = $3->address;
- $$->arr_type = $3->arr_type;
- fprintf(logfile,"\n\n%s\n\n",$$->code.c_str());
- }
- | WHILE LPAREN expression RPAREN statement
- {
- fprintf(logfile,"At line no: %d statement : WHILE LPAREN expression RPAREN statement\n\n",yylineno);
- fprintf(logfile,"while(%s)\n%s\n\n",$3->name.c_str(),$5->name.c_str());
- $$ = new node();
- $$->name = "while("+$3->name+")\n"+$5->name;
- string whileloop = newLabel();
- string endwhile = newLabel();
- $$->code += whileloop+":\n";
- $$->code += $3->code;
- $$->code += "\tMOV AX,"+$3->address+"\n";
- $$->code += "\tCMP AX,0\n";
- $$->code += "JE "+endwhile+"\n";
- $$->code += $5->code;
- $$->code += "JMP "+whileloop+"\n";
- $$->code += endwhile+":\n";
- $$->address = $3->address;
- $$->arr_type = $$->arr_type;
- }
- | PRINTLN LPAREN ID RPAREN SEMICOLON
- {
- fprintf(logfile,"At line no: %d statement : PRINTLN LPAREN ID RPAREN SEMICOLON\n\n",yylineno);
- fprintf(logfile,"println(%s);\n\n",$3->name.c_str());
- $$ = new node();
- $$->name = "println("+$3->name+");\n";
- if(table->lookUp($3->name) != NULL){
- $$->code = $3->code;
- $$->code += ";-------------------------\n";
- $$->code += "\t;println("+$3->name+to_string(table->GetTableID($3->name))+")\n";
- $$->code += "\tMOV AX,"+$3->name+to_string(table->GetTableID($3->name))+"\n";
- $$->code += "\tCALL PRINTINT\n";
- $$->code += ";-------------------------\n\n";
- }
- else
- {
- fprintf(errorfile,"Error at Line %d: Undeclared variable %s\n\n",yylineno,$3->name.c_str());
- semerror++;
- }
- }
- | RETURN expression SEMICOLON
- {
- fprintf(logfile,"At line no: %d statement : RETURN expression SEMICOLON\n\n",yylineno);
- fprintf(logfile,"return %s;\n\n",$2->name.c_str());
- $$ = new node();
- $$->name = "return "+$2->name+";\n";
- ret_type = "yes";
- $$->address = $2->address;
- $$->code+=$2->code;
- return_temp = $2->address;;
- }
- ;
- expression_statement : SEMICOLON
- {
- fprintf(logfile,"At line no: %d expression_statement : SEMICOLON\n\n",yylineno);
- fprintf(logfile,";\n\n");
- $$ = new node();
- $$->name = ";";
- $$->arr_type = false;
- $$->address = "";
- $$->code = "";
- }
- | expression SEMICOLON
- {
- fprintf(logfile,"At line no: %d expression_statement : expression SEMICOLON\n\n",yylineno);
- fprintf(logfile,"%s;\n\n",$1->name.c_str());
- $$ = new node();
- $$->name = $1->name+";";
- $$->arr_type = $1->arr_type;
- $$->address = $1->address;
- $$->code = $1->code;
- fprintf(logfile,"\n%s\n\n",$$->code.c_str());
- }
- ;
- variable : ID
- {
- fprintf(logfile,"At line no: %d variable : ID\n\n",yylineno);
- fprintf(logfile,"%s\n\n",$1->name.c_str());
- $$ = new node();
- $$->name = $1->name;
- $$->arr_size = 0;
- node* temp = table->lookUp($1->name);
- if(temp == NULL)
- {
- fprintf(errorfile,"Error at Line %d: Undeclared Variable: %s\n\n",yylineno,$1->name.c_str());
- semerror++;
- cout<<semerror<<endl;
- }
- else{
- $$->var_type = temp->var_type;
- }
- $$->address = $1->name+to_string(table->GetTableID($1->name));
- //dataseg+="\t"+$$->address+"\tDW\t?\n";
- $$->arr_type = false;
- $$->code = $1->code;
- fprintf(logfile,"\n%s\n",$$->code.c_str());
- }
- | ID { arr_var = variable_type; } LTHIRD expression RTHIRD
- {
- fprintf(logfile,"At line no: %d variable : ID LTHIRD expression RTHIRD\n\n",yylineno);
- fprintf(logfile,"%s[%s]\n\n",$1->name.c_str(),$4->name.c_str());
- $$ = new node();
- $$->name = $1->name+"["+$4->name+"]";
- node* temp = table->lookUp($1->name);
- //fprintf(errorfile,"\n\n%d-->%s\n\n",yylineno,$4->var_type.c_str());
- if(temp == NULL)
- {
- fprintf(errorfile,"Error1 at Line %d: Undeclared Variable: %s\n\n",yylineno,$1->name.c_str());
- semerror++;
- }
- else
- {
- if( $4->var_type != "int")
- {
- fprintf(errorfile,"Error2 at Line %d: Non-integer Array Index\n\n",yylineno);
- semerror++;
- }
- else if(temp->arr_size==0)
- {
- fprintf(errorfile,"Error3 at Line %d: non Array Variable\n\n",yylineno);
- semerror++;
- }
- $$->var_type = temp->var_type;
- $$->arr_size = temp->arr_size;
- }
- $$->var_type = temp->var_type;
- $$->arr_size = temp->arr_size;
- $$->code = $4->code;
- $$->code += "\tMOV BX,"+$4->address+"\n";
- $$->code+="\tSHL BX,1\n";
- $$->address = $1->name+to_string(table->GetTableID($1->name))+"[BX]";
- //dataseg+="\t"+$$->address+"\tDW\t?\n";
- $$->arr_type = true;
- fprintf(logfile,"\n%s\n",$$->code.c_str());
- }
- ;
- expression : logic_expression
- {
- fprintf(logfile,"At line no: %d expression : logic_expression\n\n",yylineno);
- fprintf(logfile,"%s\n\n",$1->name.c_str());
- $$ = new node();
- $$->name = $1->name;
- $$->var_type = $1->var_type;
- $$->arr_type = $1->arr_type;
- $$->address = $1->address;
- $$->code = $1->code+"\n";
- fprintf(logfile,"\n%s\n\n",$$->code.c_str());
- }
- | variable ASSIGNOP logic_expression
- {
- fprintf(logfile,"At line no: %d expression : variable ASSIGNOP logic_expression\n\n",yylineno);
- fprintf(logfile,"%s=%s\n\n",$1->name.c_str(),$3->name.c_str());
- $$ = new node();
- $$->name = $1->name+"="+$3->name;
- int temp2 = 0;
- /*cout<<endl;
- cout<<"---------------------------------------------"<<endl;
- cout<<"At line no : "<<yylineno<<endl;
- cout<<"var_name : "<<$1->name<<endl;
- cout<<"var_type : "<<$1->var_type<<endl;
- cout<<"var_type for logic_expression : "<<$3->var_type<<endl;
- cout<<"arr_size : "<<$1->arr_size<<endl;
- cout<<"---------------------------------------------"<<endl;
- cout<<endl;*/
- //fprintf(errorfile,"\n\n%d--> %s %s %d\n\n",yylineno,$1->var_type.c_str(),$3->var_type.c_str(),$1->arr_size);
- if($1->var_type != $3->var_type)
- {
- fprintf(errorfile,"Error at line %d : type mismatch\n\n",yylineno);
- semerror++;
- }
- $$->var_type = $1->var_type;
- $$->arr_type = $1->arr_type;
- $$->address = $3->address;
- $$->code = $3->code+"\n"+$1->code;
- $$->code+="\tMOV AX,"+$3->address+"\n";
- $$->code+="\tMOV "+$1->address+",AX\n\n";
- fprintf(logfile,"\n%s\n\n",$$->code.c_str());
- }
- ;
- logic_expression : rel_expression
- {
- fprintf(logfile,"At line no: %d logic_expression : rel_expression\n\n",yylineno);
- fprintf(logfile,"%s\n\n",$1->name.c_str());
- $$ = new node();
- $$->name = $1->name;
- $$->var_type = $1->var_type;
- $$->arr_type = $1->arr_type;
- $$->address = $1->address;
- $$->code = $1->code;
- fprintf(logfile,"\n%s\n",$$->code.c_str());
- }
- | rel_expression LOGICOP rel_expression
- {
- fprintf(logfile,"At line no: %d logic_expression : rel_expression LOGICOP rel_expression\n\n",yylineno);
- fprintf(logfile,"%s%s%s\n\n",$1->name.c_str(),$2->name.c_str(),$3->name.c_str());
- $$ = new node();
- $$->name = $1->name+$2->name+$3->name;
- $$->var_type = "int";
- $$->arr_type = $1->arr_type;
- $$->address = newTemp();
- dataseg+="\t"+$$->address+"\tDW\t?\n";
- $$->code = $3->code+$1->code;
- $$->code+="\tMOV AX,"+$1->address+"\n";
- $$->code+="\tMOV BX,"+$3->address+"\n";
- if($2->name == "&&")
- {
- $$->code+="\tAND AX,BX\n";
- $$->code+="\tMOV "+$$->address+",AX\n";
- }
- else
- {
- $$->code+="\tOR AX,BX\n";
- $$->code+="\tMOV "+$$->address+",AX\n";
- }
- fprintf(logfile,"\n%s\n",$$->code.c_str());
- }
- ;
- rel_expression : simple_expression
- {
- fprintf(logfile,"At line no: %d rel_expression : simple_expression\n\n",yylineno);
- fprintf(logfile,"%s\n\n",$1->name.c_str());
- $$ = new node();
- $$->name = $1->name;
- $$->var_type = $1->var_type;
- $$->arr_type = $1->arr_type;
- $$->address = $1->address;
- $$->code = $1->code;
- fprintf(logfile,"\n%s\n",$$->code.c_str());
- }
- | simple_expression RELOP simple_expression
- {
- fprintf(logfile,"At line no: %d rel_expression : simple_expression RELOP simple_expression\n\n",yylineno);
- fprintf(logfile,"%s%s%s\n\n",$1->name.c_str(),$2->name.c_str(),$3->name.c_str());
- $$ = new node();
- $$->name = $1->name+$2->name+$3->name;
- $$->var_type = "int";
- $$->arr_type = $1->arr_type;
- $$->address = newTemp();
- dataseg+="\t"+$$->address+"\tDW\t?\n";
- string yesLebel = newLabel();
- string skibLabel = newLabel();
- $$->code = $1->code+$3->code;
- $$->code += "\tMOV AX,"+$1->address+"\n";
- $$->code += "\tMOV BX,"+$3->address+"\n";
- $$->code+= "\tCMP AX,BX;compare\n";
- if($2->name == "<")
- {
- $$->code+="\tJL "+yesLebel+";jump yes Label\n";
- }
- else if($2->name == "<=")
- {
- $$->code+="\tJLE "+yesLebel+";jump yes Label\n";
- }
- else if($2->name == "==")
- {
- $$->code+="\tJE "+yesLebel+";jump yes Label\n";
- }
- else if($2->name == ">")
- {
- $$->code+="\tJG "+yesLebel+";jump yes Label\n";
- }
- else if($2->name == ">=")
- {
- $$->code+="\tJGE "+yesLebel+";jump yes Label\n";
- }
- else
- {
- $$->code+="\tJNE "+yesLebel+";jump yes Label\n";
- }
- $$->code+="\tMOV "+$$->address+",0 ;when condition is false , set 0 "+$$->address+"\n";
- $$->code+="\tJMP "+skibLabel+"\n";
- $$->code+=yesLebel+":\n";
- $$->code+="\tMOV "+$$->address+",1 ;when condition is true , set 1 "+$$->address+"\n";
- $$->code+=skibLabel+":\n";
- fprintf(logfile,"\n%s\n",$$->code.c_str());
- }
- ;
- simple_expression : term
- {
- fprintf(logfile,"At line no: %d simple_expression : term\n\n",yylineno);
- fprintf(logfile,"%s\n\n",$1->name.c_str());
- $$ = new node();
- $$->name = $1->name;
- $$->var_type = $1->var_type;
- $$->arr_type = $1->arr_type;
- $$->address = $1->address;
- $$->code = $1->code;
- fprintf(logfile,"\n%s\n",$$->code.c_str());
- }
- | simple_expression ADDOP term
- {
- fprintf(logfile,"At line no: %d simple_expression : simple_expression ADDOP term\n\n",yylineno);
- fprintf(logfile,"%s%s%s\n\n",$1->name.c_str(),$2->name.c_str(),$3->name.c_str());
- $$ = new node();
- $$->name = $1->name+$2->name+$3->name;
- if($1->var_type == "float" || $3->var_type == "float")
- {
- $$->var_type = "float";
- }
- else
- {
- $$->var_type = "int";
- }
- $$->arr_type = $1->arr_type;
- $$->address = newTemp();
- dataseg+="\t"+$$->address+"\tDW\t?\n";
- $$->code = $1->code+$3->code;
- if($2->name == "+")
- {
- $$->code+="\tMOV AX,"+$1->address+"\n";
- $$->code+="\tMOV BX,"+$3->address+"\n";
- $$->code+="\tADD AX,BX\n";
- }
- else
- {
- $$->code+="\tMOV AX,"+$1->address+"\n";
- $$->code+="\tMOV BX,"+$3->address+"\n";
- $$->code+="\tSUB AX,BX\n";
- }
- $$->code+="\tMOV "+$$->address+",AX\n";
- fprintf(logfile,"\n%s\n",$$->code.c_str());
- }
- ;
- term : unary_expression
- {
- fprintf(logfile,"At line no: %d term : unary_expression\n\n",yylineno);
- fprintf(logfile,"%s\n\n",$1->name.c_str());
- $$ = new node();
- $$->name = $1->name;
- $$->var_type = $1->var_type;
- $$->code = $1->code;
- $$->arr_type = $1->arr_type;
- $$->address = $1->address;
- fprintf(logfile,"\n%s\n",$$->code.c_str());
- }
- | term MULOP unary_expression
- {
- fprintf(logfile,"At line no: %d term : term MULOP unary_expression\n\n",yylineno);
- fprintf(logfile,"%s%s%s\n\n",$1->name.c_str(),$2->name.c_str(),$3->name.c_str());
- $$ = new node();
- $$->name = $1->name+$2->name+$3->name;
- if($1->var_type == "float" || $3->var_type == "float")
- {
- if($2->name == "*")
- {
- $$->var_type = "float";
- }
- else if($2->name == "/")
- {
- $$->var_type = "float";
- }
- else
- {
- fprintf(errorfile,"Error at line %d : non-Integer operand on modulus operator\n\n",yylineno);
- semerror++;
- $$->var_type = "int";
- }
- }
- else
- {
- $$->var_type = "int";
- }
- $$->address = newTemp();
- dataseg+="\t"+$$->address+"\tDW\t?\n";
- $$->code = $1->code;
- $$->code+=$3->code;
- $$->arr_type = $1->arr_type;
- $$->code+="\tMOV AX,"+$1->address+"\n";
- $$->code+="\tMOV BX,"+$3->address+"\n";
- $$->code+="\tXOR DX,DX ;DX =0\n";
- if($2->name == "*")
- {
- $$->code+="\tMUL BX\n";
- $$->code+="\tMOV "+$$->address+",AX\n";
- }
- else if($2->name == "/")
- {
- $$->code+="\tDIV BX\n";
- $$->code+="\tMOV "+$$->address+",AX\n";
- }
- else
- {
- $$->code+="\tDIV BX\n";
- $$->code+="\tMOV "+$$->address+",DX\n";
- }
- fprintf(logfile,"\n%s\n",$$->code.c_str());
- }
- ;
- unary_expression : ADDOP unary_expression
- {
- fprintf(logfile,"At line no: %d unary_expression : ADDOP unary_expression\n\n",yylineno);
- fprintf(logfile,"%s%s\n\n",$1->name.c_str(),$2->name.c_str());
- $$ = new node();
- $$->name= $1->name+$2->name;
- $$->var_type = $2->var_type;
- $$->address = $2->address;
- $$->arr_type = $2->arr_type;
- $$->code = $2->code;
- if($1->name == "-")
- {
- $$->code += "\tNEG "+$2->address+"\n";
- }
- fprintf(logfile,"\n%s\n",$$->code.c_str());
- }
- | NOT unary_expression
- {
- fprintf(logfile,"At line no: %d unary_expression : NOT unary_expression\n\n",yylineno);
- fprintf(logfile,"!%s\n\n",$2->name.c_str());
- $$ = new node();
- $$->name= "!"+$2->name;
- $$->var_type = $2->var_type;
- $$->address = $2->address;
- $$->arr_type = $2->arr_type;
- $$->code = $2->code;
- $$->code += "\tNOT "+$2->address+"\n";
- fprintf(logfile,"\n%s\n",$$->code.c_str());
- }
- | factor
- {
- fprintf(logfile,"At line no: %d unary_expression : factor\n\n",yylineno);
- fprintf(logfile,"%s\n\n",$1->name.c_str());
- $$ = new node();
- $$->name = $1->name;
- $$->var_type = $1->var_type;
- $$->code =$1->code;
- $$->arr_type = $1->arr_type;
- $$->address = $1->address;
- fprintf(logfile,"\n%s\n",$$->code.c_str());
- }
- ;
- factor : variable
- {
- fprintf(logfile,"At line no: %d factor : variable\n\n",yylineno);
- fprintf(logfile,"%s\n\n",$1->name.c_str());
- $$ = new node();
- $$->name = $1->name;
- $$->var_type = $1->var_type;
- if($1->arr_type)
- {
- string t = newTemp();
- $$->code = $1->code;
- $$->code+="\tMOV AX,"+$1->address+"\n";
- $$->code+="\tMOV "+t+",AX\n";
- dataseg+="\t"+t+"\tDW\t?\n";
- $$->address = t;
- $$->arr_type = true;
- }
- else
- {
- $$->arr_type = false;
- $$->address = $1->address;
- $$->code = $1->code;
- }
- fprintf(logfile,"\n%s\n",$$->code.c_str());
- }
- | ID LPAREN argument_list RPAREN
- {
- fprintf(logfile,"At line no: %d factor : ID LPAREN argument_list RPAREN\n\n",yylineno);
- fprintf(logfile,"%s(%s)\n\n",$1->name.c_str(),$3->name.c_str());
- $$ = new node();
- $$->name = $1->name+"("+$3->name+")";
- $$->var_type = table->lookUp($1->name)->return_type;
- node* temp = table->lookUp($1->name);
- int argno = temp->param_list.size();
- int argno2 = arglist.size();
- //fprintf(errorfile,"\n\nLine no %d : %d %d\n\n",yylineno,argno,argno2);
- if(temp==NULL)
- {
- fprintf(errorfile,"Error at line %d : %s function not declared before\n\n",yylineno,$1->name.c_str());
- semerror++;
- }
- else if(argno != argno2)
- {
- fprintf(errorfile,"Error1 at line %d : function call with inappropraite number of params\n\n",yylineno,$1->name.c_str());
- semerror++;
- }
- else if(temp->return_type == "void")
- {
- fprintf(errorfile,"Error at line %d : void function can't call in expression \n\n",yylineno,$1->name.c_str());
- semerror++;
- $$->var_type = "int";
- }
- else
- {
- for(int i = 0; i <argno ;i++ )
- {
- //fprintf(errorfile,"\n\n%d --> %s %s\n\n",i+1,arglist[i].c_str(),temp->param_type[i].c_str());
- if(arglist[i] != temp->param_type[i])
- {
- fprintf(errorfile,"Error2 at line %d : type mismatch\n\n",yylineno,$1->name.c_str());
- semerror++;
- }
- }
- for(int i = 0; i <argno ;i++)
- {
- node* tempz = table->lookUp(arglistID[i]);
- if(tempz!=NULL)
- {
- if(tempz->arr_size!=0)
- {
- fprintf(errorfile,"Error at line %d : Function call on array type variable\n\n",yylineno,$1->name.c_str());
- semerror++;
- }
- }
- }
- $$->code = $1->code;
- $$->code += $3->code;
- int len = temp->asm_param.size();
- for(int i = 0; i < len;i++)
- {
- $$->code+="\tMOV AX,"+asm_arg[i]+"\n";
- $$->code+="\tMOV "+temp->asm_param[i]+",AX\n";
- }
- $$->code += "CALL "+lowerToUPPER($1->name)+"\n";
- $$->arr_type = false;
- if(temp->return_type != "void")
- {
- string t = newTemp();
- dataseg+="\t"+t+"\tDW\t?\n";
- $$->code+= "\tPOP AX\n";
- $$->code+= "\tMOV "+t+",AX\n";
- $$->address = t;
- }
- }
- if(temp->Func_defined == false)
- {
- fprintf(errorfile,"Error at line %d : function call on non-function\n\n",yylineno);
- semerror++;
- }
- arglist.clear();
- arglistID.clear();
- asm_arg.clear();
- fprintf(logfile,"\n%s\n",$$->code.c_str());
- }
- | LPAREN expression RPAREN
- {
- fprintf(logfile,"At line no: %d factor : LPAREN expression RPAREN\n\n",yylineno);
- fprintf(logfile,"(%s)\n\n",$2->name.c_str());
- $$ = new node();
- $$->name = "("+$2->name+")";
- $$->var_type = $2->var_type;
- $$->code =$2->code;
- $$->address = $2->address;
- $$->arr_type = $2->arr_type;
- fprintf(logfile,"\n%s\n",$$->code.c_str());
- }
- | CONST_INT
- {
- fprintf(logfile,"At line no: %d factor : CONST_INT\n\n",yylineno);
- fprintf(logfile,"%s\n\n",$1->name.c_str());
- $$ = new node();
- $$->name = $1->name;
- $$->var_type = "int";
- string t = newTemp();
- dataseg+="\t"+t+"\tDW\t?\n";
- $$->code= "\tMOV AX,"+$1->name+"\n";
- $$->code+="\tMOV "+t+",AX\n";
- $$->address = t;
- $$->arr_type = false;
- fprintf(logfile,"\n%s\n",$$->code.c_str());
- }
- | CONST_FLOAT
- {
- fprintf(logfile,"At line no: %d factor : CONST_FLOAT\n\n",yylineno);
- fprintf(logfile,"%s\n\n",$1->name.c_str());
- $$ = new node();
- $$->name = $1->name;
- $$->var_type = "float";
- string t = newTemp();
- dataseg+="\t"+t+"\tDW\t?\n";
- $$->code+= "\tMOV AX,"+$1->name+"\n";
- $$->code+="\tMOV "+t+",AX\n";
- $$->address = t;
- $$->arr_type = false;
- fprintf(logfile,"\n%s\n",$$->code.c_str());
- }
- | variable INCOP
- {
- fprintf(logfile,"At line no: %d factor : variable INCOP\n\n",yylineno);
- fprintf(logfile,"%s++\n\n",$1->name.c_str());
- $$ = new node();
- $$->name = $1->name+"++";
- $$->var_type = "int";
- $$->code = $1->code;
- $$->code+= "\tINC "+$1->address+"\n";
- $$->address = $1->address;
- $$->arr_type = $1->arr_type;
- fprintf(logfile,"\n%s\n",$$->code.c_str());
- }
- | variable DECOP
- {
- fprintf(logfile,"At line no: %d factor : variable DECOP\n\n",yylineno);
- fprintf(logfile,"%s--\n\n",$1->name.c_str());
- $$ = new node();
- $$->name = $1->name+"--";
- $$->var_type = "int";
- $$->code = $1->code;
- $$->code+= "\tDEC "+$1->address+"\n";
- $$->address = $1->address;
- $$->arr_type = $1->arr_type;
- fprintf(logfile,"\n%s\n",$$->code.c_str());
- }
- ;
- argument_list : arguments
- {
- fprintf(logfile,"At line no: %d argument_list : arguments\n\n",yylineno);
- fprintf(logfile,"%s\n\n",$1->name.c_str());
- $$ = new node();
- $$->name = $1->name;
- $$->code = $1->code;
- fprintf(logfile,"\n%s\n",$$->code.c_str());
- }
- |
- {
- $$ = new node();
- fprintf(logfile,"At line no: %d argument_list : emptylist\n\n",yylineno);
- }
- ;
- arguments : arguments COMMA logic_expression
- {
- fprintf(logfile,"At line no: %d arguments : arguments COMMA logic_expression\n\n",yylineno);
- fprintf(logfile,"%s,%s\n\n",$1->name.c_str(),$3->name.c_str());
- $$ = new node();
- $$->name = $1->name+","+$3->name;
- arglist.push_back($3->var_type);
- arglistID.push_back($3->name);
- asm_arg.push_back($3->address);
- $$->code = $1->code + $3->code;
- }
- | logic_expression
- {
- fprintf(logfile,"At line no: %d arguments : logic_expression\n\n",yylineno);
- fprintf(logfile,"%s\n\n",$1->name.c_str());
- $$ = new node();
- $$->name = $1->name;
- arglist.push_back($1->var_type);
- arglistID.push_back($1->name);
- asm_arg.push_back($1->address);
- $$->code = $1->code;
- }
- ;
- %%
- int main(int argc,char *argv[])
- {
- if((inputfile=fopen(argv[1],"r"))==NULL)
- {
- printf("Cannot Open Input File.\n");
- exit(1);
- }
- logfile= fopen(argv[2],"w");
- fclose(logfile);
- errorfile= fopen(argv[3],"w");
- fclose(errorfile);
- logfile= fopen(argv[2],"a");
- errorfile= fopen(argv[3],"a");
- //printf("Hello");
- yyin=inputfile;
- table = new SymbolTable(10,logfile);
- yyparse();
- fprintf(errorfile,"\nTotal error : %d\n\n",semerror);
- fclose(logfile);
- fclose(errorfile);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement