Program for a Program#4678

Program for a Program

8 secondes
256 mégaoctets
Facile

Every programmer, at some point, feels the urge to write a compiler for a programming language. If you're one of them, here's your chance to create an interpreter for a language we'll call Syu. It's a dynamically typed, C-like language with the following features:

1) The program consists of a set of functions. Functions can be called before they are defined, so function prototypes are included. A function is defined as:

«name» ( «argument names separated by commas») «block (function body)»

Function prototype:

«name» («argument names separated by commas» );

The program execution begins with the main function. If main is absent and there's only one function, that function is executed.

2) Block:

{ «set of statements separated by semicolons» }

A block returns the value of its last statement.

3) Statements can be:

a) Function call:

«function name»(«set of parameters separated by commas»)

b) Assignment:

«variable name 1» = «variable name 2» = ... = «variable name n» = expression.

c) expression – follows standard mathematical rules; also may include ternary operators.

d) Ternary operator:

«expression 1»?«expression 2»:«expression 3»

If expression 1 is true, the result is expression 2; otherwise, it's expression 3.

e) Conditional operator:

if («expression 1») «block 1» else «block 2»

Similar to a ternary operator. The else «expression 3»; part may be omitted.

f) Loops:

I) for («expression 1»; «expression 2»; «expression 3») «block»

Executes expression 1 first. Then while expression 2 is true, block is executed, followed by expression 3. First the condition is checked – then execution.

II) while(«expression»)«block»

Executes block as long as expression is true. First the condition is checked – then execution.

III) do «block» while («expression»)

Executes block as long as expression is true. First execution – then the condition is checked.

g) Return statement:

return «expression»

Returns the value of expression and ends the function execution.

h) Break statement:

break

Exits a loop or conditional block. It may return a value.

i) Name – a sequence of Latin letters, digits, and underscores. The first character cannot be a digit.

j) String – "«set of characters»"

k) Character – '«character»'

l) Comparison operators: > (greater), < (less), >= (greater or equal), <= (less or equal), != (not equal), == (logical equal), || – logical OR, && – logical AND.

m) Assignment operators: += (add and assign), -= (subtract and assign), *= (multiply and assign), /= (divide and assign), %= (modulo and assign), ++«variable name» (add one and return value), «variable name»++ (return value and add 1), «variable name»– (return value and subtract 1), –«variable name» (subtract 1 and return value). A standard print function is available to output arguments.

Note:

1) The program is syntactically correct and performs no more than 1000000 iterations.

2) If logical errors occur, the following messages should be displayed:

a) If there's no main function and more than one function exists, display "Ambiguity in function calling".

b) If a variable is accessed before it's defined, display "No such variable".

c) If a function is called before it's defined, display "No such function".

d) If mathematical operations can't be performed with the given values, display "Incompatible types". All operations are numeric. With strings, operations + and * – concatenation of strings. Concatenating a number and a string results in the number being converted to a string. Other operations are not allowed.

e) If division by zero occurs, display "Division by zero".

3) All operations, except =, are executed left-to-right (with equal priorities).

Entrée

The input file contains a program written in the described language.

Sortie

The output file contains the result of the program's execution (specifically, what the print functions output) or the first error encountered, if the program fails to execute.

Exemples

Exemple 1

Loading
Loading