Difference between revisions of "PLC Laboratory 8"
Line 27: | Line 27: | ||
c = a + b; | c = a + b; | ||
c + a; | c + a; | ||
− | a = c | + | a = c; |
+ | c + 1.1; | ||
</syntaxhighlight > | </syntaxhighlight > | ||
Line 35: | Line 36: | ||
15 | 15 | ||
30 | 30 | ||
− | 30 | + | 30 |
− | 45 | + | 45 |
− | + | 7:0 - Variable 'a' type is int, but the assigned value is float. | |
+ | 31.1 | ||
</syntaxhighlight > | </syntaxhighlight > |
Revision as of 08:52, 30 March 2022
Contents
Interpreter of Arithmetic Expressions Using ANTLR II
Extend the interpreter of arithmetic expressions from previous Laboratory 7 with the usage of variables.
The basic description is still valid. Expressions contain +, -, *, / operators (with common priorities and left associativity) and parentheses. To simplify the task, consider we have only binary operators. There are no unary operators in our language.
But now, we have variables. Their identifiers compose from letters and they have two types: float
and int
. Before variables are used, they need to be declared (use the same syntax as in C). The language is extended with =
operator (lowest priority and right associativity).
Moreover, now, our expressions have a type. If it contains only integer numbers and variables its type is int
. In all other cases it is float
. In other words, if there is a floating point number in our expression, all other integer values will be converted to float and the resulting type is float
.
You can start with following grammar: ANTLR input file
Input specification
In the input, there are expressions and variable declarations, they are written in free formatting. Each expression and declaration ends with semicolon. Numbers can be written similarly to C language constants. it can be either: integer number or a floating point number. Variable declaration starts with the type and follows with a list of declared variables separated by ','
.
Output specification
For each expression write one line containing the result – the computed value of the expression. If there is any syntactic error in the input, you can stop the computation. If the expression contains some other (type) error, write the error instead of the resulting value.
Example
- Input
int a,b;
a = b = 15;
a + b;
float c;
c = a + b;
c + a;
a = c;
c + 1.1;
- Output
Your output may be different. Type error's messages are not precisely defined and there is no defined precision for floating point numbers.
15
30
30
45
7:0 - Variable 'a' type is int, but the assigned value is float.
31.1