Difference between revisions of "PLC Laboratory 7"
(Created blank page) |
|||
| (5 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
| + | == Interpreter of Arithmetic Expressions Using ANTLR == | ||
| + | Using ANTLR, implement an interpreter of arithmetic expressions. These 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. | ||
| + | |||
| + | As a starting point, you can use following ANTLR grammar (it describes different kind of expressions): [http://linedu.vsb.cz/~beh01/wiki_data/PLC_Lab7_expr.g4 ANTLR input file] | ||
| + | |||
| + | == Input specification == | ||
| + | |||
| + | In the input, there are expressions, they are written in 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. | ||
| + | |||
| + | == 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. | ||
| + | |||
| + | == Example == | ||
| + | * Input | ||
| + | <syntaxhighlight lang="haskell" > | ||
| + | 012-10; 2 * (0xff+5); | ||
| + | 0x23e5-0x201; | ||
| + | </syntaxhighlight > | ||
| + | |||
| + | * Output | ||
| + | <syntaxhighlight lang="haskell" > | ||
| + | 0 | ||
| + | 520 | ||
| + | 8676 | ||
| + | </syntaxhighlight > | ||
| + | |||
| + | == Solution == | ||
| + | |||
| + | * You can download the solution: [http://linedu.vsb.cz/~beh01/wiki_data/PLC_Lab7_solution.zip PLC_Lab7_solution.zip] | ||
Latest revision as of 09:07, 30 March 2022
Contents
Interpreter of Arithmetic Expressions Using ANTLR
Using ANTLR, implement an interpreter of arithmetic expressions. These 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.
As a starting point, you can use following ANTLR grammar (it describes different kind of expressions): ANTLR input file
Input specification
In the input, there are expressions, they are written in 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.
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.
Example
- Input
012-10; 2 * (0xff+5);
0x23e5-0x201;
- Output
0
520
8676
Solution
- You can download the solution: PLC_Lab7_solution.zip