Advertisement
muhammad_nasif

Untitled

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