Skip to content

AlicanBalik/compdes

Repository files navigation

There are many “calculator” programs using Reverse Polish Notation (RPN). RPN represents expressions in which the operator symbol is placed AFTER the arguments being operated on. Polish notation, in which the operator comes before the operands, was invented in the 1920s by the Polish mathematician Jan Lucasiewicz. In the late 1950s, Australian philosopher and computer scientist Charles L. Hamblin suggested placing the operator after the operands and hence created reverse polish notation. Your task will be to design and implement INTERPRETER program using POLISH NOTATION. It will be console application where program read from input, line by line, interpret commands and print the result(s) on the console.

ALPHABET:

• digits (0-9) and period (.),
• letters(a-z, A-Z),
• delimiters( “(“, “)”, “,”)

LEXEMES

• add (addition)
• sub (subtraction)
• mult (multiplication)
• div (division)
• move (assignment)
• get (read variable from console)
• put (print variable value to console)
• read (load source code from external file)
• run (execute currently loaded source code)
• end (terminate interpreter)

Syntax

variable ::= letter { letter | digit }
filename ::= variable
number ::= digit { digit } { period } { digit }
statement ::= operator (operand, operand)
operator ::= add | sub | mult | div
operand ::= variable | statement
assignment ::= get(variable)
Print “Enter {variable}: “ and read value from console. Store value to table of variables.
announcement ::= put(variable)
Print “{variable}: “ following by value of {variable}. Pull value from table of variables.
load source code ::= read(filename)
Read lines of program from “filename.cal” and print all lines to console.
execute program ::= run
Executes (interprets) loaded code, line by line
terminate interpreter ::= end
Exits to command line

Program example: Comment/Result

get(x)
move(div(x,2),y)
put(y)
move(add(mult(x,2),y),z)
put(z)
move(sub(z,mult(3,y)),x)
put(x)
Enter x: (you enter 8)
y = x / 2
y: 4
z = x*2 + y
z: 20
x = z – y
x: 8

Important

Don't forget to configure build path.
Add commons-lang and gson jars from project folder.
Add JUnit 4 library.