Table of Contents
Fetching ...

Kajal: Extracting Grammar of a Source Code Using Large Language Models

Mohammad Jalili Torkamani

TL;DR

The paper tackles the challenge of automatically extracting DSL grammars, a process traditionally manual and error-prone. It introduces Kajal, a framework that uses prompt engineering and few-shot learning to guide LLMs in inferring grammars from DSL code snippets via an API, supplemented by a feedback-driven loop and parser validation. The approach relies on a Similar Grammar Extractor and a structured prompt format to generate grammars which are then parsed by a Lark-based parser, with results saved in JSON for analysis. Experimental results on unseen data show a significant gain from few-shot prompting ($60.0\%$) over the no-few-shot baseline ($45.0\%$), confirming the value of contextual examples; the work also discusses limitations such as lack of semantic guarantees and scalability considerations, pointing to future work with open-source LLMs and larger datasets.

Abstract

Understanding and extracting the grammar of a domain-specific language (DSL) is crucial for various software engineering tasks; however, manually creating these grammars is time-intensive and error-prone. This paper presents Kajal, a novel approach that automatically infers grammar from DSL code snippets by leveraging Large Language Models (LLMs) through prompt engineering and few-shot learning. Kajal dynamically constructs input prompts, using contextual information to guide the LLM in generating the corresponding grammars, which are iteratively refined through a feedback-driven approach. Our experiments show that Kajal achieves 60% accuracy with few-shot learning and 45% without it, demonstrating the significant impact of few-shot learning on the tool's effectiveness. This approach offers a promising solution for automating DSL grammar extraction, and future work will explore using smaller, open-source LLMs and testing on larger datasets to further validate Kajal's performance.

Kajal: Extracting Grammar of a Source Code Using Large Language Models

TL;DR

The paper tackles the challenge of automatically extracting DSL grammars, a process traditionally manual and error-prone. It introduces Kajal, a framework that uses prompt engineering and few-shot learning to guide LLMs in inferring grammars from DSL code snippets via an API, supplemented by a feedback-driven loop and parser validation. The approach relies on a Similar Grammar Extractor and a structured prompt format to generate grammars which are then parsed by a Lark-based parser, with results saved in JSON for analysis. Experimental results on unseen data show a significant gain from few-shot prompting () over the no-few-shot baseline (), confirming the value of contextual examples; the work also discusses limitations such as lack of semantic guarantees and scalability considerations, pointing to future work with open-source LLMs and larger datasets.

Abstract

Understanding and extracting the grammar of a domain-specific language (DSL) is crucial for various software engineering tasks; however, manually creating these grammars is time-intensive and error-prone. This paper presents Kajal, a novel approach that automatically infers grammar from DSL code snippets by leveraging Large Language Models (LLMs) through prompt engineering and few-shot learning. Kajal dynamically constructs input prompts, using contextual information to guide the LLM in generating the corresponding grammars, which are iteratively refined through a feedback-driven approach. Our experiments show that Kajal achieves 60% accuracy with few-shot learning and 45% without it, demonstrating the significant impact of few-shot learning on the tool's effectiveness. This approach offers a promising solution for automating DSL grammar extraction, and future work will explore using smaller, open-source LLMs and testing on larger datasets to further validate Kajal's performance.

Paper Structure

This paper contains 13 sections, 1 equation, 6 figures, 1 table.

Figures (6)

  • Figure 1: Kajal Workflow.
  • Figure 2: System Field.
  • Figure 3: Example of Inference.
  • Figure 4: Kajal Experiments' Results.
  • Figure 5: Kajal GIV and PAP Metrics.
  • ...and 1 more figures