Resolvable Ambiguity

Viktor Palmkvist, Elias Castegren, Philipp Haller, David BromanarXiv 2019

Abstract

Traditional programming language design mandates strictly unambiguous grammars, often relying on arbitrary precedence rules that may conflict with user intuition or hinder language composition. This paper introduces "resolvable ambiguity," a framework where grammars can remain ambiguous provided users have a guaranteed mechanism—such as parentheses—to specify their intended interpretation. By leveraging Visibly Pushdown Automata (VPDA), the authors demonstrate that determining if an ambiguity is resolvable is decidable, offering a path toward more modular and user-centric language design.

Introduction

For decades, ambiguity in programming languages has been treated as a defect. Because determining the ambiguity of a context-free grammar is undecidable, designers typically restrict themselves to specific grammar classes (like LR or LL) and enforce rigid precedence and associativity rules. However, this approach often leads to arbitrary design choices and creates significant hurdles when attempting to compose independent domain-specific languages (DSLs).

This research challenges this status quo by proposing that ambiguity is acceptable if it is resolvable. In this paradigm, the parser does not need to guess the user's intent; instead, it ensures that the user can always provide clarifying information to uniquely identify the desired parse tree.

The Problem: The Composition Nightmare

The primary motivation for resolvable ambiguity lies in the difficulty of modular language design. When two unambiguous DSLs are merged, the resulting language is frequently ambiguous. Forcing a designer to manually resolve every potential conflict between merged grammars is a massive barrier to extensibility.

Furthermore, standard precedence rules often fail to capture user intent in complex scenarios. Instead of the compiler silently picking a "correct" interpretation that might surprise the developer, a system supporting resolvable ambiguity can identify the conflict and ask the user for clarification.

Technical Framework

The authors formalize resolvable ambiguity through several key technical innovations:

  1. Abstract Parsers and Marks: Syntax is defined using EBNF where "marks" prevent certain productions from being direct children of others, effectively encoding precedence without eliminating ambiguity entirely.
  2. The "Unparen" Function: Parentheses are treated as first-class information carriers. An ambiguity is considered "resolvable" if there exists a parenthesized version of the code that uniquely maps to each possible parse tree.
  3. Visibly Pushdown Automata (VPDA): By recognizing that the language of all possible parenthesized versions of a parse tree is "visibly pushdown," the authors utilize the closure properties of VPDAs to build robust decision procedures for both static and dynamic analysis.

Case Study: Component Modeling DSL

To evaluate their framework, the authors applied it to a DSL for hardware/software component composition. In this language, components can be linked in series (--) or parallel (||).

Multiple interpretations of a component composition expression, illustrating resolvable ambiguity.

Without strict precedence rules, a simple expression can yield multiple structural interpretations, as shown in the figure above. Under the resolvable ambiguity framework, the system can generate a "minimal witness"—the shortest set of parentheses required—to help the user disambiguate their code.

Key Results and Insights

  • Decidability: The authors proved that the dynamic resolvable ambiguity problem is decidable for languages with balanced parentheses.
  • OCaml Analysis: Testing against a substantial subset of OCaml revealed that the official reference manual is under-specified. The tool identified real-world cases, such as nested match expressions, where the compiler's silent choice could be replaced by a helpful disambiguation prompt.
  • Parentheses as Tools: This approach reframes parentheses from "noise" to be stripped away into essential tools for user-directed parsing.

Conclusion and Future Work

Resolvable ambiguity offers a promising alternative to the rigid constraints of traditional grammar design. While current limitations include handling "deep" precedence and "longest match" rules (like the dangling else problem), the framework provides a solid foundation for more flexible, modular, and user-friendly programming languages. Future work aims to expand the static analysis capabilities to cover a broader range of complex grammars.

Summary updated

Abstract

A common standpoint when designing the syntax of programming languages is that the grammar definition has to be unambiguous. However, requiring up front unambiguous grammars can force language designers to make more or less arbitrary choices to disambiguate the language. In this paper, we depart from the traditional view of unambiguous grammar design, and enable the detection of ambiguities to be delayed until parse time, allowing the user of the language to perform the disambiguation. A natural decision problem follows: given a language definition, can a user always disambiguate an ambiguous program? We introduce and formalize this fundamental problem - called the resolvable ambiguity problem - and divide it into separate static and dynamic resolvability problems. We provide solutions to the static problem for a restricted language class and sketch proofs of soundness and completeness. We also provide a sound and complete solution to the dynamic problem for a much less restricted class of languages. The approach is evaluated through two separate case studies, covering both a large existing programming language, and the composability of domain-specific languages.
BibTeX
@misc{palmkvist2019resolvable,
  author = {Viktor Palmkvist and Elias Castegren and Philipp Haller and David Broman},
  title = {{Resolvable Ambiguity}},
  year = {2019},
  eprint = {1911.05672},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1911.05672},
}
Figure