Difference between revisions of "PLC Laboratory 9"
Line 3: | Line 3: | ||
Extend the interpreter of arithmetic expressions from previous [[PLC_Laboratory_8 | Laboratory 8]]. In this laboratory, generate a stack-based target code defining the computation. | Extend the interpreter of arithmetic expressions from previous [[PLC_Laboratory_8 | Laboratory 8]]. In this laboratory, generate a stack-based target code defining the computation. | ||
− | The language 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. | + | The language 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. We have variables. Their identifiers compose from letters and they have two types: <code>float</code> and <code>int</code>. Before variables are used, they need to be declared (use the same syntax as in C). The language is extended with <code>=</code> operator (lowest priority and right associativity). |
− | |||
− | |||
Our expressions have a type. If it contains only integer numbers and variables its type is <code>int</code>. In all other cases it is <code>float</code>. 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 <code>float</code>. In our language we have also a modulo: <code>%</code> that works only for integers. | Our expressions have a type. If it contains only integer numbers and variables its type is <code>int</code>. In all other cases it is <code>float</code>. 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 <code>float</code>. In our language we have also a modulo: <code>%</code> that works only for integers. | ||
You can start with following C# codes from previous laboratory: [http://linedu.vsb.cz/~beh01/wiki_data/PLC_Lab9.zip PLC_Lab9.zip] | You can start with following C# codes from previous laboratory: [http://linedu.vsb.cz/~beh01/wiki_data/PLC_Lab9.zip PLC_Lab9.zip] | ||
− | |||
The target stack-based code will constist of following instructions: | The target stack-based code will constist of following instructions: | ||
− | * <code> | + | * <code>IADD, ISUB, IMUL, IDIV, FADD, FSUB, FMUL, FDIV, MOD</code> - it takes two values from the stack, computes the apropriate value and stores the result on stack. Variants starting with <code>I</code> computes with integer numbers, and instructions starting with <code>F</code> works with floating-point numbers. <code>MOD</code> works only for integers. |
− | * <code>PUSH n</code> - it stores the value <code>n</code> on stack. | + | * <code>PUSH (I|F) n</code> - it stores the value <code>n</code> on stack. It will either int or float. |
* <code>PRINT</code> - it takes a value from stack and prints it on output. | * <code>PRINT</code> - it takes a value from stack and prints it on output. | ||
− | |||
− | |||
== Input specification == | == Input specification == | ||
− | In the input, there are expressions, they are written in formatting. Each expression ends with semicolon | + | In the input, there are expressions, they are written in the free formatting. Each expression ends with semicolon as described before. |
== Output specification == | == Output specification == | ||
− | For each expression write the instructions (one per line) that once performed, computes the resulting value for the expression. If there is any error in the input, you can stop the computation. | + | For each expression write the instructions (one per line) that once performed, computes the resulting value for the expression and prints the results on the screen. If there is any error in the input, you can stop the computation. |
== Example == | == Example == |
Revision as of 11:46, 16 April 2024
Contents
Compiler of Arithmetic Expressions Using ANTLR
Extend the interpreter of arithmetic expressions from previous Laboratory 8. In this laboratory, generate a stack-based target code defining the computation.
The language 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. 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).
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
. In our language we have also a modulo: %
that works only for integers.
You can start with following C# codes from previous laboratory: PLC_Lab9.zip
The target stack-based code will constist of following instructions:
IADD, ISUB, IMUL, IDIV, FADD, FSUB, FMUL, FDIV, MOD
- it takes two values from the stack, computes the apropriate value and stores the result on stack. Variants starting withI
computes with integer numbers, and instructions starting withF
works with floating-point numbers.MOD
works only for integers.PUSH (I|F) n
- it stores the valuen
on stack. It will either int or float.PRINT
- it takes a value from stack and prints it on output.
Input specification
In the input, there are expressions, they are written in the free formatting. Each expression ends with semicolon as described before.
Output specification
For each expression write the instructions (one per line) that once performed, computes the resulting value for the expression and prints the results on the screen. If there is any error in the input, you can stop the computation.
Example
- Input
int a,b;
a = b = 15;
a + b;
a % b;
float c;
c = a + b;
c + a;
a = c;
c + 1.1;
c % a;
- Output
PUSH 9
PUSH 10
SUB
PRINT
PUSH 2
PUSH 3
PUSH 4
ADD
MUL
PRINT
PUSH 5
PRINT
Solution
- You can download the solution: PLC_Lab9_solution.zip