Book of programming problems

Contains advanced programming problems for schools, colleges and problem-hungry inidividuals

Translate a program from one programming language to another
Complexity (1-100): 90
Translate a program on the programming language Beta to the equivalent program on the programming language Psi.

Beta language syntax & semantics

A Beta program consists of a sequence of operators, each on separate line of text, and each starting with a natural number - a label, - in the range [1;10000]. All labels are different and in ascending order. An operator is separated from its label with spaces.

A variable name consists of one or more latin letters (no more than 8 letters), case-insensitive. Variable names should differ from keywords.

Program starts with the first operator, the last operator in the program should be END.

Variable, constant and intermediate values are integer and ranged from -32768 to 32767. At the start, all variables are set to 0.

A program may contain any number of spaces between its lexemes, but it does not contain empty lines.

Operators of Beta language:

OperatorSyntaxSemantic
Assignmentvariable=expressionan expression is an arithmetic formula consisting of variable names, constants, parenthesis, and operations +, -, *. The operator assigns a value of the expression to the left-hand variable.
JumpWALKTO labelContinues execution of the program from the given label. The label is constant and cannot contain expressions.
ConditionalIF condition THEN assignment or jumpA condition is a couple of expressions, divided by one of the relation operations =, <, >, and <>. The operator checks the condition, and, if its true, executes the operator after THEN in the same line.
Output TYPE expressionPrints the expression value starting from a new line.
Ending operatorENDAlways comes at the end of a program and stops the program execution.

Psi language syntax & semantics

A Psi program consists variable definitions and a body. Variable definitions starts with the keyword DEF, followed by variable names used in the body.Variable names are divided by comma and cannot repeat. Definitions end with a semicolon.

The program body consists of an operator sequence, enclosed in keywords START and FINISH. Operators are separated by a semicolon.

The notion of variable, constant, expression, and condition are the same as in Beta.

A Psi program can contain empty lines and extra spaces. Variable values at the start are random.

Operators of Psi language:

OperatorSyntaxSemantic
Assignmentvariable:=expressionAssigns a value of the expression to the left-hand variable.
ConditionalIF condition THEN operator; ...;operator; FIChecks the condition, and, if its true, executes one or more operators between THEN and FI
OutputPRINT expressionPrints the expression value starting from a new line.
LoopWHILE condition DO operator; ...;operator; ODExecuted one or more operators between DO and OD while the condition is true.

An example of translation

    Beta program                     an equivalent Psi program

10 N=5 DEF N, Var, C;
20 SUM=SUM+N START
35 N=N-1 Var := 0; c := 1;
40 IF N>0 THEN WALKTO 20 WHILE c < 6 DO
70 TYPE Sum IF c=1 THEN N:= 5; c:=c+1; FI;
999 END IF c = 4 THEN Var:=Var+N; c:=c+1; FI;
IF c=4 THEN
c:=c+1;
IF N>0 THEN c:=2; FI;
FI;
IF c=5 THEN Print (Var);
c:=c+1;
FI;
OD;
FINISH


Note. The input text is always syntactically correct and contains no more than 100 lines with up to 80 characters in each line.
Solution:solution.pas
There are no posts in this topic.

Post Reply

Book of programming problems is powered by UseBB 1 Forum Software | Contact Admin