Context-free grammars (CFGs) are the de-facto formalism for declaratively describing concrete syntax for programming languages and generating parsers. One of the major challenges in defining a desired syntax is ruling out all possible ambiguities in the CFG productions that determine scoping rules as well as operator precedence and associativity. Practical tools for parser generation typically apply ad-hoc approaches for resolving such ambiguities, which might result in a parser's behavior that contradicts the intents of the language designer. In this work, we present a user-friendly approach to soundly repair grammars with ambiguities, which is inspired by the programming by example line of research in automated program synthesis. At the heart of our approach is the interpretation of both the initial CFG and additional examples that define the desired restrictions in precedence and associativity, as tree automata (TAs). The technical novelties of our approach are (1) a new TA learning algorithm that constructs an automaton based on the original grammar and examples that encode the user's preferred ways of resolving ambiguities all in a single TA, and (2) an efficient algorithm for TA intersection that utilises reachability analysis and optimizations that significantly reduce the size of the resulting automaton, which results in idiomatic CFGs amenable to parser generators. We have proven the soundness of the algorithms, and implemented our approach in a tool called Greta, demonstrating its effectiveness on a series of case studies.
翻译:上下文无关语法(CFG)是声明式描述编程语言具体语法并生成解析器的实际标准形式化工具。定义期望语法的主要挑战之一在于排除CFG产生式中所有可能的歧义,这些产生式决定了作用域规则以及运算符优先级和结合性。实用的解析器生成工具通常采用临时性方法解决此类歧义,可能导致解析器行为与语言设计者的意图相悖。本研究提出一种用户友好的方法,可对存在歧义的语法进行可靠修复,其灵感来源于自动化程序合成领域中的示例编程研究方向。我们方法的核心在于将初始CFG与定义优先级和结合性约束的附加示例统一解释为树自动机(TA)。本方法的技术创新在于:(1)提出新型TA学习算法,能够基于原始语法和编码用户偏好歧义解决方式的示例,在单一TA中构建自动机;(2)设计高效的TA交集算法,利用可达性分析和优化技术显著缩减结果自动机规模,从而生成适用于解析器生成器的惯用CFG。我们已证明算法的可靠性,并在名为Greta的工具中实现了该方法,通过系列案例研究验证了其有效性。