Package -

Covfefe

Build Status docs

Covfefe is a parser generator framework for languages generated by any (deterministic or nondeterministic) context free grammar. It uses the Earley or CYK algorithm.

Usage

This framework can be imported as a Swift Package by adding it as a dependency to the Package.swift file:

.package(url: "https://github.com/palle-k/Covfefe.git", from: "0.3.0")

To add this package to a non-Swift Package Manager project:

  1. git clone https://github.com/palle-k/Covfefe.git
  2. cd Covfefe
  3. swift package generate-xcodeproj
  4. Drag the generated project file into your project
  5. Add Covfefe.framework in the Embedded Binaries section of your project settings

Example

A grammar may be specified in Backus Naur form:

let grammarString = """
<expression>       ::= <binary-operation> | <brackets> | <unary-operation> | <number> | <variable>
<brackets>         ::= '(' <expression> ')'
<binary-operation> ::= <expression> <binary-operator> <expression>
<binary-operator>  ::= '+' | '-' | '*' | '/'
<unary-operation>  ::= <unary-operator> <expression>
<unary-operator>   ::= '+' | '-'
<number>           ::= <digit> | <digit> <number>
<digit>            ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
<variable>         ::= <letter> | <letter> <variable>
<letter>           ::= "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z" | "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
"""
let grammar = try Grammar(bnfString: grammarString, start: "expression")

This grammar describes simple mathematical expressions consisting of unary and binary operations and parentheses. A syntax tree can be generated, which describes how a given word was derived from the grammar above:

let parser = EarleyParser(grammar: grammar)

let syntaxTree = try parser.syntaxTree(for: "(a+b)*(-c)")

Example Syntax Tree

Github

link
Stars:

Advertisement