muhammad_nasif

parser.y

Jun 6th, 2021
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 34.22 KB | None | 0 0
  1. %{
  2. #include<iostream>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<cmath>
  6. #include<bits/stdc++.h>
  7. #include "symbolTable.cpp"
  8. //#define YYSTYPE SymbolInfo*
  9.  
  10. using namespace std;
  11.  
  12. int yyparse(void);
  13. int yylex(void);
  14. extern int line_count;
  15. extern FILE *yyin;
  16. FILE *output;
  17. SymbolTable table(30);
  18. ofstream out("output.txt");
  19. ofstream outxx("output_test.txt");
  20. int counter = 0;            ///counts the total variables as the rules detect them... used in function_scopeTable_adjustment()
  21.  
  22. vector<SymbolInfo*> names;      ///stores all the names of the variables and functions
  23. vector<SymbolInfo*> parameter;      ///stores the parameters of a function
  24. vector<string>  parameter_type;     ///stores the parameter types of a function. Runs parallelly with vector<SymbolInfo*> parameter
  25.  
  26. void yyerror(char *s)
  27. {
  28.     //cout<<"At line no: "<<line_count<<s<<endl;
  29. }
  30.  
  31.  
  32. void printVector(){
  33.    
  34. }
  35.  
  36.  
  37. void function_scopeTable_adjustment(){
  38.     int len = names.size();
  39.     int tempCtr = 0;
  40.     cout<<"----------------------------"<<endl;
  41.     cout<<"counter: "<<counter<<endl;
  42.     cout<<"len: "<<len<<endl;
  43.    
  44.     for(int i=counter;i<len;i++){
  45.         cout<<names[i]->getName()<<" "<<names[i]->getType()<<endl;
  46.         table.Insert(names[i]->getName(), names[i]->getType());
  47.         cout<<names[i]->getName()<<" "<<names[i]->getType()<<endl;
  48.         tempCtr++;
  49.     }
  50.     counter += tempCtr;
  51.     table.printAllScope(out);
  52. }
  53.  
  54.  
  55. void parameter_to_scopeTable(){
  56.     int len = parameter.size();
  57.     for(int i=0;i<len;i++){
  58.         table.Insert(parameter[i]->getName(), parameter[i]->getType());
  59.     }
  60.     parameter.clear();
  61.     parameter_type.clear();
  62. }
  63.  
  64.  
  65.  
  66. %}
  67.  
  68. %union{
  69.     SymbolInfo *symbol;
  70. }
  71.  
  72. %token  LPAREN RPAREN COMMA SEMICOLON ASSIGNOP LCURL RCURL NOT INCOP DECOP  LTHIRD RTHIRD WHILE IF PRINTLN ELSE FOR VOID INT FLOAT RETURN CONTINUE BREAK CHAR DO NEWLINE
  73.  
  74. %token <symbol> CONST_FLOAT CONST_INT RELOP LOGICOP ADDOP ID MULOP
  75.  
  76. %type<symbol>start program unit func_declaration parameter_list compound_statement var_declaration type_specifier declaration_list statements statement expression_statement variable expression  logic_expression rel_expression simple_expression term unary_expression factor argument_list arguments func_definition
  77.  
  78. //%left
  79. //%right
  80.  
  81. %nonassoc LOWER_THAN_ELSE
  82. %nonassoc ELSE
  83.  
  84.  
  85. %%
  86.  
  87. start : program
  88.     {
  89.         //write your code in this block in all the similar blocks below
  90.         //cout<<"At line no: "<<line_count<<" start : program\n"<<endl<<endl;
  91.         out<<"At line no: "<<line_count<<" start : program\n"<<endl<<endl;
  92.        
  93.         SymbolInfo *x = new SymbolInfo($1->getName(),"program");
  94.         $$ = x;
  95.         //cout<< $$->getName() <<endl;
  96.         out<< $$->getName() <<endl;
  97.        
  98.        
  99.         cout<<"-----------\n\n\nVector_Names_List:"<<endl;
  100.         int len = names.size();
  101.         for(int i=0;i<len;i++){
  102.             cout<<names[i]->getName()<<" ";
  103.             if(names[i]->getArrLen()!=0){
  104.                 cout<<" "<<names[i]->getArrLen();
  105.             }
  106.             cout<<endl;
  107.         }
  108.         cout<<"-----------\n\n\n";
  109.         table.printAllScope(out);
  110.        
  111.     }
  112.     ;
  113.  
  114. program : program unit
  115.     {
  116.         //cout<<"At line no: "<<line_count<<" program : program unit"<<endl<<endl;
  117.         out<<"At line no: "<<line_count<<" program : program unit"<<endl<<endl;
  118.         SymbolInfo *x = new SymbolInfo($1->getName()+$2->getName(),"program");
  119.         $$ = x;
  120.         //cout<< $$->getName() <<endl<<endl;
  121.         out<< $$->getName() <<endl<<endl;      
  122.     }
  123.     | unit
  124.     {
  125.         //cout<<"At line no: "<<line_count<<" program : unit"<<endl<<endl;
  126.         out<<"At line no: "<<line_count<<" program : unit"<<endl<<endl;
  127.         SymbolInfo *x = new SymbolInfo($1->getName(),"program");
  128.         $$=x;
  129.         //cout<<$$->getName()<<endl<<endl;
  130.         out<<$$->getName()<<endl<<endl;
  131.     }
  132.     ;
  133.    
  134. unit : var_declaration
  135.     {
  136.         //cout<<"At line no: "<<line_count<<" unit : var_declaration"<<endl<<endl;
  137.         out<<"At line no: "<<line_count<<" unit : var_declaration"<<endl<<endl;
  138.         SymbolInfo *x = new SymbolInfo($1->getName(),"unit");
  139.         $$=x;
  140.         //cout<<$$->getName()<<endl<<endl;
  141.         out<<$$->getName()<<endl<<endl;
  142.     }
  143.      | func_declaration
  144.      {
  145.         //cout<<"At line no: "<<line_count<<" unit : func_declaration"<<endl<<endl;
  146.         out<<"At line no: "<<line_count<<" unit : func_declaration"<<endl<<endl;
  147.        
  148.         SymbolInfo *x = new SymbolInfo($1->getName(),"unit");
  149.     $$=x;
  150.     //cout<<$$->getName()<<endl<<endl;
  151.     out<<$$->getName()<<endl<<endl;
  152.      }
  153.      | func_definition
  154.      {
  155.         //cout<<"unit : func_definition"<<endl<<endl;
  156.     out<<"unit : func_definition"<<endl<<endl;      
  157.         SymbolInfo *x = new SymbolInfo($1->getName(),"unit");
  158.     $$=x;
  159.     //cout<<$$->getName()<<endl<<endl;
  160.         out<<$$->getName()<<endl<<endl;
  161.      }
  162.      ;
  163.      
  164. func_declaration : type_specifier ID LPAREN parameter_list RPAREN SEMICOLON
  165.         {
  166.             //cout<<"At line no: "<<line_count<<" func_declaration : type_specifier ID LPAREN parameter_list RPAREN SEMICOLON"<<endl<<endl;
  167.             out<<"At line no: "<<line_count<<" func_declaration : type_specifier ID LPAREN parameter_list RPAREN SEMICOLON"<<endl<<endl;
  168.             SymbolInfo *x = new SymbolInfo($1->getName()+$2->getName()+"("+$4->getName()+")"+";\n","func_declaration");
  169.             $$=x;
  170.             //cout<<$$->getName()<<endl<<endl;
  171.             out<<$$->getName()<<endl<<endl;
  172.            
  173.             //-----------int function_name(int x, double b, char c);
  174.             //-----------store function name in global scope and variables in local scope
  175.            
  176.             //inserted function_name in scopeTable
  177.             $2->func_param = parameter;
  178.             $2->func_param_type = parameter_type;
  179.             names.push_back($2);
  180.             function_scopeTable_adjustment();
  181.            
  182.             //inserted function parameter in scopeTable inside new Scope
  183.             table.EnterScope();
  184.             //setFuncParameter($2);
  185.             parameter_to_scopeTable();
  186.             table.ExitScope();
  187.            
  188.            
  189.            
  190.         }
  191.         | type_specifier ID LPAREN RPAREN SEMICOLON
  192.         {
  193.             //cout<<"At line no: "<<line_count<<" func_declaration : type_specifier ID LPAREN RPAREN SEMICOLON"<<endl<<endl;
  194.             out<<"At line no: "<<line_count<<" func_declaration : type_specifier ID LPAREN RPAREN SEMICOLON"<<endl<<endl;
  195.             SymbolInfo *x = new SymbolInfo($1->getName()+$2->getName()+"("+")"+";\n","func_declaration");
  196.             $$=x;
  197.             //cout<<$$->getName()<<endl<<endl;
  198.             out<<$$->getName()<<endl<<endl;
  199.        
  200.             //-------int bubble_sort();
  201.             //-------store function name in global scope
  202.            
  203.            
  204.            
  205.            
  206.  
  207.            
  208.            
  209.            
  210.            
  211.             names.push_back($2);
  212.             function_scopeTable_adjustment();
  213.             table.EnterScope();
  214.             table.ExitScope();
  215.            
  216.        
  217.        
  218.         }
  219.         ;
  220.          
  221. func_definition : type_specifier ID LPAREN parameter_list RPAREN compound_statement
  222.         {
  223.        
  224.             //cout<<"At line no: "<<line_count<<" func_definition : type_specifier ID LPAREN parameter_list RPAREN compound_statement"<<endl<<endl;
  225.             out<<"At line no: "<<line_count<<" func_definition : type_specifier ID LPAREN parameter_list RPAREN compound_statement"<<endl<<endl;
  226.             SymbolInfo *x = new SymbolInfo($1->getName()+$2->getName()+"("+$4->getName()+")"+$6->getName(),"func_definition");
  227.             $$=x;
  228.             //cout<<$$->getName()<<endl<<endl;
  229.             out<<$$->getName()<<endl<<endl;
  230.            
  231.             //---------------int function_name(double a, double b){ .. }
  232.             //---------------insert function_name in global scope and function_variables in local scope
  233.            
  234.            
  235.  
  236.            
  237.             //----search if the function exists
  238.             SymbolInfo *search = table.LookUp($2->getName());
  239.            
  240.            
  241.            
  242.            
  243.            
  244.            
  245.            
  246.            
  247.            
  248.             if(search){
  249.                 //the function is found... hence inside if
  250.                 //if function_name is declared before
  251.                 //then no need to insert it into symbolTable
  252.                
  253.                
  254.                
  255.                 table.EnterScope();
  256.                 table.ExitScope();
  257.                
  258.                 cout<<"$2 for : "<< $2->getName() <<endl;
  259.                 cout<<"$2 size: "<< parameter.size() <<endl;
  260.                 cout<<"$2 name: "<< $2->getName() <<endl;
  261.                 cout<<"$2 type: "<< $2->getType() <<endl;
  262.                
  263.                
  264.                 cout<<"Function declared before 1st"<<endl;
  265.                 cout<<"parameter size: "<< search->func_param.size() <<endl;
  266.                 cout<<"search name: "<< search->getName() <<endl;
  267.                 cout<<"search type: "<< search->getType() <<endl;
  268.                
  269.                 ///check if the previous declared function and this function
  270.                 ///has same number of parameters
  271.                 if(parameter.size() != search->func_param.size()){
  272.                     cout<<"Parameters did not match"<<endl;
  273.                 }
  274.                 else{
  275.                 ///number of parameters has matched and now
  276.                 ///we have to check if the parameter types
  277.                 ///are equal
  278.                 int len = search->func_param.size();
  279.                 for(int i=0;i<len;i++){
  280.                
  281.                     string type_1 = search->func_param_type[i];
  282.                     string type_2 = parameter_type[i];
  283.                     cout<<"Type-1: "<<type_1<<endl;
  284.                     cout<<"Type-2: "<<type_2<<endl;
  285.                    
  286.                     if( type_1.compare(type_2) !=0  ){
  287.                         //if the function parameter data types are not equal. Then comes here
  288.                         cout<<"Different parameter type error"<<endl;
  289.                         cout<<search->func_param_type[i]<<endl;
  290.                         cout<<parameter_type[i]<<endl;
  291.                         break;
  292.                     }
  293.                     else{
  294.                         //if the function parameter data types are equal. Then comes here
  295.                         cout<<"Parameter Type equal "<<parameter[i]<<endl;
  296.                     }
  297.                 }
  298.                
  299.                
  300.                
  301.                
  302.                 }
  303.             }
  304.             else{
  305.            
  306.                 //function_name not declared. Hence pushing it into scopeTable
  307.                
  308.            
  309.                 //---------------inserted function_name in global_scope directly
  310.                
  311.                 //$2->func_param = parameter;
  312.                
  313.                 //$2->setReturnType($1->getName());         //set return type of function_name. Getting return type from type_specifier($1)
  314.                 table.Insert($2->getName(), $2->getType(), parameter, $1->getName(), parameter_type);
  315.                 table.EnterScope();
  316.                
  317.                 parameter_to_scopeTable();
  318.                
  319.                 function_scopeTable_adjustment();
  320.                 table.ExitScope(); 
  321.                
  322.                 cout<<"Name: "<<$2->getName() <<endl;
  323.                 cout<<"Param_Size: "<<$2->func_param.size()<<endl;             
  324.                
  325.             }
  326.            
  327.            
  328.            
  329.            
  330.            
  331.            
  332.            
  333.            
  334.         }
  335.         | type_specifier ID LPAREN RPAREN compound_statement
  336.         {
  337.             //cout<<"At line no: "<<line_count<<" func_definition : type_specifier ID LPAREN RPAREN compound_statement"<<endl<<endl;
  338.             out<<"At line no: "<<line_count<<" func_definition : type_specifier ID LPAREN RPAREN compound_statement"<<endl<<endl;
  339.             SymbolInfo *x = new SymbolInfo($1->getName()+$2->getName()+"("+")"+$5->getName(),"func_definition");
  340.             $$=x;
  341.             //cout<<$$->getName()<<endl<<endl;         
  342.             out<<$$->getName()<<endl<<endl;
  343.            
  344.            
  345.            
  346.             //---------------int function_name(){ .. }
  347.             //---------------insert function_name in global scope and function_variables in local scope
  348.             //---------------inserted function_name directly in global scope
  349.            
  350.            
  351.            
  352.             //----search if the function exists
  353.             SymbolInfo *search = table.LookUp($2->getName());
  354.            
  355.             if(search){
  356.                 //if function_name is declared before
  357.                 //then no need to insert it into symbolTable
  358.                
  359.                 table.EnterScope();
  360.                 table.ExitScope();
  361.                
  362.                 cout<<"$2 for : "<< $2->getName() <<endl;
  363.                 cout<<"$2 size: "<< $2->func_param.size() <<endl;
  364.                 cout<<"$2 name: "<< $2->getName() <<endl;
  365.                 cout<<"$2 type: "<< $2->getType() <<endl;
  366.                
  367.                 cout<<"Function declared before 2nd"<<endl;
  368.                
  369.                 cout<<"search size: "<< search->func_param.size() <<endl;
  370.                 cout<<"search name: "<< search->getName() <<endl;
  371.                 cout<<"search type: "<< search->getType() <<endl;
  372.                                
  373.             }
  374.             else{
  375.            
  376.                 //function_name not declared. Hence pushing it into scopeTable
  377.                
  378.            
  379.                 //---------------inserted function_name in global_scope directly
  380.                 //names.push_back($2);
  381.                 vector<SymbolInfo*> dummy;
  382.                 vector<string> dummy_str;
  383.                 table.Insert($2->getName(), $2->getType(), dummy, $1->getName(), dummy_str);
  384.                
  385.                
  386.                
  387.                 //names.push_back($2);
  388.                 table.EnterScope();
  389.                 function_scopeTable_adjustment();
  390.                 table.ExitScope(); 
  391.             }
  392.            
  393.            
  394.            
  395.            
  396.            
  397.            
  398.            
  399.  
  400.         }
  401.         ;              
  402.  
  403.  
  404. parameter_list  : parameter_list COMMA type_specifier ID
  405.         {
  406.             //cout<<"At line no: "<<line_count<<" parameter_list : pararmeter_list COMMA  type_specifier ID"<<endl<<endl;
  407.             out<<"At line no: "<<line_count<<" parameter_list : pararmeter_list COMMA  type_specifier ID"<<endl<<endl;
  408.             SymbolInfo *x = new SymbolInfo($1->getName()+","+$3->getName()+$4->getName(),"parameter_list");
  409.             $$ = x;
  410.             //cout<<$$->getName()<<endl<<endl;
  411.             out<<$$->getName()<<endl<<endl;
  412.            
  413.            
  414.             ///------ insert parameter in variable container (vector)-> parameter
  415.             parameter.push_back($4);
  416.             parameter_type.push_back($3->getName());
  417.            
  418.         }
  419.         | parameter_list COMMA type_specifier
  420.         {
  421.             //cout<<"At line no: "<<line_count<<" parameter_list : pararmeter_list COMMA  type_specifier"<<endl<<endl;
  422.             out<<"At line no: "<<line_count<<" parameter_list : pararmeter_list COMMA  type_specifier"<<endl<<endl;
  423.             SymbolInfo *x = new SymbolInfo($1->getName()+","+$3->getName(),"parameter_list");
  424.             $$ = x;
  425.             //cout<<$$->getName()<<endl;
  426.             out<<$$->getName()<<endl;
  427.            
  428.            
  429.         }
  430.         | type_specifier ID
  431.         {
  432.             //cout<<"At line no: "<<line_count<<" parameter_list : type_specifier ID"<<endl<<endl;
  433.             out<<"At line no: "<<line_count<<" parameter_list : type_specifier ID"<<endl<<endl;
  434.             SymbolInfo *x = new SymbolInfo($1->getName()+$2->getName(),"parameter_list");
  435.             $$ = x;
  436.             //cout<<$$->getName()<<endl<<endl;
  437.             out<<$$->getName()<<endl<<endl;
  438.            
  439.             ///------ insert parameter in variable container (vector)-> parameter
  440.             parameter.push_back($2);
  441.             parameter_type.push_back($1->getName());
  442.            
  443.         }
  444.         | type_specifier
  445.         {
  446.             //cout<<"At line no: "<<line_count<<" parameter_list : type_specifier"<<endl<<endl;
  447.             out<<"At line no: "<<line_count<<" parameter_list : type_specifier"<<endl<<endl;
  448.             SymbolInfo *x = new SymbolInfo($1->getName(),"parameter_list");
  449.             $$ = x;
  450.             //cout<<"At line no: "<<line_count<< $$->getName()<<endl;
  451.             out<<"At line no: "<<line_count<< $$->getName()<<endl;
  452.         }
  453.         ;
  454.  
  455.        
  456. compound_statement : LCURL statements RCURL
  457.             {
  458.                 //cout<<"At line no: "<<line_count<<" compound_statement : LCURL statements RCURL"<<endl<<endl;
  459.                 out<<"At line no: "<<line_count<<" compound_statement : LCURL statements RCURL"<<endl<<endl;
  460.                 SymbolInfo *x = new SymbolInfo("{\n"+$2->getName()+"}\n","compound_statement");
  461.             $$ = x;
  462.             //cout<< $$->getName()<<endl;
  463.             out<< $$->getName()<<endl;
  464.             }
  465.             | LCURL RCURL
  466.             {
  467.                 //cout<<"At line no: "<<line_count<<" compound_statement : LCURL RCURL"<<endl<<endl;
  468.                 out<<"At line no: "<<line_count<<" compound_statement : LCURL RCURL"<<endl<<endl;
  469.                 SymbolInfo *x = new SymbolInfo(string("{ }\n"),"compound_statement");
  470.             $$ = x;
  471.             //cout<<"At line no: "<<line_count<< $$->getName()<<endl;
  472.             out<<"At line no: "<<line_count<< $$->getName()<<endl;
  473.             }
  474.             ;
  475.            
  476. var_declaration : type_specifier declaration_list SEMICOLON
  477.         {
  478.             //cout<<"At line no: "<<line_count<<" var_declaration : type_specifier declaration_list SEMICOLON"<<endl<<endl;
  479.             out<<"At line no: "<<line_count<<" var_declaration : type_specifier declaration_list SEMICOLON"<<endl<<endl;
  480.             SymbolInfo *x = new SymbolInfo($1->getName()+$2->getName()+";\n","var_declaration");
  481.             $$=x;
  482.             //cout<<$$->getName()<<endl<<endl;
  483.             out<<$$->getName()<<endl<<endl;
  484.         }
  485.          ;
  486.          
  487. type_specifier  : INT  
  488.         {
  489.             //cout<<"At line no: "<<line_count<<" type_specifier : INT"<<endl<<endl;
  490.             out<<"At line no: "<<line_count<<" type_specifier : INT"<<endl<<endl;
  491.             SymbolInfo *x = new SymbolInfo("int ","type_specifier");
  492.             $$=x;
  493.             //cout<<$$->getName()<<endl<<endl;
  494.             out<<$$->getName()<<endl<<endl;
  495.            
  496.         }
  497.         | FLOAT
  498.         {
  499.             //cout<<"At line no: "<<line_count<<" type_specifier : FLOAT"<<endl<<endl;
  500.             out<<"At line no: "<<line_count<<" type_specifier : FLOAT"<<endl<<endl;
  501.             SymbolInfo *x = new SymbolInfo("float ","type_specifier");
  502.             $$=x;
  503.             //cout<<$$->getName()<<endl<<endl;
  504.             out<<$$->getName()<<endl<<endl;
  505.         }
  506.        
  507.        
  508.         | VOID 
  509.         {
  510.             //cout<<"At line no: "<<line_count<<" type_specifier : VOID"<<endl<<endl;
  511.             out<<"At line no: "<<line_count<<" type_specifier : VOID"<<endl<<endl;
  512.             SymbolInfo *x = new SymbolInfo("void ","type_specifier");
  513.             $$=x;
  514.             //cout<<$$->getName()<<endl<<endl;         
  515.             out<<$$->getName()<<endl<<endl;        
  516.         }
  517.         ;
  518.        
  519. declaration_list : declaration_list COMMA ID
  520.           {
  521.             //------------declaration like
  522.             //------------x,y
  523.            
  524.            
  525.             //cout<<"At line no: "<<line_count<<" declaration_list : declaration_list COMMA ID"<<endl<<endl;
  526.             out<<"At line no: "<<line_count<<" declaration_list : declaration_list COMMA ID"<<endl<<endl;
  527.             SymbolInfo *x = new SymbolInfo($1->getName()+","+$3->getName(),"declaration_list");
  528.             $$ = x;
  529.             //cout<<$$->getName()<<endl<<endl;
  530.             out<<$$->getName()<<endl<<endl;
  531.            
  532.            
  533.             //-----have to insert into scopeTable
  534.             cout<<"Rule_1\n";
  535.             names.push_back($3);
  536.            
  537.           }
  538.           | declaration_list COMMA ID LTHIRD CONST_INT RTHIRD
  539.           {
  540.            
  541.            
  542.             // ----------- this section is for declartion like
  543.             //------------- int y,x[4]
  544.             //------------- that means array declaration           
  545.            
  546.             //cout<<"At line no: "<<line_count<<" declaration_list : declaration_list COMMA ID LTHIRD CONST_INT RTHIRD"<<endl;
  547.             out<<"At line no: "<<line_count<<" declaration_list : declaration_list COMMA ID LTHIRD CONST_INT RTHIRD"<<endl;
  548.             SymbolInfo *x = new SymbolInfo($1->getName()+","+$3->getName()+"["+$5->getName()+"]","declaration_list");
  549.             $$ = x;
  550.             //cout<<$$->getName()<<endl<<endl;
  551.             out<<$$->getName()<<endl<<endl;
  552.            
  553.            
  554.            
  555.             //-----have to insert into scopeTable
  556.             cout<<"Rule_2\n";
  557.             $3->setArrLen(stoi($5->getName()));
  558.             names.push_back($3);
  559.            
  560.            
  561.            
  562.           }
  563.           | ID
  564.           {
  565.             //cout<<"At line no: "<<line_count<<" declaration_list : ID"<<endl<<endl;
  566.             out<<"At line no: "<<line_count<<" declaration_list : ID"<<endl<<endl;
  567.             out<<"ID "<<$1->getName()<<" found"<<endl;
  568.            
  569.             //cout<<$1->getName()<<endl<<endl;
  570.             out<<$1->getName()<<endl<<endl;
  571.          
  572.            
  573.             SymbolInfo *x = new SymbolInfo($1->getName(),"declaration_list");
  574.             $$=x;
  575.            
  576.            
  577.            
  578.            
  579.             //-----have to insert into scopeTable
  580.             cout<<"Rule_3\n";
  581.             names.push_back($1);
  582.            
  583.           }
  584.           | ID LTHIRD CONST_INT RTHIRD
  585.           //----------- x[5]
  586.           {
  587.             //cout<<"At line no: "<<line_count<<" declaration_list : ID LTHIRD CONST_INT RTHIRD"<<endl<<endl;
  588.             out<<"At line no: "<<line_count<<" declaration_list : ID LTHIRD CONST_INT RTHIRD"<<endl<<endl;
  589.            
  590.             SymbolInfo *x = new SymbolInfo($1->getName()+"["+$3->getName()+"]","declaration_list");
  591.             $$ = x;
  592.             //cout<<$$->getName()<<endl<<endl;
  593.             out<<$$->getName()<<endl<<endl;
  594.            
  595.            
  596.            
  597.            
  598.             //---------have to insert into scopeTable
  599.             cout<<"Rule_4\n";
  600.             $1->setArrLen(stoi($3->getName()));
  601.             names.push_back($1);
  602.            
  603.            
  604.           }
  605.           ;
  606.          
  607. statements : statement
  608.         {
  609.             //cout<<"At line no: "<<line_count<<" statements : statement"<<endl<<endl;
  610.             out<<"At line no: "<<line_count<<" statements : statement"<<endl<<endl;
  611.             SymbolInfo *x = new SymbolInfo($1->getName(),"statements");
  612.             $$=x;
  613.             //cout<<$$->getName()<<endl<<endl;
  614.             out<<$$->getName()<<endl<<endl;
  615.            
  616.         }
  617.        | statements statement
  618.        {
  619.         //cout<<"At line no: "<<line_count<<" statements : statements statement"<<endl<<endl;
  620.         out<<"At line no: "<<line_count<<" statements : statements statement"<<endl<<endl;
  621.         SymbolInfo *x = new SymbolInfo($1->getName()+$2->getName(),"statements");
  622.         $$=x;
  623.         //cout<<$$->getName()<<endl<<endl;
  624.         out<<$$->getName()<<endl<<endl;
  625.        
  626.        }
  627.        ;
  628.        
  629. statement : var_declaration
  630.       {
  631.         //cout<<"At line no: "<<line_count <<" statement : var_declaration"<<endl<<endl;
  632.         out<<"At line no: "<<line_count <<" statement : var_declaration"<<endl<<endl;
  633.         SymbolInfo *x = new SymbolInfo($1->getName(),"statement");
  634.         $$ = x;
  635.         //cout<<$$->getName()<<endl<<endl;
  636.         out<<$$->getName()<<endl<<endl;
  637.       }
  638.       | expression_statement
  639.       {
  640.         //cout<<"At line no: "<<line_count<<" statement : expression_statement"<<endl<<endl;
  641.         out<<"At line no: "<<line_count<<" statement : expression_statement"<<endl<<endl;
  642.         SymbolInfo *x = new SymbolInfo($1->getName(),"statement");
  643.         $$ = x;
  644.         //cout<<$$->getName()<<endl<<endl;
  645.         out<<$$->getName()<<endl<<endl;
  646.       }
  647.       | compound_statement
  648.       {
  649.         //cout<<"At line no: "<<line_count<<" statement : compound_statement"<<endl<<endl;
  650.         out<<"At line no: "<<line_count<<" statement : compound_statement"<<endl<<endl;
  651.         SymbolInfo *x = new SymbolInfo($1->getName(),"statement");
  652.         $$ = x;
  653.         //cout<<$$->getName()<<endl<<endl;
  654.         out<<$$->getName()<<endl<<endl;
  655.       }
  656.      
  657.       | FOR LPAREN expression_statement expression_statement expression RPAREN statement
  658.       {
  659.         //cout<<"At line no: "<<line_count<<" statement : FOR LPAREN expression_statement expression_statement expression RPAREN statement"<<endl<<endl;
  660.         out<<"At line no: "<<line_count<<" statement : FOR LPAREN expression_statement expression_statement expression RPAREN statement"<<endl<<endl;
  661.         SymbolInfo *x = new SymbolInfo(string("for")+"("+$3->getName()+$4->getName()+$5->getName()+")"+$7->getName(),"statement");
  662.         $$ = x;
  663.         //cout<<$$->getName()<<endl<<endl;
  664.         out<<$$->getName()<<endl<<endl;
  665.       }
  666.       | IF LPAREN expression RPAREN statement %prec LOWER_THAN_ELSE
  667.       {
  668.         //cout<<"At line no: "<<line_count<<" statement : IF LPAREN expression RPAREN statement"<<endl<<endl;
  669.         out<<"At line no: "<<line_count<<" statement : IF LPAREN expression RPAREN statement"<<endl<<endl;
  670.         SymbolInfo *x = new SymbolInfo(string("if")+"("+$3->getName()+")"+$5->getName(),"statement");
  671.         $$ = x;
  672.         //cout<<$$->getName()<<endl<<endl;
  673.         out<<$$->getName()<<endl<<endl;    
  674.       }
  675.       | IF LPAREN expression RPAREN statement ELSE statement
  676.       {
  677.         //cout<<"At line no: "<<line_count<<" statement : IF LPAREN expression RPAREN statement ELSE statement"<<endl<<endl;
  678.         out<<"At line no: "<<line_count<<" statement : IF LPAREN expression RPAREN statement ELSE statement"<<endl<<endl;
  679.         SymbolInfo *x = new SymbolInfo(string("if")+"("+$3->getName()+")"+$5->getName()+"else"+$7->getName(),"statement");
  680.         $$=x;
  681.         //cout<<$$->getName()<<endl<<endl;
  682.         out<<$$->getName()<<endl<<endl;
  683.       }
  684.       | WHILE LPAREN expression RPAREN statement
  685.       {
  686.         //cout<<"At line no: "<<line_count<<" statement : WHILE LPAREN expression RPAREN statement"<<endl<<endl;
  687.         out<<"At line no: "<<line_count<<" statement : WHILE LPAREN expression RPAREN statement"<<endl<<endl;
  688.         SymbolInfo *x = new SymbolInfo(string("while ")+"("+$3->getName()+")"+";\n","statement");
  689.         $$=x;
  690.         //cout<<$$->getName()<<endl<<endl;
  691.         out<<$$->getName()<<endl<<endl;
  692.       }
  693.       | PRINTLN LPAREN ID RPAREN SEMICOLON
  694.       {
  695.         //cout<<"At line no: "<<line_count<<" statement : PRINTLN LPAREN ID RPAREN SEMICOLON"<<endl;
  696.         out<<"At line no: "<<line_count<<" statement : PRINTLN LPAREN ID RPAREN SEMICOLON"<<endl;
  697.         SymbolInfo *x = new SymbolInfo(string("println ")+"("+$3->getName()+")"+";\n","statement");
  698.         $$=x;
  699.         //cout<<$$->getName()<<endl<<endl;
  700.         out<<$$->getName()<<endl<<endl;
  701.       }
  702.      
  703.       | RETURN expression SEMICOLON
  704.       {
  705.         //cout<<"At line no: "<<line_count<<" statement : RETURN expression SEMICOLON"<<endl<<endl;
  706.         out<<"At line no: "<<line_count<<" statement : RETURN expression SEMICOLON"<<endl<<endl;
  707.         SymbolInfo *x = new SymbolInfo(string("return ")+$2->getName()+";\n","statement");
  708.         $$ = x;
  709.         //cout<<$$->getName() <<endl;
  710.         out<<$$->getName() <<endl;
  711.                        
  712.       }
  713.       ;
  714.      
  715. expression_statement    : SEMICOLON    
  716.             {
  717.                 //cout<<"At line no: "<<line_count<<" expression_statement : SEMICOLON"<<endl<<endl;
  718.                 out<<"At line no: "<<line_count<<" expression_statement : SEMICOLON"<<endl<<endl;
  719.                 SymbolInfo *x = new SymbolInfo(";\n","expression_statement");
  720.                 $$ = x;
  721.                 //cout<<$$->getName()<<endl<<endl;
  722.                 out<<$$->getName()<<endl<<endl;
  723.             }  
  724.             | expression SEMICOLON
  725.             {
  726.                 //cout<<"At line no: "<<line_count<<" expression_statement : expression SEMICOLON"<<endl<<endl;
  727.                 out<<"At line no: "<<line_count<<" expression_statement : expression SEMICOLON"<<endl<<endl;
  728.                 SymbolInfo *x = new SymbolInfo($1->getName()+";\n","expression_statement");
  729.                 $$ = x;
  730.                 //cout<<$$->getName()<<endl<<endl; 
  731.                 out<<$$->getName()<<endl<<endl;
  732.             }  
  733.             ;
  734.      
  735. variable : ID
  736.     {
  737.         //cout<<"At line no: "<<line_count<< " variable : ID"<<endl<<endl;
  738.         out<<"At line no: "<<line_count<< " variable : ID"<<endl<<endl;
  739.         SymbolInfo *x = new SymbolInfo($1->getName(),"int");
  740.         $$ = x;
  741.         //cout<<$$->getName()<<endl<<endl;
  742.         out<<$$->getName()<<endl<<endl;
  743.        
  744.     }
  745.      | ID LTHIRD expression RTHIRD
  746.      {
  747.         //cout<<"At line no: "<<line_count<<" variable : ID LTHIRD expression RTHIRD"<<endl<<endl;
  748.         out<<"At line no: "<<line_count<<" variable : ID LTHIRD expression RTHIRD"<<endl<<endl;
  749.         SymbolInfo *x = new SymbolInfo($1->getName()+ "["+ $3->getName()+"]","rel_expression");
  750.         $$ = x;
  751.         //cout<<$$->getName()<<endl<<endl;
  752.         out<<$$->getName()<<endl<<endl;
  753.      }
  754.      ;
  755.      
  756.  expression : logic_expression
  757.         {
  758.             //cout<<"At line no: "<<line_count<< " expression : logic_expression"<<endl<<endl;
  759.             out<<"At line no: "<<line_count<< " expression : logic_expression"<<endl<<endl;
  760.             SymbolInfo *x = new SymbolInfo($1->getName(),"expression");
  761.             $$ = x;
  762.             //cout<<$$->getName()<<endl<<endl;
  763.             out<<$$->getName()<<endl<<endl;
  764.         }
  765.        | variable ASSIGNOP logic_expression
  766.        {
  767.         //cout<<"At line no: "<<line_count<<" expression : variable ASSIGNOP logic_expression" <<endl<<endl;
  768.         out<<"At line no: "<<line_count<<" expression : variable ASSIGNOP logic_expression" <<endl<<endl;
  769.         SymbolInfo *x = new SymbolInfo($1->getName()+"="+$3->getName(),"expression");
  770.         $$ = x;
  771.         //cout<<$$->getName()<<endl<<endl;
  772.         out<<$$->getName()<<endl<<endl;
  773.        }    
  774.        ;
  775.            
  776. logic_expression : rel_expression
  777.         {
  778.             //cout<<"At line no: "<<line_count << " logic_expression : rel_expression"<<endl<<endl;
  779.             out<<"At line no: "<<line_count << " logic_expression : rel_expression"<<endl<<endl;
  780.            
  781.             SymbolInfo *x = new SymbolInfo($1->getName(),"logic_expression");
  782.             $$ = x;
  783.             //cout<<$$->getName()<<endl<<endl;
  784.             out<<$$->getName()<<endl<<endl;
  785.            
  786.         }
  787.          | rel_expression LOGICOP rel_expression    
  788.         {
  789.             //cout<<"At line no: "<<line_count<<" logic_expression : rel_expression LOGICOP rel_expression"<<endl<<endl;
  790.             out<<"At line no: "<<line_count<<" logic_expression : rel_expression LOGICOP rel_expression"<<endl<<endl;
  791.             SymbolInfo *x = new SymbolInfo($1->getName()+$2->getName()+$3->getName(),"logic_expression");
  792.             $$ = x;
  793.             //cout<<$$->getName()<<endl<<endl;
  794.             out<<$$->getName()<<endl<<endl;        
  795.         }
  796.          ;
  797.            
  798. rel_expression  : simple_expression
  799.         {
  800.             //cout<<"At line no: "<<line_count<<" rel_expression : simple_expression"<<endl<<endl;
  801.             out<<"At line no: "<<line_count<<" rel_expression : simple_expression"<<endl<<endl;
  802.             SymbolInfo *x = new SymbolInfo($1->getName(),"rel_expression");
  803.             $$ = x;
  804.             //cout<<$$->getName()<<endl<<endl;
  805.             out<<$$->getName()<<endl<<endl;
  806.            
  807.         }
  808.         | simple_expression RELOP simple_expression
  809.         {
  810.             //cout<<"At line no: "<<line_count<<" rel_expression : simple_expression RELOP simple_expression"<<endl<<endl;
  811.             out<<"At line no: "<<line_count<<" rel_expression : simple_expression RELOP simple_expression"<<endl<<endl;
  812.             SymbolInfo *x = new SymbolInfo($1->getName()+ $2->getName()+ $3->getName(),"rel_expression");
  813.             $$ = x;
  814.             //cout<<$$->getName()<<endl<<endl;
  815.             out<<$$->getName()<<endl<<endl;
  816.         }
  817.         ;
  818.                
  819. simple_expression : term
  820.         {
  821.             //cout<<"At line no: "<<line_count<< " simple_expression : term"<<endl<<endl;
  822.             out<<"At line no: "<<line_count<< " simple_expression : term"<<endl<<endl;
  823.             SymbolInfo *x = new SymbolInfo($1->getName(),"simple_expression");
  824.             $$ = x;
  825.             //cout<<$$->getName()<<endl<<endl;
  826.             out<<$$->getName()<<endl<<endl;
  827.         }
  828.           | simple_expression ADDOP term
  829.         {
  830.             //cout<<"At line no: "<<line_count<< " simple_expression : simple_expression ADDOP term"<<endl<<endl;
  831.             out<<"At line no: "<<line_count<< " simple_expression : simple_expression ADDOP term"<<endl<<endl;
  832.             SymbolInfo *x = new SymbolInfo($1->getName()+$2->getName()+$3->getName(),"simple_expression");
  833.             $$ = x;
  834.             //cout<<$$->getName()<<endl<<endl;
  835.             out<<$$->getName()<<endl<<endl;
  836.            
  837.         }
  838.           ;
  839.                    
  840. term :  unary_expression
  841.     {
  842.         //cout<<"At line no: "<<line_count<<" term : unary_expression"<<endl<<endl;
  843.         out<<"At line no: "<<line_count<<" term : unary_expression"<<endl<<endl;
  844.         SymbolInfo *x = new SymbolInfo($1->getName(),"term");
  845.         $$ = x;
  846.         //cout<<$$->getName()<<endl<<endl;
  847.         out<<$$->getName()<<endl<<endl;
  848.     }
  849.      |  term MULOP unary_expression
  850.     {
  851.         //cout<<"At line no: "<<line_count<<" term : term MULOP unary_expression"<<endl<<endl;
  852.         out<<"At line no: "<<line_count<<" term : term MULOP unary_expression"<<endl<<endl;
  853.         SymbolInfo *x = new SymbolInfo($1->getName()+$2->getName()+$3->getName(),"term");
  854.         $$ = x;
  855.         //cout<<$$->getName()<<endl<<endl;
  856.         out<<$$->getName()<<endl<<endl;
  857.     }
  858.      ;
  859.  
  860. unary_expression : ADDOP unary_expression
  861.         {
  862.             //cout<<"At line no: "<<line_count<<" unary_expression : ADDOP unary_expression"<<endl;
  863.             out<<"At line no: "<<line_count<<" unary_expression : ADDOP unary_expression"<<endl;
  864.             SymbolInfo *x = new SymbolInfo($1->getName()+$2->getName(),"unary_expression");
  865.             $$ = x;
  866.             //cout<<$$->getName()<<endl<<endl;
  867.             out<<$$->getName()<<endl<<endl;
  868.            
  869.         }  
  870.          | NOT unary_expression
  871.         {
  872.             //cout<<"At line no: "<<line_count<<" unary_expression : NOT unary_expression"<<endl<<endl;
  873.             out<<"At line no: "<<line_count<<" unary_expression : NOT unary_expression"<<endl<<endl;
  874.             SymbolInfo *x = new SymbolInfo("!"+$2->getName(),"unary_expression");
  875.             $$ = x;
  876.             //cout<<$$->getName()<<endl<<endl;
  877.             out<<$$->getName()<<endl<<endl;
  878.         }
  879.          | factor
  880.         {
  881.             //cout<<"At line no: "<<line_count<<" unary_expression : factor"<<endl<<endl;
  882.             out<<"At line no: "<<line_count<<" unary_expression : factor"<<endl<<endl;
  883.             SymbolInfo *x = new SymbolInfo($1->getName(),"unary_expression");
  884.             $$ = x;
  885.             //cout<<$$->getName()<<endl<<endl;
  886.             out<<$$->getName()<<endl<<endl;
  887.         }
  888.          ;
  889.    
  890. factor  : variable
  891.     {
  892.         //cout<<"At line no: "<<line_count<<" factor : variable"<<endl<<endl;
  893.         out<<"At line no: "<<line_count<<" factor : variable"<<endl<<endl;
  894.         SymbolInfo *x = new SymbolInfo($1->getName(),"factor");
  895.         $$ = x;
  896.         //cout<<$$->getName()<<endl<<endl;
  897.         out<<$$->getName()<<endl<<endl;
  898.     }
  899.     | ID LPAREN argument_list RPAREN
  900.     {
  901.         //cout<<"At line no: "<<line_count<<" factor : ID LPAREN argument_list RPAREN"<<endl<<endl;
  902.         out<<"At line no: "<<line_count<<" factor : ID LPAREN argument_list RPAREN"<<endl<<endl;
  903.        
  904.         SymbolInfo *x = new SymbolInfo($1->getName()+"("+$3->getName()+")","factor");
  905.         $$ = x;
  906.         //cout<<$$->getName()<<endl<<endl;
  907.         out<<$$->getName()<<endl<<endl;
  908.     }
  909.     | LPAREN expression RPAREN
  910.     {
  911.         //cout<<"At line no: "<<line_count<<" factor : LPAREN expression RPAREN"<<endl;
  912.         out<<"At line no: "<<line_count<<" factor : LPAREN expression RPAREN"<<endl;
  913.         SymbolInfo *x = new SymbolInfo("("+$2->getName()+")","factor");
  914.         $$ = x;
  915.         //cout<<$$->getName()<<endl<<endl;
  916.         out<<$$->getName()<<endl<<endl;
  917.        
  918.     }
  919.     | CONST_INT
  920.     {
  921.         //cout<<"At line no: "<<line_count<<" factor : CONST_INT\n"<<endl;
  922.         out<<"At line no: "<<line_count<<" factor : CONST_INT\n"<<endl;
  923.         SymbolInfo *x = new SymbolInfo($1->getName(),"int");
  924.         $$ = x;
  925.         //cout<<$$->getName()<<endl<<endl;
  926.         out<<$$->getName()<<endl<<endl;
  927.     }
  928.     | CONST_FLOAT
  929.     {
  930.         //cout<<"At line no: "<<line_count<<" factor : CONST_FLOAT\n"<<endl<<endl;
  931.         out<<"At line no: "<<line_count<<" factor : CONST_FLOAT\n"<<endl<<endl;
  932.         SymbolInfo *x = new SymbolInfo($1->getName(),"float");
  933.         $$ = x;
  934.         //cout<<$$->getName()<<endl<<endl;
  935.         out<<$$->getName()<<endl<<endl;    
  936.     }
  937.     | variable INCOP
  938.     {
  939.         //cout<<"At line no: "<<line_count<<" factor : variable INCOP\n"<<endl;
  940.         out<<"At line no: "<<line_count<<" factor : variable INCOP\n"<<endl;
  941.         SymbolInfo *x = new SymbolInfo($1->getName() + "++","factor");
  942.         $$ = x;
  943.         //cout<<$$->getName()<<endl<<endl;
  944.         out<<$$->getName()<<endl<<endl;    
  945.     }
  946.     | variable DECOP
  947.     {
  948.         //cout<<"At line no: "<<line_count<<" factor : variable DECOP\n"<<endl<<endl;
  949.         out<<"At line no: "<<line_count<<" factor : variable DECOP\n"<<endl<<endl;
  950.         SymbolInfo *x = new SymbolInfo($1->getName()+"--","factor");
  951.         $$ = x;
  952.         //cout<<$$->getName()<<endl<<endl;
  953.         out<<$$->getName()<<endl<<endl;
  954.     }
  955.     ;
  956.    
  957. argument_list : arguments
  958.     {
  959.         //cout<<"At line no: "<<line_count<<" argument_list : arguments"<<endl<<endl;
  960.         out<<"At line no: "<<line_count<<" argument_list : arguments"<<endl<<endl;
  961.         SymbolInfo *x = new SymbolInfo($1->getName(),"argument_list");
  962.         $$ = x;
  963.         //cout<<$$->getName()<<endl<<endl;
  964.         out<<$$->getName()<<endl<<endl;
  965.    
  966.     }
  967.     |
  968.     {
  969.         SymbolInfo *argumentInfo = new SymbolInfo("","argument_list");
  970.         $$ = argumentInfo;
  971.         //cout<<"At line no: "<<line_count<<$$->getName()<<endl<<endl;
  972.         out<<"At line no: "<<line_count<<$$->getName()<<endl<<endl;
  973.         //cout<<$$->getName()<<endl<<endl;     
  974.         out<<$$->getName()<<endl<<endl;
  975.     }
  976.     ;
  977.    
  978. arguments : arguments COMMA logic_expression
  979.         {
  980.             //cout<<"At line no: "<<line_count<<" arguments : arguments COMMA logic_expression"<<endl<<endl;
  981.             out<<"At line no: "<<line_count<<" arguments : arguments COMMA logic_expression"<<endl<<endl;
  982.             SymbolInfo *x = new SymbolInfo($1->getName()+","+$3->getName(),"argument_list");
  983.             $$ = x;
  984.             //cout<<$$->getName()<<endl<<endl;
  985.             out<<$$->getName()<<endl<<endl;
  986.         }
  987.           | logic_expression
  988.           {
  989.                 //cout<<"At line no: "<<line_count<<" arguments : logic_expression"<<endl<<endl;
  990.                 out<<"At line no: "<<line_count<<" arguments : logic_expression"<<endl<<endl;
  991.                 SymbolInfo *x = new SymbolInfo($1->getName(),"argument_list");
  992.             $$ = x;
  993.             //cout<<$$->getName()<<endl<<endl;
  994.             out<<$$->getName()<<endl<<endl;
  995.            
  996.           }
  997.           ;
  998.  
  999.  
  1000. %%
  1001. int main(int argc,char *argv[])
  1002. {
  1003.     FILE  *fp, *fp2, *fp3;
  1004.  
  1005. //  if((fp=fopen(argv[1],"r"))==NULL)
  1006. //  {
  1007. //      printf("Cannot Open Input File.\n");
  1008. //      exit(1);
  1009. //  }
  1010.    
  1011. //  //cout<<"At line no: "<<line_count<<argv[2]<<endl;
  1012.    
  1013. //  fp2= fopen(argv[2],"w");
  1014. //  fclose(fp2);
  1015. //  fp3= fopen(argv[3],"w");
  1016. //  fclose(fp3);
  1017.    
  1018. //  fp2= fopen(argv[2],"a");
  1019. //  fp3= fopen(argv[3],"a");
  1020.    
  1021.     output = fopen("output_scope.txt","w");
  1022.     fclose(output);
  1023.     output = fopen("output_scope.txt","a");
  1024.    
  1025.    
  1026.     yyin=fp;
  1027.    
  1028.     yyin = fopen("input.txt","r");
  1029.    
  1030.     yyparse();
  1031.  
  1032. //  fclose(fp2);
  1033. //  fclose(fp3);
  1034.    
  1035.     return 0;
  1036. }
Advertisement
Add Comment
Please, Sign In to add comment