Skip to main content
QUICK REVIEW

[Paper Review] Learning to Complete Code with Sketches

Daya Guo, Alexey Svyatkovskiy|arXiv (Cornell University)|Jun 18, 2021
Software Engineering ResearchComputer Science8 citations
TL;DR

This paper introduces GRAMMFORMER, a Transformer-based model that generates code completions as sketches—partial code with intentional 'holes' (■) where uncertainty exists—guided by programming language grammar. It outperforms standard autoregressive models by generating 10–50% more accurate completions and 37–50% longer, more precise sketches, using a reinforcement learning objective that rewards long, hole-minimal predictions matching ground-truth code.

ABSTRACT

Code completion is usually cast as a language modelling problem, i.e., continuing an input in a left-to-right fashion. However, in practice, some parts of the completion (e.g., string literals) may be very hard to predict, whereas subsequent parts directly follow from the context. To handle this, we instead consider the scenario of generating code completions with "holes" inserted in places where a model is uncertain. We develop Grammformer, a Transformer-based model that guides code generation by the programming language grammar, and compare it to a variety of more standard sequence models. We train the models on code completion for C# and Python given partial code context. To evaluate models, we consider both ROUGE as well as a new metric RegexAcc that measures success of generating completions matching long outputs with as few holes as possible. In our experiments, Grammformer generates 10-50% more accurate completions compared to traditional generative models and 37-50% longer sketches compared to sketch-generating baselines trained with similar techniques.

Motivation & Objective

  • To address the limitation of standard code language models in hallucinating incorrect code by enabling models to explicitly indicate uncertainty through 'holes' in code completions.
  • To develop a method that generates more precise and longer sketches—partial code with placeholders—by leveraging programming language grammar during generation.
  • To design a new evaluation metric, REGEXACC, that measures sketch correctness and concreteness by matching regex patterns derived from sketches to ground-truth code.
  • To train a model using reinforcement learning that optimizes for long, accurate sketches with minimal holes, avoiding incorrect predictions.
  • To demonstrate that grammar-guided, sketch-aware generation outperforms standard autoregressive models in code completion tasks for Python and C#.

Proposed method

  • GRAMMFORMER uses a Transformer-based architecture that generates code by constructing a partial syntax tree, allowing non-terminal nodes to be left as 'holes' (■) in the output.
  • The model is trained via reinforcement learning with a reward function that maximizes sketch length (number of terminal tokens) while minimizing holes, encouraging precise and complete suggestions.
  • A new metric, REGEXACC, is introduced: it converts sketches into regex patterns (replacing ■ with ".+"), then checks if the regex matches the ground-truth code, scaled by the proportion of non-hole tokens.
  • The training data is synthesized by inserting holes into ground-truth code completions, creating a supervised signal for the reinforcement learning objective.
  • The model leverages the formal grammar of the programming language to guide generation, ensuring syntactic correctness even with holes.
  • Unlike standard autoregressive models, GRAMMFORMER does not generate tokens left-to-right blindly; instead, it uses grammar-aware decoding to prioritize structurally valid completions.

Experimental results

Research questions

  • RQ1Can a neural code generation model be trained to produce sketches with intentional 'holes' rather than hallucinating tokens, improving reliability in code completion?
  • RQ2How does grammar-guided, sketch-aware generation compare to standard autoregressive language modeling in terms of accuracy and completeness of code completions?
  • RQ3Can a reinforcement learning objective that rewards long, hole-minimal sketches improve the quality of code completion compared to supervised baselines?
  • RQ4To what extent does the new metric REGEXACC correlate with human evaluation of sketch quality in code completion?
  • RQ5Does the ability to generate longer, more concrete sketches (with fewer holes) lead to better developer experience and reduced debugging effort?

Key findings

  • GRAMMFORMER generates 10–50% more accurate code completions than standard autoregressive language models on C# and Python code completion benchmarks.
  • GRAMMFORMER produces sketches that are 37–50% longer than those from sketch-generating baselines trained with similar techniques, indicating higher precision and concreteness.
  • The proposed REGEXACC metric effectively captures the quality of sketches by measuring regex match with ground-truth code while accounting for the proportion of actual tokens predicted.
  • Reinforcement learning with a grammar-aware objective significantly outperforms standard left-to-right autoregressive generation in the sketch generation task.
  • The model’s ability to leave non-terminal nodes as holes enables it to produce semantically valid, structurally correct code suggestions even when uncertain about specific values.
  • Human evaluation is needed to further validate the trade-off between sketch correctness and concreteness, as current results are based on automated metrics.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.