Parse code into operation triads IR
Company: Mithril
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
## Problem: Operation-Triads Parsing (Mini Language)
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 flat list of **operation triads** (three-address/triad form), or
- an equivalent structured representation (e.g., AST/JSON).
### Language
#### 1) Statements
1. **Assignment** (one per line)
- Examples:
- `a = 1`
- `x = b + c`
2. **If–Else** (block form)
```
if (condition) {
assignment
} else {
assignment
}
```
#### 2) Expressions
- Operands: variable identifiers (`a`, `b`, `x`) or integers (`1`, `2`, `10`)
- Binary operators: `+`, `-`, `*`, `/`
- Conditions use comparison operators (at least `==`), e.g. `a + b == 3`
- The condition is a binary comparison between two expressions.
#### 3) Control-flow constraints
- `if–else` blocks **cannot be nested**.
- Every `if` **must** have an `else`.
- Each `{ ... }` block contains **exactly one assignment statement**.
### Input
- A list/array of strings, each string is one line of code, in order.
Example input:
```
[a = 1,
if (a + b == 3) {
x = 10
} else {
x = 20
}
]
```
### Output
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, _)
```
### Requirements
- Tokenize and parse the input lines.
- Enforce the stated constraints (non-nested `if`, must have `else`, exactly one assignment per block).
- Output must be deterministic and serializable.
- Clearly define how temporaries and labels are generated.
Quick Answer: 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.