Source-linked AI summary

CodePAD: Sequence-based Code Generation with Pushdown Automaton

Yihong Dong, Xue Jiang, Yuchen Liu, Ge Li, Zhi Jin

arXiv:2211.00818v4cs.SEcs.AI

TL;DR

Ensuring grammatical correctness in sequence-based code generation requires constraining valid next tokens and recognizing completed code. CodePAD adds a PDA module and PDA-state learning to sequence-based models; the paper reports improved base-model performance and gains for pretrained models.

  • Problem

    Ensuring grammatical correctness during code generation requires constraining valid next tokens and recognizing completed code, addressing terminal-symbol mismatches and non-accept endings.

    Source: §1 · pp. 1–2
  • Method

    CodePAD equips sequence-based models with a PDA module for valid next-token prediction and accepting-state completion, and uses PDA states in decoder learning.

    Source: §1 · p. 2, §5.1 · p. 5, §9 · p. 9
  • Results

    CodePAD dramatically enhances base-model performance and outperforms SOTA tree-based methods without pre-training on three benchmark datasets; pre-trained models also improve significantly.

    Source: §1 · p. 2, §9 · p. 9
  • Takeaways & Limitations

    The PDA module is presented as ensuring grammatical correctness while retaining the advantages of sequence-based code generation.

    Source: §1 · p. 2

    CodePAD ensures grammatical correctness but does not prevent compilation errors.

    Source: §8 · p. 8

Abstract

from arXiv · show

In the process of code generation, it is essential to guarantee the generated code satisfies grammar constraints of programming language (PL). However, neglecting grammar constraints is a fatal drawback of commonly used sequence-based code generation. In this paper, we devise a pushdown automaton (PDA)-based methodology to address this problem, exploiting the principle that PL is a subset of PDA recognizable language and code accepted by PDA is grammatical. Specifically, we construct a PDA module and design an algorithm to constrain the generation of sequence-based models to ensure grammatical correctness. Guided by this methodology, we further propose CodePAD, a sequence-based code generation framework equipped with a PDA module, to integrate the deduction of PDA into deep learning. Additionally, this framework can leverage states of PDA deduction (including state representation, state prediction task, and joint prediction with state) to assist models in learning PDA deduction. To comprehensively evaluate CodePAD, we construct a PDA for Python and conduct extensive experiments on four public benchmark datasets. CodePAD can leverage existing sequence-based models, and we show that it can achieve 100\% grammatical correctness percentage on these benchmark datasets. Thus, it relatively improve 17\% CodeBLEU on CONALA, 8\% EM on DJANGO, and 15\% CodeBLEU on JUICE-10K compared to base models. In addition, our method significantly enhances pre-trained models, e.g., CodeBLEU of CodeGen-350M improvement from 3.21 to 21.54 on MBPP in zero-shot setting.

1 Introduction

Most sequence-based code generation does not guarantee grammatical correctness, limiting usability. CodePAD integrates a PDA module into sequence-based generation, and experiments report improvements across benchmark datasets.

1 Introduction

  • The PDA methodology constrains next-token prediction to valid terminal symbols and requires completed code to end in an accept state, addressing TSM and ENS errors.The paper treats ENS errors as tolerable until code generation ends.Source: §1 · pp. 1–2
  • Experiments on four public benchmark datasets verify CodePAD's effectiveness and generality, with significant improvements to base models, including pre-trained models in zero-shot settings.The introduction reports these outcomes without specifying metric values.Source: §1 · p. 2
  • CodePAD combines a PDA module and deduction-simulation algorithm with sequence-based generation, and uses PDA states through representation, prediction, and joint prediction.The state mechanisms are intended to help models learn PDA deduction.Source: §1 · p. 2

2 Motivation Example

  • At each generation step, shrinking the candidate set reduces token-selection difficulty while supporting grammatical correctness and code-generation quality.This is the stated motivation for integrating PDA into deep learning.Source: §2 · p. 3

3 Related Work

The section contrasts sequence-based and tree-based code generation, then motivates PDA constraints as a way to obtain grammatical correctness without tree-based methods’ costs. It outlines a PDA methodology that derives valid next symbols from grammar transitions and automaton state.

3.1 DL-based Code Generation

  • The section introduces sequence-based and tree-based approaches as the two prevailing kinds of deep-learning code generation.It then discusses their distinct trade-offs around grammatical correctness and generation.Source: §3.1 · p. 3

3.1.1 Sequence-based Approaches

  • Sequence-based approaches are commonly used for code generation, but they generally cannot guarantee grammatical correctness.The section describes them as directly generating token sequences and as convenient for obtaining partial code.Source: §3.1.1 · p. 3

3.1.2 Tree-based Approaches

  • Tree-based approaches can ensure grammatical correctness, but their AST sequences are about 1.5–2 times longer than token sequences and make snippet extraction and data preparation harder.The paper proposes its method as ensuring grammatical correctness while avoiding these tree-based disadvantages.Source: §3.1.2 · p. 3

3.2 Domain-specific Grammatical Sequence-based Code Generation

4 Methodology

  • A PDA module and algorithm derive a grammar-valid set of next symbols from the current state and stack, accounting for epsilon transitions.The methodology treats programming-language grammars as PDA-recognizable; in its Python construction, grammar nonterminals define states and terminals define input symbols.Source: §4 · p. 4

5 CodePAD

CodePAD adds a PDA module to sequence-based code generation and uses PDA states in the decoder, an auxiliary state-prediction task, and joint prediction. At inference, PDA constraints restrict tokens to valid choices, while training combines token and state prediction.

5 CodePAD

  • CodePAD adds a PDA module to a sequence-based framework and uses state representation, state prediction, and joint prediction to help generate grammatical code.Its framework is described as applicable to encoder-decoder and decoder-only sequence models.Source: §5 · pp. 4–5

5.1 State Representation

  • The decoder receives each token’s corresponding PDA state alongside the token sequence, and converts both into representation h.The paper describes the states as information about the status of the valid generated prefix.Source: §5.1 · p. 5

5.2 Token Prediction Task

  • The decoder uses its representation h_t and the NL representation sequence z to calculate token-generation probabilities.The token probability combines generation from the vocabulary with copying from the input.Source: §5.2 · p. 5

5.3 State Prediction Task

  • An auxiliary task predicts the next PDA state from prior tokens, prior states, and the input, providing guidance for token generation.For Python for_stmt, the tokens ‘for’, ‘in’, and ‘:’ are fixed pairings.Source: §5.3 · p. 5

5.4 Training and Inference

  • Training minimizes token- and state-prediction cross-entropy losses, while inference constrains each generated token to the PDA module’s valid set.The weighting parameter α controls the impact of the two losses.Source: §5.4 · pp. 5–6

5.4.1 Joint Prediction with State

  • Joint prediction combines token and state probabilities to discourage tokens associated with inappropriate PDA states.The state used in this combination is the actual PDA state obtained from the transition function.Source: §5.4.1 · p. 6

6 Experiment Setup

The experiments evaluate CodePAD with a Python PDA on four public datasets, comparing sequence-based and tree-based baselines using code-quality and grammatical-correctness metrics.

6.1 PL and Datasets

  • The experiments use a PDA for Python 2 and Python 3 on CONALA, DJANGO, JUICE-10K, and MBPP.The four datasets are public benchmarks.Source: §6.1 · p. 6

6.2 Baselines

  • Baselines include LSTM and Transformer sequence models, tree-based systems, and pretrained encoder-decoder and decoder-only models.The tree-based comparisons include TRANX, ML-TRANX, TRANX-RL, APT, and Transformer-AST.Source: §6.2 · pp. 6–7

6.3 Evaluation Metrics

  • Evaluation uses EM, BLEU-4, CodeBLEU, grammatical correctness, and grammatical correctness percentage.GCP is the percentage of generated codes that are grammatical.Source: §6.3 · p. 7

6.4 Implementation Details

  • Reported performance averages five runs to mitigate training instability, while baseline settings follow their respective papers.Source: §6.4 · p. 7

7 Experimental Results

CodePAD outperforms non-pretrained tree-based methods on three benchmarks, improves on its base models, and raises pretrained-model performance in zero-shot MBPP experiments.

7.1 RQ1: CodePAD vs Tree-based Methods

  • CodePAD outperforms state-of-the-art non-pretrained tree-based methods on three benchmarks in BLEU, CodeBLEU, and EM.The authors attribute the advantage to shorter code-token sequences than AST-node sequences and recommend Transformer for large datasets and long code generation.Source: §7.1 · p. 7

7.2 RQ2: Ablation Study

  • 17% relative CodeBLEU on CONALA, 8% EM on DJANGO, and 15% CodeBLEU on JUICE-10K are reported over base models.Ablations show each component contributes; removing the PDA module sharply reduces GCP, while reducing only JP or SR causes relatively small decreases.Source: §7.2 · p. 7

7.3 RQ3: Improvements of Sequence-based Methods with PDA Module

  • (14.44, 21.54), versus (1.55, 3.21), is the reported BLEU and CodeBLEU pair for CodeGen-350M without prompt on zero-shot MBPP.Pretrained sequence models improve with the PDA module; the authors suggest it guides models that can answer questions but may not organize answers into well-formed code.Source: §7.3 · p. 8

7.4 RQ4: Case Study

  • In the zero-shot example, CodeGen-350M with PDA generates grammatical code, though it remains short of the GOLD reference.Table 4 presents examples for the task of finding similar elements in two tuple lists.Source: §7.4 · p. 8, Table 4 · p. 8

8 Limitation

The work is limited by language coverage, the distinction between grammatical correctness and compilation, and added computational overhead.

8 Limitation

  • A specific PDA is needed for each programming language, and the study builds one only for Python 2 and Python 3.Source: §8 · p. 8
  • The method ensures grammatical correctness but cannot prevent compilation errors.Source: §8 · p. 8
  • Using a PDA increases CPU operation and memory overhead, which the authors describe as acceptable.Source: §8 · p. 8

9 Conclusion and Discussion

CodePAD combines a sequence-based code generator with PDA deduction and PDA-state features. The authors report improvements over base models and non-pretrained tree-based methods, and identify potential beyond Python.

9 Conclusion and Discussion

  • CodePAD integrates PDA deduction into deep learning for sequence-based code generation.Source: §9 · p. 8
  • The PDA module guarantees grammatical correctness, while state representation, state prediction, and joint prediction use PDA state information to improve code generation.The ablation study indicates that each component contributes.Source: §9 · p. 9
  • The authors identify potential for zero-shot code generation in niche programming languages with little data and for grammar constraints on context-free languages beyond programming languages.Source: §9 · p. 9

A Syntax-strings and Token-types in Python

This section identifies the Python grammar inventory summarized in Table 6.

A Syntax-strings and Token-types in Python

B Deterministic Pushdown Automaton

The paper motivates a general PDA for programming-language grammar constraints and describes background conditions for deterministic PDAs and model architecture.

B Deterministic Pushdown Automaton

  • Memory constraints make most programming languages deterministic context-free, but the method uses a general PDA because it is more accessible to construct and also applies to DPDAs.Source: B Deterministic Pushdown Automaton · p. 10
  • A PDA is deterministic when each state, stack symbol, and input or epsilon combination has one transition, and epsilon transitions exclude input-symbol transitions for that state and stack symbol.Source: B Deterministic Pushdown Automaton · p. 10

C Elementary Knowledge of Model Architecture

D.1 Datasets

The experiments use four Python code-generation datasets, tune model hyperparameters, and compare with tree-based and pre-trained sequence-based methods.

D.1 Datasets

  • CONALA contains 2,879 annotated NL questions and Python3 solutions, while DJANGO contains 18,805 NL-annotated Python2 examples.Table 7 presents dataset statistics.Source: §D.1 · p. 11, Table 7 · p. 11
  • JUICE-10K uses 10K randomly selected JUICE training samples, while MBPP contains 974 problems, each with three test cases.Source: §D.1 · p. 11

D.2 Hyperparameters

  • The experiments select α on validation sets, use beam size 2 for MBPP and 15 elsewhere, and set learning rates by model family.Transformer-based CodePAD uses 0.0001 and LSTM-based CodePAD uses 0.001.Source: §D.2 · p. 11

D.3.1 Non-pre-trained Tree-based Methods

  • Tree-based baselines include TRANX, ML-TRANX, TRANX-RL, and APT, using AST generation, mutual learning, dynamic branch selection, and antecedent-prioritized loss, respectively.Source: §D.3.1 · p. 11

D.3.2 Pre-trained Sequence-based Methods

  • Pre-trained sequence-based baselines include BART, CodeT5, CodeGen, and InCoder, with approaches spanning text denoising, code semantics, causal code generation, and NL-programming-data training.Source: §D.3.2 · pp. 11–12

D.4 Research Questions

The experiments examine baseline performance, component contributions, pre-trained model improvements, and grammatical generation; validation results vary with α, and the CONALA compilation rate is 99.2%.

D.4 Research Questions

  • The research questions compare CodePAD with non-pre-trained tree-based methods, assess component contributions and pre-trained sequence-based improvements, and examine PDA-assisted grammatical generation.Source: §D.4 · p. 12

E Effect of α

  • CodePAD performs relatively well across CONALA, DJANGO, and JUICE-10K at α = 1.0, while the α trend above 0.4 varies by base model and dataset.The experiments use validation-selected α values: LSTM/Transformer settings are (0.6, 1.0), (0.8, 1.0), and (1.0, 0.4) on CONALA, DJANGO, and JUICE-10K, respectively.Source: Figure 6 · p. 12, E Effect of α · p. 12

F Compilation Error Analysis

  • 99.2% is CodePAD’s compilation rate on CONALA; observed errors include type assignment, duplicate-parameter, and mapping errors, with the first two also occurring in tree-based methods.The authors state that transfer-function changes can address the first two types, while engineering methods can avoid mapping errors; Table 8 presents examples.Source: Table 8 · p. 12, F Compilation Error Analysis · p. 13
Loading 2211.00818v4…