Introduction
Context-Free Grammars (CFGs) are foundational to computer science, used extensively for defining programming languages and data formats. However, they possess a significant limitation: they cannot express context-sensitive constraints, such as ensuring a variable is declared before use or verifying that the counts of different characters in a string are equal. This paper introduces Answer Set Grammars (ASGs), a powerful formalism that extends CFGs with Answer Set Programming (ASP) annotations to capture these complex constraints. Furthermore, the authors provide a framework to automatically learn these semantic constraints from examples using Inductive Logic Programming (ILP).
The Challenge of Context-Sensitivity
While CFGs are efficient for syntax, they struggle with global constraints. Existing solutions like Attribute Grammars or Definite Clause Grammars (DCGs) often rely on procedural logic, which can be brittle and difficult to reason about. In fields such as software fuzzing or intrusion detection, where grammars are often incomplete or slightly incorrect, manually fixing rules is a tedious process. ASGs offer a purely declarative alternative to represent these constraints and, crucially, a method to induce them automatically from positive and negative string examples.
Methodology: Representation and Induction
The authors propose two primary contributions to bridge the gap between syntax and semantics:
1. Representation (ASGs)
An ASG is a CFG where production rules are annotated with ASP code. When a string is parsed, the annotations for the chosen rules are collected into an ASP program. If this program has a valid "answer set" (a stable model), the string is accepted. This allows the grammar to enforce global constraints across the entire parse tree.
2. Induction (Learning)
The authors treat grammar learning as an ILP problem, utilizing the ILASP (Inductive Learning of Answer Set Programs) solver. The core technique involves mapping the CFG's parse trees into background knowledge for the solver, allowing it to search for the simplest ASP annotations that correctly classify a set of positive ($E^+$) and negative ($E^-$) strings.
Complexity Analysis and Key Insights
A major contribution of the paper is a rigorous complexity analysis, demonstrating how the computational difficulty of deciding string membership scales based on the fragment of ASP used in the annotations.
- Complexity Tuning: As shown in the table above, propositional Horn ASGs are NP-complete, while first-order unstratified ASGs can reach NEXP. This allows developers to precisely control the power of the grammar.
- Declarative over Procedural: Unlike DCGs, ASGs are entirely declarative. Rule order does not matter, and constraints are evaluated globally, avoiding the side-effect pitfalls of traditional attribute grammars.
- Learning "On Top" of Syntax: A key insight is that learning only the annotations while keeping the CFG fixed is significantly more efficient than learning a complex grammar from scratch. This allows the learner to focus purely on the semantic logic of the language.
Experimental Results
The approach was evaluated on several formal language benchmarks, including context-sensitive languages ($a^n b^n c^n$, copy languages) and NP-complete problems (subset-sum, 3-colourability).
- Efficiency: Most benchmark languages were learned in under 10 seconds using only a handful of examples (often 1-3 positive and 2-10 negative strings).
- Flexibility: The system successfully learned the 3-colourability constraint for graphs, demonstrating its ability to handle complex combinatorial logic.
- Performance Scaling: While fast for small benchmarks, performance is sensitive to the initial CFG. A very general CFG for the $a^n b^n c^n$ task increased learning time to over 3.7 hours, highlighting the importance of the starting syntax.
Conclusion and Future Work
ASGs provide a robust framework for representing and inducing context-sensitive constraints. While the current framework assumes a known context-free syntax and relies on an oracle for counterexamples, it represents a significant step toward automated grammar refinement. Future research will explore scaling to larger programs and identifying the specific class of ASGs that captures PSPACE-complete Context-Sensitive Grammars.
Summary updated