Grammar-based Game Description Generation using Large Language Models

Tsunehiko Tanaka, Edgar Simo-SerraarXiv 2024 · DOI

TL;DR

This paper introduces a two-stage framework that enables Large Language Models (LLMs) to generate functional, grammatically correct game code from natural language descriptions. By first generating a minimal task-specific grammar and then using an iterative, parser-guided decoding process, the authors significantly improve the compilability and functionality of generated games compared to standard one-shot LLM approaches.

What Problem Does This Paper Address?

Automated game design often relies on Game Description Languages (GDLs)—domain-specific languages like Ludii that allow computers to simulate and evaluate games. While LLMs are excellent at understanding natural language, they struggle to output GDL code that actually works. The strict syntax of GDLs means a single misplaced parenthesis or an undefined term makes the entire game unplayable.

Existing search-based methods for game generation are robust but don't handle natural language well. Conversely, "one-shot" LLM generation often produces code that looks like GDL but fails to compile. The challenge is bridging the gap between ambiguous human descriptions and the rigid, complex requirements of a machine-readable game engine.

Methods

The authors propose a framework called Grammar-based Game Description Generation (GGDG). Instead of asking the LLM to write the whole game at once, they break the process into two iterative stages:

  1. Rule Decoding Stage: The LLM first generates a "minimal grammar"—a subset of the full GDL grammar containing only the rules necessary for the specific game described. Because the full Ludii grammar is too large for most LLM context windows, this stage iteratively identifies undefined symbols and asks the LLM to select the correct definitions from the master grammar.
  2. Game Description Decoding Stage: Using the minimal grammar from Stage 1, the framework generates the actual game code. It employs an Earley parser to validate the LLM's output in real-time. If the LLM produces an invalid sequence, the parser identifies the longest valid prefix and provides a set of candidate symbols that could legally follow it. The LLM then chooses the best candidate and continues generating.
Overview of the two-stage GGDG framework

This iterative refinement acts as a "guardrail," ensuring that every step of the generation process adheres to the formal syntax of the language.

Datasets

  • Ludii Game Portal: The authors used the publicly available Ludii database, which contains over 1,000 games ranging from board games to puzzles.
  • Evaluation Set: 100 randomly selected games from various categories (racing, mancala, puzzles, etc.).
  • Supervised Fine-Tuning (SFT) Data: A separate set of games from the Ludii portal was used to fine-tune the Llama3-8B model to better understand GDL syntax before applying the GGDG framework.

Key Insights and Ideas

  • The "Minimal Grammar" Bridge: The full GDL grammar is massive (~15k tokens). By tasking the LLM with generating a "minimal grammar" first, the authors effectively compress the relevant syntax into a size that fits within the model's context window, enhancing its "In-Context Learning" capabilities.
  • Parser-in-the-Loop: Integrating a formal parser (Earley parser) directly into the decoding loop transforms the LLM from a "stochastic parrot" into a guided logic engine. The parser doesn't just say "this is wrong"; it provides the valid "vocabulary" for the next step.
  • SFT and Grammar are Complementary: While fine-tuning (SFT) gives the model better intuition about the language, the grammar-based decoding provides the necessary structural enforcement. The best results came from combining both.
Fig. 3. Minimal Backus-Naur Form (BNF) grammar G[y] for Tic-Tac- Toe. Redundant rules are omitted for simplicity.

Key Results

The authors compared their GGDG method against standard Game Description Generation (GDG) baselines using metrics like Compilability (does it run?) and Functionality (is it playable?).

  • Compilability Boost: GGDG achieved a compilability score of 64.0%, more than doubling the baseline GDG score of 27.0%.
  • Functionality: Playable games increased from 26.3% (baseline) to 56.7% (GGDG).
  • State of the Art: When combined with Supervised Fine-Tuning (SFT+GGDG), the system reached 72.0% compilability and 70.3% functionality.
  • Quality: The Normalized Concept Distance (NCD), which measures how closely the generated game's behavior matches the ground truth, improved significantly (lower is better), dropping from 0.75 to 0.46.

Limitations and Future Work

  • Complexity Scaling: The framework struggles with very long game descriptions (500-1000 tokens). As the description grows, the minimal grammar also expands, potentially exceeding context limits or leading to redundant rule generation.
  • Spatial Reasoning: LLMs still struggle to understand complex spatial layouts (e.g., non-standard board shapes). Some generated games were syntactically correct but logically flawed in how they placed pieces on the board.
  • Inference Cost: The iterative nature of the decoding process is computationally expensive. GGDG takes an average of 143.9 seconds per game, compared to just 7.7 seconds for the baseline.
  • Future Directions: The authors suggest exploring more powerful models (like Llama-70B or GPT-4o) to improve spatial reasoning and investigating ways to optimize inference speed through better caching or specialized fine-tuning for the sub-tasks.

Summary updated

BibTeX
@misc{tanaka2024grammar,
  author = {Tsunehiko Tanaka and Edgar Simo-Serra},
  title = {{Grammar-based Game Description Generation using Large Language Models}},
  year = {2024},
  eprint = {2407.17404},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2407.17404},
}
Figure