Incremental-grammar-enhancement-work

Incremental grammar enhancement

Introduction

This document demonstrates how to use Large Language Models (LLMs) and Extended Backus-Naur Form (EBNF) to generate and incrementally develop grammars for Domain Specific Languages (DSLs).

Procedure outline

If LLM speaks Raku

TBD...

Using BNF

  1. Come up with sentences from a certain Domain Specific Language (DSL).

  2. Request a certain Large Language Model (LLM) -- for example, ChatGPT or PaLM -- to generate a corresponding grammar in Backus-Naur Form (BNF).

  3. Using the obtained BNF string create a corresponding Raku object that can be used generate new random sentences. One of:

    • Raku class for "FunctionalParsers"

    • Raku grammar

  4. With Raku object generate a set of random sentences.

  5. Request LLM to come up with, say, 5-10 variations of each sentence.

  6. Request BNF for the new, enhanced set of sentences.

  7. Is the obtained grammar large or comprehensive enough?

    • If not then go to step 2.

    • If yes finish.

Setup

Here are the packages we are going to use:

use Grammar::TokenProcessing;
use EBNF::Grammar;
use FunctionalParsers;
use WWW::OpenAI;
use WWW::PaLM;

Several iterations using OpenAI

my @startSentences = [
  'I hate R', 'I really love WL', 'We really hate WL', 'I love R', 
  'I love Julia', 'I really hate R', 'We hate R', 'I often hate WL', 'I often hate Perl',
  'I like Perl', 'We often hate R'
];
my $request1 = "Generate BNF grammar for the sentences: {@startSentences.join(', ')}";
my $variations1 = openai-completion($request1, format=>'values', temperature => 0.15, max-tokens => 600);
$variations1
my $variations2 = $variations1.lines.grep({ EBNF::Grammar::Relaxed.parse($_, rule => 'rule') }).join("\n");
my $grCode = ebnf-interpret($variations2, style => 'inverted', name => 'First', rule-type => 'rule');
say $grCode;
my $gr = ebnf-interpret($variations2, style => 'inverted', name=>'First', rule-type => 'rule'):eval;
my $grTopRule = "<{grammar-top-rule($grCode)}>";
say $grTopRule;

Generate random sentences:

my @genSentences = (^12).map({ random-sentence-generation($gr, $grTopRule) }).sort;

.say for @genSentences;

Make variations for each sentence:

my $k = 1;
my $request2 = "Make 4 variations of each of the sentences: {@genSentences.map({ "{$k++}) $_"}).join("\n")}";
$request2 
my $answer2 = openai-completion($request2, format=>'values', temperature => 0.65, max-tokens => 600);
$answer2

Split the sentences:

my @varSentences = $answer2.lines; 
.say for @varSentences;

Generate new BNF:

my $request3 = "Generate BNF grammar for the sentences: {[|@genSentences, |@varSentences].join(', ')}";
my $answer3 = openai-completion($request1, format=>'values', temperature => 0.15, max-tokens => 600);
$answer3

References

Articles

Packages, repositories

EBNF::Grammar v0.1.6

EBNF grammar and interpreters.

Authors

  • Anton Antonov

License

Artistic-2.0

Dependencies

FunctionalParsers:ver<0.1.6+>:auth<zef:antononcube>:api<1>

Test Dependencies

Provides

  • EBNF::Actions::EBNF::Standard
  • EBNF::Actions::JavaScript::Nearley
  • EBNF::Actions::MermaidJS::Graph
  • EBNF::Actions::Raku::AST
  • EBNF::Actions::Raku::FunctionalParsers
  • EBNF::Actions::Raku::Grammar
  • EBNF::Actions::WL::FunctionalParsers
  • EBNF::Grammar
  • EBNF::Grammar::Standardish

The Camelia image is copyright 2009 by Larry Wall. "Raku" is a trademark of the Yet Another Society. All rights reserved.

Built with Podlite — the markup and publishing tools behind this site.