Difference between revisions of "PLC Laboratory 2"

From Marek Běhálek Wiki
Jump to navigation Jump to search
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Interpreter of Arithmetic Expressions ==
+
== Lexical analyzer ==
  
Implement an interpreter of arithmetic expressions. These expressions contain <code>+, -, *, /</code> operators (with common priorities and left associativity) and parentheses.  
+
Write a program, that reads an input and converts it into a sequence of lexical symbols – ''tokens''. Each token is a pair, it composes from a type and possibly a value.  
  
To simplify the task, consider we have only binary operators. There are no unary operators in our language. Moreover, we can use only positive integers in our expressions.
+
The tokens definition depends on you, and it is considered a part of the solution.
  
 
== Input specification ==
 
== Input specification ==
  
The first line of the input contains a number <code>N</code>. It defines the number of expressions your program should evaluate. These expressions are on next <code>N</code> lines. Each line contains exactly one expression.  
+
The input may be containing the following symbols:
 +
* ''identifiers'' - consisting of a sequence of letters and numbers starting with a letter
 +
* ''numbers'' - formed by a sequence of decimal digits
 +
* ''operators'' - symbols <code>'+', '-', '*' and '/'</code>,
 +
* ''delimiters'' - symbols <code>'(', ')' and ';'</code>,
 +
* ''keywords'' - <code>div</code> and <code>mod</code>.
 +
 
 +
Symbols can be separated by a sequence of spaces, tabs, and line breaks.  
 +
 
 +
There can be notes in the input. Notes are preceded by a sequence <code>//</code> and continue to the end of the line.
 +
 
 +
''White spaces and notes does not produce any lexical symbols.''
  
 
== 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, write text <code>ERROR</code> instead.
+
Converts the given input into a sequence of tokens and write them on output. Write each token on a separated line.
  
 
== Example ==  
 
== Example ==  
 
* Input
 
* Input
 
<syntaxhighlight lang="haskell" >
 
<syntaxhighlight lang="haskell" >
2
+
    -2 + (245 div 3);  // note
2 * (3+5)
+
2 mod 3 * hello
15 - 2**7
 
 
</syntaxhighlight >
 
</syntaxhighlight >
  
* Output
+
* Output  
 +
''Your output can be different, it depends on your definition of tokens.''
 
<syntaxhighlight lang="haskell" >
 
<syntaxhighlight lang="haskell" >
16
+
OP:-
ERROR
+
NUM:2
 +
OP:+
 +
LPAR
 +
NUM:245
 +
DIV
 +
NUM:3
 +
RPAR
 +
SEMICOLON
 +
NUM:2
 +
MOD
 +
NUM:3
 +
OP:*
 +
ID:hello
 
</syntaxhighlight >
 
</syntaxhighlight >

Latest revision as of 10:27, 14 February 2022

Lexical analyzer

Write a program, that reads an input and converts it into a sequence of lexical symbols – tokens. Each token is a pair, it composes from a type and possibly a value.

The tokens definition depends on you, and it is considered a part of the solution.

Input specification

The input may be containing the following symbols:

  • identifiers - consisting of a sequence of letters and numbers starting with a letter
  • numbers - formed by a sequence of decimal digits
  • operators - symbols '+', '-', '*' and '/',
  • delimiters - symbols '(', ')' and ';',
  • keywords - div and mod.

Symbols can be separated by a sequence of spaces, tabs, and line breaks.

There can be notes in the input. Notes are preceded by a sequence // and continue to the end of the line.

White spaces and notes does not produce any lexical symbols.

Output specification

Converts the given input into a sequence of tokens and write them on output. Write each token on a separated line.

Example

  • Input
    -2 + (245 div 3);  // note
2 mod 3 * hello
  • Output

Your output can be different, it depends on your definition of tokens.

OP:-
NUM:2
OP:+
LPAR
NUM:245
DIV
NUM:3
RPAR
SEMICOLON
NUM:2
MOD
NUM:3
OP:*
ID:hello