原文地址: IMP
擴充現有的語言IMP,添加Boolean Expression和if-then-else以及while等語法。node
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).express
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.ide
An expression is an arithmetic built from numbers, variables (identifiers) and binary operators +, -, * and / and unary operator -.ui
This is the grammar for IMP1. Note that non-terminal symbols are not enclosed in angle brackets and is used in place of ::=.spa
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
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.prototype
Submit as one archive that contains the following files:code
(本文出自csprojectedu.com,轉載請註明出處)orm