Menu

Homework Programming Language Processor Class Really Need Help Codes Thank Much Getsource Q43828126

Homework from programming language processor class… I really needhelp with the codes… Thank you very much
Question 3 Answer the questions to add the followig if-then-else statement (the if-then statement with the optional else clau
getsource h
#include table.h #endif #include getSource.h #define MAXTABLE 100 #define MAXNAME 31 #define MAXLEVEL5 *名前表の最大長さ* *名前の最大長

/**************getSource.c************/
#include <stdio.h>#include <string.h>#include “getSource.h”
#define MAXLINE 120 /*縲€�題。後�譛€螟ァ譁�ュ玲焚縲€*/#define MAXERROR 30 /*縲€縺薙l莉・荳翫�繧ィ繝ゥ繝シ縺後≠縺」縺溘i邨ゅj縲€*/#define MAXNUM 14 /*縲€螳壽焚縺ョ譛€螟ァ譯∵焚縲€*/#define TAB 5 /*縲€繧ソ繝悶�繧ケ繝壹�繧ケ縲€*/#define INSERT_C “#0000FF” /* 謖ソ蜈・譁�ュ励�濶イ */#define DELETE_C “#FF0000” /* 蜑企勁譁�ュ励�濶イ */#define TYPE_C “#00FF00” /* 繧ソ繧、繝励お繝ゥ繝シ譁�ュ励�濶イ */
static FILE *fpi; /*縲€繧ス繝シ繧ケ繝輔ぃ繧、繝ォ縲€*/static FILE *fptex; /*縲€LaTeX蜃コ蜉帙ヵ繧。繧、繝ォ縲€*/static char line[MAXLINE]; /*縲€�題。悟�縺ョ蜈・蜉帙ヰ繝�ヵ繧。繝シ縲€*/static int lineIndex; /*縲€谺。縺ォ隱ュ繧€譁�ュ励�菴咲スョ縲€*/static char ch; /*縲€譛€蠕後↓隱ュ繧薙□譁�ュ励€€*/
static Token cToken; /*縲€譛€蠕後↓隱ュ繧薙□繝医�繧ッ繝ウ縲€*/static KindT idKind; /*縲€迴セ繝医�繧ッ繝ウ(Id)縺ョ遞ョ鬘槭€€*/static int spaces; /*縲€縺昴�繝医�繧ッ繝ウ縺ョ蜑阪�繧ケ繝壹�繧ケ縺ョ蛟区焚縲€*/static int CR; /*縲€縺昴�蜑阪�CR縺ョ蛟区焚縲€*/static int printed; /*縲€繝医�繧ッ繝ウ縺ッ蜊ー蟄玲ク医∩縺九€€*/
static int errorNo = 0; /*縲€蜃コ蜉帙@縺溘お繝ゥ繝シ縺ョ謨ー縲€*/static char nextChar(); /*縲€谺。縺ョ譁�ュ励r隱ュ繧€髢「謨ー縲€*/static int isKeySym(KeyId k); /*縲€t縺ッ險伜捷縺具シ溘€€*/static int isKeyWd(KeyId k); /*縲€t縺ッ莠育エ�ェ槭°�溘€€*/static void printSpaces();/*縲€繝医�繧ッ繝ウ縺ョ蜑阪�繧ケ繝壹�繧ケ縺ョ蜊ー蟄励€€*/static void printcToken(); /*縲€繝医�繧ッ繝ウ縺ョ蜊ー蟄励€€*/
struct keyWd { /*縲€莠育エ�ェ槭d險伜捷縺ィ蜷榊燕(KeyId)縲€*/char *word;KeyId keyId;};
static struct keyWd KeyWdT[] = {/*縲€莠育エ�ェ槭d險伜捷縺ィ蜷榊燕(KeyId)縺ョ陦ィ縲€*/{“begin”, Begin},{“end”, End},{“if”, If},{“then”, Then},{“while”, While},{“do”, Do},{“return”, Ret},{“function”, Func},{“var”, Var},{“const”, Const},{“odd”, Odd},{“write”, Write},{“writeln”,WriteLn},{“$dummy1”,end_of_KeyWd},/*縲€險伜捷縺ィ蜷榊燕(KeyId)縺ョ陦ィ縲€*/{“+”, Plus},{“-“, Minus},{“*”, Mult},{“/”, Div},{“(“, Lparen},{“)”, Rparen},{“=”, Equal},{“<“, Lss},{“>”, Gtr},{“<>”, NotEq},{“<=”, LssEq},{“>=”, GtrEq},{“,”, Comma},{“.”, Period},{“;”, Semicolon},{“:=”, Assign},{“$dummy2”,end_of_KeySym}};
int isKeyWd(KeyId k) /*縲€繧ュ繝シk縺ッ莠育エ�ェ槭°�溘€€*/{return (k < end_of_KeyWd);}
int isKeySym(KeyId k) /*縲€繧ュ繝シk縺ッ險伜捷縺具シ溘€€*/{if (k < end_of_KeyWd)return 0;return (k < end_of_KeySym);}
static KeyId charClassT[256];/*縲€譁�ュ励�遞ョ鬘槭r遉コ縺呵。ィ縺ォ縺吶k縲€*/
static void initCharClassT()/*縲€譁�ュ励�遞ョ鬘槭r遉コ縺呵。ィ繧剃ス懊k髢「謨ー縲€*/{int i;for (i=0; i<256; i++)charClassT[i] = others;for (i=’0′; i<=’9′; i++)charClassT[i] = digit;for (i=’A’; i<=’Z’; i++)charClassT[i] = letter;for (i=’a’; i<=’z’; i++)charClassT[i] = letter;charClassT[‘+’] = Plus; charClassT[‘-‘] = Minus;charClassT[‘*’] = Mult; charClassT[‘/’] = Div;charClassT[‘(‘] = Lparen; charClassT[‘)’] = Rparen;charClassT[‘=’] = Equal; charClassT[‘<‘] = Lss;charClassT[‘>’] = Gtr; charClassT[‘,’] = Comma;charClassT[‘.’] = Period; charClassT[‘;’] = Semicolon;charClassT[‘:’] = colon;}
int openSource(char fileName[])/*縲€繧ス繝シ繧ケ繝輔ぃ繧、繝ォ縺ョopen縲€*/{char fileNameO[30];if ( (fpi = fopen(fileName,”r”)) == NULL ) {printf(“can’t open %sn”, fileName);return 0;}strcpy(fileNameO, fileName);strcat(fileNameO,”.html”); /* strcat(fileNameO,”.tex”);*/if ( (fptex = fopen(fileNameO,”w”)) == NULL ) {/*縲€.html�医∪縺溘�.tex�峨ヵ繧。繧、繝ォ繧剃ス懊k縲€*/printf(“can’t open %sn”, fileNameO);return 0;}return 1;}
void closeSource()/*縲€繧ス繝シ繧ケ繝輔ぃ繧、繝ォ縺ィ.html�医∪縺溘�.tex�峨ヵ繧。繧、繝ォ繧団lose縲€*/{fclose(fpi);fclose(fptex);}
void initSource(){lineIndex = -1; /*縲€蛻晄悄險ュ螳壹€€*/ch = ‘n’;printed = 1;initCharClassT();fprintf(fptex,”<HTML>n”); /*縲€html繧ウ繝槭Φ繝峨€€*/fprintf(fptex,”<HEAD>n<TITLE>compiled sourceprogram</TITLE>n</HEAD>n”);fprintf(fptex,”<BODY>n<PRE>n”);/* 縲€LaTeX繧ウ繝槭Φ繝峨€€fprintf(fptex,”documentstyle[12pt]{article}n”);  fprintf(fptex,”begin{document}n”);fprintf(fptex,”fboxsep=0ptn”);fprintf(fptex,”definsert#1{$fbox{#1}$}n”);fprintf(fptex,”defdelete#1{$fboxrule=.5mmfbox{#1}$}n”);fprintf(fptex,”rmn”);*/}
void finalSource(){if (cToken.kind==Period)printcToken();elseerrorInsert(Period);fprintf(fptex,”n</PRE>n</BODY>n</HTML>n”);/* fprintf(fptex,”nend{document}n”); */}/*縲€騾壼クク縺ョ繧ィ繝ゥ繝シ繝。繝�そ繝シ繧ク縺ョ蜃コ蜉帙�莉墓婿�亥盾閠�∪縺ァ�峨€€*//*void error(char *m){if (lineIndex > 0)printf(“%*sn”, lineIndex, “***^”);elseprintf(“^n”);printf(“*** error *** %sn”, m);errorNo++;if (errorNo > MAXERROR){printf(“too many errorsn”);printf(“abort compilationn”);exit (1);}}*/
void errorNoCheck()/*縲€繧ィ繝ゥ繝シ縺ョ蛟区焚縺ョ繧ォ繧ヲ繝ウ繝医€∝、壹☆縺弱◆繧臥オゅo繧翫€€*/{if (errorNo++ > MAXERROR){fprintf(fptex, “too manyerrorsn</PRE>n</BODY>n</HTML>n”);/* fprintf(fptex, “too many errorsnend{document}n”);*/printf(“abort compilationn”);exit (1);}}
void errorType(char *m)/*縲€蝙九お繝ゥ繝シ繧�.html�医∪縺溘�.tex�峨ヵ繧。繧、繝ォ縺ォ蜃コ蜉帙€€*/{printSpaces();fprintf(fptex, “<FONT COLOR=%s>%s</FONT>”, TYPE_C,m);/* fprintf(fptex, “(stackrel{mbox{scriptsize%s}}{mbox{“, m); */printcToken();/* fprintf(fptex, “}})”); */errorNoCheck();}
void errorInsert(KeyId k)/*縲€keyString(k)繧�.html�医∪縺溘�.tex�峨ヵ繧。繧、繝ォ縺ォ謖ソ蜈・縲€*/{fprintf(fptex, “<FONTCOLOR=%s><b>%s</b></FONT>”, INSERT_C,KeyWdT[k].word);// if (k < end_of_KeyWd) /*縲€莠育エ�ェ槭€€*/// fprintf(fptex, ” insert{{bf %s}}”,KeyWdT[k].word);// else /*縲€貍皮ョ怜ュ舌°蛹コ蛻�j險伜捷縲€*/// fprintf(fptex, ” insert{$%s$}”, KeyWdT[k].word);*/errorNoCheck();}
void errorMissingId()/*縲€蜷榊燕縺後↑縺�→縺ョ繝。繝�そ繝シ繧ク繧�.html�医∪縺溘�.tex�峨ヵ繧。繧、繝ォ縺ォ謖ソ蜈・縲€*/{fprintf(fptex, “<FONT COLOR=%s>Id</FONT>”,INSERT_C);/* fprintf(fptex, “insert{Id}”); */errorNoCheck();}
void errorMissingOp()/*縲€貍皮ョ怜ュ舌′縺ェ縺�→縺ョ繝。繝�そ繝シ繧ク繧�.html�医∪縺溘�.tex�峨ヵ繧。繧、繝ォ縺ォ謖ソ蜈・縲€*/{fprintf(fptex, “<FONT COLOR=%s>@</FONT>”,INSERT_C);/* fprintf(fptex, “insert{$otimes$}”); */errorNoCheck();}
void errorDelete() /*縲€莉願ェュ繧薙□繝医�繧ッ繝ウ繧定ェュ縺ソ謐ィ縺ヲ繧九€€*/{int i=(int)cToken.kind;printSpaces();printed = 1;if (i < end_of_KeyWd) /*縲€莠育エ�ェ槭€€*/fprintf(fptex, “<FONTCOLOR=%s><b>%s</b></FONT>”, DELETE_C,KeyWdT[i].word);/* fprintf(fptex, “delete{{bf %s}}”, KeyWdT[i].word);*/else if (i < end_of_KeySym) /*縲€貍皮ョ怜ュ舌°蛹コ蛻�j險伜捷縲€*/fprintf(fptex, “<FONT COLOR=%s>%s</FONT>”,DELETE_C, KeyWdT[i].word);/* fprintf(fptex, “delete{$%s$}”, KeyWdT[i].word); */else if (i==(int)Id) /*縲€Identfier縲€*/fprintf(fptex, “<FONT COLOR=%s>%s</FONT>”,DELETE_C, cToken.u.id);/* fprintf(fptex, “delete{%s}”, cToken.u.id); */else if (i==(int)Num) /*縲€Num縲€*/fprintf(fptex, “<FONT COLOR=%s>%d</FONT>”,DELETE_C, cToken.u.value);/* fprintf(fptex, “delete{%d}”, cToken.u.value); */}
void errorMessage(char *m)/*縲€繧ィ繝ゥ繝シ繝。繝�そ繝シ繧ク繧�.html�医∪縺溘�.tex�峨ヵ繧。繧、繝ォ縺ォ蜃コ蜉帙€€*/{fprintf(fptex, “<FONT COLOR=%s>%s</FONT>”, TYPE_C,m);/* fprintf(fptex, “$^{%s}$”, m); */errorNoCheck();}
void errorF(char *m)/*縲€繧ィ繝ゥ繝シ繝。繝�そ繝シ繧ク繧貞�蜉帙@縲√さ繝ウ繝代う繝ォ邨ゆコ�€€*/{errorMessage(m);fprintf(fptex, “fatalerrorsn</PRE>n</BODY>n</HTML>n”);/* fprintf(fptex, “fatal errorsnend{document}n”); */if (errorNo)printf(“total %d errorsn”, errorNo);printf(“abort compilationn”);exit (1);}
int errorN() /*縲€繧ィ繝ゥ繝シ縺ョ蛟区焚繧定ソ斐☆縲€*/{return errorNo;}
char nextChar() /*縲€谺。縺ョ�第枚蟄励r霑斐☆髢「謨ー縲€*/{char ch;if (lineIndex == -1){if (fgets(line, MAXLINE, fpi) != NULL){/* puts(line); *//*縲€騾壼クク縺ョ繧ィ繝ゥ繝シ繝。繝�そ繝シ繧ク縺ョ蜃コ蜉帙�蝣エ蜷茨シ亥盾閠�∪縺ァ�峨€€*/lineIndex = 0;} else {errorF(“end of filen”); /* end of file縺ェ繧峨さ繝ウ繝代う繝ォ邨ゆコ�*/}}if ((ch = line[lineIndex++]) == ‘n’){/*縲€ch縺ォ谺。縺ョ�第枚蟄励€€*/lineIndex = -1; /*縲€縺昴l縺梧隼陦梧枚蟄励↑繧画ャ。縺ョ陦後�蜈・蜉帶コ門y縲€*/return ‘n’; /*縲€譁�ュ励→縺励※縺ッ謾ケ陦梧枚蟄励r霑斐☆縲€*/}return ch;}
Token nextToken() /*縲€谺。縺ョ繝医�繧ッ繝ウ繧定ェュ繧薙〒霑斐☆髢「謨ー縲€*/{int i = 0;int num;KeyId cc;Token temp;char ident[MAXNAME];printcToken(); /*縲€蜑阪�繝医�繧ッ繝ウ繧貞魂蟄励€€*/spaces = 0; CR = 0;while (1){ /*縲€谺。縺ョ繝医�繧ッ繝ウ縺セ縺ァ縺ョ遨コ逋ス繧�隼陦後r繧ォ繧ヲ繝ウ繝医€€*/if (ch == ‘ ‘)spaces++;else if (ch == ‘t’)spaces+=TAB;else if (ch == ‘n’){spaces = 0; CR++;}else break;ch = nextChar();}switch (cc = charClassT[ch]) {case letter: /* identifier */do {if (i < MAXNAME)ident[i] = ch;i++; ch = nextChar();} while ( charClassT[ch] == letter|| charClassT[ch] == digit );if (i >= MAXNAME){errorMessage(“too long”);i = MAXNAME – 1;}ident[i] = ‘ ‘;for (i=0; i<end_of_KeyWd; i++)if (strcmp(ident, KeyWdT[i].word) == 0) {temp.kind = KeyWdT[i].keyId; /*縲€莠育エ�ェ槭�蝣エ蜷医€€*/cToken = temp; printed = 0;return temp;}temp.kind = Id; /*縲€繝ヲ繝シ繧カ縺ョ螳」險€縺励◆蜷榊燕縺ョ蝣エ蜷医€€*/strcpy(temp.u.id, ident);break;case digit: /* number */num = 0;do {num = 10*num+(ch-‘0’);i++; ch = nextChar();} while (charClassT[ch] == digit);if (i>MAXNUM)errorMessage(“too large”);temp.kind = Num;temp.u.value = num;break;case colon:if ((ch = nextChar()) == ‘=’) {ch = nextChar();temp.kind = Assign; /*縲€”:=”縲€*/break;} else {temp.kind = nul;break;}case Lss:if ((ch = nextChar()) == ‘=’) {ch = nextChar();temp.kind = LssEq; /*縲€”<=”縲€*/break;} else if (ch == ‘>’) {ch = nextChar();temp.kind = NotEq; /*縲€”<>”縲€*/break;} else {temp.kind = Lss;break;}case Gtr:if ((ch = nextChar()) == ‘=’) {ch = nextChar();temp.kind = GtrEq; /*縲€”>=”縲€*/break;} else {temp.kind = Gtr;break;}default:temp.kind = cc;ch = nextChar(); break;}cToken = temp; printed = 0;return temp;}
Token checkGet(Token t, KeyId k) /*縲€t.kind == k縺ョ繝√ぉ繝�け縲€*//*縲€t.kind == k 縺ェ繧峨€∵ャ。縺ョ繝医�繧ッ繝ウ繧定ェュ繧薙〒霑斐☆縲€*//*縲€t.kind != k 縺ェ繧峨お繝ゥ繝シ繝。繝�そ繝シ繧ク繧貞�縺励€》 縺ィ k縺悟�縺ォ險伜捷縲√∪縺溘�莠育エ�ェ槭↑繧峨€€*//*縲€t 繧呈昏縺ヲ縲∵ャ。縺ョ繝医�繧ッ繝ウ繧定ェュ繧薙〒霑斐☆�� t 繧� k縺ァ鄂ョ縺肴鋤縺医◆縺薙→縺ォ縺ェ繧具シ峨€€*//*縲€縺昴l莉・螟悶�蝣エ蜷医€〔 繧呈諺蜈・縺励◆縺薙→縺ォ縺励※縲》 繧定ソ斐☆縲€*/{if (t.kind==k)return nextToken();if ((isKeyWd(k) && isKeyWd(t.kind)) ||(isKeySym(k) && isKeySym(t.kind))){errorDelete();errorInsert(k);return nextToken();}errorInsert(k);return t;}
static void printSpaces() /*縲€遨コ逋ス繧�隼陦後�蜊ー蟄励€€*/{while (CR– > 0)fprintf(fptex, “n”);/* fprintf(fptex, ” parn”); */while (spaces– > 0)fprintf(fptex, ” “);/* fprintf(fptex, ” “); */CR = 0; spaces = 0;}
void printcToken() /*縲€迴セ蝨ィ縺ョ繝医�繧ッ繝ウ縺ョ蜊ー蟄励€€*/{int i=(int)cToken.kind;if (printed){printed = 0; return;}printed = 1;printSpaces(); /*縲€繝医�繧ッ繝ウ縺ョ蜑阪�遨コ逋ス繧�隼陦悟魂蟄励€€*/if (i < end_of_KeyWd) /*縲€莠育エ�ェ槭€€*/fprintf(fptex, “<b>%s</b>”, KeyWdT[i].word);/* fprintf(fptex, “{bf %s}”, KeyWdT[i].word); */else if (i < end_of_KeySym) /*縲€貍皮ョ怜ュ舌°蛹コ蛻�j險伜捷縲€*/fprintf(fptex, “%s”, KeyWdT[i].word);/* fprintf(fptex, “$%s$”, KeyWdT[i].word); */else if (i==(int)Id){ /*縲€Identfier縲€*/switch (idKind) {case varId:fprintf(fptex, “%s”, cToken.u.id); return;case parId:fprintf(fptex, “<i>%s</i>”, cToken.u.id);return;/* fprintf(fptex, “{sl %s}”, cToken.u.id); return; */case funcId:fprintf(fptex, “<i>%s</i>”, cToken.u.id);return;/* fprintf(fptex, “{it %s}”, cToken.u.id); return; */case constId:fprintf(fptex, “<tt>%s</tt>”, cToken.u.id);return;/* fprintf(fptex, “{sf %s}”, cToken.u.id); return; */}}else if (i==(int)Num) /*縲€Num縲€*/fprintf(fptex, “%d”, cToken.u.value);}
void setIdKind (KindT k)/*縲€迴セ繝医�繧ッ繝ウ(Id)縺ョ遞ョ鬘槭r繧サ繝�ヨ縲€*/{idKind = k;}Question 3 Answer the questions to add the followig if-then-else statement (the if-then statement with the optional else clause) to PL/O’. Production rule statement + if condition then statement; (else statement! Action A statement ‘if condition then statement; (else statement, e)’ works as follows. 1. Evaluate condition. 2. If the value of condition is true, execute statement]. 3. If the value of condition is false and statement, exists, execute statement2. Description To resolve ambiguity of the grammar of PL/0′, we use the follow- ing rule. • When we find an else, we relate the else to the nearest then which has not be related to any else yet. Question 3-1 Write object codes for a statement ‘if condition then statement; else statement2′ like object codes for a do-while statement of Fig.1. Question 3-2 Modify get Source.h and get Source.c to register a token else to your PL/0 compiler. Explain the modification in your report. Question 3-3 Modify a function statement in compile.c so that your PL/0′ compiler can output object codes for if-then-else statements. Explain the modificaiton in your report. #include “table.h” #endif #include “getSource.h” #define MAXTABLE 100 #define MAXNAME 31 #define MAXLEVEL5 *名前表の最大長さ* *名前の最大長さ *ブロックの最大深さい。 typedef struct tableE{ * 名前表のエントリーの型 KindT kind; *名前の種類別 char name[MAXNAME); * 名前のつづり* union{ int value; * 定数の場合:値* struct { RelAddr raddr, 関数の場合:先頭アドレス* int pars; * 関数の場合:パラメタ数* RelAddr raddr; *変数、パラメタの場合:アドレ }u; }TabelE; static TabelE name Table MAXTABLET Z T */ Show transcribed image text Question 3 Answer the questions to add the followig if-then-else statement (the if-then statement with the optional else clause) to PL/O’. Production rule statement + if condition then statement; (else statement! Action A statement ‘if condition then statement; (else statement, e)’ works as follows. 1. Evaluate condition. 2. If the value of condition is true, execute statement]. 3. If the value of condition is false and statement, exists, execute statement2. Description To resolve ambiguity of the grammar of PL/0′, we use the follow- ing rule. • When we find an else, we relate the else to the nearest then which has not be related to any else yet. Question 3-1 Write object codes for a statement ‘if condition then statement; else statement2′ like object codes for a do-while statement of Fig.1. Question 3-2 Modify get Source.h and get Source.c to register a token else to your PL/0 compiler. Explain the modification in your report. Question 3-3 Modify a function statement in compile.c so that your PL/0’ compiler can output object codes for if-then-else statements. Explain the modificaiton in your report.
#include “table.h” #endif #include “getSource.h” #define MAXTABLE 100 #define MAXNAME 31 #define MAXLEVEL5 *名前表の最大長さ* *名前の最大長さ *ブロックの最大深さい。 typedef struct tableE{ * 名前表のエントリーの型 KindT kind; *名前の種類別 char name[MAXNAME); * 名前のつづり* union{ int value; * 定数の場合:値* struct { RelAddr raddr, 関数の場合:先頭アドレス* int pars; * 関数の場合:パラメタ数* RelAddr raddr; *変数、パラメタの場合:アドレ }u; }TabelE; static TabelE name Table MAXTABLET Z T */

Expert Answer


Answer to Homework from programming language processor class… I really need help with the codes… Thank you very much getsource…

OR