扩充现有的语言IMP,添加 Boolean Expression
和
if-then-else
以及while等语法。

Requirement
Step 1
Download a prototype software package IMP1.zip, unzip the package and run
Linux command make to build the executable named imp. The package implements a
small program language IMP1. If you run ./imp progfile, it will parse the
program stored in a file named progfile, and build an abstract syntax tree for
the program, display the program to standard output and then interprets the
program (abstract syntax tree).
An IMP1 program is a list of statement separated by “;”. A statement is either
an assignment statement, a print statement, the empty statement or a compound
statement. An assignment statement has the form x = e where x is variable
(identifier) and e an expression, a print statement has the form print e where
print is a keyword and e an expression. A compound statement has the form {
stmtlist } where stmtlist is a list of statements separated by “;”. The empty
statement is written as the empty string.
An expression is an arithmetic built from numbers, variables (identifiers) and
binary operators +, -, * and / and unary operator -.
This is the grammar for IMP1. Note that non-terminal symbols are not enclosed
in angle brackets and is used in place of ::=.
program -> stmtlist
stmtlist -> stmt | stmtlist ; stmt
stmt -> id = exp | print exp | { stmtlist } |
exp -> exp + mulexp | exp - mulexp | mulexp
mulexp -> mulexp * priexp | mulexp / primexp |
primexp -> primexp ( exp ) | - primexp | id | number
Step 2
Extend IMP1 with Boolean expressions and input, if-then-else and while
statements. An input statement has the form read x where read is a keyword and
x a variable. Since the else branch of an if-then-else statement can be empty,
there is no need for if-then statements. Let’s call the extended programming
language IMP2. You will need to do the following.
- a. Write an unambiguous grammar for IMP2.
- b. Write five programs in IMP2. Your programs must collectively use all types of statements. For instance, you may have an IMP2 program that reads three numbers and printing the maximum of these numbers and another IMP2 program that prints the sum of integers from 1 to n where n is obtained by an input statement.
- c. Implement IMP2 by modifying the implementation for IMP1. You will need to add new tokens, grammar rules, and new classes for abstract syntax tree nodes of new language constructs.
- d. Test your implementation on the IMP2 programs you wrote.
Step 3
Submit as one archive that contains the following files:
- a. One file for the grammar for IMP2.
- b. Five files containing your IMP2 programs.
- c. Flex, Bison, C++ files for your implementation of IMP2 programming language. You must use comments to clearly identify the modification you made to the Flex, Bison, C++ files for IMP1 you downloaded.
- d. Screen snapshots for your tests.
- e. A description of division of work among group members. All students will receive the same grade and are expected to contribute roughly with the same effort.