The following is an interpreter I developed for my CS280 (Programming Language Concepts) class.
This project is built on two other components that I had developed earlier in the class.
- A lexical analyzer
- A recursive-descent parser for a Pascal-Like Simple Language.
The grammar rules of the PLSL was described in EBNF notations.
Using previous components (lexical analyzer and parser), the interpreter is responsible for:
-
Building information of variables types in a map container for all the defined variables.
-
Evaluating expressions and determining their values and types.
-
Using a map container that keeps a record of the defined variables in the parsed program.
-
Printing the results of an unsuccessful parsing and interpreting by the parser/interpreter functions, as well as error messages that might be detected by the lexical analyzer.
The PLSL consists of three main parts in the respective order.
- Program Declaration
- Variable Declaration
- Program Body
The Program Declaration block consists of PROGRAM
token followed by an identifier in the beginning of a program.
PROGRAM circle;
The Variable Declaration block is where the variables that will be used in the program are declared.
The block is start with the VAR
token then followed by variable declarations.
VAR
r, a, p, b : REAL;
i, j, sum : INTEGER;
str1, str2: STRING;
PLSL has three different types.
- INTEGER
- REAL
- STRING
The beginning of the Program Body is defined by the BEGIN
token and the end of the body is defined with the END
token.
BEGIN
i := 8;
p := 6;
a := 0;
sum := 0;
str1 := 'Hello World!';
j := -3;
IF ( p < 5) THEN
a := (3.14) + j * - p
ELSE
IF (p > 0 ) THEN
str1 := 'Goodbye!';
(*Display the results*)
WRITELN ('The output results are: ' , p, ' ', str1, ' ', a, ' ', i);
END