Grape: Grammar-Preserving Rule Embedding

Qihao Zhu, Zeyu Sun, Wenjie Zhang, Yingfei Xiong, Lu ZhangIJCAI 2022

Abstract

Grape is a novel embedding approach designed to represent context-free grammar (CFG) rules as a graph, capturing their inherent structural and content-based relationships. By utilizing a Graph Neural Network (GNN) to generate these embeddings, Grape significantly enhances the performance of code generation and semantic parsing models. Experimental results across six benchmarks demonstrate that Grape outperforms state-of-the-art baselines by up to 6.4 percentage points, while also drastically reducing the rate of grammatically incorrect predictions.

Introduction

Neural networks used for code generation and semantic parsing typically represent underlying grammar rules using standard word embeddings. However, this approach is fundamentally limited for context-free languages (CFLs) like Python, Java, or SQL. Standard embeddings treat grammar rules as isolated tokens, ignoring two vital pieces of meta-information: structural relationships (parent-child hierarchies in rule expansion) and content information (shared symbols across different rules).

By neglecting these relationships, existing models fail to recognize that structurally similar rules should have similar representations. This leads to higher error rates and a lack of "grammatical common sense" within the model. Grape (Grammar-Preserving Rule Embedding) addresses this by treating the grammar as a first-class citizen, focusing on the internal machinery of the language rules to improve downstream generation tasks.

Methodology

Grape transforms a flat list of grammar rules into a rich, structured representation through two main components: the Grammar Relation Graph and a specialized neural architecture.

The Grape architecture extracts a relation graph from grammar rules and uses a GNN with gating mechanisms to produce enhanced grammar vectors.

Grammar Relation Graph

Instead of a sequence, the grammar is modeled as a directed graph where each node is a production rule. A directed edge is established from Rule 1 to Rule 2 if Rule 1 produces a non-terminal symbol that Rule 2 subsequently expands. This captures the generative flow of the language.

Neural Architecture

The Grape model employs a multi-layered approach to process this graph:

  1. Definition Encoding: This layer uses a gating mechanism to encode the actual symbols (the "content") within a rule.
  2. Neighbor Encoding: A modified Graph Convolutional Network (GCN) iterates through the graph to aggregate information from a rule’s neighbors, allowing the embedding to be influenced by its structural context.
  3. Gating Layer: A final fusion step integrates content and structural information into a single, high-dimensional "Grammar Vector."

This vector replaces traditional one-hot or word embeddings in base models like TreeGen.

Experimental Results

Grape was evaluated across six benchmarks covering four context-free languages, including Python (HearthStone, Django), Java (Concode), and Regular Expressions (StrReg).

Key Performance Gains

  • Accuracy Boost: Grape improved the accuracy of the base TreeGen model by 0.8 to 6.4 percentage points across all benchmarks. The largest gain (6.4 points) occurred in Regex synthesis, likely because structural embeddings compensate for sparse training data in complex grammars.
  • Reduction in "Illegal" Moves: On the Atis semantic parsing benchmark, Grape helped the model learn grammatical constraints more effectively, reducing the rate of predicting grammatically incorrect rules from 1.11% to just 0.15%.
  • Generality: When applied to a Method Naming task using an S-Transformer base model, Grape provided a 1.6 F1 score improvement, demonstrating its architecture-agnostic utility.

Visualization and Analysis

To understand why Grape succeeds, the authors utilized t-SNE visualizations to map the resulting rule embeddings.

t-SNE visualization of Grape embeddings shows that rules with similar functional roles (e.g., argument definitions) cluster together in the vector space.

As shown in Figure 5, Grape successfully clusters rules that serve similar functions—such as various ways to define an "argument"—even if those rules do not share many common tokens. This confirms that the model successfully translates structural similarity into vector proximity, providing the "grammatical common sense" that standard embeddings lack.

Conclusion

Grape demonstrates that representing the structural relationships of context-free grammars through graph-based embeddings significantly improves the performance and reliability of code generation models. While it introduces a slight computational overhead (approximately 13% increase in training time), the gains in accuracy and grammatical correctness are substantial. Future research may explore scaling Grape to massive multi-language foundation models and adapting it for dynamic, evolving grammars.

Summary updated

BibTeX
@inproceedings{zhu2022grape,
  author = {Qihao Zhu and Zeyu Sun and Wenjie Zhang and Yingfei Xiong and Lu Zhang},
  title = {{Grape: Grammar-Preserving Rule Embedding}},
  booktitle = {International Joint Conference on Artificial Intelligence},
  year = {2022},
  doi = {10.24963/ijcai.2022/631},
}
Figure