NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Structured Code Representations Enable Data-Efficient Adaptation of Code Language Models

Mayank Agarwal, Yikang Shen, Bailin Wang, Yoon Kim, Jie ChenarXiv 2024

Abstract

Current language models tailored for code tasks often adopt the pre-training-then-fine-tuning paradigm from natural language processing, modeling source code as plain text. This approach, however, overlooks the unambiguous structures inherent in programming languages. In this work, we explore data-efficient adaptation of pre-trained code models by further pre-training and fine-tuning them with program structures. Specifically, we represent programs as parse trees—also known as concrete syntax trees (CSTs)—and adapt pre-trained models on serialized CSTs. Although the models that we adapt have been pre-trained only on the surface form of programs, we find that a small amount of continual pre-training and fine-tuning on CSTs without changing the model architecture yields improvements over the baseline approach across various code tasks. The improvements are found to be particularly significant when there are limited training examples, demonstrating the effectiveness of integrating program structures with plain-text representation even when working with backbone models that have not been pre-trained with structures.

Introduction

Most modern Large Language Models (LLMs) for code, such as CodeT5 or CodeGen, treat source code as a flat sequence of plain text, similar to natural language. While this "surface-form" approach is powerful when trained on massive datasets, it ignores the fundamental truth that code follows strict, formal grammars and possesses deep compositional structures. Adapting these massive models to specific downstream tasks usually requires thousands of labeled examples, which are often unavailable for proprietary or low-resource domain-specific languages. This research demonstrates that explicitly incorporating the "grammar" of programming languages—via serialized Concrete Syntax Trees (CSTs)—significantly boosts the data efficiency of code language models, allowing them to learn more from less.

Methodology: Adapting to Concrete Syntax Trees

The proposed method adapts pre-trained models to Concrete Syntax Trees (CSTs). Unlike Abstract Syntax Trees (ASTs), which simplify code, CSTs retain every detail—including whitespace and punctuation—making them perfectly invertible back into the original source code.

Overview of structured code representation, serialization, and the Masked Subtree/Node Prediction pre-training objectives.

The core of the approach is serialization, converting the 2D tree structure into a 1D sequence using a hybrid pre-post-order traversal. To make this compatible with standard Transformers without architectural changes, the authors:

  1. Augment the Tokenizer: Special tokens are added for non-terminal nodes (e.g., function_definition) so the model treats structural elements as atomic units.
  2. Continual Pre-training: A lightweight round of pre-training (~15 hours) is performed using two new objectives: Masked SubTree Prediction (MSP), where the model predicts missing branches, and Masked Node Prediction (MNP), where it predicts grammar rule sequences.
  3. Structured Fine-tuning: The model is fine-tuned on specific tasks using these serialized trees instead of plain text.

Results and Discussion

The most striking finding is the efficiency gain provided by structural information. In code translation and generation tasks, structured models consistently outperform base models using significantly less training data. For instance, on the CoNaLa code generation dataset, a structured CodeT5 model (CodeT5-S) trained on just 100 examples outperformed the base model trained on the entire 1.8k example dataset—an 18x improvement in data efficiency.

Performance comparison across various code translation tasks (Java ↔ C#, Java ↔ Python) showing consistent gains for structured models (CodeT5-S, CodeGen-S) over their plain-text counterparts.

Key results include:

  • Code Translation: On the Java ↔ C# task, CodeT5-S achieved higher CodeBLEU scores with 3k examples than the base model did with the full 10.3k dataset.
  • Functional Correctness: In the MBPP benchmark, structured models were significantly better at following complex instructions, such as using specific lambda expressions, where base models often hallucinated simpler, incorrect logic.
  • Robustness to Complexity: Structured models are significantly more robust as code complexity increases. Using Cyclomatic Complexity as a metric, the authors found that while base models' performance drops as logic becomes more nested, structured models maintain much higher accuracy.

Conclusion

Integrating program structures with plain-text representations enables pre-trained models to learn more effectively from limited data. While serialized CSTs increase sequence length and depend on reliable parsers like Tree-sitter, the "plug-and-play" nature of this approach makes it a powerful tool for adapting LLMs to low-resource coding environments. Future work may explore richer representations like Control Flow Graphs to further enhance model understanding.

Summary updated

BibTeX
@misc{agarwal2024structured,
  author = {Mayank Agarwal and Yikang Shen and Bailin Wang and Yoon Kim and Jie Chen},
  title = {{Structured Code Representations Enable Data-Efficient Adaptation of Code Language Models}},
  year = {2024},
  eprint = {2401.10716},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2401.10716},
}
Figure