This question evaluates proficiency in parsing grammar-like schemas, scope-based name resolution for nested types, symbol-table construction, and designing efficient data structures for answering schema queries.
You are given a single string schema describing message types in a simplified, protobuf-like language. Your task is to parse the schema and support two query functions.
message <MessageName> { <items> }
<field_name> <type>;
<type>
is either:
int
,
long
,
float
,
double
,
bool
,
string
Inner
declared inside
Outer
has the
fully-qualified name
Outer.Inner
.
Outer.Inner
), or
Inner
). For unqualified names, resolve to the
nearest enclosing scope
first, then outward, then top-level.
Implement the following two functions after parsing schema:
get_size(message_name) -> int
get_type(message_name, field_name) -> string | null
field_name
in
message_name
.
null
(or an equivalent sentinel) if the message or field does not exist.
Given:
message Vehicle {
wheels int;
engine Engine;
message Engine {
model string;
}
}
get_size("Vehicle") == 2
(fields:
wheels
,
engine
)
get_type("Vehicle", "engine") == "Engine"
get_size("Vehicle.Engine") == 1
get_type("Vehicle.Engine", "model") == "string"