Homework 3 : Compiling Simple Expressions toP–CodeZhiyao LiangMUST [email protected] 17 December 2018AbstractThe task of this assignment is to implement a compiler that canevaluate simple arithmetic expressions. The focus is on the code generationpart. [2] [1].1 PreparationYou need to have some programming environment to edit, compile and runC programs.1The concepts of stack should be understood. The picture above describesa stack, which is found from the web at https://www.cs.cmu.edu/~adamchik/15-121/lectures/Stacks%20and%20Queues/pix/stack.bmpBesides the textbook [2], especially chapter 8, there are several documentsthat are helpful for this homework: In the folder slides at the ftp site of this course:– The PowerPoint file for Code Generation. This file presents mainideas in chapter 8 of the textbook. Especially, some pictures ofparse trees and the flow chart of translating while–statement andif–statement are shown.– p-code.pdf and p-code.txt. The two files have the same content,which describes (a subset of) P-code instructions, which explanations,that are relevant to this project. In the folder ExpS--to--P, which is provided with this homework, thereare several files .– The .exp files are the code of ExpS programs.– The .p files are the P-code programs translated from the corresponding.exp files. P-code program for array.exp is not provided. gencode.c which is provided for this homework. This file combinesthe code provided in Chapter 8 of the textbook. This file describes ageneral skeleton of a code generator.2 P-code MachineThe runtime environment of a P–code virtual machine (PVM) can be implementedas follows:There are 4 pieces of data structures: code area: It is an array of P–code instructions. There are two waysthat instructions can be loaded into the code area:1. All together: A sequence of P–code instructions can be loaded alltogether to the code area from an existing P–code program file.2. On the fly: P–code instruction will be appended one-by-one tothe instruction array by some language translator.2When a PVM is running, the current instruction’s address (an indexin the array) is remembered. By default, current = current + 1, afterthe current instruction is executed. A special case is a jumpinginstruction: when the current is a jump instruction to some label, andthe corresponding label instruction is at address j, then current willbe changed to j+1.Once an instruction is added to the code area, it should not be removedor changed. data area: It is an array of integers (since we only require integeroperations). The values of all variables will be saved in this area. Theaddress of a variable x is the index in the array where x’s value issaved. symbol table: It is a data structure that supports the following operations:– lookup(x): Given a name (a string) return its address in the dataarea. If x does not exists, some special signal is returned. Thenames include variable names and label names. The value of alabel is the corresponding label instruction address in the codearea.– insert(x): Allocate a new slot in the data area for the value ofthe name x (x is not initialized at the moment). Nothing is doneif x is already existing. The address of x should be registeredin the symbol table so that it can be looked up. Therefore, thesymbol table remembers the next available address in the dataarea, and the size of the data area. When a name x is used byan instruction (lod or lda), if x is new (lookup(x) cannot find it),x is inserted into the symbol table. A special kind of name is alabel. The value of a label is the address of the correspondinglabel defining instruction in the code area. stack: A stack is needed by the P-code instructions. (Read the descriptionof P-code instructions p-code.pdf). It is an array of integer.A stack remembers:– the stack top, which is an index in the array, it is the addresswhere the latest data item is saved. Data are added or removedfrom the stack at the top. Initially, when the stack is empty, thetop could be -1.3– the size of the stack: the maximum number of data items of thestack, which can be implemented as the size of the array.The stack supports 2 operations:– pop(): When the stack is not empty, return the data (integer) atthe top, and top = top ? 1. When the stack is empty, nothing ispopped, and some error message is printed out.– push(c): When the stack is not full, top = top + 1, and c issaved at the top in the data area. When the stack is already full,nothing is pushed, and some error message is printed.There are two ways that a source program can be translated to andexecuted as P-code instructions. Compiled. A source program is translated to a P-code program, andthen the P-code program is loaded and executed. Interpreted. A statement of the source program is translated to asequence of P-code instructions, then, this sequence of P–code instructionsare executed. Especially, for a structured statement: Compound–statement, If–statement, or While–statement, the PVM will wait untilthe whole structured statement is provided and then start to run thestructured statement.There are two different ways to print results on the screen when a PVMis running. Verbose mode: After each expression or assignment statement, thevalue of the expression or the RHS of the assignment is printed. Thecalculator described in homework 1 could be considered in the verbosemode. Silent mode: Only the write instruction (wri) will print an integer onthe screen. Otherwise, nothing is printed.A programmer can decide which mode to choose.2.1 P–code programA P–code program is a sequence of P–code instructions. A semicolon ; willstart a comment that stop at the end of line. Detailed descriptions of P–codeinstructions are in tP–Code留学生作业代写、代做C++编程作业、代写arithmetic expression作业、代写C/C++语言作he textbook and the file p-code.pdf. A P–code programwill stop when there is no more instruction to run, or the halting instruction(stp) is executed.43 The enriched ExprS languageThe language Expr–Simple (ExpS) is defined the same as in homework 1; inorder to make the language more powerful and let the code generation taskmore interesting, we allow additional features in ExpS, as follows: Comparison operators are allowed: >, =, 6 more token types are added for them. While–statement is allowed, and the token while is considered a reservedword. If–statement is allowed, and the two tokens if and else are consideredas reserved words. Curly braces, { and }, are allowed. A simple statement (assignment or expression statement) will end ata semicolon or newline. EOF token is not required. If we require that every ExpS programends with a quit, then we don’t need the EOF token.A programmer has the freedom to decide the token types.3.1 Updated lexical rules of ExprSCorrespondingly, the updated lexical rules to define tokens of ExpS aredescribed by regular–expressions as follows in Table 3.1. This table is thesame as we saw in homework 1, but with additional token types.More descriptions of the tokens are in the document for homework 1.3.2 Updated syntax rules of ExpSThe updated grammar that allows while and if statements are shown in Table3.2. For the tokens that are not ID or numbers or keywords, their stringliterals, instead of their token types, are directly shown in the grammarfor simplicity of presentation. For two items in the grammar (variable orconstant) X and Y , we use space in stead of · (which is used in homework1) to describe the concatenation of X and Y ; i.e, X Y means the same asX · Y .Note that the grammar rules in Table 3.2 is not ideal for writing arecursive–descendent parser, since it does not represent the precedence and5ASSIGN : =P LUS : +MINUS : T IMES : DIV ID : /LP AREN : (LCUR : {RCUR : }RP AREN : )EQ : ==NEQ : ! =GT : >LT : GT E : >=LT E : Digit : 0 . . . 9N atural : Digit · Digit?NUMBER : N atural | M inus · N aturalLetter : a . . . z | A . . . ZID : Letter · Letter?QUIT : quitW HILE : whileIF : ifELSE : elseRET URN : \n SEMI : ;Table 1: the lexical rules of tokensassociativity of operators in the expressions. You can adjust the grammaraccording to your consideration.4 Tasks to do1. Implement a PVM (virtual machine of P–code). When a PVM isrunning, it will execute the instructions that are in its memory of codearea.2. Write a language translator that can translate ExpS statements toP–code instructions. There are two choices to design the translator.6program → statementList QUITstatementList → statement | statement statementListstatement → assignmentSt | expressionSt | whileSt |ifSt | compoundStassignment → ID = expressionassignmentSt → assignment ;expression → NUMBER | ID | ( expression ) |expression operator expressionexpressionSt → expression ;operator → + | ? | ? | / |== | ! = | > | = | whileSt → W HILE · ( expression ) statementifSt → IF ( expression ) statement |IF ( expression ) statement ELSE statementcompoundSt → { statementList }Table 2: Grammar of the ExpS language An interpreter: An ExpS statement is translated to some P–codeinstructions, then these instructions are executed, then continueto process the next ExpS statement. The calculator of homework1can be implemented as an interpreter. A compiler: An ExpS program (a sequence of statements) aretranslated all together to a sequence of P–code instructions, thenthese instructions are executed.You are only required to implement one (interpreter of compiler).Bonus points will be earned if you can do the following parts You program can work both as a compiler or interpreter. Support arrays of the ExpS language. You may modify the grammar ofExpS in Table 3.2 for arrays. Hint: the grammar listed in cgencode.cwill be helpful.Here is some recommended strategy of doing this homework. Continue to use the data structure of homework 1. Translate simple expressions first, i.e. only the assignment and arithmeticstatements of ExpS. Then try the if and while statements.7 If your homework 1 work is not perfect, you can focus on the codegenerator part in this homework, i.e, the code generator can assumesome parse tree is already in the memory.5 Submitting your homework Due time: 12 January 2018 Saturday, 7:00pm. At most 3 students can form a group to do this homework together. At the top of the every text file that you changed or created (maybejust the .c and .h files), record your name (if mutiple students formagroup, provide all the names) and date as comments. Please attach all of your source code and document files to the email,possibly zip them into a single file. Any information about how to runand understand your code (a readme file) will be helpful. Attention: Do not attach any binary files (.o, .obj, .exe, .out), sincegoogle will consider them virus. Just send the source code and textualdocuments. Send your email to:[email protected] The title (subject) of your email should be like:[name][hmkNum info]Name can be in Chinese or English (Pingying). For example, if astudent wants to submit homework 5, whose name Shan Li, then thetitle of the email should be:[Li, Shan][hmk3 code-generator]If it is a group work, include all members’ name in the email title.8References[1] Andrew W. Appel. Modern Compiler Implementation in C: Basic Techniques.Cambridge University Press, New York, NY, USA, 1997.[2] Kenneth C. Louden. Compiler Construction Principles and Practice.PWS Publishing Company, 1997. ISBN 0-534-93972-4http://www.cs.sjsu.edu/~louden/cmptext/.转自:http://ass.3daixie.com/2019011023291421.html