Homework Programming Language Processor Class Really Need Help Codes Much Thanks Compilec Q43828099
homework from Programming Language Processor class.. I reallyneed help with the codes. Very much thanks!
/*************** compile.c *************/
#include “getSource.h”#ifndef TBL#define TBL#include “table.h”#endif#include “codegen.h”
#define MINERROR 3 /*縲€繧ィ繝ゥ繝シ縺後%繧御サ・荳九↑繧牙ョ溯。後€€*/#define FIRSTADDR 2 /*縲€蜷�ヶ繝ュ繝�け縺ョ譛€蛻昴�螟画焚縺ョ繧「繝峨Ξ繧ケ縲€*/
static Token token; /*縲€谺。縺ョ繝医�繧ッ繝ウ繧貞�繧後※縺翫¥縲€*/
static void block(int pIndex); /*縲€繝悶Ο繝�け縺ョ繧ウ繝ウ繝代う繝ォ縲€*//*縲€pIndex 縺ッ縺薙�繝悶Ο繝�け縺ョ髢「謨ー蜷阪�繧、繝ウ繝�ャ繧ッ繧ケ縲€*/static void constDecl(); /*縲€螳壽焚螳」險€縺ョ繧ウ繝ウ繝代う繝ォ縲€*/static void varDecl(); /*縲€螟画焚螳」險€縺ョ繧ウ繝ウ繝代う繝ォ縲€*/static void funcDecl(); /*縲€髢「謨ー螳」險€縺ョ繧ウ繝ウ繝代う繝ォ縲€*/static void statement(); /*縲€譁��繧ウ繝ウ繝代う繝ォ縲€*/static void expression(); /*縲€蠑上�繧ウ繝ウ繝代う繝ォ縲€*/static void term(); /*縲€蠑上�鬆��繧ウ繝ウ繝代う繝ォ縲€*/static void factor(); /*縲€蠑上�蝗�蟄舌�繧ウ繝ウ繝代う繝ォ縲€*/static void condition(); /*縲€譚。莉カ蠑上�繧ウ繝ウ繝代う繝ォ縲€*/static int isStBeginKey(Token t);/*縲€繝医�繧ッ繝ウt縺ッ譁��蜈磯�ュ縺ョ繧ュ繝シ縺具シ溘€€*/
int compile(){int i;printf(“start compilationn”);initSource(); /*縲€getSource縺ョ蛻晄悄險ュ螳壹€€*/token = nextToken(); /*縲€譛€蛻昴�繝医�繧ッ繝ウ縲€*/blockBegin(FIRSTADDR);/*縲€縺薙l莉・蠕後�螳」險€縺ッ譁ー縺励>繝悶Ο繝�け縺ョ繧ゅ�縲€*/block(0); /*縲€0 縺ッ繝€繝溘��井クサ繝悶Ο繝�け縺ョ髢「謨ー蜷阪�縺ェ縺�シ峨€€*/finalSource();i = errorN(); /*縲€繧ィ繝ゥ繝シ繝。繝�そ繝シ繧ク縺ョ蛟区焚縲€*/if (i!=0)printf(“%d errorsn”, i);/* listCode(); */ /*縲€逶ョ逧�さ繝シ繝峨�繝ェ繧ケ繝茨シ亥ソ�ヲ√↑繧会シ峨€€*/return i<MINERROR;/*縲€繧ィ繝ゥ繝シ繝。繝�そ繝シ繧ク縺ョ蛟区焚縺悟ー代↑縺�°縺ゥ縺�°縺ョ蛻、螳壹€€*/}
void block(int pIndex) /*縲€pIndex縺ッ縺薙�繝悶Ο繝�け縺ョ髢「謨ー蜷阪�繧、繝ウ繝�ャ繧ッ繧ケ縲€*/{int backP;backP = genCodeV(jmp, 0);/*縲€蜀�Κ髢「謨ー繧帝」帙�雜翫☆蜻ス莉、縲∝セ後〒繝舌ャ繧ッ繝代ャ繝√€€*/while (1) { /*縲€螳」險€驛ィ縺ョ繧ウ繝ウ繝代う繝ォ繧堤ケー繧願ソ斐☆縲€*/switch (token.kind){case Const: /*縲€螳壽焚螳」險€驛ィ縺ョ繧ウ繝ウ繝代う繝ォ縲€*/token = nextToken();constDecl(); continue;case Var: /*縲€螟画焚螳」險€驛ィ縺ョ繧ウ繝ウ繝代う繝ォ縲€*/token = nextToken();varDecl(); continue;case Func: /*縲€髢「謨ー螳」險€驛ィ縺ョ繧ウ繝ウ繝代う繝ォ縲€*/token = nextToken();funcDecl(); continue;default: /*縲€縺昴l莉・螟悶↑繧牙ョ」險€驛ィ縺ッ邨ゅo繧翫€€*/break;}break;}backPatch(backP); /*縲€蜀�Κ髢「謨ー繧帝」帙�雜翫☆蜻ス莉、縺ォ繝代ャ繝√€€*/changeV(pIndex, nextCode());/*縲€縺薙�髢「謨ー縺ョ髢句ァ狗分蝨ー繧剃ソョ豁」縲€*/genCodeV(ict, frameL());/*縲€縺薙�繝悶Ο繝�け縺ョ螳溯。梧凾縺ョ蠢�ヲ∬ィ俶�蝓溘r縺ィ繧句多莉、縲€*/statement(); /*縲€縺薙�繝悶Ο繝�け縺ョ荳サ譁�€€*/genCodeR(); /*縲€繝ェ繧ソ繝シ繝ウ蜻ス莉、縲€*/blockEnd(); /*縲€繝悶Ο繝�け縺檎オゅ▲縺溘%縺ィ繧稚able縺ォ騾」邨。縲€*/}
void constDecl() /*縲€螳壽焚螳」險€縺ョ繧ウ繝ウ繝代う繝ォ縲€*/{Token temp;while(1){if (token.kind==Id){setIdKind(constId); /*縲€蜊ー蟄励�縺溘a縺ョ諠��ア縺ョ繧サ繝�ヨ縲€*/temp = token; /*縲€蜷榊燕繧貞�繧後※縺翫¥縲€*/token = checkGet(nextToken(), Equal);/*縲€蜷榊燕縺ョ谺。縺ッ”=”縺ョ縺ッ縺壹€€*/if (token.kind==Num)enterTconst(temp.u.id, token.u.value);/*縲€螳壽焚蜷阪→蛟、繧偵ユ繝シ繝悶Ν縺ォ縲€*/elseerrorType(“number”);token = nextToken();}elseerrorMissingId();if (token.kind!=Comma){ /*縲€谺。縺後さ繝ウ繝槭↑繧牙ョ壽焚螳」險€縺檎カ壹¥縲€*/if (token.kind==Id){/*縲€谺。縺悟錐蜑阪↑繧峨さ繝ウ繝槭r蠢倥l縺溘%縺ィ縺ォ縺吶k縲€*/errorInsert(Comma);continue;}elsebreak;}token = nextToken();}token = checkGet(token, Semicolon);/*縲€譛€蠕後�”;”縺ョ縺ッ縺壹€€*/}
void varDecl() /*縲€螟画焚螳」險€縺ョ繧ウ繝ウ繝代う繝ォ縲€*/{while(1){if (token.kind==Id){setIdKind(varId); /*縲€蜊ー蟄励�縺溘a縺ョ諠��ア縺ョ繧サ繝�ヨ縲€*/enterTvar(token.u.id);/*縲€螟画焚蜷阪r繝��繝悶Ν縺ォ縲∫分蝨ー縺ッtable縺梧アコ繧√k縲€*/token = nextToken();}elseerrorMissingId();if (token.kind!=Comma){ /*縲€谺。縺後さ繝ウ繝槭↑繧牙、画焚螳」險€縺檎カ壹¥縲€*/if (token.kind==Id){/*縲€谺。縺悟錐蜑阪↑繧峨さ繝ウ繝槭r蠢倥l縺溘%縺ィ縺ォ縺吶k縲€*/errorInsert(Comma);continue;}elsebreak;}token = nextToken();}token = checkGet(token, Semicolon);/*縲€譛€蠕後�”;”縺ョ縺ッ縺壹€€*/}
void funcDecl() /*縲€髢「謨ー螳」險€縺ョ繧ウ繝ウ繝代う繝ォ縲€*/{int fIndex;if (token.kind==Id){setIdKind(funcId); /*縲€蜊ー蟄励�縺溘a縺ョ諠��ア縺ョ繧サ繝�ヨ縲€*/fIndex = enterTfunc(token.u.id, nextCode());/*縲€髢「謨ー蜷阪r繝��繝悶Ν縺ォ逋サ骭イ縲€*//*縲€縺昴�蜈磯�ュ逡ェ蝨ー縺ッ縲√∪縺壹€∵ャ。縺ョ繧ウ繝シ繝峨�逡ェ蝨ーnextCode()縺ィ縺吶k縲€*/token = checkGet(nextToken(), Lparen);blockBegin(FIRSTADDR);/*縲€繝代Λ繝。繧ソ蜷阪�繝ャ繝吶Ν縺ッ髢「謨ー縺ョ繝悶Ο繝�け縺ィ蜷後§縲€*/while(1){if (token.kind==Id){ /*縲€繝代Λ繝。繧ソ蜷阪′縺ゅk蝣エ蜷医€€*/setIdKind(parId); /*縲€蜊ー蟄励�縺溘a縺ョ諠��ア縺ョ繧サ繝�ヨ縲€*/enterTpar(token.u.id); /*縲€繝代Λ繝。繧ソ蜷阪r繝��繝悶Ν縺ォ逋サ骭イ縲€*/token = nextToken();}elsebreak;if (token.kind!=Comma){/*縲€谺。縺後さ繝ウ繝槭↑繧峨ヱ繝ゥ繝。繧ソ蜷阪′邯壹¥縲€*/if (token.kind==Id){ /*縲€谺。縺悟錐蜑阪↑繧峨さ繝ウ繝槭r蠢倥l縺溘%縺ィ縺ォ縲€*/errorInsert(Comma);continue;}elsebreak;}token = nextToken();}token = checkGet(token, Rparen); /*縲€譛€蠕後�”)”縺ョ縺ッ縺壹€€*/endpar(); /*縲€繝代Λ繝。繧ソ驛ィ縺檎オゅo縺」縺溘%縺ィ繧偵ユ繝シ繝悶Ν縺ォ騾」邨。縲€*/if (token.kind==Semicolon){errorDelete();token = nextToken();}block(fIndex);/*縲€繝悶Ο繝�け縺ョ繧ウ繝ウ繝代う繝ォ縲√◎縺ョ髢「謨ー蜷阪�繧、繝ウ繝�ャ繧ッ繧ケ繧呈ク。縺吶€€*/token = checkGet(token, Semicolon);/*縲€譛€蠕後�”;”縺ョ縺ッ縺壹€€*/} elseerrorMissingId(); /*縲€髢「謨ー蜷阪′縺ェ縺�€€*/}
void statement() /*縲€譁��繧ウ繝ウ繝代う繝ォ縲€*/{int tIndex;KindT k;int backP, backP2; /*縲€繝舌ャ繧ッ繝代ャ繝∫畑縲€*/
while(1) {switch (token.kind) {case Id: /*縲€莉」蜈・譁��繧ウ繝ウ繝代う繝ォ縲€*/tIndex = searchT(token.u.id, varId);/*縲€蟾ヲ霎コ縺ョ螟画焚縺ョ繧、繝ウ繝�ャ繧ッ繧ケ縲€*/setIdKind(k=kindT(tIndex));/*縲€蜊ー蟄励�縺溘a縺ョ諠��ア縺ョ繧サ繝�ヨ縲€*/if (k != varId && k != parId)/*縲€螟画焚蜷阪°繝代Λ繝。繧ソ蜷阪�縺ッ縺壹€€*/errorType(“var/par”);token = checkGet(nextToken(), Assign);/*縲€”:=”縺ョ縺ッ縺壹€€*/expression(); /*縲€蠑上�繧ウ繝ウ繝代う繝ォ縲€*/genCodeT(sto, tIndex); /*縲€蟾ヲ霎コ縺ク縺ョ莉」蜈・蜻ス莉、縲€*/return;case If: /*縲€if譁��繧ウ繝ウ繝代う繝ォ縲€*/token = nextToken();condition(); /*縲€譚。莉カ蠑上�繧ウ繝ウ繝代う繝ォ縲€*/token = checkGet(token, Then); /*縲€”then”縺ョ縺ッ縺壹€€*/backP = genCodeV(jpc, 0); /*縲€jpc蜻ス莉、縲€*/statement(); /*縲€譁��繧ウ繝ウ繝代う繝ォ縲€*/backPatch(backP); /*縲€荳翫�jpc蜻ス莉、縺ォ繝舌ャ繧ッ繝代ャ繝√€€*/return;case Ret: /*縲€return譁��繧ウ繝ウ繝代う繝ォ縲€*/token = nextToken();expression(); /*縲€蠑上�繧ウ繝ウ繝代う繝ォ縲€*/genCodeR(); /*縲€ret蜻ス莉、縲€*/return;case Begin: /*縲€begin . . end譁��繧ウ繝ウ繝代う繝ォ縲€*/token = nextToken();while(1){statement(); /*縲€譁��繧ウ繝ウ繝代う繝ォ縲€*/while(1){if (token.kind==Semicolon){ /*縲€谺。縺�”;”縺ェ繧画枚縺檎カ壹¥縲€*/token = nextToken();break;}if (token.kind==End){ /*縲€谺。縺憩nd縺ェ繧臥オゅj縲€*/token = nextToken();return;}if (isStBeginKey(token)){ /*縲€谺。縺梧枚縺ョ蜈磯�ュ險伜捷縺ェ繧峨€€*/errorInsert(Semicolon); /*縲€”;”繧貞ソ倥l縺溘%縺ィ縺ォ縺吶k縲€*/break;}errorDelete(); /*縲€縺昴l莉・螟悶↑繧峨お繝ゥ繝シ縺ィ縺励※隱ュ縺ソ謐ィ縺ヲ繧九€€*/token = nextToken();}}case While: /*縲€while譁��繧ウ繝ウ繝代う繝ォ縲€*/token = nextToken();backP2 = nextCode(); /*縲€while譁��譛€蠕後�jmp蜻ス莉、縺ョ鬟帙�蜈医€€*/condition(); /*縲€譚。莉カ蠑上�繧ウ繝ウ繝代う繝ォ縲€*/token = checkGet(token, Do); /*縲€”do”縺ョ縺ッ縺壹€€*/backP = genCodeV(jpc, 0);/*縲€譚。莉カ蠑上′蛛ス縺ョ縺ィ縺埼」帙�蜃コ縺冕pc蜻ス莉、縲€*/statement(); /*縲€譁��繧ウ繝ウ繝代う繝ォ縲€*/genCodeV(jmp, backP2);/*縲€while譁��蜈磯�ュ縺ク縺ョ繧ク繝」繝ウ繝怜多莉、縲€*/backPatch(backP);/*縲€蛛ス縺ョ縺ィ縺埼」帙�蜃コ縺冕pc蜻ス莉、縺ク縺ョ繝舌ャ繧ッ繝代ャ繝√€€*/return;case Write: /*縲€write譁��繧ウ繝ウ繝代う繝ォ縲€*/token = nextToken();expression(); /*縲€蠑上�繧ウ繝ウ繝代う繝ォ縲€*/genCodeO(wrt); /*縲€縺昴�蛟、繧貞�蜉帙☆繧仇rt蜻ス莉、縲€*/return;case WriteLn: /*縲€writeln譁��繧ウ繝ウ繝代う繝ォ縲€*/token = nextToken();genCodeO(wrl); /*縲€謾ケ陦後r蜃コ蜉帙☆繧仇rl蜻ス莉、縲€*/return;case End: case Semicolon: /*縲€遨コ譁�r隱ュ繧薙□縺薙→縺ォ縺励※邨ゅj縲€*/return;default: /*縲€譁��蜈磯�ュ縺ョ繧ュ繝シ縺セ縺ァ隱ュ縺ソ謐ィ縺ヲ繧九€€*/errorDelete(); /*縲€莉願ェュ繧薙□繝医�繧ッ繝ウ繧定ェュ縺ソ謐ィ縺ヲ繧九€€*/token = nextToken();continue;}}}
int isStBeginKey(Token t)/*縲€繝医�繧ッ繝ウt縺ッ譁��蜈磯�ュ縺ョ繧ュ繝シ縺具シ溘€€*/{switch (t.kind){case If: case Begin: case Ret:case While: case Write: case WriteLn:return 1;default:return 0;}}
void expression() /*縲€蠑上�繧ウ繝ウ繝代う繝ォ縲€*/{KeyId k;k = token.kind;if (k==Plus || k==Minus){token = nextToken();term();if (k==Minus)genCodeO(neg);}elseterm();k = token.kind;while (k==Plus || k==Minus){token = nextToken();term();if (k==Minus)genCodeO(sub);elsegenCodeO(add);k = token.kind;}}
void term() /*縲€蠑上�鬆��繧ウ繝ウ繝代う繝ォ縲€*/{KeyId k;factor();k = token.kind;while (k==Mult || k==Div){token = nextToken();factor();if (k==Mult)genCodeO(mul);elsegenCodeO(div);k = token.kind;}}
void factor() /*縲€蠑上�蝗�蟄舌�繧ウ繝ウ繝代う繝ォ縲€*/{int tIndex, i;KeyId k;if (token.kind==Id){tIndex = searchT(token.u.id, varId);setIdKind(k=kindT(tIndex));/*縲€蜊ー蟄励�縺溘a縺ョ諠��ア縺ョ繧サ繝�ヨ縲€*/switch (k) {case varId: case parId: /*縲€螟画焚蜷阪°繝代Λ繝。繧ソ蜷阪€€*/genCodeT(lod, tIndex);token = nextToken(); break;case constId: /*縲€螳壽焚蜷阪€€*/genCodeV(lit, val(tIndex));token = nextToken(); break;case funcId: /*縲€髢「謨ー蜻シ縺ウ蜃コ縺励€€*/token = nextToken();if (token.kind==Lparen){i=0; /*縲€i縺ッ螳溷シ墓焚縺ョ蛟区焚縲€*/token = nextToken();if (token.kind != Rparen) {for (; ; ) {expression(); i++; /*縲€螳溷シ墓焚縺ョ繧ウ繝ウ繝代う繝ォ縲€*/if (token.kind==Comma){ /* 谺。縺後さ繝ウ繝槭↑繧牙ョ溷シ墓焚縺檎カ壹¥ */token = nextToken();continue;}token = checkGet(token, Rparen);break;}} elsetoken = nextToken();if (pars(tIndex) != i)errorMessage(“#par”); /*縲€pars(tIndex)縺ッ莉ョ蠑墓焚縺ョ蛟区焚縲€*/}else{errorInsert(Lparen);errorInsert(Rparen);}genCodeT(cal, tIndex); /*縲€call蜻ス莉、縲€*/break;}}else if (token.kind==Num){ /*縲€螳壽焚縲€*/genCodeV(lit, token.u.value);token = nextToken();}else if (token.kind==Lparen){ /*縲€縲�(縲阪€悟屏蟄舌€阪€�)縲阪€€*/token = nextToken();expression();token = checkGet(token, Rparen);}switch (token.kind){ /*縲€蝗�蟄舌�蠕後′縺セ縺溷屏蟄舌↑繧峨お繝ゥ繝シ縲€*/case Id: case Num: case Lparen:errorMissingOp();factor();default:return;}}void condition() /*縲€譚。莉カ蠑上�繧ウ繝ウ繝代う繝ォ縲€*/{KeyId k;if (token.kind==Odd){token = nextToken();expression();genCodeO(odd);}else{expression();k = token.kind;switch(k){case Equal: case Lss: case Gtr:case NotEq: case LssEq: case GtrEq:break;default:errorType(“rel-op”);break;}token = nextToken();expression();switch(k){case Equal: genCodeO(eq); break;case Lss: genCodeO(ls); break;case Gtr: genCodeO(gr); break;case NotEq: genCodeO(neq); break;case LssEq: genCodeO(lseq); break;case GtrEq: genCodeO(greq); break;}}}
Question 1 To introduce the following do-while statement to PL/0′, answer the following questions. Production rule statement → do statement while condition Action A statement ‘do statement while condition works as follows 1. Execute statement. 2. If the value of condition is true, go to the step 1. Otherwise, exit this loop. Question 1-1 To add a token do to a set of starting tokens of statement, modify a function isStBeginKey in compile.c and explain the modification in your report. Question 1-2 Modify a function statement in compile.c so that your PL/O compiler can output object codes of Fig.1 for do-while statements. Explain the modificaiton in your report. labell: Object codes of statement Object codes of condition jpc label2 jmp label1 label2: Figure 1: Object codes for a do-while statement Statement then statemene Statement – if condition Statement it condition (do – statemene while y condition) – Statement – repent statement then statement until a condition! Statements else statement – cardituen xhen – statement the stron Show transcribed image text Question 1 To introduce the following do-while statement to PL/0’, answer the following questions. Production rule statement → do statement while condition Action A statement ‘do statement while condition works as follows 1. Execute statement. 2. If the value of condition is true, go to the step 1. Otherwise, exit this loop. Question 1-1 To add a token do to a set of starting tokens of statement, modify a function isStBeginKey in compile.c and explain the modification in your report. Question 1-2 Modify a function statement in compile.c so that your PL/O compiler can output object codes of Fig.1 for do-while statements. Explain the modificaiton in your report. labell: Object codes of statement Object codes of condition jpc label2 jmp label1 label2: Figure 1: Object codes for a do-while statement
Statement then statemene Statement – if condition Statement it condition (do – statemene while y condition) – Statement – repent statement then statement until a condition! Statements else statement – cardituen xhen – statement the stron
Expert Answer
Answer to homework from Programming Language Processor class.. I really need help with the codes. Very much thanks! /*************…
OR