gf-core
GF Rust Runtime
Rust crate for working with Grammatical Framework.
This crate contains a Rust implementation of the Grammatical Framework runtime.
Note: this is not a full Grammatical Framework compiler, only a runtime.
Applicability
This crate will enable the user to use GF in pure Rust source code bases. This makes it possible to have GF-powered applications and services without the need for a Haskell driven-GF backend server.
Quickstart
Here's a short example that illustrates the use of the runtime. For longer examples, please consult the official GF documentation.
Step 01: Write a grammar
To use this crate we need a grammar. Typically we want to write three grammars; an abstract grammar, and two concrete grammars.
Abstact Grammar File: Hello.gf
-- a "Hello World" grammar
abstract Hello = {
flags startcat = Greeting ;
cat Greeting ; Recipient ;
fun
Hello : Recipient -> Greeting ;
World, Mum, Friends : Recipient ;
}
Concrete English Grammar File: HelloEng.gf
concrete HelloEng of Hello = {
lincat Greeting, Recipient = {s : Str} ;
lin
Hello recip = {s = "hello" ++ recip.s} ;
World = {s = "world"} ;
Mum = {s = "mum"} ;
Friends = {s = "friends"} ;
}
Concrete Italian Grammar File: HelloIta.gf
concrete HelloIta of Hello = {
lincat Greeting, Recipient = {s : Str} ;
lin
Hello recip = {s = "ciao" ++ recip.s} ;
World = {s = "mondo"} ;
Mum = {s = "mamma"} ;
Friends = {s = "amici"} ;
}
Step 02: Compile the grammars into PGF
Once we have our grammars written, we need to compile them into PGF (Portable Grammar Format). To do this, we need the GF CLI, which is part of the GF binary.
The GF binary and cli can be downloaded from this page
# Compile the grammar into Hello.pgf
gf -make HelloEng.gf HelloIta.gf
Running this command, GF will look at Hello.gf
(since it's what the concrete syntaxes depend on) and then produce the PGF file: Hello.pgf
.
Step 03: use the runtime
Here's an example of using the gf-core runtime:
This program will:
- Load the
Food.pgf
grammar file - Parse it and convert it to JSON
- Create a GFGrammar from the JSON
- Parse the sentence "this fish is delicious" into an AST
- Linearizes the AST back to English text
- Shows available concrete grammars (English and Italian)
use *;
use fs;
use Bytes;