Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In the lecture we discussed the technique of Emulation by Interpretation. Now let's try to implement a simple version of it. Notice that in most

image text in transcribedimage text in transcribed

In the lecture we discussed the technique of "Emulation by Interpretation". Now let's try to implement a simple version of it. Notice that in most cases the emulation will target binaries, but for the sake of readability, we will use a very restrictive form of C program as the target of emulation. For this task we will use Python 3. If you really, really want to use a different programming language, post on Piazza, mention which language you want to use, and we can discuss whether that will be allowed. Write a Python3 program that takes 1 command line argument, which is used to provide the path (including name) of a target source file. The Python 3 program should then read in the target source file and mimic its computation. The target source file uses a subset of the C programming language and contains some simple integer arithmetic. For simplicity, you can assume that it has the following properties: important to your Python3 program, but they provide exact-width integer types and some basic I/O functions to C programs. - There will then be only 1 function, that is, int main() without any arguments. The curly brackets { and } for main () will each be on a separate line. - The body of main() will have no indentation. Moreover, there will only be 4 types of statements, namely: - Declaration of variables, e.g. int32_t aaa;. Note that for simplicity, there will be no variable initialization during declaration. - Assignment to variables, e.g. aaa =23;. Note that for simplicity, you can assume that on the right hand side of the equal sign, there will always be 2 operands (could be integer literals or other variables), and the arithmetic operator will only be +,,,1,%. That is, addition, subtraction, multiplication, division, and modulo. - Print out variables, e.g. printf("\%d , aaa) ; . For simplicity, you can assume that the format string will always be "\% ", and it only prints one variable (no integer literals). - Return value, e.g. return 0; . After returning from main (), the remaining C statements should not be executed. Make sure you return the same value in your Python 3 program though. - All variables are of the type int32_t, that is, 32-bit signed integers. You can also assume that they are all statically allocated (memory is allocated at "compile time", and there would be no dynamic memory allocation via the use of malloc () and pointers). You can also assume that all variables will be declared and have values assigned before they are first referenced (used on the right hand side of an assignment statement). - To make it easier to parse, you can assume that there will be exactly 1 space character between arithmetic operators in assignment statements, after comma in printf statements, and after the data type in variable declarations. There will be no empty lines in the input source files. You can refer to the following source files as examples. If you save each of the sample inputs above and compile with gcc, you can see that despite the very restrictive syntax, they are all valid C programs, and the resulting binaries should run. Make sure that your Python3 interpreter delivers the same outputs on those C files as the binaries compiled with gcc, and can be invoked like this: python3 -a1p1.py Hints: - Iterate the C source file line-by-line, then use Python's str.split(' ') (see https://docs.python.org/ 3.8/1ibrary/stdtypes. html\#str.split) to get the lexemes on each line, and decide which type of statement it is. - You can use Python's dictionary type (see https ://docs . python . org/3.8/tutorial/datastructures . html\#dictionaries) as the symbol table (seehttps://en.wikipedia.org/wiki/Symbol_table) to keep track of the variables in the C source file and their corresponding values. - Converting string into an integer in Python 3 is easy by calling int (str). Most of the arithmetic operations can be directly performed in Python3, however, you would want to pay attention to integer overflow and modulo with negative numbers. The emulation has to follow the behavior of binaries compiled with gcc. - Please remember to include the Python source file in your submission . zip file

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

Students also viewed these Databases questions