Comments (2)
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.
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)
- dot output label is not human friendly HOT 1
- Swift 5.5 and Linux Unit tests
- Performance issues related to memory HOT 1
- BNF.md says unicode scalars are supported, but they aren't HOT 1
- Consider merging develop branch to master?
- ABNF support HOT 3
- Result Builders for Grammar HOT 5
- Re-creating the image in the readme HOT 1
- Ambiguous Grammar HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from covfefe.