Advertisement
AbdRoufBUET

Untitled

Jul 14th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 43.70 KB | None | 0 0
  1. %{
  2. #include<iostream>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<cmath>
  6. #include "1505097.h"
  7.  
  8. extern FILE * yyin;
  9. extern int yylineno;
  10.  
  11.  
  12. using namespace std;
  13.  
  14. string variable_type;
  15. int yyparse(void);
  16. int yylex(void);
  17. FILE *logfile;
  18. FILE * errorfile;
  19. FILE * inputfile;
  20. vector<string> arglist;
  21. vector<string> arglistID;
  22. vector<string> asm_arg;
  23. vector<string> temp_asm_param;
  24. string arr_var;
  25.  
  26. string paramlist[100][2];
  27. int paramlistlen = 0;
  28.  
  29. string ret_type = "no";
  30.  
  31. SymbolTable *table;
  32. int stxerrorno = 0;
  33. int semerror = 0;
  34.  
  35.  
  36.  
  37.  
  38. int labelCount=0;
  39. int tempCount=0;
  40. string dataseg = ".DATA\nglobaltemp\tDW\t?\n";
  41. string PUSHALLREG = "\tPUSH AX\n\tPUSH BX\n\tPUSH CX\n\tPUSH DX\n";
  42. string POPALLREG = "\tPOP DX\n\tPOP CX\n\tPOP BX\n\tPOP AX\n";
  43. string return_temp;
  44.  
  45.  
  46.  
  47. string newLabel()
  48. {
  49.     string temp;
  50.     temp = "L"+to_string(labelCount);
  51.     labelCount++;
  52.     return temp;
  53. }
  54.  
  55. string newTemp()
  56. {
  57.     string temp;
  58.     temp = "t"+to_string(tempCount);
  59.     tempCount++;
  60.     return temp;
  61. }
  62.  
  63. string lowerToUPPER(string str)
  64. {
  65.     int l = str.size();
  66.     for(int i = 0; i < l ; i++)
  67.     {
  68.         if(str[i]>= 'a' && str[i]<= 'z')
  69.         {
  70.             str[i] = str[i] - 'a' + 'A';
  71.         }
  72.     }
  73.  
  74.     return str;
  75. }
  76.  
  77.  
  78.  
  79. void yyerror(char *s)
  80. {
  81.     fprintf(logfile,"Syntax Error at line %d : %s",yylineno,s);
  82.     stxerrorno++;
  83. }
  84.  
  85.  
  86. %}
  87.  
  88. %union{
  89. node* symVal;
  90. }
  91.  
  92. %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
  93. %token <symVal>ID
  94. %token <symVal>CONST_INT
  95. %token <symVal>CONST_FLOAT
  96. %token <symVal>CONST_CHAR
  97. %token <symVal>ADDOP
  98. %token <symVal>MULOP
  99. %token <symVal>LOGICOP
  100. %token <symVal>RELOP
  101. %token <symVal>BITOP
  102.  
  103.  
  104. %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
  105. %nonassoc LOWER_THEN_ELSE
  106. %nonassoc ELSE
  107.  
  108. %%
  109.  
  110. start : program    
  111.         {
  112.             fprintf(logfile,"At line no: %d start : program\n\n",yylineno);
  113.             fprintf(logfile,"%s\n\n",$1->name.c_str());
  114.             $$ = new node();
  115.             $$->name = $1->name;
  116.  
  117.             fprintf(logfile,"-----------------------Symbol Table--------------------\n\n");
  118.             table->PrintAllScopeTable();
  119.  
  120.             $$->code = ".MODEL SMALL\n";
  121.             $$->code+=".STACK 100H\n";
  122.             $$->code += dataseg+".CODE\n"+$1->code+"\n\n";
  123.             fprintf(logfile,"\n%s\n",$$->code.c_str());
  124.             char c;
  125.             FILE *asmOut = fopen("1505097.asm","w");
  126.             if(semerror == 0 && stxerrorno == 0)
  127.             {
  128.                
  129.                 fprintf(asmOut,"%s\n",$$->code.c_str());
  130.                
  131.            
  132.  
  133.        
  134.             FILE *fp = fopen("println.txt","r");
  135.             while ((c = fgetc(fp)) != EOF){
  136.                 fputc(c,logfile);
  137.                 fputc(c,asmOut);
  138.             }
  139.             fprintf(logfile,"END MAIN\n\n");
  140.             fprintf(asmOut,"END MAIN\n\n");
  141.             fclose(fp);
  142.             fclose(asmOut);
  143.            
  144.  
  145.             }
  146.            
  147.         }
  148.     ;
  149.  
  150. program : program unit     
  151.         {
  152.             fprintf(logfile,"At line no: %d program : program unit\n\n",yylineno);
  153.             fprintf(logfile,"%s%s\n\n",$1->name.c_str(),$2->name.c_str());
  154.             $$ = new node();
  155.             $$->name = $1->name+$2->name;
  156.  
  157.             $$->code = $1->code+$2->code;
  158.         }      
  159.     | unit                 
  160.     {
  161.         fprintf(logfile,"At line no: %d program : unit\n\n",yylineno);
  162.         fprintf(logfile,"%s\n\n",$1->name.c_str());
  163.         $$ = new node();
  164.         $$->name = $1->name;
  165.  
  166.         $$->code = $1->code;
  167.     }
  168.     ;
  169.    
  170. unit : var_declaration     
  171.         {
  172.             fprintf(logfile,"At line no: %d unit : var_declaration\n\n",yylineno);
  173.             fprintf(logfile,"%s\n\n",$1->name.c_str());
  174.             $$ = new node();
  175.             $$->name = $1->name;
  176.  
  177.             $$->code = $1->code;
  178.          }
  179.      | func_declaration    
  180.      {
  181.          fprintf(logfile,"At line no: %d unit : func_declaration\n\n",yylineno);
  182.          fprintf(logfile,"%s\n\n",$1->name.c_str());
  183.          $$ = new node();
  184.          $$->name = $1->name;
  185.  
  186.          $$->code = $1->code;
  187.     }
  188.      | func_definition     
  189.      {
  190.          fprintf(logfile,"At line no: %d unit : func_definition\n\n",yylineno);
  191.          fprintf(logfile,"%s\n\n",$1->name.c_str());
  192.          $$ = new node();
  193.          $$->name = $1->name;
  194.  
  195.          $$->code = $1->code;
  196.          }
  197.      ;
  198.      
  199. func_declaration : type_specifier ID LPAREN parameter_list RPAREN SEMICOLON    
  200.                 {
  201.                     fprintf(logfile,"At line no: %d func_declaration : type_specifier ID LPAREN parameter_list RPAREN SEMICOLON\n\n",yylineno);
  202.                     fprintf(logfile,"%s %s(%s);\n\n",$1->name.c_str(),$2->name.c_str(),$4->name.c_str());
  203.                     $$ = new node();
  204.                     $$->name=$1->name+" "+$2->name+"("+$4->name+");\n";
  205.                     //return_type = $1->name;
  206.  
  207.                     //cout<<return_type<<endl;
  208.  
  209.                     $2->return_type = $1->name;
  210.                     //fprintf(logfile,"----------------\n%s\n\n",$2->return_type.c_str());
  211.                     int len  = paramlistlen;
  212.                     for(int i = 0; i < len; i++)
  213.                     {
  214.                         $2->param_type.push_back(paramlist[i][0]);
  215.                         $2->param_list.push_back(paramlist[i][1]);
  216.                     }
  217.                     if(!table->Insert($2))
  218.                     {
  219.                         fprintf(errorfile,"Error at Line %d: Multiple Declaration of %s function\n\n",yylineno,$2->name.c_str());
  220.                         semerror++;
  221.                     }
  222.                     paramlistlen = 0;
  223.  
  224.                     $$->code = $2->code;
  225.                 }  
  226.         | type_specifier ID LPAREN RPAREN SEMICOLON                            
  227.         {  
  228.             fprintf(logfile,"At line no: %d func_declaration : type_specifier ID LPAREN RPAREN SEMICOLON\n\n",yylineno);
  229.             fprintf(logfile,"%s %s();\n\n",$1->name.c_str(),$2->name.c_str());
  230.             $$ = new node();
  231.             $$->name = $1->name+" "+$2->name+"();\n";
  232.             //return_type = $1->name;
  233.  
  234.             //cout<<return_type<<endl;
  235.  
  236.             $2->return_type = $1->name;
  237.             if(!table->Insert($2))
  238.             {
  239.                 fprintf(errorfile,"Error at Line %d: Multiple Declaration of %s function\n\n",yylineno,$2->name.c_str());
  240.                 semerror++;
  241.             }
  242.  
  243.             $$->code = $2->code;
  244.         }
  245.         ;
  246.          
  247. func_definition : type_specifier ID LPAREN parameter_list RPAREN compound_statement    
  248.                 {
  249.                     fprintf(logfile,"At line no: %d func_definition : type_specifier ID LPAREN parameter_list RPAREN compound_statement\n\n",yylineno);
  250.                     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());
  251.                     $$ = new node();
  252.                     $$->name = $1->name+" "+$2->name+"("+$4->name+")\n"+$6->name;
  253.  
  254.                     int scopeID;
  255.  
  256.                     if(table->lookUp($2->name)!=NULL)
  257.                     {
  258.                         node* temp = table->lookUp($2->name);
  259.                         int len = paramlistlen;
  260.  
  261.                         //cout<<"pararlist len : "<<paramlistlen<<endl;
  262.                         //cout<<"temp->param_list.size() : "<<temp->param_list.size()<<endl;
  263.                         int len2 = temp->param_list.size();
  264.                         if(temp->Func_defined == true)
  265.                         {
  266.                             fprintf(errorfile,"Error at Line %d: Multiple Definition of %s function\n\n",yylineno,$2->name.c_str());
  267.                             semerror++;
  268.                            
  269.                         }
  270.                         else if(len!=len2)
  271.                         {
  272.                             fprintf(errorfile,"Error at Line %d: Argument no mismatch of %s function\n\n",yylineno,$2->name.c_str());
  273.                             semerror++;
  274.  
  275.  
  276.                         }else
  277.                         {
  278.                             for(int i = 0 ; i < len ; i++)
  279.                             {
  280.                                 if(temp->param_type[i]!=paramlist[i][0])
  281.                                 {
  282.                                     fprintf(errorfile,"Error at Line %d: Parameter type mismatch of %s function\n\n",yylineno,$2->name.c_str());
  283.                                     semerror++;
  284.                                     break;
  285.                                 }
  286.                             }
  287.  
  288.                             temp->Func_defined = true;
  289.                         }
  290.                        
  291.                     }
  292.                     else
  293.                     {
  294.                         $2->return_type = $1->name;
  295.                         $2->Func_defined = true;
  296.                         int len = paramlistlen;
  297.                         for(int i = 0; i < len; i++)
  298.                         {
  299.                             $2->param_type.push_back(paramlist[i][0]);
  300.                             $2->param_list.push_back(paramlist[i][1]);
  301.                             $2->asm_param.push_back(temp_asm_param[i]);
  302.                            
  303.                         }
  304.                        
  305.                         table->Insert($2);
  306.                        
  307.  
  308.  
  309.                     }
  310.  
  311.                     if(ret_type == "yes" && $1->name == "void")
  312.                     {
  313.                         fprintf(errorfile,"Error at line %d : void function can't return \n\n",yylineno);
  314.                         semerror++;
  315.                     }
  316.                    
  317.                    
  318.                    
  319.                     $$->code = $2->code;
  320.                     if($2->name != "main")
  321.                     {
  322.                        
  323.                         $$->code += lowerToUPPER($2->name)+" PROC\n";
  324.                         $$->code += PUSHALLREG+"\n";
  325.                         $$->code += $6->code;
  326.                         $$->code += POPALLREG+"\n";
  327.                         if(ret_type == "yes")
  328.                         {
  329.                             $$->code+= "\tPOP globaltemp\n";
  330.                             $$->code+= $6->code;
  331.                             $$->code+="\tPUSH "+$6->address+"\n";
  332.                             $$->code+="\tPUSH globaltemp\n";
  333.                         }
  334.                         $$->code+="\tRET\n\n";
  335.                         $$->code+=lowerToUPPER($2->name)+" ENDP\n\n";
  336.                        
  337.                     }
  338.                     else
  339.                     {
  340.                         $$->code += "\nMAIN PROC\n";
  341.                         $$->code += "\tMOV AX,@DATA\n";
  342.                         $$->code += "\tMOV DS,AX\n\n";
  343.                         $$->code+= $6->code;
  344.                         $$->code+= "\tMOV AX,4CH\n";
  345.                         $$->code+= "\tINT 21H\n\n";
  346.                         $$->code+= lowerToUPPER($2->name)+" ENDP\n\n";
  347.                     }
  348.  
  349.  
  350.  
  351.  
  352.                    
  353.  
  354.                     ret_type = "no";
  355.                     paramlistlen = 0;
  356.                     temp_asm_param.clear();
  357.  
  358.  
  359.  
  360.  
  361.                    
  362.                 }
  363.         | type_specifier ID LPAREN RPAREN compound_statement                           
  364.         {
  365.             fprintf(logfile,"At line no: %d func_definition : type_specifier ID LPAREN RPAREN compound_statement\n\n",yylineno);
  366.             fprintf(logfile,"%s %s()\n%s\n\n",$1->name.c_str(),$2->name.c_str(),$5->name.c_str());
  367.             $$ = new node();
  368.             $$->name = $1->name+" "+$2->name+"()\n"+$5->name;
  369.            
  370.  
  371.             //cout<<return_type<<endl;
  372.  
  373.             node* temp;
  374.             temp = table->lookUp($2->name);
  375.            
  376.             if(temp==NULL)
  377.             {
  378.                 $2->return_type = $1->name;
  379.                 $2->Func_defined = true;
  380.                 table->Insert($2);
  381.  
  382.                
  383.             }
  384.             else
  385.             {
  386.                 if(temp->Func_defined == true)
  387.                 {
  388.                     fprintf(errorfile,"Error at Line %d: Multiple Definition of %s function\n\n",yylineno,$2->name.c_str());
  389.                     semerror++;
  390.                 }
  391.                 else
  392.                 {
  393.                     temp->Func_defined = true;
  394.                 }
  395.             }
  396.  
  397.             if(ret_type == "yes" && $1->name == "void")
  398.             {
  399.                 fprintf(errorfile,"Error at line %d : void function can't return \n\n",yylineno);
  400.                 semerror++;
  401.             }
  402.            
  403.  
  404.             $$->code = $2->code;
  405.  
  406.             if($2->name == "main")
  407.             {
  408.                 $$->code += "\nMAIN PROC\n";
  409.                 $$->code += "\tMOV AX,@DATA\n";
  410.                 $$->code += "\tMOV DS,AX\n\n";
  411.                 $$->code+= $5->code;
  412.                 $$->code+= "\tMOV AX,4CH\n";
  413.                 $$->code+= "\tINT 21H\n\n";
  414.                 $$->code+= lowerToUPPER($2->name)+" ENDP\n\n";
  415.             }
  416.             else
  417.             {
  418.                 $$->code += lowerToUPPER($2->name)+" PROC\n";
  419.                 $$->code += PUSHALLREG+"\n";
  420.                 $$->code += $5->code;
  421.                 $$->code += POPALLREG+"\n";
  422.                 if(ret_type == "yes")
  423.                 {
  424.                    
  425.                     $$->code+= "\tPOP globaltemp\n";
  426.                     $$->code+= $5->code;
  427.                     $$->code+="\tPUSH "+$5->address+"\n";
  428.                     $$->code+="\tPUSH globaltemp\n";
  429.                 }
  430.                 $$->code+="\tRET\n\n";
  431.                 $$->code+=lowerToUPPER($2->name)+" ENDP\n\n";
  432.             }
  433.             ret_type = "no";
  434.            
  435.            
  436.  
  437.            
  438.         }
  439.         ;              
  440.  
  441.  
  442. parameter_list  : parameter_list COMMA type_specifier ID       
  443.                 {
  444.                     fprintf(logfile,"At line no: %d parameter_list  : parameter_list COMMA type_specifier ID\n\n",yylineno);
  445.                     fprintf(logfile,"%s,%s %s\n\n",$1->name.c_str(),$3->name.c_str(),$4->name.c_str());
  446.                     $$ = new node();
  447.                     $$->name = $1->name+","+$3->name+" "+$4->name;
  448.  
  449.                    
  450.                    
  451.                     $4->var_type = $3->name;
  452.                     paramlist[paramlistlen][0] = $3->name;
  453.                     paramlist[paramlistlen][1] = $4->name;
  454.                     //dataseg+="\t"+paramlist[paramlistlen][1]+to_string(table->getCurrentScopeID()+1)+"\tDW\t?\n";
  455.  
  456.  
  457.  
  458.  
  459.  
  460.                     paramlistlen++;
  461.  
  462.                 }
  463.         | parameter_list COMMA type_specifier                  
  464.         {
  465.             fprintf(logfile,"At line no: %d parameter_list  : parameter_list COMMA type_specifier\n\n",yylineno);
  466.             fprintf(logfile,"%s,%s\n\n",$1->name.c_str(),$3->name.c_str());
  467.             $$ = new node();
  468.             $$->name = $1->name+","+$3->name;
  469.  
  470.            
  471.         }
  472.         | type_specifier ID                                
  473.          {
  474.              fprintf(logfile,"At line no: %d parameter_list  : type_specifier ID\n\n",yylineno);
  475.              fprintf(logfile,"%s %s\n\n",$1->name.c_str(),$2->name.c_str());
  476.              $$ = new node();
  477.              $$->name = $1->name+" "+$2->name;
  478.  
  479.            
  480.  
  481.              $2->var_type = $1->name;
  482.  
  483.              paramlist[paramlistlen][0] = $1->name;
  484.             paramlist[paramlistlen][1] = $2->name;
  485.             //dataseg+="\t"+paramlist[paramlistlen][1]+to_string(table->getCurrentScopeID()+1)+"\tDW\t?\n";
  486.             paramlistlen++;
  487.         }
  488.         | type_specifier                                       
  489.         {
  490.             fprintf(logfile,"At line no: %d parameter_list  : type_specifier\n\n",yylineno);
  491.             fprintf(logfile,"%s\n\n",$1->name.c_str());
  492.             $$ = new node();
  493.             $$->name = $1->name;
  494.  
  495.         }
  496.         ;
  497.  
  498.        
  499. compound_statement : LCURL {
  500.                             table->EnterScope();
  501.                             for(int i = 0; i < paramlistlen ; i++)
  502.                             {
  503.                                 node* temp = new node();
  504.                                 temp->name = paramlist[i][1];
  505.                                 temp->var_type = paramlist[i][0];
  506.                                 temp->type  = "ID";
  507.                                 table->Insert(temp);
  508.                             }
  509.  
  510.                         for(int i = 0; i < paramlistlen;i++)
  511.                         {
  512.                             temp_asm_param.push_back(paramlist[i][1]+to_string(table->getCurrentScopeID()));
  513.                         }
  514.  
  515.                         for(int i = 0; i < paramlistlen;i++)
  516.                         {
  517.                             dataseg+="\t"+paramlist[i][1]+to_string(table->getCurrentScopeID())+"\tDW\t?\n";
  518.                         }
  519.                         } statements {
  520.  
  521.                            
  522.  
  523.                             fprintf(logfile,"\n\n%d - >\n %s\n\n",yylineno,dataseg.c_str());
  524.                             table->PrintAllScopeTable();
  525.                              
  526.  
  527.                      } RCURL {
  528.                           table->ExitScope();}
  529.                           {
  530.                             fprintf(logfile,"At line no: %d compound_statement : LCURL statements RCURL\n\n",yylineno);
  531.                             fprintf(logfile,"{\n%s}\n\n",$3->name.c_str());
  532.                             $$ = new node();
  533.                             $$->name = "{\n"+$3->name+"}\n";
  534.  
  535.                            
  536.                            
  537.                             $$->address = $3->address;
  538.                             $$->code = $3->code;
  539.                           }    
  540.             | LCURL RCURL                      
  541.              {
  542.                  fprintf(logfile,"At line no: %d compound_statement : LCURL RCURL\n\n",yylineno);
  543.                  fprintf(logfile,"{}\n\n");
  544.                  $$ = new node();
  545.                  $$->name = "{}\n";
  546.             }
  547.             ;
  548.            
  549. var_declaration : type_specifier declaration_list SEMICOLON    
  550.                 {
  551.                     fprintf(logfile,"At line no: %d var_declaration : type_specifier declaration_list SEMICOLON\n\n",yylineno);
  552.                     fprintf(logfile,"%s %s;\n",$1->name.c_str(),$2->name.c_str());
  553.                     $$ = new node();
  554.                     $$->name = $1->name+" "+$2->name+";";
  555.                 }
  556.          ;
  557.          
  558. type_specifier  : INT  
  559.                 {
  560.                     fprintf(logfile,"At line no: %d type_specifier : INT\n\n",yylineno);
  561.                     fprintf(logfile,"int\n\n");
  562.                     $$ = new node();
  563.                     $$->name = "int"; //cout<< "hello1"<<endl;
  564.                     variable_type = "int";
  565.                 }
  566.         | FLOAT        
  567.          {
  568.              fprintf(logfile,"At line no: %d type_specifier : FLOAT\n\n",yylineno);
  569.              fprintf(logfile,"float\n\n");
  570.              $$ = new node();
  571.              $$->name = "float"; //cout<< "hello2"<<endl;
  572.              variable_type = "float";
  573.         }
  574.         | VOID         
  575.          {
  576.              fprintf(logfile,"At line no: %d type_specifier : VOID\n\n",yylineno);
  577.              fprintf(logfile,"void\n\n");
  578.              $$ = new node();
  579.              $$->name = "void"; //cout<< "hello3"<<endl;
  580.              variable_type = "void";
  581.         }
  582.         ;
  583.        
  584. declaration_list : declaration_list COMMA ID                       
  585.                     {
  586.                         fprintf(logfile,"At line no: %d declaration_list : declaration_list COMMA ID\n\n",yylineno);
  587.                         fprintf(logfile,"%s,%s\n\n",$1->name.c_str(),$3->name.c_str());
  588.                         $$ = new node();
  589.                         $$->name = $1->name +","+ $3->name;
  590.                         $$->var_type = variable_type;
  591.  
  592.  
  593.                         if(variable_type == "void")
  594.                         {
  595.                             fprintf(errorfile,"Error at Line %d : Variable type can't be void\n\n",yylineno);
  596.                             semerror++;
  597.                         }
  598.                         else
  599.                         {
  600.                                 $3->var_type = variable_type;
  601.                                 if(!table->Insert($3))
  602.                                 {
  603.                                     fprintf(errorfile,"Error at Line %d: Multiple Declaration of %s\n\n",yylineno,$3->name.c_str());
  604.                                     semerror++;
  605.                                 }
  606.                         }
  607.  
  608.                         //table->PrintCurrentScopeTable();
  609.  
  610.                         dataseg+="\t"+$3->name+to_string(table->GetTableID($3->name))+"\tDW\t?\n";
  611.                         $$->arr_type = false;
  612.  
  613.                     }
  614.           | declaration_list COMMA ID {arr_var = variable_type;} LTHIRD CONST_INT RTHIRD       
  615.            {
  616.                fprintf(logfile,"At line no: %d declaration_list : declaration_list COMMA ID LTHIRD CONST_INT RTHIRD\n\n",yylineno);
  617.                fprintf(logfile,"%s,%s[%s]\n\n",$1->name.c_str(),$3->name.c_str(),$6->name.c_str());
  618.                $$ = new node();
  619.                $$->name = $1->name +","+ $3->name+"["+$6->name+"]";
  620.                 $$->var_type = arr_var;
  621.                
  622.  
  623.                 //cout<<yylineno<<"-->"<<$6->name<<endl;
  624.                if(arr_var == "void")
  625.                {
  626.                    fprintf(errorfile,"Error at Line %d : Variable type can't be void\n\n",yylineno);
  627.                    semerror++;
  628.                }
  629.                else
  630.                {
  631.                    $3->var_type = arr_var;
  632.                    $3->arr_size = atoi($6->name.c_str());
  633.                     if(!table->Insert($3))
  634.                     {
  635.                         fprintf(errorfile,"Error at Line %d: Multiple Declaration of %s\n\n",yylineno,$3->name.c_str());
  636.                         semerror++;
  637.                     }
  638.  
  639.                    
  640.                }
  641.  
  642.                //table->PrintCurrentScopeTable();
  643.  
  644.        
  645.                 dataseg+="\t"+$3->name+to_string(table->GetTableID($3->name))+"\tDW\t"+$6->name+"\tDUP(?)\n";
  646.                 $$->arr_type = true;
  647.             }
  648.           | ID                                                     
  649.            {       
  650.                fprintf(logfile,"At line no: %d declaration_list : ID\n\n",yylineno);
  651.                fprintf(logfile,"%s\n\n",$1->name.c_str());
  652.                $$ = new node();
  653.                $$->name= $1->name;
  654.                $$->var_type = variable_type;
  655.  
  656.                $1->type = "ID";
  657.                if(variable_type == "void")
  658.                {
  659.                    fprintf(errorfile,"Error at Line %d : Variable type can't be void\n\n",yylineno);
  660.                    semerror++;
  661.                }
  662.                else
  663.                {
  664.                     $1->var_type = variable_type;
  665.                     if(!table->Insert($1))
  666.                     {
  667.                         fprintf(errorfile,"Error at Line %d: Multiple Declaration of %s\n\n",yylineno,$1->name.c_str());
  668.                         semerror++;
  669.                     }
  670.                 }
  671.  
  672.                
  673.                 dataseg+="\t"+$1->name+to_string(table->GetTableID($1->name))+"\tDW\t?\n";
  674.                 $$->arr_type = false;
  675.  
  676.  
  677.  
  678.  
  679.                //table->PrintCurrentScopeTable();
  680.             }
  681.           | ID {arr_var = variable_type;} LTHIRD CONST_INT RTHIRD                              
  682.            {
  683.                fprintf(logfile,"At line no: %d declaration_list : ID LTHIRD CONST_INT RTHIRD\n\n",yylineno);
  684.                fprintf(logfile,"%s[%s]\n\n",$1->name.c_str(),$4->name.c_str());
  685.                $$ = new node();
  686.                $$->name = $1->name+"["+$4->name+"]";
  687.                $$->var_type = arr_var;
  688.  
  689.                //cout<<yylineno<<"-->"<<$4->name<<endl;
  690.                if(arr_var == "void")
  691.                {
  692.                    fprintf(errorfile,"Error at Line %d : Variable type can't be void\n\n",yylineno);
  693.                    semerror++;
  694.                }
  695.                else
  696.                {
  697.                    //table->PrintAllScopeTable();
  698.                    $1->var_type = arr_var;
  699.                    $1->arr_size = atoi($4->name.c_str());
  700.  
  701.                    
  702.                     if(!table->Insert($1))
  703.                     {
  704.                         fprintf(errorfile,"Error404 at Line %d: Multiple Declaration of %s\n\n",yylineno,$1->name.c_str());
  705.                         semerror++;
  706.                     }
  707.  
  708.                     //table->PrintCurrentScopeTable();
  709.                }
  710.  
  711.                //table->PrintAllScopeTable();statements
  712.                 //$$->code = $1->code;
  713.                 //$$->address = $1->address;
  714.                 dataseg+="\t"+$1->name+to_string(table->GetTableID($1->name))+"\tDW\t"+$4->name+"\tDUP(?)\n";
  715.                 $$->arr_type = true;
  716.  
  717.  
  718.             }
  719.           ;
  720.          
  721. statements : statement             
  722.             {
  723.                 fprintf(logfile,"At line no: %d statements : statement\n\n",yylineno);
  724.                 fprintf(logfile,"%s\n\n",$1->name.c_str());
  725.                 $$ = new node();
  726.                 $$->name= $1->name;
  727.  
  728.                 $$->arr_type = $1->arr_type;
  729.                 $$->address = $1->address;
  730.                 $$->code = $1->code;
  731.  
  732.                 fprintf(logfile,"\n%s\n",$$->code.c_str());
  733.             }
  734.        | statements statement      
  735.        {
  736.            fprintf(logfile,"At line no: %d statements : statements statement\n\n",yylineno);
  737.            fprintf(logfile,"%s\n%s\n\n",$1->name.c_str(),$2->name.c_str());
  738.            $$ = new node();
  739.             $$->name= $1->name+"\n"+$2->name;
  740.  
  741.  
  742.             $$->arr_type = $1->arr_type;
  743.             $$->address = $2->address;
  744.             $$->code = $1->code+$2->code;
  745.             fprintf(logfile,"\n%s\n",$$->code.c_str());
  746.         }
  747.        ;
  748.        
  749. statement : var_declaration                                                                
  750.         {
  751.             fprintf(logfile,"At line no: %d statement : var_declaration\n\n",yylineno);
  752.             fprintf(logfile,"%s\n\n",$1->name.c_str());
  753.             $$ = new node();
  754.             $$->name= $1->name;
  755.  
  756.             $$->code = $1->code;
  757.             $$->address = $1->address;
  758.             $$->arr_type = $1->arr_type;
  759.           }
  760.       | expression_statement                                                               
  761.       {
  762.           fprintf(logfile,"At line no: %d statement : expression_statement\n\n",yylineno);
  763.           fprintf(logfile,"%s\n\n",$1->name.c_str());
  764.           $$ = new node();
  765.           $$->name= $1->name;
  766.  
  767.             $$->arr_type = $1->arr_type;
  768.             $$->address = $1->address;
  769.             $$->code = $1->code;
  770.             fprintf(logfile,"\n%s\n",$$->code.c_str());
  771.           }
  772.       | compound_statement                                                                 
  773.       {
  774.           fprintf(logfile,"At line no: %d statement : compound_statement\n\n",yylineno);
  775.           fprintf(logfile,"%s\n\n",$1->name.c_str());
  776.           $$ = new node();
  777.           $$->name= $1->name;
  778.  
  779.             $$->code = $1->code;
  780.             $$->address = $1->address;
  781.             $$->arr_type = $1->arr_type;
  782.           }
  783.       | FOR LPAREN expression_statement expression_statement expression RPAREN statement   
  784.       {
  785.           fprintf(logfile,"At line no: %d statement : FOR LPAREN expression_statement expression_statement expression RPAREN statement\n\n",yylineno);
  786.           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());
  787.           $$ = new node();
  788.           $$->name = "for("+$3->name+$4->name+$5->name+")\n"+$7->name;
  789.            
  790.             string loop = newLabel();
  791.             string skip = newLabel();
  792.            
  793.             $$->code = $3->code;
  794.             $$->code+= loop+":\n";
  795.             $$->code+=$4->code;
  796.             $$->code+="\tCMP "+$4->address+",0\n";
  797.             $$->code+="\tJE "+skip+"\n";
  798.             $$->code+=$7->code;
  799.             $$->code+=$5->code;
  800.             $$->code+="\tJMP "+loop+"\n";
  801.             $$->code+=skip+":\n";
  802.  
  803.             $$->address = $3->address;
  804.             $$->arr_type = $3->arr_type;
  805.  
  806.         }
  807.       | IF LPAREN expression RPAREN statement %prec LOWER_THEN_ELSE                        
  808.       {
  809.           fprintf(logfile,"At line no: %d statement : IF LPAREN expression RPAREN statement\n\n",yylineno);
  810.           fprintf(logfile,"if(%s)\n%s\n\n",$3->name.c_str(),$5->name.c_str());
  811.           $$ = new node();
  812.           $$->name="if("+$3->name+")\n"+$5->name;
  813.  
  814.         string endif = newLabel();
  815.  
  816.           $$->code = $3->code;
  817.           $$->code += "\tMOV AX,"+$3->address+"\n";
  818.           $$->code += "\tCMP AX,1\n";
  819.           $$->code += "\tJNE "+endif+"\n";
  820.           $$->code += $5->code;
  821.           $$->code += endif+": \n";
  822.  
  823.           $$->address  = $3->address;
  824.           $$->arr_type = $3->arr_type;
  825.  
  826.         }
  827.       | IF LPAREN expression RPAREN statement ELSE statement                               
  828.       {
  829.             fprintf(logfile,"At line no: %d statement : IF LPAREN expression RPAREN statement ELSE statement\n\n",yylineno);
  830.             fprintf(logfile,"if(%s)\n%s else \n%s\n\n",$3->name.c_str(),$5->name.c_str(),$7->name.c_str());
  831.             $$ = new node();
  832.             $$->name = "if("+$3->name+")\n"+$5->name+"else\n"+$7->name;
  833.  
  834.             string elselabel = newLabel();
  835.             string endifelse = newLabel();
  836.  
  837.             $$->code = $3->code;
  838.             $$->code += "\tMOV AX,"+$3->address+"\n";
  839.             $$->code += "\tCMP AX,1\n";
  840.             $$->code += "\tJNE "+elselabel+"\n";
  841.             $$->code += $5->code;
  842.             $$->code += "\tJMP "+endifelse+"\n";
  843.             $$->code += elselabel+":\n";
  844.             $$->code += $7->code;
  845.             $$->code += endifelse+":\n";
  846.  
  847.             $$->address = $3->address;
  848.             $$->arr_type = $3->arr_type;
  849.  
  850.             fprintf(logfile,"\n\n%s\n\n",$$->code.c_str());
  851.  
  852.         }
  853.       | WHILE LPAREN expression RPAREN statement
  854.         {
  855.             fprintf(logfile,"At line no: %d statement : WHILE LPAREN expression RPAREN statement\n\n",yylineno);
  856.             fprintf(logfile,"while(%s)\n%s\n\n",$3->name.c_str(),$5->name.c_str());
  857.             $$ = new node();
  858.             $$->name = "while("+$3->name+")\n"+$5->name;
  859.  
  860.             string whileloop = newLabel();
  861.             string endwhile = newLabel();
  862.  
  863.             $$->code += whileloop+":\n";
  864.             $$->code += $3->code;
  865.             $$->code += "\tMOV AX,"+$3->address+"\n";
  866.             $$->code += "\tCMP AX,0\n";
  867.             $$->code += "JE "+endwhile+"\n";
  868.             $$->code += $5->code;
  869.             $$->code += "JMP "+whileloop+"\n";
  870.             $$->code += endwhile+":\n";
  871.  
  872.             $$->address = $3->address;
  873.             $$->arr_type = $$->arr_type;
  874.  
  875.         }
  876.       | PRINTLN LPAREN ID RPAREN SEMICOLON                                                 
  877.       {
  878.           fprintf(logfile,"At line no: %d statement : PRINTLN LPAREN ID RPAREN SEMICOLON\n\n",yylineno);
  879.           fprintf(logfile,"println(%s);\n\n",$3->name.c_str());
  880.           $$ = new node();
  881.           $$->name = "println("+$3->name+");\n";
  882.             if(table->lookUp($3->name) != NULL){
  883.           $$->code = $3->code;
  884.           $$->code += ";-------------------------\n";
  885.           $$->code += "\t;println("+$3->name+to_string(table->GetTableID($3->name))+")\n";
  886.           $$->code += "\tMOV AX,"+$3->name+to_string(table->GetTableID($3->name))+"\n";
  887.           $$->code += "\tCALL PRINTINT\n";
  888.           $$->code += ";-------------------------\n\n";
  889.             }
  890.             else
  891.             {
  892.                 fprintf(errorfile,"Error at Line %d: Undeclared variable %s\n\n",yylineno,$3->name.c_str());
  893.                 semerror++;
  894.             }
  895.         }
  896.       | RETURN expression SEMICOLON                                                        
  897.       {
  898.             fprintf(logfile,"At line no: %d statement : RETURN expression SEMICOLON\n\n",yylineno);
  899.             fprintf(logfile,"return %s;\n\n",$2->name.c_str());
  900.             $$ = new node();
  901.             $$->name = "return "+$2->name+";\n";
  902.             ret_type = "yes";
  903.  
  904.             $$->address = $2->address;
  905.             $$->code+=$2->code;
  906.             return_temp = $2->address;;
  907.            
  908.         }
  909.       ;
  910.      
  911. expression_statement : SEMICOLON       
  912.                     {
  913.                         fprintf(logfile,"At line no: %d expression_statement : SEMICOLON\n\n",yylineno);
  914.                         fprintf(logfile,";\n\n");
  915.                         $$ = new node();
  916.                         $$->name = ";";
  917.  
  918.                         $$->arr_type = false;
  919.                         $$->address = "";
  920.                         $$->code = "";
  921.  
  922.  
  923.                     }  
  924.             | expression SEMICOLON     
  925.             {
  926.                 fprintf(logfile,"At line no: %d expression_statement : expression SEMICOLON\n\n",yylineno);
  927.                 fprintf(logfile,"%s;\n\n",$1->name.c_str());
  928.                 $$ = new node();
  929.                 $$->name = $1->name+";";
  930.  
  931.                 $$->arr_type = $1->arr_type;
  932.                 $$->address = $1->address;
  933.                 $$->code = $1->code;
  934.  
  935.                 fprintf(logfile,"\n%s\n\n",$$->code.c_str());
  936.             }
  937.             ;
  938.      
  939. variable : ID                          
  940.             {
  941.                 fprintf(logfile,"At line no: %d variable : ID\n\n",yylineno);
  942.                 fprintf(logfile,"%s\n\n",$1->name.c_str());
  943.                 $$ = new node();
  944.                 $$->name = $1->name;
  945.                 $$->arr_size = 0;
  946.                 node* temp = table->lookUp($1->name);
  947.  
  948.                 if(temp == NULL)
  949.                 {
  950.                     fprintf(errorfile,"Error at Line %d: Undeclared Variable: %s\n\n",yylineno,$1->name.c_str());
  951.                     semerror++;
  952.                     cout<<semerror<<endl;
  953.                 }
  954.                 else{
  955.                     $$->var_type = temp->var_type;
  956.  
  957.                 }
  958.  
  959.                 $$->address = $1->name+to_string(table->GetTableID($1->name));
  960.                 //dataseg+="\t"+$$->address+"\tDW\t?\n";
  961.                 $$->arr_type = false;
  962.                 $$->code = $1->code;
  963.  
  964.                 fprintf(logfile,"\n%s\n",$$->code.c_str());
  965.  
  966.                
  967.  
  968.             }      
  969.      | ID { arr_var = variable_type; } LTHIRD expression RTHIRD    
  970.         {
  971.              fprintf(logfile,"At line no: %d variable : ID LTHIRD expression RTHIRD\n\n",yylineno);
  972.              fprintf(logfile,"%s[%s]\n\n",$1->name.c_str(),$4->name.c_str());
  973.              $$ = new node();
  974.              $$->name = $1->name+"["+$4->name+"]";
  975.              
  976.              
  977.  
  978.              node* temp = table->lookUp($1->name);
  979.              
  980.             //fprintf(errorfile,"\n\n%d-->%s\n\n",yylineno,$4->var_type.c_str());
  981.              if(temp == NULL)
  982.              {
  983.                     fprintf(errorfile,"Error1 at Line %d: Undeclared Variable: %s\n\n",yylineno,$1->name.c_str());
  984.                     semerror++;
  985.             }
  986.             else
  987.             {
  988.                 if( $4->var_type != "int")
  989.                 {
  990.                     fprintf(errorfile,"Error2 at Line %d: Non-integer Array Index\n\n",yylineno);
  991.                     semerror++;
  992.                 }
  993.                 else if(temp->arr_size==0)
  994.                 {
  995.                     fprintf(errorfile,"Error3 at Line %d: non Array Variable\n\n",yylineno);
  996.                     semerror++;
  997.                 }
  998.                 $$->var_type = temp->var_type;
  999.                 $$->arr_size = temp->arr_size;
  1000.             }
  1001.  
  1002.             $$->var_type = temp->var_type;
  1003.             $$->arr_size = temp->arr_size;
  1004.  
  1005.             $$->code = $4->code;
  1006.             $$->code += "\tMOV BX,"+$4->address+"\n";
  1007.             $$->code+="\tSHL BX,1\n";
  1008.             $$->address = $1->name+to_string(table->GetTableID($1->name))+"[BX]";
  1009.             //dataseg+="\t"+$$->address+"\tDW\t?\n";
  1010.             $$->arr_type = true;
  1011.  
  1012.             fprintf(logfile,"\n%s\n",$$->code.c_str());
  1013.            
  1014.  
  1015.            
  1016.          }
  1017.      ;
  1018.      
  1019. expression : logic_expression                  
  1020.             {
  1021.                 fprintf(logfile,"At line no: %d expression : logic_expression\n\n",yylineno);
  1022.                 fprintf(logfile,"%s\n\n",$1->name.c_str());
  1023.                 $$ = new node();
  1024.                 $$->name = $1->name;
  1025.  
  1026.                 $$->var_type = $1->var_type;
  1027.  
  1028.                 $$->arr_type = $1->arr_type;
  1029.                 $$->address = $1->address;
  1030.                 $$->code = $1->code+"\n";
  1031.  
  1032.                 fprintf(logfile,"\n%s\n\n",$$->code.c_str());
  1033.             }
  1034.        | variable ASSIGNOP logic_expression    
  1035.        {
  1036.             fprintf(logfile,"At line no: %d expression : variable ASSIGNOP logic_expression\n\n",yylineno);
  1037.             fprintf(logfile,"%s=%s\n\n",$1->name.c_str(),$3->name.c_str());
  1038.             $$ = new node();
  1039.             $$->name = $1->name+"="+$3->name;
  1040.             int temp2 = 0;
  1041.  
  1042.            
  1043.  
  1044.  
  1045.             /*cout<<endl;
  1046.             cout<<"---------------------------------------------"<<endl;
  1047.             cout<<"At line no : "<<yylineno<<endl;
  1048.             cout<<"var_name : "<<$1->name<<endl;
  1049.             cout<<"var_type : "<<$1->var_type<<endl;
  1050.             cout<<"var_type for logic_expression : "<<$3->var_type<<endl;
  1051.             cout<<"arr_size : "<<$1->arr_size<<endl;
  1052.             cout<<"---------------------------------------------"<<endl;
  1053.             cout<<endl;*/
  1054.  
  1055.  
  1056.             //fprintf(errorfile,"\n\n%d--> %s %s %d\n\n",yylineno,$1->var_type.c_str(),$3->var_type.c_str(),$1->arr_size);
  1057.             if($1->var_type != $3->var_type)
  1058.             {
  1059.  
  1060.                
  1061.                 fprintf(errorfile,"Error at line %d : type mismatch\n\n",yylineno);
  1062.                 semerror++;
  1063.             }
  1064.  
  1065.  
  1066.             $$->var_type = $1->var_type;
  1067.  
  1068.  
  1069.             $$->arr_type = $1->arr_type;
  1070.             $$->address = $3->address;
  1071.             $$->code = $3->code+"\n"+$1->code;
  1072.  
  1073.             $$->code+="\tMOV AX,"+$3->address+"\n";
  1074.             $$->code+="\tMOV "+$1->address+",AX\n\n";
  1075.  
  1076.             fprintf(logfile,"\n%s\n\n",$$->code.c_str());
  1077.  
  1078.         }
  1079.        ;
  1080.            
  1081. logic_expression : rel_expression                      
  1082.                 {
  1083.                     fprintf(logfile,"At line no: %d logic_expression : rel_expression\n\n",yylineno);
  1084.                     fprintf(logfile,"%s\n\n",$1->name.c_str());
  1085.                     $$ = new node();
  1086.                     $$->name = $1->name;
  1087.                     $$->var_type = $1->var_type;
  1088.  
  1089.                     $$->arr_type = $1->arr_type;
  1090.                     $$->address = $1->address;
  1091.                     $$->code = $1->code;
  1092.  
  1093.                     fprintf(logfile,"\n%s\n",$$->code.c_str());
  1094.                 }
  1095.          | rel_expression LOGICOP rel_expression       
  1096.             {
  1097.                 fprintf(logfile,"At line no: %d logic_expression : rel_expression LOGICOP rel_expression\n\n",yylineno);
  1098.                 fprintf(logfile,"%s%s%s\n\n",$1->name.c_str(),$2->name.c_str(),$3->name.c_str());
  1099.                 $$ = new node();
  1100.                 $$->name = $1->name+$2->name+$3->name;
  1101.                 $$->var_type = "int";
  1102.                
  1103.                 $$->arr_type = $1->arr_type;
  1104.                 $$->address = newTemp();
  1105.                 dataseg+="\t"+$$->address+"\tDW\t?\n";
  1106.                 $$->code = $3->code+$1->code;
  1107.                 $$->code+="\tMOV AX,"+$1->address+"\n";
  1108.                 $$->code+="\tMOV BX,"+$3->address+"\n";
  1109.  
  1110.                 if($2->name == "&&")
  1111.                 {
  1112.                     $$->code+="\tAND AX,BX\n";
  1113.                     $$->code+="\tMOV "+$$->address+",AX\n";
  1114.                 }
  1115.                 else
  1116.                 {
  1117.                     $$->code+="\tOR AX,BX\n";
  1118.                     $$->code+="\tMOV "+$$->address+",AX\n";
  1119.                 }
  1120.  
  1121.                 fprintf(logfile,"\n%s\n",$$->code.c_str());
  1122.                
  1123.             }  
  1124.          ;
  1125.            
  1126. rel_expression  : simple_expression                    
  1127.                 {
  1128.                     fprintf(logfile,"At line no: %d rel_expression  : simple_expression\n\n",yylineno);
  1129.                     fprintf(logfile,"%s\n\n",$1->name.c_str());
  1130.                     $$ = new node();
  1131.                     $$->name = $1->name;
  1132.                     $$->var_type = $1->var_type;
  1133.  
  1134.                     $$->arr_type = $1->arr_type;
  1135.                     $$->address = $1->address;
  1136.                     $$->code = $1->code;
  1137.  
  1138.                     fprintf(logfile,"\n%s\n",$$->code.c_str());
  1139.                 }
  1140.         | simple_expression RELOP simple_expression    
  1141.             {
  1142.                 fprintf(logfile,"At line no: %d rel_expression  : simple_expression RELOP simple_expression\n\n",yylineno);
  1143.                 fprintf(logfile,"%s%s%s\n\n",$1->name.c_str(),$2->name.c_str(),$3->name.c_str());
  1144.                 $$ = new node();
  1145.                 $$->name = $1->name+$2->name+$3->name;
  1146.                 $$->var_type = "int";
  1147.  
  1148.                 $$->arr_type = $1->arr_type;
  1149.                 $$->address = newTemp();
  1150.                 dataseg+="\t"+$$->address+"\tDW\t?\n";
  1151.                 string yesLebel = newLabel();
  1152.                 string skibLabel = newLabel();
  1153.                 $$->code = $1->code+$3->code;
  1154.                 $$->code += "\tMOV AX,"+$1->address+"\n";
  1155.                 $$->code += "\tMOV BX,"+$3->address+"\n";
  1156.                 $$->code+= "\tCMP AX,BX;compare\n";
  1157.  
  1158.  
  1159.                 if($2->name == "<")
  1160.                 {
  1161.                     $$->code+="\tJL "+yesLebel+";jump yes Label\n";
  1162.                 }
  1163.                 else if($2->name == "<=")
  1164.                 {
  1165.                     $$->code+="\tJLE "+yesLebel+";jump yes Label\n";
  1166.                 }
  1167.                 else if($2->name == "==")
  1168.                 {
  1169.                     $$->code+="\tJE "+yesLebel+";jump yes Label\n";
  1170.                 }
  1171.                 else if($2->name == ">")
  1172.                 {
  1173.                     $$->code+="\tJG "+yesLebel+";jump yes Label\n";
  1174.                 }
  1175.                 else if($2->name == ">=")
  1176.                 {
  1177.                     $$->code+="\tJGE "+yesLebel+";jump yes Label\n";
  1178.                 }
  1179.                 else
  1180.                 {
  1181.                     $$->code+="\tJNE "+yesLebel+";jump yes Label\n";
  1182.                 }
  1183.                 $$->code+="\tMOV "+$$->address+",0 ;when condition is false , set 0 "+$$->address+"\n";
  1184.                 $$->code+="\tJMP "+skibLabel+"\n";
  1185.                 $$->code+=yesLebel+":\n";
  1186.                 $$->code+="\tMOV "+$$->address+",1 ;when condition is true , set 1 "+$$->address+"\n";
  1187.                 $$->code+=skibLabel+":\n";
  1188.                
  1189.                 fprintf(logfile,"\n%s\n",$$->code.c_str());
  1190.  
  1191.                
  1192.             }
  1193.         ;
  1194.                
  1195. simple_expression : term                               
  1196.                 {
  1197.                     fprintf(logfile,"At line no: %d simple_expression : term\n\n",yylineno);
  1198.                     fprintf(logfile,"%s\n\n",$1->name.c_str());
  1199.                     $$ = new node();
  1200.                     $$->name = $1->name;
  1201.                     $$->var_type = $1->var_type;
  1202.  
  1203.                     $$->arr_type = $1->arr_type;
  1204.                     $$->address = $1->address;
  1205.                     $$->code = $1->code;
  1206.  
  1207.                     fprintf(logfile,"\n%s\n",$$->code.c_str());
  1208.                 }
  1209.           | simple_expression ADDOP term               
  1210.           {
  1211.               fprintf(logfile,"At line no: %d simple_expression : simple_expression ADDOP term\n\n",yylineno);
  1212.               fprintf(logfile,"%s%s%s\n\n",$1->name.c_str(),$2->name.c_str(),$3->name.c_str());
  1213.               $$ = new node();
  1214.               $$->name = $1->name+$2->name+$3->name;
  1215.  
  1216.               if($1->var_type == "float" || $3->var_type == "float")
  1217.                 {
  1218.                    
  1219.                         $$->var_type = "float";
  1220.                 }
  1221.                 else
  1222.                 {
  1223.                     $$->var_type = "int";
  1224.                 }
  1225.  
  1226.                 $$->arr_type = $1->arr_type;
  1227.                 $$->address = newTemp();
  1228.                 dataseg+="\t"+$$->address+"\tDW\t?\n";
  1229.                 $$->code = $1->code+$3->code;
  1230.  
  1231.                 if($2->name == "+")
  1232.                 {
  1233.                     $$->code+="\tMOV AX,"+$1->address+"\n";
  1234.                     $$->code+="\tMOV BX,"+$3->address+"\n";
  1235.                     $$->code+="\tADD AX,BX\n";
  1236.                    
  1237.                 }
  1238.                 else
  1239.                 {
  1240.                     $$->code+="\tMOV AX,"+$1->address+"\n";
  1241.                     $$->code+="\tMOV BX,"+$3->address+"\n";
  1242.                     $$->code+="\tSUB AX,BX\n";
  1243.                 }
  1244.  
  1245.                 $$->code+="\tMOV "+$$->address+",AX\n";
  1246.  
  1247.                 fprintf(logfile,"\n%s\n",$$->code.c_str());
  1248.  
  1249.  
  1250.             }
  1251.           ;
  1252.                    
  1253. term :  unary_expression                       
  1254.         {
  1255.             fprintf(logfile,"At line no: %d term :  unary_expression\n\n",yylineno);
  1256.             fprintf(logfile,"%s\n\n",$1->name.c_str());
  1257.             $$ = new node();
  1258.             $$->name = $1->name;
  1259.             $$->var_type = $1->var_type;
  1260.  
  1261.             $$->code = $1->code;
  1262.             $$->arr_type = $1->arr_type;
  1263.             $$->address = $1->address;
  1264.  
  1265.             fprintf(logfile,"\n%s\n",$$->code.c_str());
  1266.         }
  1267.      |  term MULOP unary_expression            
  1268.      {
  1269.          fprintf(logfile,"At line no: %d term : term MULOP unary_expression\n\n",yylineno);
  1270.          fprintf(logfile,"%s%s%s\n\n",$1->name.c_str(),$2->name.c_str(),$3->name.c_str());
  1271.          $$ = new node();
  1272.          $$->name = $1->name+$2->name+$3->name;
  1273.  
  1274.          if($1->var_type == "float" || $3->var_type == "float")
  1275.          {
  1276.              if($2->name == "*")
  1277.              {
  1278.                  $$->var_type = "float";
  1279.              }
  1280.              else if($2->name == "/")
  1281.              {
  1282.                  $$->var_type = "float";
  1283.              }
  1284.              else
  1285.              {
  1286.                  fprintf(errorfile,"Error at line %d : non-Integer operand on modulus operator\n\n",yylineno);
  1287.                  semerror++;
  1288.                  $$->var_type = "int";
  1289.              }
  1290.          }
  1291.          else
  1292.          {
  1293.              $$->var_type = "int";
  1294.          }
  1295.            
  1296.        
  1297.          $$->address = newTemp();
  1298.          dataseg+="\t"+$$->address+"\tDW\t?\n";
  1299.          $$->code = $1->code;
  1300.          $$->code+=$3->code;
  1301.          $$->arr_type = $1->arr_type;
  1302.  
  1303.          $$->code+="\tMOV AX,"+$1->address+"\n";
  1304.          $$->code+="\tMOV BX,"+$3->address+"\n";
  1305.          $$->code+="\tXOR DX,DX ;DX =0\n";
  1306.  
  1307.          if($2->name == "*")
  1308.          {
  1309.              $$->code+="\tMUL BX\n";
  1310.             $$->code+="\tMOV "+$$->address+",AX\n";
  1311.          }
  1312.          else if($2->name == "/")
  1313.          {
  1314.              $$->code+="\tDIV BX\n";
  1315.             $$->code+="\tMOV "+$$->address+",AX\n";
  1316.          }
  1317.          else
  1318.          {
  1319.              $$->code+="\tDIV BX\n";
  1320.             $$->code+="\tMOV "+$$->address+",DX\n";
  1321.          }
  1322.  
  1323.         fprintf(logfile,"\n%s\n",$$->code.c_str());
  1324.  
  1325.  
  1326.     }
  1327.      ;
  1328.  
  1329. unary_expression : ADDOP unary_expression      
  1330.                     {
  1331.                         fprintf(logfile,"At line no: %d unary_expression : ADDOP unary_expression\n\n",yylineno);
  1332.                         fprintf(logfile,"%s%s\n\n",$1->name.c_str(),$2->name.c_str());
  1333.                         $$ = new node();
  1334.                         $$->name= $1->name+$2->name;
  1335.                         $$->var_type = $2->var_type;
  1336.  
  1337.                         $$->address = $2->address;
  1338.                         $$->arr_type = $2->arr_type;
  1339.                         $$->code = $2->code;
  1340.  
  1341.                         if($1->name == "-")
  1342.                         {
  1343.                             $$->code += "\tNEG "+$2->address+"\n";
  1344.                         }
  1345.  
  1346.                         fprintf(logfile,"\n%s\n",$$->code.c_str());
  1347.                     }
  1348.          | NOT unary_expression                
  1349.          {
  1350.              fprintf(logfile,"At line no: %d unary_expression : NOT unary_expression\n\n",yylineno);
  1351.              fprintf(logfile,"!%s\n\n",$2->name.c_str());
  1352.              $$ = new node();
  1353.             $$->name= "!"+$2->name;
  1354.             $$->var_type = $2->var_type;
  1355.  
  1356.             $$->address = $2->address;
  1357.             $$->arr_type = $2->arr_type;
  1358.             $$->code = $2->code;
  1359.             $$->code += "\tNOT "+$2->address+"\n";
  1360.             fprintf(logfile,"\n%s\n",$$->code.c_str());
  1361.  
  1362.  
  1363.  
  1364.         }
  1365.          | factor                              
  1366.          {
  1367.              fprintf(logfile,"At line no: %d unary_expression : factor\n\n",yylineno);
  1368.              fprintf(logfile,"%s\n\n",$1->name.c_str());
  1369.              $$ = new node();
  1370.              $$->name = $1->name;
  1371.              $$->var_type = $1->var_type;
  1372.  
  1373.  
  1374.             $$->code =$1->code;
  1375.             $$->arr_type = $1->arr_type;
  1376.             $$->address = $1->address;
  1377.  
  1378.             fprintf(logfile,"\n%s\n",$$->code.c_str());
  1379.             }
  1380.          ;
  1381.    
  1382. factor  : variable                         
  1383.         {
  1384.             fprintf(logfile,"At line no: %d factor  : variable\n\n",yylineno);
  1385.             fprintf(logfile,"%s\n\n",$1->name.c_str());
  1386.             $$ = new node();
  1387.             $$->name = $1->name;
  1388.             $$->var_type = $1->var_type;
  1389.            
  1390.  
  1391.             if($1->arr_type)
  1392.             {
  1393.                 string t = newTemp();
  1394.                 $$->code = $1->code;
  1395.                 $$->code+="\tMOV AX,"+$1->address+"\n";
  1396.                 $$->code+="\tMOV "+t+",AX\n";
  1397.                 dataseg+="\t"+t+"\tDW\t?\n";
  1398.                 $$->address = t;
  1399.                 $$->arr_type = true;
  1400.  
  1401.                
  1402.             }
  1403.             else
  1404.             {
  1405.                 $$->arr_type = false;
  1406.                 $$->address = $1->address;
  1407.                 $$->code = $1->code;
  1408.             }
  1409.  
  1410.             fprintf(logfile,"\n%s\n",$$->code.c_str());
  1411.  
  1412.            
  1413.         }
  1414.     | ID LPAREN argument_list RPAREN       
  1415.     {
  1416.         fprintf(logfile,"At line no: %d factor  : ID LPAREN argument_list RPAREN\n\n",yylineno);
  1417.         fprintf(logfile,"%s(%s)\n\n",$1->name.c_str(),$3->name.c_str());
  1418.         $$ = new node();
  1419.         $$->name = $1->name+"("+$3->name+")";
  1420.         $$->var_type = table->lookUp($1->name)->return_type;
  1421.  
  1422.  
  1423.         node* temp = table->lookUp($1->name);
  1424.         int argno = temp->param_list.size();
  1425.  
  1426.         int argno2 = arglist.size();
  1427.  
  1428.         //fprintf(errorfile,"\n\nLine no %d : %d %d\n\n",yylineno,argno,argno2);
  1429.         if(temp==NULL)
  1430.         {
  1431.             fprintf(errorfile,"Error at line %d : %s function not declared before\n\n",yylineno,$1->name.c_str());
  1432.             semerror++;
  1433.         }
  1434.         else if(argno != argno2)
  1435.         {
  1436.             fprintf(errorfile,"Error1 at line %d : function call with inappropraite number of params\n\n",yylineno,$1->name.c_str());
  1437.             semerror++;
  1438.         }
  1439.         else if(temp->return_type == "void")
  1440.         {
  1441.             fprintf(errorfile,"Error at line %d : void function can't call in expression \n\n",yylineno,$1->name.c_str());
  1442.             semerror++;
  1443.             $$->var_type = "int";
  1444.         }
  1445.         else
  1446.         {
  1447.             for(int i = 0; i <argno ;i++ )
  1448.             {
  1449.                 //fprintf(errorfile,"\n\n%d --> %s %s\n\n",i+1,arglist[i].c_str(),temp->param_type[i].c_str());
  1450.                 if(arglist[i] != temp->param_type[i])
  1451.                 {
  1452.                    
  1453.                     fprintf(errorfile,"Error2 at line %d : type mismatch\n\n",yylineno,$1->name.c_str());
  1454.                     semerror++;
  1455.                 }
  1456.             }
  1457.  
  1458.             for(int i = 0; i <argno ;i++)
  1459.             {
  1460.                 node* tempz = table->lookUp(arglistID[i]);
  1461.                 if(tempz!=NULL)
  1462.                 {
  1463.                     if(tempz->arr_size!=0)
  1464.                     {
  1465.                         fprintf(errorfile,"Error at line %d : Function call on array type variable\n\n",yylineno,$1->name.c_str());
  1466.                         semerror++;
  1467.                     }
  1468.                 }
  1469.             }
  1470.  
  1471.             $$->code = $1->code;
  1472.             $$->code += $3->code;
  1473.            
  1474.            
  1475.            
  1476.            
  1477.            
  1478.            
  1479.            
  1480.            
  1481.             int len = temp->asm_param.size();
  1482.             for(int i = 0; i < len;i++)
  1483.             {
  1484.                 $$->code+="\tMOV AX,"+asm_arg[i]+"\n";
  1485.                 $$->code+="\tMOV "+temp->asm_param[i]+",AX\n";
  1486.             }
  1487.             $$->code += "CALL "+lowerToUPPER($1->name)+"\n";
  1488.             $$->arr_type = false;
  1489.             if(temp->return_type != "void")
  1490.             {
  1491.                 string t = newTemp();
  1492.                 dataseg+="\t"+t+"\tDW\t?\n";
  1493.                 $$->code+= "\tPOP AX\n";
  1494.                 $$->code+= "\tMOV "+t+",AX\n";
  1495.                 $$->address = t;
  1496.             }
  1497.         }
  1498.  
  1499.         if(temp->Func_defined == false)
  1500.         {
  1501.             fprintf(errorfile,"Error at line %d : function call on non-function\n\n",yylineno);
  1502.             semerror++;
  1503.         }
  1504.  
  1505.        
  1506.  
  1507.  
  1508.  
  1509.  
  1510.  
  1511.  
  1512.         arglist.clear();
  1513.         arglistID.clear();
  1514.         asm_arg.clear();
  1515.  
  1516.         fprintf(logfile,"\n%s\n",$$->code.c_str());
  1517.     }
  1518.     | LPAREN expression RPAREN         
  1519.         {
  1520.             fprintf(logfile,"At line no: %d factor  : LPAREN expression RPAREN\n\n",yylineno);
  1521.             fprintf(logfile,"(%s)\n\n",$2->name.c_str());
  1522.             $$ = new node();
  1523.             $$->name = "("+$2->name+")";
  1524.             $$->var_type = $2->var_type;
  1525.  
  1526.            
  1527.             $$->code =$2->code;
  1528.             $$->address = $2->address;
  1529.             $$->arr_type = $2->arr_type;
  1530.  
  1531.             fprintf(logfile,"\n%s\n",$$->code.c_str());
  1532.  
  1533.            
  1534.         }
  1535.     | CONST_INT                            
  1536.         {
  1537.             fprintf(logfile,"At line no: %d factor  : CONST_INT\n\n",yylineno);
  1538.             fprintf(logfile,"%s\n\n",$1->name.c_str());
  1539.             $$ = new node();
  1540.             $$->name = $1->name;
  1541.             $$->var_type = "int";
  1542.  
  1543.             string t = newTemp();
  1544.             dataseg+="\t"+t+"\tDW\t?\n";
  1545.             $$->code= "\tMOV AX,"+$1->name+"\n";
  1546.             $$->code+="\tMOV "+t+",AX\n";
  1547.             $$->address = t;
  1548.             $$->arr_type = false;
  1549.  
  1550.             fprintf(logfile,"\n%s\n",$$->code.c_str());
  1551.         }
  1552.     | CONST_FLOAT                          
  1553.     {
  1554.         fprintf(logfile,"At line no: %d factor  : CONST_FLOAT\n\n",yylineno);
  1555.         fprintf(logfile,"%s\n\n",$1->name.c_str());
  1556.         $$ = new node();
  1557.         $$->name = $1->name;
  1558.         $$->var_type = "float";
  1559.  
  1560.         string t = newTemp();
  1561.         dataseg+="\t"+t+"\tDW\t?\n";
  1562.         $$->code+= "\tMOV AX,"+$1->name+"\n";
  1563.         $$->code+="\tMOV "+t+",AX\n";
  1564.         $$->address = t;
  1565.         $$->arr_type = false;
  1566.  
  1567.         fprintf(logfile,"\n%s\n",$$->code.c_str());
  1568.     }
  1569.     | variable INCOP    
  1570.     {
  1571.         fprintf(logfile,"At line no: %d factor  : variable INCOP\n\n",yylineno);
  1572.         fprintf(logfile,"%s++\n\n",$1->name.c_str());
  1573.         $$ = new node();
  1574.         $$->name = $1->name+"++";
  1575.         $$->var_type = "int";
  1576.        
  1577.         $$->code = $1->code;
  1578.         $$->code+= "\tINC "+$1->address+"\n";
  1579.         $$->address = $1->address;
  1580.         $$->arr_type = $1->arr_type;
  1581.  
  1582.         fprintf(logfile,"\n%s\n",$$->code.c_str());
  1583.  
  1584.        
  1585.     }
  1586.     | variable DECOP                       
  1587.     {
  1588.         fprintf(logfile,"At line no: %d factor  : variable DECOP\n\n",yylineno);
  1589.         fprintf(logfile,"%s--\n\n",$1->name.c_str());
  1590.         $$ = new node();
  1591.         $$->name = $1->name+"--";
  1592.         $$->var_type = "int";
  1593.  
  1594.         $$->code = $1->code;
  1595.  
  1596.         $$->code+= "\tDEC "+$1->address+"\n";
  1597.         $$->address = $1->address;
  1598.         $$->arr_type = $1->arr_type;
  1599.  
  1600.         fprintf(logfile,"\n%s\n",$$->code.c_str());
  1601.     }
  1602.     ;
  1603.    
  1604. argument_list : arguments                  
  1605.                 {  
  1606.                     fprintf(logfile,"At line no: %d argument_list : arguments\n\n",yylineno);
  1607.                     fprintf(logfile,"%s\n\n",$1->name.c_str());
  1608.                     $$ = new node();
  1609.                     $$->name = $1->name;
  1610.                     $$->code = $1->code;
  1611.                     fprintf(logfile,"\n%s\n",$$->code.c_str());
  1612.                 }
  1613.               |                        
  1614.               {
  1615.                 $$ = new node();
  1616.                   fprintf(logfile,"At line no: %d argument_list : emptylist\n\n",yylineno);
  1617.             }
  1618.               ;
  1619.    
  1620. arguments : arguments COMMA logic_expression           
  1621.             {
  1622.                 fprintf(logfile,"At line no: %d arguments : arguments COMMA logic_expression\n\n",yylineno);
  1623.                 fprintf(logfile,"%s,%s\n\n",$1->name.c_str(),$3->name.c_str());
  1624.                 $$ = new node();
  1625.                 $$->name = $1->name+","+$3->name;
  1626.  
  1627.                 arglist.push_back($3->var_type);
  1628.                 arglistID.push_back($3->name);
  1629.                 asm_arg.push_back($3->address);
  1630.  
  1631.                 $$->code = $1->code + $3->code;
  1632.             }
  1633.           | logic_expression                       
  1634.           {
  1635.             fprintf(logfile,"At line no: %d arguments : logic_expression\n\n",yylineno);
  1636.             fprintf(logfile,"%s\n\n",$1->name.c_str());
  1637.             $$ = new node();             
  1638.             $$->name = $1->name;
  1639.  
  1640.            
  1641.  
  1642.             arglist.push_back($1->var_type);
  1643.             arglistID.push_back($1->name);
  1644.             asm_arg.push_back($1->address);
  1645.  
  1646.             $$->code = $1->code;
  1647.  
  1648.         }
  1649.           ;
  1650.  
  1651.  
  1652. %%
  1653.  
  1654.  
  1655. int main(int argc,char *argv[])
  1656. {
  1657.  
  1658.     if((inputfile=fopen(argv[1],"r"))==NULL)
  1659.     {
  1660.         printf("Cannot Open Input File.\n");
  1661.         exit(1);
  1662.     }
  1663.  
  1664.     logfile= fopen(argv[2],"w");
  1665.     fclose(logfile);
  1666.     errorfile= fopen(argv[3],"w");
  1667.     fclose(errorfile);
  1668.    
  1669.     logfile= fopen(argv[2],"a");
  1670.     errorfile= fopen(argv[3],"a");
  1671.     //printf("Hello");
  1672.  
  1673.     yyin=inputfile;
  1674.  
  1675.     table = new SymbolTable(10,logfile);
  1676.     yyparse();
  1677.    
  1678.     fprintf(errorfile,"\nTotal error : %d\n\n",semerror);
  1679.     fclose(logfile);
  1680.     fclose(errorfile);
  1681.    
  1682.     return 0;
  1683. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement