Difference between revisions of "PLC Laboratory 8"

From Marek Běhálek Wiki
Jump to navigation Jump to search
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Interpreter of Arithmetic Expressions Using ANTLR II ==
+
== Type checking of Arithmetic Expressions Using ANTLR II ==
  
 
Extend the interpreter of arithmetic expressions from previous [[PLC_Laboratory_7 | Laboratory 7]] with the usage of variables.  
 
Extend the interpreter of arithmetic expressions from previous [[PLC_Laboratory_7 | Laboratory 7]] with the usage of variables.  
Line 5: Line 5:
 
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.  
 
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: <code>float</code> and <code>int</code>. Before variables are used, they need to be defined. The language is extended with <code>=</code> operator (lowest priority and right associativity).  
+
Now, 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).  
  
Moreover, our expression now has a type. If it contains only integer numbers and variables it is <code>int</code>. In all other cases it is <code>float</code>.
+
Moreover, 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>.
 +
 
 +
Also now, in our language we have also a modulo: <code>%</code> that works only for integers.
 +
 
 +
* You can start with following grammar: [http://linedu.vsb.cz/~beh01/wiki_data/PLC_Lab8_expr.g4 ANTLR input file]
 +
* Or you can start with following C# codes (containing symbol table and class solving printing of errors): [http://linedu.vsb.cz/~beh01/wiki_data/PLC_Lab8.zip PLC_Lab8.zip]
  
 
== Input specification ==
 
== Input specification ==
  
In the input, there are expressions, they are writtent with free formatting. Each expression ends with semicolon. Numbers can be written similarly to C language constants. it can be either: decimal , octal (starting with zero) or hexadecimal (starting with characters 0x) number.
+
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 <code>','</code>.
  
 
== Output specification ==
 
== Output specification ==
  
For each expression write one line containing the result – the computed value of the expression. If there is any error in the input, you can stop the computation.
+
Go through all expression, if there is any syntactic error in the input, you can stop the computation, if these expressions contain some other (type) error, write these errors.
  
 
== Example ==  
 
== Example ==  
 
* Input
 
* Input
<syntaxhighlight lang="haskell" >
+
<syntaxhighlight lang="csharp" >
012-10; 2 * (0xff+5);
+
int a,b;
0x23e5-0x201;
+
a = b = 15;
 +
a + b;
 +
a % b;
 +
float c;
 +
c = a + b;
 +
c + a;
 +
a = c;
 +
c + 1.1;
 +
c % a;
 
</syntaxhighlight >
 
</syntaxhighlight >
  
 
* Output
 
* Output
<syntaxhighlight lang="haskell" >
+
''Your output may be different. Type error's messages are not precisely defined.
0
+
<syntaxhighlight lang="bash" >
520
+
8:0 - Variable 'a' type is int, but the assigned value is float.
8676
+
10:2 - Module can be used only with integers.
 
</syntaxhighlight >
 
</syntaxhighlight >
 +
 +
== Solution ==
 +
 +
* You can download the solution: [http://linedu.vsb.cz/~beh01/wiki_data/PLC_Lab8_solution.zip PLC_Lab8_solution.zip]

Latest revision as of 11:38, 16 April 2024

Type checking 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.

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, 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.

Also now, in our language we have also a modulo: % that works only for integers.

  • You can start with following grammar: ANTLR input file
  • Or you can start with following C# codes (containing symbol table and class solving printing of errors): PLC_Lab8.zip

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

Go through all expression, if there is any syntactic error in the input, you can stop the computation, if these expressions contain some other (type) error, write these errors.

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

Your output may be different. Type error's messages are not precisely defined.

8:0 - Variable 'a' type is int, but the assigned value is float.
10:2 - Module can be used only with integers.

Solution