Covfefe is a parser framework for languages generated by any (deterministic or nondeterministic) context free grammar. It implements the Earley and CYK algorithm.
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", majorVersion: 0, minor: 4)
.package(url: "https://github.com/palle-k/Covfefe.git", majorVersion: 0, minor: 3)
Alternatively, it can be added as a dependency via CocoaPods (iOS, tvOS, watchOS and macOS).
target 'Your-App-Name' do
use_frameworks!
pod 'Covfefe'
end
target 'Your-App-Name' do
use_frameworks!
pod 'Covfefe', '0.3.7'
end
Some grammar features are not available when using Swift 4.0 (Grouping, Repetitions, Optional Sequences and Character Ranges, EBNF import)
git clone https://github.com/palle-k/Covfefe.git
cd Covfefe
swift package generate-xcodeproj
- Drag the generated project file into your project
- Add
Covfefe.framework
in the Embedded Binaries section of your project settings
Grammars can be specified in a superset of EBNF or a superset of BNF, which adopts some features of EBNF (documented here).
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 = '0' ... '9';
variable = {letter};
letter = 'A' ... 'Z' | 'a' ... 'z';
"""
let grammar = try Grammar(ebnf: 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)")