Problem Description:
Write a recursive descent compiler for the following grammar:
Here,
Since
Your lexical analyzer should be hand-coded for this assignment; that is, you will not use a lexical analyzer generator.
The statements will be encoded for a hypothetical stack machine with the following instructions:
PUSHv— push v (an integer constant) on the stack
RVALUE l— push the contents of variable l
LVALUEl— push the address of the variable l
POP— throw away the top value on the stack
STO— the rvalue on top of the stack is place in the lvalue below it and both are popped
COPY— push a copy of the top value on the stack
ADD — pop the top two values off the stack, add them, and push the result
SUB — pop the top two values off the stack, subtract them, and push the result
MPY — pop the top two values off the stack, multiply them, and push the result
DIV — pop the top two values off the stack, divide them, and push the result
MOD — pop the top two values off the stack, compute the modulus, and push the result
POW — pop the top two values off the stack, compute the exponentiation operation,
and push the result
HALT— stop execution
You may use
C, Java, Ada, or Python to write your program. You will, of course, find it necessary to remove left recursion in certain productions. Your compiler will prompt for the name of the input program file, and send the output to standard output.
No symbol table is necessary for this exercise. If you construct one, management would be suitably impressed.
On errors, a “meaningful” error message should be printed; i.e., “DO expected in line xx”, “expression expected”, etc. Management would be impressed if you attempted to recover from an error rather than simply halting compilation.
For example, if you are translatiing
begin answer := alpha + 2 * gamma div (C3P0 – R2D2) end
Your compiled code would be:
LVALUE answer
RVALUE alpha
PUSH 2
RVALUE gamma
MPY
RVALUE C3P0
RVALUE R2D2
SUB
DIV
ADD
STO
HALT
begin alpha := 20; gamma := 11; C3P0 := 5; R2D2 := 4; answer := alpha + 2 * gamma div (C3P0 - R2D2); gun := (i mod ammo)^2^2^2 end
代写CS&Finance|建模|代码|系统|报告|考试
编程类:C++,JAVA ,数据库,WEB,Linux,Nodejs,JSP,Html,Prolog,Python,Haskell,hadoop算法,系统 机器学习
金融类:统计,计量,风险投资,金融工程,R语言,Python语言,Matlab,建立模型,数据分析,数据处理
服务类:Lab/Assignment/Project/Course/Qzui/Midterm/Final/Exam/Test帮助代写代考辅导
E-mail:850190831@qq.com 微信:BadGeniuscs 工作时间:无休息工作日-早上8点到凌晨3点
如果您用的手机请先保存二维码到手机里面,识别图中二维码。如果用电脑,直接掏出手机果断扫描。