Syntactic Brittleness in Neural Code Representations

You are reading version 1 of this direction, frozen 2026-10-04. The latest version is version 2.

Version 1 of 2154 papers236 conceptsfrozen 2026-10-04

In software engineering, a fundamental principle is that the meaning of a program is independent of its surface-level formatting. A for loop can be rewritten as a while loop, and a variable named counter can be renamed to i without changing the program's execution logic. However, a growing body of research reveals that neural models for code—including Large Language Models (LLMs) and specialized embeddings—are surprisingly "brittle," often failing to maintain consistent predictions when faced with these simple, semantics-preserving transformations.

The Gap Between Syntax and Semantics

The core issue is a misalignment between how humans understand code (as a logical execution flow) and how neural models process it (as a sequence of statistical patterns). Ke Wang and colleagues demonstrated this in COSET: A Benchmark for Evaluating Neural Program Embeddings, showing that models often rely on "surface-level syntax" rather than underlying logic. When code is refactored—even if the logic remains identical—model accuracy frequently plummets.

This fragility suggests that models are often "cheating" by using syntactic proxies. For example, Ke Wang found in Demystifying What Code Summarization Models Learned that models like code2vec often ignore global program logic, instead relying on tiny, local syntactic snippets to make predictions. If a model associates the keyword sort with a specific task, it might fail to understand the same logic if the function is renamed or the sorting algorithm is implemented manually.

Benchmarking the Fragility

To quantify this "trustworthiness gap," researchers have developed rigorous stress tests. Shiqi Wang introduced ReCode: Robustness Evaluation of Code Generation Models, which applies over 30 semantic-preserving transformations to prompts. The results showed that even state-of-the-art models are sensitive to semantically irrelevant changes like formatting adjustments or variable renaming.

This sensitivity is particularly dangerous in security contexts. In LLMs Cannot Reliably Identify and Reason About Security Vulnerabilities (Yet?), Saad Ullah and others found that models like GPT-4 frequently provide "the right answers for the wrong reasons." A model might correctly identify a vulnerability because of a specific function name, but if that name is changed or if whitespace is added, the model's security posture changes—even though the code's execution path remains the same.

Why Models Fail: Spurious Correlations and Tokenization

Why do these models struggle with transformations that a first-year computer science student would find trivial?

  1. Spurious Correlations: Models often learn "false friends"—statistical patterns in naming conventions that don't reflect actual logic. Md Mahbubur Rahman argues in Towards Causal Deep Learning for Vulnerability Detection that models overfit to these correlations. When identifiers are randomized, performance on tasks like code search can collapse, as shown in How Does Naming Affect Language Models on Code Analysis Tasks?.
  2. Tokenization Misalignment: Yinxi Li identifies a fundamental "drift" in TokDrift. While code follows strict grammar, LLMs use subword tokenizers (like BPE). A simple change from camelCase to snake_case completely changes the token sequence, forcing the model to process a "new" problem even though the logic is identical.

Engineering for Semantic Invariance

To move beyond pattern matching, researchers are exploring ways to bake "semantic invariance" directly into model architectures and training loops.

  • Symmetry-Preserving Architectures: Kexin Pei developed SYMC, a Transformer architecture that is mathematically guaranteed to be invariant to certain code symmetries, such as reordering independent instructions. As detailed in Exploiting Code Symmetries for Learning Program Semantics, this approach allows models to recognize functional equivalence by design rather than by chance.
  • Robust Training and Defense: Frameworks like CodeFort by Shiqi Wang use adversarial and contrastive learning to make models resilient to "noisy" inputs. Similarly, CoTR uses a dual-defense strategy of data augmentation and adversarial training to harden code translation models.
  • Formal Reasoning: Moving toward formal specifications (like JML) helps evaluate if a model truly understands behavior. Thanh Le-Cong and Bach Le 0001 introduced FormalBench to test if LLMs can infer program semantics, revealing that while models are improving, they still rely heavily on syntactic patterns for complex logic like loops.

The transition from "statistical code mimics" to "robust program reasoners" requires models that can see through the syntax to the underlying execution logic. Until then, as Bach Le 0001 notes in Evaluating Program Repair with Semantic-Preserving Transformations, high benchmark scores may continue to mask a significant lack of natural robustness.

Papers in this version

Showing 60 of 154 papers.

History & citationVersion 2 · 2026-10-04

Current · Permanent link

  1. Version 2 (latest) · 2026-10-04 · 126 papers
  2. Version 1 · 2026-10-04 · 154 papers
Cite this direction

Latest version (a living page):

@misc{lacuna_adversarial_vulnerability_in_code_models_9aae870b,
  title = {adversarial vulnerability in code models},
  author = {{Lacuna}},
  howpublished = {Lacuna research direction},
  year = {2026},
  url = {https://lacuna.tiptreesystems.com/direction/adversarial-vulnerability-in-code-models/rdr_9aae870b0fc44474ade63346ddd9e72f},
  note = {Latest version of a living research direction; cite a version for a fixed text. Accessed 2026-11-23},
}

This exact version (version 1, frozen text):

@misc{lacuna_syntactic_brittleness_in_neural_code_rep_9aae870b_v1,
  title = {Syntactic Brittleness in Neural Code Representations},
  author = {{Lacuna}},
  howpublished = {Lacuna research direction},
  year = {2026},
  url = {https://lacuna.tiptreesystems.com/direction/syntactic-brittleness-in-neural-code-representations/rdr_9aae870b0fc44474ade63346ddd9e72f/v/1},
  note = {Version 1, frozen 2026-10-04. Accessed 2026-11-23},
}