+1 (208) 254-6996 [email protected]
  

compilers

COMP 525 – Fall 2021

Don't use plagiarized sources. Get Your Custom Essay on
Compilers
Just from $13/Page
Order Essay

Homework 1

This homework assignment builds on the class exercise of an expression parser.

1. Adapt the parser we did in class so that it can handle

numbers with multiple digits

spaces

For example, evaluate “51 +3 – 1004+16 “

2. Create a new operator ^ that does exponentiation. Exponents have the highest precedence.

For example, “3*2^5+1” evaluates to 97

Plan on writing another recursive function. You should use the C pow command to evaluate.

3. Adapt the parser to handle floating-point numbers, for example:

“51.2+3.789”

4. Adapt the assembly generator to produce RISCY assembly code. (Don’t bother implementing floating point or power). You should use the following instructions and registers:

registers: A, B, C, D

instructions: (note that you can use any register in place of A and B)

add a,b adds A = A + B

sub a,b subs A=A-B

.move a,b A=B

push a saves A on the stack

pop b retrieves B from the stack

li a,57 puts number 57 into register A

.call mul calls a function called MUL. The A and B registers hold the numbers, the product is returned in A. (You must use those registers.)

.call div same, but for dividing

.call printint prints a number in A (must be one or two digits)

An assembler and an emulator are provided so you can test your code.

Your program should prompt the user for a string and output a file exp.riscy that compiles with the python riscyasm.py exp.riscy and can be emulated using ./riscy exp.riscy.bin.txt You will need to use C FILE commands to save, and have your program put the included start.riscy and end.riscy code at the beginning and end of your work.

#define INTEGER_TYPE 1 #define OPERATOR_TYPE 2 #include <stdio.h> //like I’m defining a Token class typedef struct { //integer will get populated if I’m parsing an int int integer; //op will get populated if it’s an operator char op; //tells which type it is? //1 is integer, 2 is operator int type; //might expand this so it can handle floats } Token; //const char* line=”5*3*2*1″; const char* line=”5*2+6*2″; int pointer=0; void gettoken(Token* token) { //look at line[pointer] //is line[pointer] a number or an operator? if(line[pointer]>=’0′ && line[pointer]<=’9′) { token->type=INTEGER_TYPE; //convert it to an integer token->integer = line[pointer] – ‘0’; } else { token->type=OPERATOR_TYPE; token->op=line[pointer]; } } //advance: move to next token void advance() { pointer++; } //term returns the evaluation int term() { Token firsttoken; gettoken(&firsttoken); //integer advance(); //moved past the integer printf(“term: just parsed %d\n”,firsttoken.integer); //check the next token without moving past it Token optoken; gettoken(&optoken); // *? if(optoken.type==OPERATOR_TYPE && optoken.op==’*’) { //move past the asterisk advance(); printf(“term: parsed an *\n”); int secondvalue = term(); return firsttoken.integer * secondvalue; } else return firsttoken.integer; //return integer } int expression() { int firstvalue = term(); if(line[pointer]==’\0′) //hit the end { printf(“expression: reached end\n”); return firstvalue; } else { Token plus; gettoken(&plus); //get the + advance(); printf(“expression: +\n”); int secondvalue=expression(); return firstvalue+secondvalue; } } void parse() { pointer=0; int answer=expression(); printf(“the answer is %d\n”,answer); } int main(int argc, char* argv[]) { parse(); return 0; }

Order your essay today and save 10% with the discount code ESSAYHELP