首页 > > 详细

program程序设计讲解、C++编程辅导、C++语言程序调试解析C/C++编程|辅导R语言编程

Simple Pascal interpreter
The purpose of this assignment is to give you practice writing a parser and an interpreter for simplified Pascal. You can start this assignment with your solution to Assignment #2, or you can use Asgn02Cpp.zip.

Modify the frontend parser to include the WHILE, FOR, IF, and CASE statements of the simplified Pascal. Use the syntax diagrams and the parse trees shown in the lecture notes. Then modify the interpreter’s backend executor to execute those statements.

Make any necessary modifications to the other classes. For example, you may need to add more parse tree node types.

Suggested order
Here is a suggested order to implement the additional statements:

1.WHILE statement. It’s similar to the REPEAT statement.
2.IF statement. Handle statements with and without an ELSE part. Be sure to handle a “dangling else” correctly.
3.FOR statement. It’s a more elaborate parse tree, so build it carefully according to the parse tree in the lecture notes.
4.CASE statement. The most elaborate parse tree. Build it very carefully according to the parse tree in the lecture notes.

For each statement, first make sure your parse tree is correct by visually inspecting the tree that the -parse option prints. Then work on executing the statement.

Test input files
Here are test input files that you should run for each of the statements and the expected runtime output. You may want to create your own simpler tests before trying these. In particular, TestIf.txt has some complicated expressions that will require modifications to the expression parser and executor. Click on the links to download the files.

You can use the online Pascal compilers to verify the output of the test files. But you’ll need to add variable declarations to turn them into valid Pascal programs.

TestWhile.txt TestWhile.out.txt
PROGRAM TestCase;
BEGIN
i := 3; even := -999; odd := -999; prime := -999;
CASE i+1 OF
1: j := i;
-8: j := 8*i;
5, 7, 4: j := 574*i;
END;
write('j = '); writeln(j);
writeln;
FOR i := -5 TO 15 DO BEGIN
CASE i OF
2: BEGIN even := i; prime := i END;
-4, -2, 0, 4, 6, 8, 10, 12, 14: even := i;
-5, -3, -1, 1, 3, 5,
7, 9, 11, 13, 15: BEGIN
odd := i;
CASE i OF
2, 3, 5, 7, 11, 13: prime := i
END
END
END;
write('i ='); write(i:3);
write(', even = '); IF even <> -999 THEN write(even:3) ELSE write('...');
write(', odd = '); IF odd <> -999 THEN write(odd:3) ELSE write('...');
write(', prime = '); IF prime <> -999 THEN write(prime:3) ELSE write('...');
writeln;
even := -999; odd := -999; prime := -999
END;

writeln; writeln('Done!')
END. j = 1722

i = -5, even = ..., odd = -5, prime = ...
i = -4, even = -4, odd = ..., prime = ...
i = -3, even = ..., odd = -3, prime = ...
TestCase.out.txt i = -2, even = -2, odd = ..., prime = ...
i = -1, even = ..., odd = -1, prime = ...
i = 0, even = 0, odd = ..., prime = ...
i = 1, even = ..., odd = 1, prime = ... i = 2, even = 2, odd = ..., prime = 2 i = 3, even = ..., odd = 3, prime = 3 i = 4, even = 4, odd = ..., prime = ... i = 5, even = ..., odd = 5, prime = 5 i = 6, even = 6, odd = ..., prime = ... i = 7, even = ..., odd = 7, prime = 7 i = 8, even = 8, odd = ..., prime = ...
i = 9, even = ..., odd = 9, prime = ... i = 10, even = 10, odd = ..., prime = ... i = 11, even = ..., odd = 11, prime = 11 i = 12, even = 12, odd = ..., prime = ... i = 13, even = ..., odd = 13, prime = 13 i = 14, even = 14, odd = ..., prime = ... i = 15, even = ..., odd = 15, prime = ...

Done!
What to submit to Canvas
A zip file that contains:

All of your C++ source files and any extra input test programs you wrote.
Cut-and-paste text files of the parse trees of each of the four input test programs that were generated from the -parse command option.
Cut-and-paste text files of the runtime output of the above simple Pascal test programs that were generated from the -execute command option.

Submit to Assignment #3: Simple Pascal Interpreter


Rubric
Your submission will be graded according to these criteria:

Criteria Max points
WHILE statement
Parse tree
Runtime output 25
10
15
IF statement
Parse tree
Runtime output 25
10
15
FOR statement
Parse tree
Runtime output 25
10
15
CASE statement
Parse tree
Runtime output 25

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!