Difference between revisions of "PLC Laboratory 9"

From Marek Běhálek Wiki
Jump to navigation Jump to search
Line 34: Line 34:
 
c = a + b;
 
c = a + b;
 
c + a;
 
c + a;
a = c;
+
c = a;
 
c + 1.1;
 
c + 1.1;
c % a;
+
10 % a;
 
</syntaxhighlight >
 
</syntaxhighlight >
  
 
* Output
 
* Output
 
<syntaxhighlight lang="haskell" >
 
<syntaxhighlight lang="haskell" >
PUSH 9
+
PUSH I 15
PUSH 10
+
SAVE b
SUB
+
LOAD b
 +
SAVE a
 +
LOAD a
 
PRINT
 
PRINT
PUSH 2
+
LOAD a
PUSH 3
+
LOAD b
PUSH 4
 
 
ADD
 
ADD
MUL
 
 
PRINT
 
PRINT
PUSH 5
+
LOAD a
 +
LOAD b
 +
MOD
 +
PRINT
 +
LOAD a
 +
LOAD b
 +
ADD
 +
SAVE c
 +
LOAD c
 +
PRINT
 +
LOAD c
 +
LOAD a
 +
ADD
 +
PRINT
 +
LOAD a
 +
SAVE c
 +
LOAD c
 +
PRINT
 +
LOAD c
 +
PUSH F 1.1
 +
ADD
 +
PRINT
 +
PUSH I 10
 +
LOAD a
 +
MOD
 
PRINT
 
PRINT
 
</syntaxhighlight >
 
</syntaxhighlight >

Revision as of 12:10, 16 April 2024

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:

  • ADD, SUB, MUL, DIV, MOD - it takes two values from the stack, computes the apropriate value and stores the result on stack. If necessary, integers are automatically casted into floating-point numbers. MOD works only for integers.
  • PUSH (I|F) n - it stores the value n on stack. It will either int or float.
  • LOAD a - it loads the value of variable a to the stack.
  • SAVE a - it takes the value from the stack and stores in into variable a.
  • 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;
c = a;
c + 1.1;
10 % a;
  • Output
PUSH I 15
SAVE b
LOAD b
SAVE a
LOAD a
PRINT
LOAD a
LOAD b
ADD
PRINT
LOAD a
LOAD b
MOD
PRINT
LOAD a
LOAD b
ADD
SAVE c
LOAD c
PRINT
LOAD c
LOAD a
ADD
PRINT
LOAD a
SAVE c
LOAD c
PRINT
LOAD c
PUSH F 1.1
ADD
PRINT
PUSH I 10
LOAD a
MOD
PRINT

Solution