Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

What to do: For mathematical expressions, we generate object code of a very simple stack machine and write it to Verta Run it with Lumasin

What to do: For mathematical expressions, we generate object code of a very simple stack machine and write it to Verta
Run it with Lumasin (so-called simulator) and check the result.
It is similar to Java's programming & execution environment, so please understand what the javac command and java command are doing.
Question 1-) Modify the Exp class (to calculate an integer) so that it produces an instruction word, make sure that it is correct by executing the command word, make it by modifying it from Exp class. , Create an instruction to add that value to the stack and change it to generate an operation instruction instead of actually doing the calculation.Please understand and change it by thinking about the principle.Calculate the constant so use it There are only a few instructions, just to call the methods genCodeV and genCodeO to create an instruction.
Methods such as term, fact, and prim do not perform calculations, so you do not need to return values, and all methods are void with no return value. (Copy and modify Exp.java.) Remove calculations and declarations of num, num 1, num 2. However, of course, it is necessary to call the term, fact, prim, method.
For processing expressions
When constructing an instance of the Exp class, you need to pass the created cellV as an argument to generate the instruction code.
Question2-)There is no% or ^ operation in the specification of the virtual machine CellV (execute method), so extend the display method (printCode) and execution method (execute) of the instruction of the simulator by adding appropriate declaration. Modify CellV.java written in.
Source code :
Exp.java:
public class Exp {
String[] cal;
String token;
int idx;
public Exp(String[] eee,int iii) { // constructor
// formal marameter --> instance value
cal = eee;
idx = iii;
token = cal[iii];
}
int expression() { /* Expression */
char k;
int num1=term();
while ( (token.charAt(0)== '+') || (token.charAt(0) == '-') ) {
k = token.charAt(0);
token = cal[++idx];
int num2=term();
if (k == '+') { num1=num1+num2; }
else { num1=num1-num2; }
}
return num1;
}
int term() { /* Term */
char k;
int num1=factor();
while ( (token.charAt(0) == '*') || (token.charAt(0) == '/') ) {
k = token.charAt(0);
token = cal[++idx];
int num2=factor();
if (k == '*') { num1=num1*num2; }
else { num1=num1um2; }
}
return num1;
}
int factor() { /* Factor */
int num=0;
if ( (token.charAt(0)>='0') && (token.charAt(0)
num = Integer.parseInt(token); /* String --> int */
token = cal[++idx];
} else if (token.charAt(0) == '(') { /* ( expression ) */
token = cal[++idx];
num = expression();
if (token.charAt(0)!=')') System.out.println(" ) expected. ");
token = cal[++idx];
} else {
System.out.println("syntax error ");
}
return num;
}
}
CellV.java
public class CellV {
boolean pCode, pTrace; /* display flag */
public CellV( ) { }
/* instruction code */
static final int ict = 1; /* InstVal */
static final int lit = 2; /* InstVal */
static final int lod = 3; /* InstAddr */
static final int lda = 4; /* InstAddr, load address */
static final int sto = 5; /* InstAddr, store */
static final int cal = 6; /* InstAddr */
static final int ret = 7; /* InstRet */
static final int dit = 8; /* display taihi */
static final int dik = 9; /* display kaihuku */
static final int jmp = 10; /* InstVal */
static final int jpc = 11; /* InstVal */
static final int arr = 12; /* InstAddr */
static final int opr = 13;
/* oprator code */
static final int neg = 20;
static final int add = 21;
static final int sub = 22;
static final int mul = 23;
static final int div = 24;
static final int eql = 25; /* == */
static final int neq = 26; /* != */
static final int lss = 27; /*
static final int gtr = 28; /* > */
static final int leq = 29; /*
static final int geq = 30; /* >= */
static final int lid = 31; /* LoadInDirect */
static final int ini = 32; /* read number */
static final int sid = 33; /* StoreInDirect */
static final int prt = 34;
static final int prl = 35; /* print line */
static final int stp = 36; /* stop */
static final int MAXCODE = 200; /* length of object code */
static final int MAXMEM = 2000; /* size of stack */
static final int MAXREG = 500;
Inst code[] = new Inst[MAXCODE]; /* object code */
int cIndex = -1; /* index of code[] */
int stack[] = new int[MAXMEM]; /* stack */
int top = 0; /* stack pointer */
int pc = 0; /* program counter */
int o1 = 0, o2 = 0;
/* instruction type */
class Inst { /* instruction form */
int opCode;
Inst(int op) { opCode = op; }
public String toString() { return codeName(opCode); }
}
class InstVal extends Inst { /* value instruction */
int value;
InstVal(int op, int v) { super(op); value = v; }
public String toString() { return super.toString() + " , value=" + value; }
}
class InstOp extends Inst { /* operation */
int optr;
InstOp(int op, int o) { super(op); optr = o; }
public String toString() { return super.toString() + " , optr=" + codeName(optr); }
}
String codeName(int c) { /* kind of instruction */
switch (c) {
case ict: return "ict"; case lit: return "lit";
case lod: return "lod"; case cal: return "cal";
case ret: return "ret"; case dit: return "dit";
case dik: return "dik"; case jmp: return "jmp";
case jpc: return "jpc"; case arr: return "arr";
case sto: return "sto"; case opr: return "opr";
case neg: return "neg"; case add: return "add";
case sub: return "sub"; case mul: return "mul";
case div: return "div"; case eql: return "eql";
case neq: return "neq"; case lss: return "lss";
case gtr: return "gtr"; case leq: return "leq";
case geq: return "geq"; case prt: return "prt";
case prl: return "prl"; case ini: return "ini";
case sid: return "sid"; case lid: return "lid";
case lda: return "lda"; case stp: return "stp";
default: return " ";
}
}
/* code generator */
int genCodeV(int op, int v) { /* V type (value) */
checkMax();
code[cIndex] = new InstVal(op, v);
return cIndex;
}
int genCodeO(int p) { /* O type (operation) */
checkMax();
code[cIndex] = new InstOp(opr, p);
return cIndex;
}
void checkMax() { /* check length of code[] */
if (++cIndex
else { System.out.println(" code[] overflow"); }
}
int nextCode() { return cIndex + 1; } /* return next instruction */
void printCode() { /* display code */
System.out.println("--- code ---");
for (int i = 0; i
}
void error() { /* run-time error */
code[pc] = new InstOp(opr, stp); /* generate stop instruction */
}
void execute() {
Inst i;
int dnum=0;
int mcycl=0;
do {
dnum++;
mcycl++;
i = code[pc++];
switch (o1 = (i.opCode)) {
case ict:
top += ( (InstVal) i).value;
if (top >= MAXMEM - MAXREG)
{ System.out.println(" stack overflow "); error(); }
break;
case lit:
stack[top++] = ( (InstVal) i).value; break;
case jmp:
pc = ( (InstVal) i).value; break;
case jpc: /* false */
if (stack[--top] == 0) { pc = ( (InstVal) i).value; } break;
case opr:
o2 = ( (InstOp) i).optr;
switch (o2) {
case neg: stack[top - 1] = -stack[top - 1]; continue;
case add: --top; stack[top - 1] += stack[top]; continue;
case sub: --top; stack[top - 1] -= stack[top]; continue;
case mul: --top; stack[top - 1] *= stack[top];
mcycl=mcycl+1; continue;
case div: --top; stack[top - 1] /= stack[top];
mcycl=mcycl+2; continue;
case eql: /* true->1, false->0 */
--top; stack[top - 1] = (stack[top - 1] == stack[top] ? 1 : 0); continue;
case neq: --top; stack[top - 1] = (stack[top - 1] != stack[top] ? 1 : 0); continue;
case lss: --top; stack[top - 1] = (stack[top - 1]
case gtr: --top; stack[top - 1] = (stack[top - 1] > stack[top] ? 1 : 0); continue;
case leq: --top; stack[top - 1] = (stack[top - 1]
case geq: --top; stack[top - 1] = (stack[top - 1] >= stack[top] ? 1 : 0); continue;
case prt:
System.out.print(stack[--top]); continue;
case prl:
System.out.println(); continue;
case sid:
stack[stack[top - 2]] = stack[top - 1]; top -= 2; continue;
case lid:
stack[top - 1] = stack[stack[top - 1]]; continue;
}
}
}
while (o2 != stp);
System.out.println("dynamic object step "+dnum);
System.out.println("machine cycle "+mcycl);
}
}
Cell.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Cell {
public static void main(String[] args) throws IOException {
System.out.println("input expression with = ");
BufferedReader r =
new BufferedReader(new InputStreamReader(System.in));
String s = r.readLine();
String[] token = s.split(" ");
int n=token.length;
if (n
System.out.println(" expression missed ");
} else {
CellV cellV = new CellV();
System.out.println("--- start compilation ---");
for (int i=0 ; i
System.out.print(token[i]+" ");
}
Exp exp = new Exp(token, 0,cellV);
exp.expression();
System.out.println(" ");
cellV.genCodeO(CellV.prt); /* print instruction */
cellV.genCodeO(CellV.prl);
cellV.genCodeO(CellV.stp); /* stop instruction */
cellV.printCode(); /* display object code */
System.out.println("--- start execution ---");
cellV.execute(); /* execute object code on VM */
} /* n>0 */
} /* main */
} /* class Cell */
<>
image text in transcribed
I need correct response soon.
Thank you.
123 14) :3 ?????? Exp??? CelIV??? (????????) exp.expresson code 0 lit, 123 1: lit, 4 2 opr, div 3: lit 3 4: opr, mul 5 opr, wrt 6: opr, wr 7: jmp, o exxpression ??code[ cellV.genCodeCodey cellV.genCodeO0 execute ??????????? 90 ?????8 ????????13 ?1 ?????? Cell main???? Cen1???? ?????? cellv Exp???? ?????? exp corre ??????? exp? ??????? Exp?expression() ????????? : celV@genCodeO0 ???????? cerv?genCodeO() ???????? ???????? ???????? ceIV?execute() ???????? ?2 ?????(??????)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Algorithmic Trading Navigating The Digital Frontier

Authors: Alex Thompson

1st Edition

B0CHXR6CXX, 979-8223284987

More Books

Students also viewed these Databases questions