skip to content

Your team must publish a normative grammar for an annotation syntax that other teams will implement independently — what is the risk of making it an ordered-choice grammar?

level: principalimportance: nice to knowfreq 26%

answer

  1. the grammar is the specification
  2. meaning is operational, not declarative
  3. no independent description of the accepted set
  4. equivalence checking is out of reach
  5. conformance corpus carries the weight

basics

~20 s

An ordered-choice grammar defines its language operationally: the accepted set is whatever that recognizer accepts, with no independent description to check against. Conformance becomes behavioural agreement, so the specification must ship with a test corpus, not just rules.

solid answer

~50 s

The attraction is genuine — the grammar cannot be ambiguous, and it is executable, so the specification is testable. The risk is that it says what a particular recognizer does rather than which strings are in the language. There is no separate description of the accepted set, so a reimplementer cannot derive behaviour from the rules alone; they must reproduce the commitment semantics precisely, including that a succeeded alternative is never retried. Deciding whether two such grammars accept the same inputs is not something a tool can do in general, so you cannot mechanically check that a tidied-up version is equivalent. And since order carries meaning, a diff that looks cosmetic can be a breaking change. The mitigations are procedural: freeze ordering, forbid overlapping alternatives by convention, and publish a conformance corpus that is as normative as the grammar.

go deeper

for a junior

Recall that in this style of grammar the order of alternatives is part of the meaning, so a published grammar's line order is not something to tidy up.

for a middle

Explain why the specification is operational: the language is whatever this recognizer accepts, so there is no separate description of the accepted set to check an implementation against.

for a senior

Point at the practical consequence: a reordering diff is a compatibility break, an unreachable alternative is silent, and only a conformance corpus turns the grammar into something teams can build against.

for a principal

Own the trade-off. Unambiguous and executable is worth a lot when you can maintain a corpus; when independent parties reimplement it with other approaches, the inability to state the language apart from one recognizer is what eventually bites.

## What you are actually publishing An ordered-choice grammar is a program. Its meaning is the behaviour of the recognizer it describes, and the language it defines is, by construction, *the set of inputs that recognizer accepts*. That is a strength for a single implementation and a liability for a standard, because a standard's job is to let several independent implementations agree without consulting each other. Compare what each side of the choice gives you: | You want to… | Ordered-choice grammar | A declarative rule set | |---|---|---| | Guarantee one interpretation | free, by construction | needs ambiguity to be resolved | | Describe the accepted set independently | not available | the rules are the description | | Compare two candidate specifications | not decidable in general | tooling exists for useful cases | | Execute the specification directly | yes | needs a generator | | Have overlap reported at build time | no | a deterministic generator reports conflicts | ## The four risks, concretely 1. **Conformance has no definition but behaviour.** "Implements the specification" means "agrees with this recognizer on every input", which is a statement about infinitely many inputs. Without a corpus, each team tests against its own reading and the disagreements surface as documents that parse differently in two places. 2. **Equivalence cannot be checked.** Deciding whether two such grammars accept the same inputs is not a question a tool can answer in general. So refactoring the normative grammar — splitting a rule, reordering for clarity — cannot be validated mechanically; it can only be tested. 3. **Ordering is invisible in review.** Moving one alternative above another is a one-line diff that no reviewer reads as semantic. It can make an alternative unreachable and silently shrink the accepted set, which is a compatibility break disguised as tidying. 4. **A specification error is silent.** There is no ambiguity report, so a mistake does not surface when the grammar is compiled. It surfaces when someone finally writes the input that discriminates between what you meant and what you wrote, usually after several implementations have shipped. ## Why 'cannot be ambiguous' is not 'cannot be wrong' This is the confusion the question is really testing. Determinism says every input has at most one interpretation *under this grammar*. It says nothing about whether that interpretation is the one the syntax was meant to have. A grammar with a shadowed alternative is perfectly deterministic and perfectly wrong: it assigns a definite, unintended meaning to a whole family of documents, and does so consistently enough that nobody notices for a long time. ## Making it work anyway The formalism is not disqualified — plenty of published syntaxes are specified this way. What changes is the process around it: - **Ship a conformance corpus as part of the normative text.** Inputs paired with expected trees, including one input per alternative and one per boundary case. This, not the rules, is what implementations are tested against. - **Forbid overlapping alternatives by convention** where you can, so the order is not load-bearing. If one alternative is a prefix of another, require an explicit boundary predicate rather than relying on position. - **Treat alternative order as frozen API.** Any reordering goes through the same review as a change to the accepted set, because that is what it is. - **Version the grammar and say what a version bump may do.** Adding an alternative at the end is usually safe; inserting one earlier is not, because it can capture inputs that previously fell through. - **Document the commitment rule in prose.** A reimplementer coming from a different parsing tradition will assume backtracking is unlimited, and will produce a parser that accepts strictly more than yours unless told otherwise. - **Keep the grammar small enough to read.** Its meaning is operational, so its size is the size of the specification a human must simulate in their head. ## The judgement call There is no universally right answer, which is what makes this a lead's decision. If the syntax is small, the implementations are few, and you can afford to publish and maintain a corpus, an executable unambiguous grammar is an excellent specification precisely because it is testable. If the syntax will be reimplemented by parties you do not control, in tools built on a different parsing approach, the inability to state the language independently of one recognizer is the thing that will eventually cost you — and the mitigation is to spend the effort on the corpus, since that is what the other teams will actually build against.

  • Which grammar edits are safe to publish as a compatible revision?
    Appending an alternative at the end of a choice is usually safe, since inputs that already matched still match an earlier alternative. Inserting one earlier, or reordering, can capture inputs that previously fell through and is a breaking change. Every such edit needs the conformance corpus re-run, because nothing else will detect it.
  • What should the conformance corpus contain to be worth its normative status?
    At least one input per alternative that only that alternative should accept, one per boundary case where a name is a prefix of another, and inputs that must be rejected. The rejections matter most: an implementation that accepts too much passes an accept-only corpus while diverging on real documents.
  • What must you tell a reimplementer who has only worked with declarative rule sets?
    That order carries meaning and that commitment is possessive: once an alternative succeeds, a later failure does not re-enter that choice. A parser built on unlimited backtracking will accept strictly more inputs than the specification, and the difference shows up only on the inputs where an early alternative wins.

saying these in an interview costs you the question

  • Treats reordering alternatives as a cosmetic, non-breaking edit
  • Assumes a tool can prove two such grammars accept the same inputs
  • Expects independent implementations to agree without a shared corpus
  • Says the grammar is a complete specification because it is executable
  • Confuses cannot be ambiguous with cannot be wrong