//File parser.cpp #include #include #include #include #include "parser.h" #include "TokFiler.h" #include "QuadList.h" #include "QuadFiler.h" #include "FileManager.h" #include "Token.h" #include "pmglobs.h" #include "intstack.h" #include "idDescTab.h" #include "funcTab.h" #include "Quad.h" void parser::distributeProgName(char* pName) { pf.storeFileName(pName); char *tokenPtr = new char[strlen(pName) + 1]; char *ptr1 = new char[strlen(pName) + 1]; char *quadPtr = new char[strlen(pName) + 1]; char *ptr2 = new char[strlen(pName) + 1]; strcpy(tokenPtr, pName); ptr1 = strtok(tokenPtr, "."); strcat(ptr1, ".tok"); tf.storeFileName(ptr1); strcpy(quadPtr, pName); ptr2 = strtok(quadPtr, "."); strcat(ptr2, ".quad"); qf.store(ptr2); cout << "in distributeProgramName, ptr2 == " << ptr2 << endl; } void parser::emitallQuads() { int i; Quadruple q; QuadList* p=qlist; int seg=0; while (p!=NULL) { for (i=0; iquadSeg[i].oper!=0) { q=p->quadSeg[i]; qf.emitQuad(q); } // end of if. } // end of for. p=p->next; seg++; } // end of while. } parser::parser() { s.push(bottommark); Quadnum=1; linenumber=0; level=0; vcntprime=0; varcnt=0; localtempsmax=0; varmax=BASE; startaddrforglobaltemps=BASE; startaddrforlocaltemps=0; qlist= new QuadList; null.setClass(RESERVED); //RESERVED==0 null.setValue(0); } void parser::error(unsigned int n,unsigned int line) { cout<<"\n\nERROR #"<enterQuad(Quadnum,q); Quadnum++; } void parser::createASSIGNQuad() { Token opnd1,opnd2; int type1,type2,level1,level2,val; attributes attribs2; bool func1,func2,declared; getopndinfo(opnd1,level1,type1,func1); if (opnd1.getClass()==IDENTIFIER && func1) //return from funccall error(84,linenumber); // is tempclass getopndinfo(opnd2,level2,type2,func2); if (level2==0 && func2) error(84,linenumber); if (type1!=type2) error(82,linenumber); if (level1==0 && opnd1.getClass()==IDENTIFIER) { val=opnd1.getValue()+LEVELOADDRSHIFT; opnd1.setValue(val); } if (level2==0 && opnd2.getClass()==IDENTIFIER) { val=opnd2.getValue()+LEVELOADDRSHIFT; opnd2.setValue(val); }; Quadruple q; q.setQuad(assignop,opnd1,null,opnd2.getValue()); qlist->enterQuad(Quadnum,q); Quadnum++; freetempaddrs(); } void parser::createARITHQuad(int oper) { Token opnd1,opnd2; int type1,type2,level1,level2,val; bool func1,func2; getopndinfo(opnd1,level1,type1,func1); if (opnd1.getClass()==IDENTIFIER && func1) error(84,linenumber); getopndinfo(opnd2,level2,type2,func2); if (opnd2.getClass()==IDENTIFIER && func2) error(84,linenumber); if (type1!=1 || type2!=1) //both opnds must be of type INTEGER error(83,linenumber); int tempaddr=alloctempaddr(); qs.push(tempaddr); //addr for temp already adjusted for level qs.push(1); //temp type is INTEGER qs.push(tempclass); if (level1==0 && opnd1.getClass()==IDENTIFIER) { val=opnd1.getValue()+LEVELOADDRSHIFT; opnd1.setValue(val); } if (level2==0 && opnd2.getClass()==IDENTIFIER) { val=opnd2.getValue()+LEVELOADDRSHIFT; opnd2.setValue(val); } Quadruple q; q.setQuad(oper,opnd1,opnd2,tempaddr); qlist->enterQuad(Quadnum,q); Quadnum++; freetempaddrs(); } void parser::createBOOLQuad() { Token opnd1,opnd2; int type1,type2,level1,level2,val; bool func1,func2; getopndinfo(opnd1,level1,type1,func1); if (opnd1.getClass()==IDENTIFIER && func1) error(84,linenumber); int Quadoper=qs.pop() ; getopndinfo(opnd2,level2,type2,func2); if (opnd2.getClass()==IDENTIFIER && func2) error(84,linenumber); if (type1!=1 || type2!=1) //both opnds must be of type INTEGER error(83,linenumber); int tempaddr=alloctempaddr(); qs.push(tempaddr); //addr for ternp already adjusted for level qs.push(2); //temp type is BOOLEAN qs.push(tempclass); if (level1==0 && opnd1.getClass()==IDENTIFIER) { val=opnd1.getValue()+LEVELOADDRSHIFT; opnd1.setValue(val); }; if (level2==0 && opnd2.getClass()==IDENTIFIER) { val=opnd2.getValue()+LEVELOADDRSHIFT; opnd2.setValue(val); }; Quadruple q; q.setQuad(Quadoper,opnd1,opnd2,tempaddr); qlist->enterQuad(Quadnum,q); Quadnum++; freetempaddrs(); } void parser::createNOTQuad() { Token opnd; int type,level1,val; bool func; int NOToper=qs.pop(); getopndinfo(opnd,level1,type,func); if (opnd.getClass()==IDENTIFIER && func) error(84,linenumber); if (type!=2) error(85,linenumber); int tempaddr=alloctempaddr(); qs.push(tempaddr); qs.push(2); qs.push(tempclass); if (level1==0 && opnd.getClass()==IDENTIFIER) { val=opnd.getValue()+LEVELOADDRSHIFT; opnd.setValue(val); }; Quadruple q; q.setQuad(NOToper,opnd,null,tempaddr); qlist->enterQuad(Quadnum,q); Quadnum++; freetempaddrs(); }; void parser::createODDQuad() { Token opnd; int type,level1,val; bool func; int ODDoper=qs.pop(); getopndinfo(opnd,level1,type,func); if (opnd.getClass()==IDENTIFIER && func) error(84,linenumber); if (type!=1) //argument of ODD must be of type INFEGER error(8l,linenumber); int tempaddr=alloctempaddr(); qs.push(tempaddr); qs.push(2); qs.push(tempclass); if (level1==0 && opnd.getClass()==IDENTIFIER) { val=opnd.getValue()+LEVELOADDRSHIFT; opnd.setValue(val); }; Quadruple q; q.setQuad(ODDoper,opnd,null,tempaddr); qlist->enterQuad(Quadnum,q); Quadnum++; freetempaddrs(); } void parser::createJUMPQuad(int oper) { Token opnd; int type,level1,val; bool func; Quadruple q; if (oper==BF) { getopndinfo(opnd,level1,type,func); if (opnd.getClass()==IDENTIFIER && func) error(84,linenumber); if (level1==0 && opnd.getClass()==IDENTIFIER) { val=opnd.getValue()+LEVELOADDRSHIFT; opnd.setValue(val); }; q.setQuad(BF,opnd,null,EMPTY); qlist->enterQuad(Quadnum,q); Quadnum++; } else { q.setQuad(B,null,null,EMPTY); qlist->enterQuad(Quadnum,q); Quadnum++; } } void parser::createfuncASPQuad() { //used only at beginning of func Token opnd; opnd.setClass(NUMBER); opnd.setValue(-localtempsmax); Quadruple q; q.setQuad(ASP,opnd,null,EMPTY); qlist->enterQuad(Quadnum,q); Quadnum++; } void parser::createfuncFSPQuad() { Token opnd; opnd.setClass(NUMBER); opnd.setValue(-localtempsmax); Quadruple q; q.setQuad(FSP,opnd,null,EMPTY); qlist->enterQuad(Quadnum,q); Quadnum++; } void parser::createRTNQuad() { Quadruple q; q.setQuad(RTN,null,null,EMPTY); qlist->enterQuad(Quadnum,q); Quadnum++; } void parser::createHALTQuad() { Quadruple q; q.setQuad(haltop,null,null,EMPTY); qlist->enterQuad(Quadnum,q); } void parser::savePROGinfo() { //cout << "In parser::savePROGinfo(), qs stack is: "; qs.display(); int temp=qs.pop(); if (temp==identclass) { int idinx=qs.pop(); bool duplicate=iddt.createEntry(idinx,level); if (duplicate) error(70,linenumber); //cannot have duplicate at this juncture iddt.saveLevel(idinx,0); iddt.saveAddr(idinx,NILADDR); iddt.saveType(idinx,0); } else cout<<"\nPROBLEM in savePROGinfo()!!!\n"; } void parser::saveVARinfo() { int ternp,addr; int type=qs.pop(); for (int k=vcnt+vcntprime; k>vcntprime; k--) { temp=qs.pop(); if (temp==IDENTIFIER) { int idinx=qs.pop(); bool duplicate=iddt.createEntry(idinx,level); if (duplicate) error(70,linenumber); iddt.saveLevel(idinx,level); iddt.saveType(idinx,type); if (level==1) { addr=-k; localmax--; } else if (level==0) { addr=k+BASE; } iddt.saveAddr(idinx,addr); } else cout<<"\nPROBLEM in saveVARinfo!!!\n"; } // end of for loop. vcntprime+=vcnt; vcnt=0; if (level==0) { varcnt=vcntprime; varmax=varcnt+BASE; startaddrforglobaltemps=varmax; } else if (level==1) { startaddrforlocaltemps=localmax; } } void parser::saveFUNCinfo() { int idinx; bool declared; int type=qs.pop(); /*int temPP=*/ qs.pop(); //throw away the class (i.e., identifier) int idinxp=qs.pop(); ft.createEntry(idinxp); ft.savePcnt(idinxp,pcnt); if (pcnt>10) error(100,linenumber); for (int i=i; i<=pcnt; i++) { temp=qs.pop(); idinx=qs.pop(); iddt.saveAddr(idinx,6+i); declared=iddt.retriveAttributes(idinx,attribs); if (!declared) error(71,linenumber); ft.saveType(idinxp,pcnt-i+1,attribs.type); } int Quadnumber=qs.pop(); ft.saveQnum(idinxp,Quadnumber); level=0; bool duplicate=iddt.createEntry(idinxp,level); if (duplicate) error(70,linenumber); iddt.saveLevel(idinxp,level); iddt.saveAddr(idinxp,NILADDR); //no level 0 addr for func iddt.saveType(idinxp,type); iddt.saveKind(idinxp,true); level=1; duplicate=iddt.createEntry(idinxp,level); if (duplicate) error(70,linenumber); iddt.saveLevel(idinxp,level); iddt.saveAddr(idinxp,6); iddt.saveType(idinxp,type); iddt.saveKind(idinxp,true); } void parser::FORMALparam() { int type=qs.pop(); temp=qs.pop(); int idinx=qs.pop(); int tempp=qs.pop(); int idinxp=qs.pop(); if (temp==IDENTIFIER && tempp==IDENTIFIER) { pcnt++; bool duplicate=iddt.createEntry(idinx,level); if (duplicate) error(70,linenumber); iddt.saveLevel(idinx,level); iddt.saveType(idinx,type); qs.push(idinx); qs.push(temp); qs.push(idinxp); qs.push(tempp); } else cout<<"\nPROBLEM in FORMALparam!!!\n"; } void parser::ACTUALparam() { Token opnd; int type1,type2,level1; bool func; getopndinfo(opnd,level1,type2,func); if (opnd.getClass()==IDENTIFIER && func) //return from funccall error(84,linenumber); //is teinpclass pcnt=qs.pop(); pcnt++; temp=qs.pop(); int idinx=qs.pop(); ft.retrieveFAttributes(idinx,fattribs); type1=fattribs.type[pcnt]; if (type1!=type2) error(87,linenumber); if (level1 == 0 && opnd.getClass() == IDENTIFIER) { int val=opnd.getValue()+LEVELOADDRSHIFT; opnd.setValue(val); } Quadruple q; q.setQuad(PSH,opnd,null,EMPTY); qlist->enterQuad(Quadnum,q); Quadnum++; qs.push(idinx); qs.push(temp) ; qs.push(pcnt); } void parser::Fcallret() { pcnt=qs.pop(); temp=qs.pop(); int idinx=qs.pop(); ft.retrieveFAttributes(idinx,fattribs); if (fattribs.pcnt!=pcnt) //checks for correct # of params error(86,linenumber); Token opnd2; opnd2.setClass(NUMBER); opnd2.setValue(1); Quadruple q; q.setQuad(ASP,opnd2,null,EMPTY); qlist->enterQuad(Quadnum,q); Quadnum++; q.setQuad(JSR,null,null,fattribs.qnum); qlist->enterQuad(Quadnum,q); Quadnum++; int tempaddr=alloctempaddr(); qs.push(tempaddr); iddt.retriveAttributes(idinx,attribs); //throw away boolean "declared" if (!attribs.func) error(88,linenumber); if (attribs.type==1) qs.push(1); //temp type is INTEGER else if (attribs.type==2) qs.push(2); //temp type is BOOLEAN qs.push(TEMP); q.setQuad(POP,null,null,tempaddr); qlist->enterQuad(Quadnum,q); Quadnum++; if (pcnt>0) { opnd2.setClass(NUMBER); opnd2.setValue(pcnt); q.setQuad(FSP,opnd2,null,EMPTY); qlist->enterQuad(Quadnum,q); Quadnum++; } } void parser::Tokensequence() { tf.retrieveTokenFile(); Token tok; intconst intok; int stacksymbol; int qnumsaved,idinx,intliteral; bool endparse=false; cout<<"\n"; s.push(program); tok=tf.getNextToken(); while (!endparse) { while (tok.getClass()==NEWLINE) { linenumber++; tok=tf.getNextToken(); } intok=getintok(tok); stacksymbol=s.pop() ; switch (stacksymbol) { //nonterminals case program:if (intok==PROGRAMrw) { // 1 s.push(haltop); s.push(period); s.push(ident); s.push(body); s.push(setlevel0); s.push(repair0); s.push(subprogs); s.push(decs); s.push(qnum); s.push(B); s.push(heading); cout << "case program, s stack ==\n"; s.display(); } else error(10,linenumber); break; case heading: if (intok==PROGRAMrw) {// 2 s.push(semicolon); s.push(saveproginfo); s.push(ident); cout << "case heading, s stack ==\n"; s.display(); tok=tf.getNextToken(); } else error(11,linenumber); break; case decs: switch (intok) { case DECLARErw: s.push(moredeclists); // 4 s.push(declist); s.push(initdecs); tok=tf.getNextToken(); break; case FUNCTIONrw: case BEGINrw: break;//do nothing 5 default: error(13,linenumber); } // end of local switch. break; case moredeclists:switch(intok) { case idTOK: s.push(moredeclists); //6 s.push(declist); break; case FUNCTIONrw: case BEGINrw: break;//do nothing 7 default: error(14,linenumber); } // end of local switch. break; case declist: if (intok==idTOK) { //8 s.push(savevarinfo); s.push(semicolon); s.push(type); s.push(colon); s.push(identlist); } else error(15,linenumber); break; case identlist: if (intok==idTOK) { // 9 s.push(moreidents); s.push(ident); s.push(incvcnt); } else error(16,linenumber); break; case moreidents:switch (intok) { case commaTOK: s.push(identlist); // 10 tok=tf.getNextToken(); break; case colonTOK: break;//do nothing default: error(17,linenumber); } // end of local switch. break; case type: switch (intok) { case INTEGERrw: s.push(integer); // 12 tok=tf.getNextToken(); break; case BOOLEANrw: s.push(booleanSEM); // 13 tok=tf.getNextToken(); break; default: error(18,linenumber); } // end of local switch. break; case funcs: switch (intok) { case FUNCTIONrw: s.push(funcs); // 14 s.push(semicolon); s.push(function); break; case BEGINrw: break;//do nothing is default: error(19,linenumber); } // end of local switch. break; case function: if (intok==FUNCTIONrw) { // 16 s.push(clearlevel1); s.push(RTN); s.push(FSP); s.push(adjustasp); s.push(body); s.push(ASP); s.push(qnum); s.push(decs); s.push(funcheading); } else error(20,linenumber); break; case funcheading:if (intok==FUNCTIONrw) { // 17 s.push(savefuncinfo); s.push(semicolon); s.push(type); s.push(colon); s.push(rparen); s.push(paramlist); s.push(ident); tok=tf.getNextToken(); } else error(2l,linenumber); break; case paramlist: switch (intok) { case idTOK: s.push(moreparams); // 18 s.push(formalparam); s.push(type); s.push(colon); s.push(ident); break; case rparenTOK: break;//do nothing 19 default: error(22,linenumber); } // end of local switch. break; case moreparams: switch (intok) { case semicolonTOK: s.push(moreparams); // 20 s.push(formalparam); s.push(type); s.push(colon); s.push(ident); tok=tf.getNextToken(); break; case rparenTOK: break;//do nothing 21 default: error(23,linenumber); } break; case body: if (intok==BEGINrw) { // 22 == new 17. s.push(end); s.push(semicolon); s.push(stmtlist); tok=tf.getNextToken(); } else error(24,linenumber); break; case stmtlist: switch (intok) { // 18 new case idTOK: // will add. break; case READrw: // will add. break; case WRITErw: s.push(rparen); //28 s.push(write); s.push(expr); s.push(lparen); tok=tf.getNextToken(); break; case WRITELNrw: // will add. break; case IFrw: // will add. break; case WHILErw: // will add. break; case ENDrw: // will add. break; case ELSErw: // will add. break; case semicolonTOK: s.push(morestmts); s.push(stmt); tok=tf.getNextToken(); cout << "case stmtlist:semicolon, intok == " << intok << " semicolonTOK == " << semicolonTOK << endl; cout << "case stmtlist:semicolon, intok == " << intok << " stmtlist == " << stmtlist << endl; cout << "case stmtlist:semicolon, s stack == " << endl; s.display(); break; default: error(241, linenumber); } break; case morestmts: switch (intok) { case semicolonTOK: s.push(morestmts); // 23 s.push(stmt); tok=tf.getNextToken(); break; case ENDrw: cout << "case morestmts:ENDrw, s stack ==\n"; s.display(); break;//do nothing 24 default: error(25,linenumber); } break; case stmt: cout << "stmt case, intok == " << intok << " stmt == " << stmt << "\n"; cout << "stmt case, intok == " << intok << " semicolon == " << semicolon << "\n"; switch (intok) { case BEGINrw: s.push(body); //25 break; case idTOK: s.push(assignop); //26 s.push(condition); s.push(assignment); s.push(ident); break; case GETrw: s.push(rparen); //27 s.push(get); s.push(ident); s.push(lparen); tok=tf.getNextToken(); break; case WRITErw: s.push(rparen); //28 s.push(write); s.push(expr); s.push(lparen); tok=tf.getNextToken(); break; case IFrw: s.push(repair0); //29 s.push(stmt); s.push(qnum); s.push(BF); s.push(then); s.push(condition); tok=tf.getNextToken(); break; case WHILErw: s.push(patch); s.push(B);// 30 s.push(repair1); s.push(stmt); s.push(qnum); s.push(BF); s.push(doSYN); s.push(condition); s.push(qnum); tok=tf.getNextToken(); break; case ENDrw: cout << "case stmt:ENDrw\n"; s.display(); // added for test. cout <<"case ENDrw in stmt, intok == " << intok << " ENDrw == " << ENDrw << endl; break; // do nothing. case semicolon: cout <<"case semicolon in stmt, intok == " << intok << " semicolon == " << semicolon << endl; break;//do nothing 31 default: error(26,linenumber); } break; case condition: switch (intok) { case NOTrw: case lparenTOK: case idTOK: // 32 case intTOK: case ODDrw: case FALSErw: case TRUErw: s.push(clist); s.push(expr); break; default: error(27,linenumber); } break; case clist: switch (intok) { case equalTOK: case notequalTOK: //33 case greaterTOK: case greatereqTOK: case lessTOK: case lesseqTOK: s.push(bool2); s.push(expr); s.push(relop); break; case semicolonTOK: case ENDrw: //34 case rparenTOK: case commaTOK: case THENrw: case DOrw: break;//do nothing default: error(28,linenumber); } break; case expr: switch (intok) { case NOTrw: case lparenTOK: case idTOK: // 35 case intTOK: case ODDrw: case FALSErw: case TRUErw: s.push(elist); s.push(term); break; default: error(29,linenumber); } break; case elist: switch (intok) { case plusTOK: s.push(elist); //36 s.push(addop); s.push(term); tok=tf.getNextToken(); break; case minusTOK: s.push(elist); //37 s.push(subop); s.push(term); tok=tf.getNextToken(); break; case semicolonTOK: case ENDrw: //38 case rparenTOK: case commaTOK: case equalTOK: case notequalTOK: case greaterTOK: case greatereqTOK: case lessTOK: case lesseqTOK: case THENrw: case DOrw: break;//do nothing default: error(30,linenumber); } break; case term: switch (intok) { case NOTrw: case lparenTOK: case idTOK: //39 case intTOK: case ODDrw: case FALSErw: case TRUErw: s.push(tlist); s.push(factor); break; default: error(3l,linenumber); } break; case tlist: switch (intok) { case multTOK: s.push(tlist); //40 s.push(mulop); s.push(factor); tok=tf.getNextToken(); break; case divTOK: s.push(tlist); // 41 s.push(divop); s.push(factor); tok=tf.getNextToken(); break; case plusTOK: case minusTOK: case semicolonTOK: case ENDrw: //42 case rparenTOK: case commaTOK: case equalTOK: case notequalTOK: case greaterTOK: case greatereqTOK: case lessTOK: case lesseqTOK: case THENrw: case DOrw: break;//do nothing default: error(32,linenumber); } break; case factor: switch (intok) { case NOTrw: s.push(not); // 43 s.push(primary); tok=tf.getNextToken(); break; case lparenTOK: case idTOK: case intTOK: case ODDrw: case FALSErw: case TRUErw: s.push(primary); //44 break; default: error(33,linenumber); } break; case primary: switch (intok) { case lparenTOK: s.push(rparen); //45 s.push(condition); tok=tf.getNextToken(); break; case idTOK: s.push(funccall); //46 s.push(ident); break; case intTOK: s.push(unsignedint); //47 break; case FALSErw: s.push(false); //48 break; case TRUErw: s.push(true); //49 break; case ODDrw: s.push(odd); //50 s.push(rparen); s.push(expr); s.push(lparen); tok=tf.getNextToken(); break; default: error(34,linenumber); } break; case funccall: switch (intok) { case lparenTOK: s.push(fcallret); //51 s.push(rparen); s.push(arguments); s.push(initpcnt); tok=tf.getNextToken(); break; case multTOK: case divTOK: case plusTOK: case minusTOK: case semicolonTOK: case ENDrw: // 52 case rparenTOK: case commaTOK: case equalTOK: case notequalTOK: case greaterTOK: case greatereqTOK: case lessTOK: case lesseqTOK: case THENrw: case DOrw: break;//do nothina default: error(35,linenumber); } break; case arguments: switch (intok) { case NOTrw: case lparenTOK: case idTOK: //53 case intTOK: case ODDrw: case FALSErw: case TRUErw: s.push(moreargs); s.push(actualparam); s.push(condition); break; case rparenTOK: break;//do nothing 54 default: error(36,linenumber); } break; case moreargs: switch (intok) { case commaTOK: s.push(moreargs); //55 s.push(actualparam); s.push(condition); tok=tf.getNextToken(); break; case rparenTOK: break;//do nothing 56 default: error(37,linenumber); } break; case relop: switch (intok) { case equalTOK: s.push(eqop); //57 tok=tf.getNextToken(); break; case notequalTOK: s.push(noteqop); //58 tok=tf.getNextToken(); break; case lessTOK: s.push(lessop); //59 tok=tf.getNextToken(); break; case greaterTOK: s.push(greaterop); //60 tok=tf.getNextToken(); break; case lesseqTOK: s.push(lesseqop); //61 tok=tf.getNextToken(); break; case greatereqTOK: s.push(greateqop); //62 tok=tf.getNextToken(); break; default: error(38,linenumber); } break; case ident: if (intok==idTOK) { s.push(tok.getValue()); // 63 s.push(idSEM); tok=tf.getNextToken(); } else error(39,linenumber); cout << "case ident, intok == " << intok << " idTOK == " << idTOK << endl; cout << "case ident, s stack == " << endl; s.display(); break; case unsignedint: if (intok==intTOK) { //64 s.push(tok.getValue()); s.push(intSEM); tok=tf.getNextToken(); } else error(40,linenumber); break; //terminals case false1: if (intok==FALSErw) { s.push(0); s.push(falseSEM); tok=tf.getNextToken(); } else error(50,linenumber); break; case true1: if (intok==TRUErw) { s.push(1); s.push(trueSEM); tok=tf.getNextToken(); } else error(5l,linenumber); break; case lparen: if (intok==lparenTOK) tok=tf.getNextToken(); else error(52,linenumber); break; case rparen: if (intok==rparenTOK) tok=tf.getNextToken(); else error(53,linenumber); break; case assignment: if (intok==assignTOK) tok=tf.getNextToken(); else error(54,linenumber); break; case semicolon:if (intok== semicolonTOK) tok=tf.getNextToken(); else if (intok == ENDrw) { ; // do nothing } else error(55,linenumber); cout << "case semicolon, UPPER LEVEL, s stack is: \n"; s.display(); cout << "case semicolon, UPPER LEVEL, intok == " << intok << " semicolonTOK == " << semicolonTOK << endl; break; case colon: if (intok==colonTOK) tok=tf.getNextToken(); else error(56,linenumber); break; case then: if (intok==THENrw) tok=tf.getNextToken(); else error(57,linenumber); break; case doSYN: if (intok==DOrw) tok=tf.getNextToken(); else error(58,linenumber); break; case end: if (intok==ENDrw) tok=tf.getNextToken(); else error(59,linenumber); cout << "case end, intok == " << intok << " ENDrw == " << ENDrw << endl; cout << "case end, next tok == " << endl; tok.display(); break; case period:if (intok==periodTOK) tok=tf.getNextToken(); else error(60,linenumber); cout << "case period, intok == " << intok << " periodTOK == " << periodTOK << endl; break; case bottommark: if (intok==endmark) { cout<<"Program syntactically correct\n"; endparse=true; //exit(1); } else error(6l,linenumber); break; //Semantic actions case haltop: createHALTQuad(); break; case assignop: createASSIGNQuad(); break; case get: createIOQuad(get); break; case write: createIOQuad(write); break; case addop: createARITHQuad(addop); break; case subop: createARITHQuad(subop); break; case mulop: createARITHQuad(mulop); break; case divop: createARITHQuad(divop); break; case eqop: qs.push(eqop); break; case noteqop: qs.push(noteqop); break; case lessop: qs.push(lessop); break; case greaterop: qs.push(greaterop); break; case lesseqop: qs.push(lesseqop); break; case greateqop: qs.push(greateqop); break; case not: qs.push(not); createNOTQuad(); break; case odd: qs.push(odd); createODDQuad(); break; case B: createJUMPQuad(B); break; case BF: createJUMPQuad(BF); break; case RTN: createRTNQuad(); break; case FSP: createfuncFSPQuad(); break; case ASP: createfuncASPQuad(); break; case qnum: qs.push(Quadnum-1); break; case bool2: createBOOLQuad(); break; case repair0: qnumsaved=qs.pop(); qlist->repairQuad(qnumsaved,Quadnum); break; case repair1: qnumsaved=qs.pop(); qlist->repairQuad(qnumsaved,Quadnum+1); break; case patch: qnumsaved=qs.pop() ; qlist->repairQuad(Quadnum-1,qnumsaved+1); break; case adjustasp: qnumsaved=qs.pop(); qlist->adjustQuad(qnumsaved+1,-localtempsmax); break; case integer: qs.push(1); break; case booleanSEM: qs.push(2); break; case idSEM: idinx=s.pop(); //tok value write on s by ident qs.push(idinx); qs.push(identclass); cout << "case idSEM, s stack == " << endl; s.display(); cout << "case idSEM, qs stack == " << endl; qs.display(); break; case intSEM: intliteral=s.pop(); //literal write on s by unsignedint qs.push(intliteral); qs.push(numclass); break; case falseSEM: case trueSEM: intliteral=s.pop(); qs.push(intliteral); qs.push(boolclass); break; case initdecs: vcntprime=0; vcnt=0; break; case savevarinfo: saveVARinfo(); break; case incvcnt: vcnt++; break; case initfunc: pcnt=0; level=1; localmax=0; localtempsmax=0; temp=qs.pop(); if (temp==identclass) { idinx=qs.pop(); qs.push(Quadnum); qs.push(idinx); qs.push(temp); } else cout<<"\nPROBLEM in initfunc!\n"; break; case savefuncinfo:saveFUNCinfo(); break; case formalparam: FORMALparam(); break; case initpcnt: pcnt=0; qs.push(pcnt); break; case fcallret: Fcallret(); break; case actualparam: ACTUALparam(); break; case saveproginfo:savePROGinfo(); break; case setlevel0: level=0; break; case clearlevel1: iddt.clearLevel1Entries(); break; } // end of switch. } tf.closeTokenFile(); qf.createQuadFile(); emitallQuads(); qf.closeQuadFile(); } // end of all parser class methods. // Page B-26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42