Giter Club home page Giter Club logo

expression's Introduction

Covfefe

Build Status docs CocoaPods CocoaPods license

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

Usage

Swift Package Manager

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

Swift 4.1

.package(url: "https://github.com/palle-k/Covfefe.git", majorVersion: 0, minor: 4)

Swift 4.0

.package(url: "https://github.com/palle-k/Covfefe.git", majorVersion: 0, minor: 3)

CocoaPods

Alternatively, it can be added as a dependency via CocoaPods (iOS, tvOS, watchOS and macOS).

Swift 4.1

target 'Your-App-Name' do
  use_frameworks!
  pod 'Covfefe'
end

Swift 4.0

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)

To add this framework manually:

  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

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)")

Example Syntax Tree

expression's People

Contributors

palle-k avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.