CryptoPatrick / code

Code · Language

pgf2json

Read compiled Grammatical Framework grammars (PGF) and turn them into JSON.

What it is

When the Grammatical Framework compiler builds a grammar, it writes a binary Portable Grammar Format (PGF) file. pgf2json reads that binary format into Rust data structures and converts it into structured JSON: functions, categories and each language’s concrete syntax. It can also parse a sentence with the loaded grammar. It is the loading layer underneath gf-core.

Using it

use pgf2json::{read_pgf, pgf_to_json, parse, language, types};

// Load a compiled grammar
let pgf = read_pgf("./grammars/Food.pgf")?;

// The whole grammar as JSON
let json = pgf_to_json(&pgf)?;

// Parse a sentence with the English concrete syntax
let lang = language::read_language("FoodEng").unwrap();
let typ = types::start_cat(&pgf);
let trees = parse(&pgf, &lang, &typ, "this pizza is delicious")?;

Ideas for using it

  • Inspect a grammar. Dump a PGF to JSON to see what a grammar actually contains, which is handy when debugging a grammar written by someone else.
  • Grammars in the browser. Ship the JSON to a web front end and run parsing or generation in JavaScript.
  • Diff grammar versions. JSON makes it easy to compare two compiled versions of a grammar.

Status

Stable for PGF 1.0. Support for PGF 2.1 is experimental: there is a known format difference still to handle.