Parse code into operation triads IR
Problem: Operation-Triads Parsing (Mini Language)
You are tasked with parsing a small, line-based programming language and deserializing it into a deterministic intermediate representation (IR). The language consists of assignment statements and non-nested if–else blocks.
Your output can be either a flat list of operation triads (three-address/triad form) or an equivalent structured representation (e.g., Abstract Syntax Tree (AST) or JSON).
Language Specification
1) Statements
-
Assignment (one per line)
- Examples:
a = 1x = b + c
- Examples:
-
If–Else (block form)
if (condition) { assignment } else { assignment }
2) Expressions
- Operands: Variable identifiers (e.g.,
a,b,x) or integers (e.g.,1,2,10). - Binary Operators:
+,-,*,/. - Conditions: Use comparison operators (at least
==), e.g.,a + b == 3. A condition is always a binary comparison between two expressions.
3) Control-flow Constraints
if–elseblocks cannot be nested.- Every
ifmust have anelse. - Each
{ ... }block contains exactly one assignment statement.
Input
A list or array of strings, where each string represents 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 (e.g., t1, t2, …) and labels (e.g., L1, L2, …) as needed.
Example Triad-style Output for the Sample Input:
("=", 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 all stated constraints (non-nested
if, must haveelse, exactly one assignment per block). - The output must be deterministic and serializable.
- Clearly define how temporaries and labels are generated.
AI interview prep
Core dilemma
Translating a structured, line-based mini-language with expressions and conditional control flow into a flat, deterministic intermediate representation (IR) while correctly managing temporary variables, labels, and preserving execution order.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Parse code into operation triads IR
Problem: Operation-Triads Parsing (Mini Language)
You are tasked with parsing a small, line-based programming language and deserializing it into a deterministic intermediate representation (IR). The language consists of assignment statements and non-nested if–else blocks.
Your output can be either a flat list of operation triads (three-address/triad form) or an equivalent structured representation (e.g., Abstract Syntax Tree (AST) or JSON).
Language Specification
1) Statements
-
Assignment (one per line)
- Examples:
a = 1x = b + c
- Examples:
-
If–Else (block form)
if (condition) { assignment } else { assignment }
2) Expressions
- Operands: Variable identifiers (e.g.,
a,b,x) or integers (e.g.,1,2,10). - Binary Operators:
+,-,*,/. - Conditions: Use comparison operators (at least
==), e.g.,a + b == 3. A condition is always a binary comparison between two expressions.
3) Control-flow Constraints
if–elseblocks cannot be nested.- Every
ifmust have anelse. - Each
{ ... }block contains exactly one assignment statement.
Input
A list or array of strings, where each string represents 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 (e.g., t1, t2, …) and labels (e.g., L1, L2, …) as needed.
Example Triad-style Output for the Sample Input:
("=", 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 all stated constraints (non-nested
if, must haveelse, exactly one assignment per block). - The output must be deterministic and serializable.
- Clearly define how temporaries and labels are generated.