Skip to content

Latest commit

 

History

History

lab3

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Lab 3: Minimal BASIC Interpreter

due on 2014-11-27 23:59 UTC 8view as PDFDownload Starter FilesSlides

Introduction

In this assignment, your mission is to build a minimal BASIC interpreter. You may start with the code for the expression evaluator (aka calculator) in Computer Programming I (SE105).

You are also free to use StandfordCPPLib and other STL functions.

BASIC Language and Interpreter

The programming language BASIC - the name is an acronym for Beginner's All-purpose Symbolic Instruction Code - was developed in the mid-1960s at Dartmouth College by John Kemeny and Thomas Kurtz.

A Plus B

In BASIC, a program consists of a sequence of numbered statements, as illustrated by the simple program below:

10 REM Program to add two numbers
20 INPUT n1
30 INPUT n2
40 LET total = n1   n2
50 PRINT total
60 END
Lexical

Identifiers are formed by one or more letters. Keywords that are reserved words in the language and cannot be used as identifiers. Integer literals are composed of digits only. This language is case-insensitive, which means i and I are the same variable, and IFif,If, and even iF have the same meaning.

Line Numbers

The line numbers at the beginning of the line establish the sequence of operations in a program. In the absence of any control statements to the contrary, the statements in a program are executed in ascending numerical order starting at the lowest number.

Line numbers are also used to provide a simple editing mechanism. Statements need not be entered in order, because the line numbers indicate their relative position. Moreover, as long as the user has left gaps in the number sequence, new statements can be added in between other statements

For example, to change the program that adds two numbers into one that adds three numbers, you would need to make the following changes:

1. Add a new line to read in the third value by typing in the command 35 INPUT n3 . This statement is inserted into the program between line 30 and line 40.

2. Type in a new assignment statement, as 40 LET total = n1   n2   n3 . This statement replaces the old line 40 with the updated version.

The standard mechanism for deleting lines was to type in a line number with nothing after it on the line. Note that this operation actually deleted the line and did not simply replace it with a blank line that would appear in program listings.

Sequential Statements
REM This statement is used for comments.
LET var = exp This statement is BASIC's assignment statement.
PRINT exp This statement print the value of the expression on the console and then print a newline character.
INPUT var This statement print a prompt consisting of the string " ? " and then to read in a value to be stored in the variable.
END Marks the end of the program. Execution halts when this line is reached. Execution also stops if the program continues past the last numbered line.
Control Statements

For example, the following BASIC program simulates a countdown from 10 to 0:

10 REM Program to simulate a countdown
20 LET T = 10
30 IF T < 0 THEN 70
40 PRINT T
50 LET T = T - 1
60 GOTO 30
70 END
GOTO n This statement transfers control unconditionally to line n in the program. If line n does not exist, your BASIC interpreter should generate an error message informing the user of that fact.
IF exp cmp exp THEN n This statement performs a conditional transfer of control. On encountering such a statement, the BASIC interpreter begins by evaluating condition, which in the minimal version of BASIC consists of two arithmetic expressions joined by one of the operators <, >, or =. If the result of the comparison is true, control passes to line n, just as in the GOTO statement; if not, the program continues with the next line in sequence.
Expressions

Expressions are used in LET, PRINT, and IF statements.

int_const The simplest expressions are variables and integer constants.
var
( exp ) These may be combined into larger expressions by enclosing an expression in parentheses or by joining two expressions with the operators , -, *, and /, just as in the interpreter presented in the reader.
exp op exp
Executed Directly

The LET, PRINT, and INPUT statements can be executed directly by typing them without a line number, in which case they are evaluated immediately. Thus, if you type in "PRINT 2 2" your program should respond immediately with 4.

The statements GOTO, IF, REM, and END are legal only if they appear as part of a program, which means that they must be given a line number.

BASIC Interpreter

These commands control the BASIC interpreter, which don't contained in BASIC program.

RUN This command starts program execution beginning at the lowest-numbered line.
LIST This command lists the steps in the program in numerical sequence.
CLEAR This command deletes all program and variables.
QUIT This command exits from the BASIC interpreter by calling exit(0).
HELP This command provides a simple help message describing your interpreter.

Error Reporting

DIVIDE BY ZERO - Calculating some number divide by zero.

INVALID NUMBER - User types wrong value to answer INPUT statement.

VARIABLE NOT DEFINED - A variable used before assigned it.

LINE NUMBER ERROR - GOTO statement's line number not exist.

SYNTAX ERROR - Any other errors.

Hints

1. Recall the TokenScanner in homework 1, it may help you in this lab

2. If you are not sure about some functions of the interpreter, try the Demo.

3. You only have to support the integer type of variables

4. If you want to include header files from StanfordCPPLib, add the "../StandfordCPPLib" before the name of header files. e.g. #include "../StanfordCPPLib/tokenscanner.h"

5. You may found some "unreasonable" bugs in Demo, just make sure yours perform exactly same as the Demo, and you can report bugs here in QA. You can hand in two versions of this lab if it is necessary, a naive one to pass our test and a better version withREADME to earn 10% score for bonus.

Grading

Your implementation will be evaluated using score in Test folder. You can evaluate your implementation by yourself. Try "./score -f -c" to evaluate your program. Type "./score -h" for more details about our score program.

Make sure that you did make in Basic folder before running score.

You will get full score (100) if you pass this test. But DONOT try to change the test program or cheat, we will run score again and check your code manualy!

Hand-in

Your code must be written in C/C . And you are not allowed to use compiler-compiler or any other tools to generate your codes. You should work in Basic folder. You may add or modify files in this folder. Only keep your Makefile produce executable file namedBasic when we type make in this folder correctly. You should turn in your Basic folder by typing./handin your_student_idand then upload the corresponding your_student_id.tar.gz

Submit your file to

ftp://public.sjtu.edu.cn/upload/lab3/
Username: azard5, Password: public
Filename: your_student_id.tar.gz

Do NOT turn in StanfordCPPLib and other executable files.

If you have any question, please contact [email protected]

2014-11-07