Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Variables: the variables in Espresso language are one letter characters: a to z and A to Z. The language is case sensitive so a and
Variables: the variables in Espresso language are one letter characters: "a" to "z" and "A" to "Z". The language is case sensitive so "a" and "A" refer to two different variables. Also Read or PRINT are not valid keywords. Infix-expr: the infix-expr's are used in assignment or print statements. An infix-expr is an algebraic expression in infix format. The operators in an infix-expr include: "", "-", "*""and "%". The operators are integer binary operators (there is no unary operator such as -x). The operands are either variables or positive integer constants. You can assume that there is exactly one space between operands and operators (including parenthesis) in an infix expression. This means that your program will be tested for the programs that include statements with exactly one space between their operands and operators. Below are some examples of valid infix-expr: Your interpreter Your interpreter should take the input program file as a command line argument. Given an input program file your program must read the statements one by one (each statement is in a line) and execute them. In case there is an error in a statement, the program should print the statement and an appropriate message about the error, and then terminate the execution of the input program. There are two possible types of errors: 1. Syntax error: if a statement is invalid (violates the rules of the Espresso language) it has a syntax error. For example if the infix expression is invalid (all examples we mentioned above), it is a syntax error, or if a variable name is invalid (such as x2), or you found an invalid operator or character (such as !,^, &, ...). For these types of errors, it's enough that your program prints "syntax error" along with the line number and the statement that contains the error. 2. Undefined variable error: If a statement tries to access the value of a variable that is not initialized before, your program must print an undefined variable error message. For instance if the first statement of a program is Z = x + 3 " your program should print: Line 1. Z = X+3 error: variable x is not defined. The following shows some examples: Sample program read x X=X^2 print x Expected output: Enter an integer number for variable x: 25 Line 2.x = x^2 Syntax error 3 Guideline: Handling Variables: There are different ways to handle variables. Here is a suggestion: you can define a class Variable in your program to store the value for each variable. The class variable has two field member: value which store the integer value and initialized which is a boolean variable (like a flag) that indicates whether the variable has been initialized or not (defined before or not). class Variable private int value; private boolean initialized; public Variable initialized = false; 1 public void setValue(int v){ this.value=V; this.initialized true; } public int getValue() throws Undefined variableException { if (!initialized) throw new Undefined variableException(); else return value; Note that getValue() throws an exception if we try to get the value of an un-initialized (undefined) variable. Espresso Language description: Statements: each line of a program in Espresso language contains a single statement. There are three types of statements in Espresso language: 1. The assignment statement the syntax of an assignment statement is variable = infix-expr When an assignment statement is executed the infix.expr is evaluate and its value is assigned to the variable 2. The input statement: the syntax of an input statement is read variable When an input statement is executed the program asks the user to enter an integer and assigns the entered value to the specified variable. 3. The output statement the syntax of an output statement is print infix-expr When an output statement is executed the infix-expr is evaluate and its value is printed on the screen
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started