This question evaluates parsing and IR-generation skills, including tokenization, AST or three-address (operation triads) construction, deterministic control-flow serialization, and management of temporaries and labels.
You are given a small, line-based programming language consisting of assignment statements and non-nested if–else blocks. Your task is to parse the input program and deserialize it into a deterministic intermediate representation (IR).
You may output either:
a = 1
x = b + c
if (condition) {
assignment
} else {
assignment
}
a
,
b
,
x
) or integers (
1
,
2
,
10
)
+
,
-
,
*
,
/
==
), e.g.
a + b == 3
if–else
blocks
cannot be nested
.
if
must
have an
else
.
{ ... }
block contains
exactly one assignment statement
.
Example input:
[a = 1,
if (a + b == 3) {
x = 10
} else {
x = 20
}
]
Produce an IR that preserves execution order and control flow.
If using operation triads, each instruction is a tuple:
(op, arg1, arg2)
You may introduce temporary values t1, t2, … and labels L1, L2, … as needed.
Example (one acceptable triad-style output for the sample):
(=, 1, a)
(+, a, b) -> t1
(==, t1, 3) -> t2
(if_false, t2, L1)
(=, 10, x)
(goto, L2, _)
(label, L1, _)
(=, 20, x)
(label, L2, _)
if
, must have
else
, exactly one assignment per block).