All Projects → shnewto → Bnf

shnewto / Bnf

Licence: mit
Parse BNF grammar definitions

Programming Languages

rust
11053 projects
grammar
57 projects

Projects that are alternatives of or similar to Bnf

Surelog
SystemVerilog 2017 Pre-processor, Parser, Elaborator, UHDM Compiler. Provides IEEE Design/TB C/C++ VPI and Python AST API.
Stars: ✭ 116 (-6.45%)
Mutual labels:  parser
Commonmark Java
Java library for parsing and rendering CommonMark (Markdown)
Stars: ✭ 1,675 (+1250.81%)
Mutual labels:  parser
Awesome Hungarian Nlp
A curated list of NLP resources for Hungarian
Stars: ✭ 121 (-2.42%)
Mutual labels:  parser
Luqum
A lucene query parser generating ElasticSearch queries and more !
Stars: ✭ 118 (-4.84%)
Mutual labels:  parser
Rust hdl
Stars: ✭ 120 (-3.23%)
Mutual labels:  parser
Spectre.cli
An extremely opinionated command-line parser.
Stars: ✭ 121 (-2.42%)
Mutual labels:  parser
Ltgt
Lightweight HTML processor
Stars: ✭ 117 (-5.65%)
Mutual labels:  parser
Black Widow
GUI based offensive penetration testing tool (Open Source)
Stars: ✭ 124 (+0%)
Mutual labels:  parser
Pq
a command-line Protobuf parser with Kafka support and JSON output
Stars: ✭ 120 (-3.23%)
Mutual labels:  parser
Resume Parser
AWS Lambda function queried using AWS API Gateway to parse resumes using Lever API
Stars: ✭ 123 (-0.81%)
Mutual labels:  parser
Crass
A Ruby CSS parser that's fully compliant with the CSS Syntax Level 3 specification.
Stars: ✭ 118 (-4.84%)
Mutual labels:  parser
Html2pug
Converts HTML to Pug 🐶
Stars: ✭ 118 (-4.84%)
Mutual labels:  parser
Webrtc Sdp
Rust SDP parser for WebRTC
Stars: ✭ 121 (-2.42%)
Mutual labels:  parser
Instagram Parser
Парсер аккаунтов подписчиков и подписок в Instagram
Stars: ✭ 118 (-4.84%)
Mutual labels:  parser
Vue Styleguide Generator
React inspired style guide generator for Vue.js
Stars: ✭ 123 (-0.81%)
Mutual labels:  parser
Vte
Parser for virtual terminal emulators
Stars: ✭ 117 (-5.65%)
Mutual labels:  parser
Go Wasm
WebAssembly binary file parser written in go
Stars: ✭ 121 (-2.42%)
Mutual labels:  parser
Dan Jurafsky Chris Manning Nlp
My solution to the Natural Language Processing course made by Dan Jurafsky, Chris Manning in Winter 2012.
Stars: ✭ 124 (+0%)
Mutual labels:  parser
Whois Parser
Go(Golang) module for domain whois information parsing.
Stars: ✭ 123 (-0.81%)
Mutual labels:  parser
Php Po Parser
Parse Gettext *.PO files with PHP
Stars: ✭ 121 (-2.42%)
Mutual labels:  parser

bnf

.github/workflows/ci.yml codecov Crates.io Version Crates.io LICENSE

A library for parsing Backus–Naur form context-free grammars inspired by the JavaScript libraries prettybnf and erratic

What does a parsable BNF grammar look like?

The following grammar from the Wikipedia page on Backus-Naur form exemplifies a compatible grammar. (*Note: parser allows for an optional ';' to indicate the end of a production)

 <postal-address> ::= <name-part> <street-address> <zip-part>

      <name-part> ::= <personal-part> <last-name> <opt-suffix-part> <EOL>
                    | <personal-part> <name-part>

  <personal-part> ::= <initial> "." | <first-name>

 <street-address> ::= <house-num> <street-name> <opt-apt-num> <EOL>

       <zip-part> ::= <town-name> "," <state-code> <ZIP-code> <EOL>

<opt-suffix-part> ::= "Sr." | "Jr." | <roman-numeral> | ""
    <opt-apt-num> ::= <apt-num> | ""

Output

Take the following grammar for DNA sequences to be input to this library's parse function.

<dna> ::= <base> | <base> <dna>
<base> ::= "A" | "C" | "G" | "T"

The output is a Grammar object representing a tree that looks like this:

Grammar {
    productions: [
        Production {
            lhs: Nonterminal(
                "dna"
            ),
            rhs: [
                Expression {
                    terms: [
                        Nonterminal(
                            "base"
                        )
                    ]
                },
                Expression {
                    terms: [
                        Nonterminal(
                            "base"
                        ),
                        Nonterminal(
                            "dna"
                        )
                    ]
                }
            ]
        },
        Production {
            lhs: Nonterminal(
                "base"
            ),
            rhs: [
                Expression {
                    terms: [
                        Terminal(
                            "A"
                        )
                    ]
                },
                Expression {
                    terms: [
                        Terminal(
                            "C"
                        )
                    ]
                },
                Expression {
                    terms: [
                        Terminal(
                            "G"
                        )
                    ]
                },
                Expression {
                    terms: [
                        Terminal(
                            "T"
                        )
                    ]
                }
            ]
        }
    ]
}

Once the Grammar object is populated, to generate a random sentence from it call the object's generate function. grammar.generate(). For the above grammar you could expect something like TGGC or AG.

If the generate function can't find a production for a nonterminal it tries to evaluate it will print the identifer as a nonterminal, i.e. <identifier>.

The generate function will return an error if it detects an infinite loop caused by a production such as <PATTERN> ::= <PATTERN>.

Parse Example

extern crate bnf;
use bnf::Grammar;

fn main() {
    let input =
        "<postal-address> ::= <name-part> <street-address> <zip-part>

            <name-part> ::= <personal-part> <last-name> <opt-suffix-part> <EOL>
                            | <personal-part> <name-part>

        <personal-part> ::= <initial> \".\" | <first-name>

        <street-address> ::= <house-num> <street-name> <opt-apt-num> <EOL>

            <zip-part> ::= <town-name> \",\" <state-code> <ZIP-code> <EOL>

        <opt-suffix-part> ::= \"Sr.\" | \"Jr.\" | <roman-numeral> | \"\"
            <opt-apt-num> ::= <apt-num> | \"\"";

    let grammar: Result<Grammar, _> = input.parse();
    match grammar {
        Ok(g) => println!("{:#?}", g),
        Err(e) => println!("Failed to make grammar from String: {}", e),
    }
}

Generate Example

extern crate bnf;
use bnf::Grammar;

fn main() {
    let input =
        "<dna> ::= <base> | <base> <dna>
        <base> ::= \"A\" | \"C\" | \"G\" | \"T\"";
    let grammar: Grammar = input.parse().unwrap();
    let sentence = grammar.generate();
    match sentence {
        Ok(s) => println!("random sentence: {}", s),
        Err(e) => println!("something went wrong: {}!", e)
    }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].