Grammar-Guided Code Generation with Reinforcement Learning

Resolve Bottlenecks,
Find Innovative Solutions
Generate Solutions

Solution Overview

Problem

Software development environments face challenges with code completion tools that generate realistic-looking but incorrect code suggestions, leading to user confusion and errors, as they often 'hallucinate' plausible but incorrect code snippets.

Innovation Solution

A code generation system utilizing a non-terminal selector model and a neural transformer model with attention to predict and expand non-terminal symbols in a grammar-guided approach, generating syntactically correct code sketches with holes to reflect user intent, allowing users to fill in uncertain positions.

Engineering Contradictions & Design Principles

VSEngineering Contradiction Analysis

1Reliability

If code completion tools generate realistic-looking code suggestions, then the suggestions appear plausible and useful to users, but the suggestions may be incorrect and hallucinate, leading to user confusion and errors

Engineering Contradiction:
Improveaccuracy of code suggestionsVSAvoidhallucination of incorrect code
Core Design Contradiction:
ReliabilityVSObject-generated harmful factors

Solution Approach 1:

The code generation process is segmented into multiple independent models: a non-terminal selector model that identifies which non-terminal symbols to expand, and a non-terminal expansion model that generates the actual code expansions. This segmentation allows each model to specialize in its function, improving overall reliability while reducing hallucinations through distributed decision-making

Inventive Principle:
Principle #1Segmentation

Solution Approach 2:

The system implements feedback mechanisms where the non-terminal selector model receives feedback from the expansion model about which non-terminals are most likely to be expanded next. This feedback loop allows the system to learn from its own generation process and adjust predictions to reduce incorrect suggestions while maintaining plausible code structures

Inventive Principle:
Principle #23Feedback

2Productivity

If the system generates complete code snippets, then development time is reduced, but the system may introduce errors through hallucinated code

Engineering Contradiction:
Improvedevelopment speedVSAvoidcode correctness
Core Design Contradiction:
ProductivityVSReliability

Solution Approach 1:

The system generates partial code completions that expand non-terminal symbols to their most likely expansions while leaving some ambiguity unresolved. By performing partial expansions rather than forcing complete code generation, the system maintains productivity through helpful suggestions while reducing the risk of introducing incorrect hallucinated code

Inventive Principle:
Principle #16Partial or excessive action

Solution Approach 2:

The system changes parameters of the generation process by using reinforcement learning with carefully designed reward functions that balance completion speed with code correctness. The reward function parameters are tuned to penalize hallucinations while rewarding productive completions, allowing the system to optimize both productivity and reliability simultaneously

Inventive Principle:
Principle #35Parameter changes

3Ease of operation

If the system provides detailed code suggestions, then user experience is improved, but the complexity of the generation system increases

Engineering Contradiction:
Improveuser experienceVSAvoidsystem complexity
Core Design Contradiction:
Ease of operationVSDevice complexity

Solution Approach 1:

The non-terminal expansion model serves multiple functions: it generates code expansions, identifies plausible completions, and provides rankings of suggestions. This multi-functionality allows the system to provide detailed user-friendly suggestions without requiring separate specialized components, thereby improving ease of operation while managing system complexity

Inventive Principle:
Principle #6Universality (Multi-functionality)

Data Source

PatentUS12197896B2Code generation with reinforcement learning
Publication Date: 2025.01.14 MICROSOFT TECHNOLOGY LICENSING LLC
  • US12197896B2 patent drawing
  • US12197896B2 patent drawing
  • US12197896B2 patent drawing

AI summary

A code generation system uses a non-terminal expansion model and a non-terminal selector model to generate a code sketch to complete a partially-formed source code snippet. The non-terminal expansion model is a neural transformer model trained on a supervised dataset through reinforcement learning to learn to predict the production rule to expand for a given non-terminal symbol. The non-terminal selector model is trained through reinforcement learning to predict the non-terminal symbol to expand given a partial-code state. The models are used in a two-step beam search to generate the top candidate code sketches, where a candidate code sketch may contain a hole that represents an unexpanded non-terminal symbol.