Coder Social home page Coder Social logo

Example usage about covfefe HOT 2 CLOSED

mlajtos avatar mlajtos commented on July 23, 2024
Example usage

from covfefe.

Comments (2)

palle-k avatar palle-k commented on July 23, 2024

Hi!

The Earley parser returns a syntax tree with the non-terminals as nodes and the ranges, in which terminals were identified, as leafs:

let tree: SyntaxTree<NonTerminal, Range<String.Index>>

Each value of this type SyntaxTree<NonTerminal, Range<String.Index>> is therefore either a case node(...) or a case leaf(...) and you can process them recursively through pattern matching:

switch tree {
case .node(key: let nonTerminal, children: let subtrees) where nonTerminal.name == "expression":
    // parse whole expression

case .node(key: let nonTerminal, children: let subtrees) where nonTerminal.name == "binary-operation":
    // parse binary operation
    let operatorSymbol = getOperator(subtrees[1]) // the 0th subtree is the left hand side expression, the 1st subtree is the binary-operator, the 2nd subtree is the right hand side expression.

// more cases for the remaining non-terminals
...

case .leaf(let range):
    // range is the position in which this leaf was identified in the parsed string.
    // you can get the text at this position using
    let leafText = String(parsedExpression[range])
}

This forms the base on which you can process your tree, for example mapping it into an expression type or evaluating it.

I have implemented an example project, which evaluates math expressions here: https://github.com/palle-k/ExpressionSolver

Let me know if this helps or if you have any further questions :)

from covfefe.

mlajtos avatar mlajtos commented on July 23, 2024

Yup. This is it, now I can hack together something. Thank you. ☺️

I added #6 – I think it should be mentioned somewhere because it is a great starting point for anybody that is dumb like me. :)

from covfefe.

Related Issues (10)

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.