pcod
One time when he was bored
Ionel created the following simplified pseudo-code language.
A variable identifier is represented by a lowercase letter of the English alphabet.
The values of a variable are integers having at most 9 digits.
Before using a variable, it must be defined via an assignment statement.
Constants are positive integers having
at most 9 digits.
Allowed statements
are:
Assignment:
let v = const
(variable v takes the value of
constant const )
Display:
write v
(displays the value of variable v;
the equivalent of writeln(v)
or cout<<v<<'\n';
or printf("%ld\n",v);)
Increment:
inc a, b
(the value of variable a is incremented
by the value of variable b)
Initial test loop:
while a < b
instruction_1
instruction_2
...
instruction_n
endwhile
(while expression a<b
is true execute the statements
instruction_1
are executed. . . , instruction_n
)
where:
a, b
- variables
const - constant
instruction_1, instruction_2,.
. ., instruction_n - any allowed
statement
(let, write,
inc or while)
From a syntactical point of view Ionel writes his algorithms correctly, it's
only the endwhile keyword that
gives him problems quite often (he misplaces it or uses it more/fewer times
than he should). Also, Ionel forgets to define all the variables he uses.
Task
Write a program that interprets algorithms written in simplified pseudo-code, by determining any errors they may contain.
Input data
Input file pcod.in
contains an algorithm in simplified pseudo-code.
Statements
are written only in lowercase letters and may contain an infinite number of
separating spaces.
Each statement
is written on one line, except for the while
statement,
which expands on more lines.
Output data
Output file pcod.out
will contain the data displayed by the algorithm. If an error is detected the
proper error code will be displayed and algorithm interpretation ends. In this
case the output file will contain only the error message. Constraints
Example
pcod.in
pcod.out
Explanations
let s = 0 undefined symbol
a non-stated variable
(w) is detected
let p=1 6
let p=1 runtime error
The algorithm loops
infinitely because the value of variable t
does not change during execution
let x=2 nesting error
The first while
loop has no endwhile correspondent
Time limit: 0.3 seconds/test prof. Alin
Burta
The error messages can be:
(meaning: an undefined variable was used)
(meaning:
error processing while instructions)
(meaning:
the algorithm loops infinitely)
let p= 1
let t=1
inc t,p
inc s,t
write s
write w
let s=0
let n=4
let t=1
while t<n
inc s,t
inc t,p
endwhile
write s
write n
4
let s=0
let n=4
let t=1
while t<n
inc s,t
endwhile
write s
let y=0
let z=0
while y<x
inc y,x
let z=0
while z<x
inc z,x
endwhile
write y
Buzau "B.P.
Hasdeu" National High-School
Contact:
allbu2003@yahoo.com