Comments (1)
The key thing to remember is that the structure of the output mirrors the
structure of the grammar. In your first grammar you have these rules:
query <- (expression @" "? expr_op? @" "?)+
expression <- negation? @" "? lvalue @" " operator @" " rvalue
You try to parse this string:
'not distance is 20 or not date is today and not date is tomorrow'
And you get this sequence of elements from the root node of the tree:
not distance is 20 or
not date is today and
not date is tomorrow
This mirrors the structure of the query
rule whose expression is (expression @" "? expr_op? @" "?)+
-- a repetition of one or more instances of the
expression expression @" "? expr_op? @" "?
. So each element of a match for
query
will contain both an expression
and a expr_op
.
In order to get this output:
not distance is 20
or
not date is today
and
not date is tomorrow
You would need a grammar where the query
rule was a repetion of either
expression
or expr_op
, i.e. (expression / expr_op)+
(with spaces added
where necessary). This is probably not what you want, because it means the
grammar would match nonsense queries like or or and or
.
If I change the grammar such that all nodes are required I get the expected
output.
I'm not sure what you mean by "required" here, and the grammar presented after
this paragraph is identical to the first one.
Then you give another version of the query
rule:
query <- expression (@" " expr_op @" " expression)*
This rule is a sequence of two elements: it requires one expression
followed
by zero or more instances of (" " expr_op " " expression)
. Here is the output
showing which part of the rule is responsible for each part:
not distance is 20 expression
or not date is today and not date is tomorrow (@" " expr_op @" " expression)*
If you look inside the second element here you'll see multiple elements for each
match of the (...)*
expression:
or not date is today
and not date is tomorrow
This rule looks to me like the correct grammar, as you seem to want your query
language to allow one or more expressions
joined together by and
/or
operators. Every other version of query
you've shown will also match other
strings that won't make any sense, e.g. allowing the string to start or end with
an operator, or be composed entirely of operators.
from canopy.
Related Issues (20)
- installation via npm is broken HOT 4
- TypeError: Invalid options argument HOT 3
- Installation via npm does not work HOT 1
- Can we have predicates? HOT 1
- `make test` does not work so as the installation instruction HOT 3
- Parsing spaces in a string with Choices HOT 1
- Make the package web-friendly HOT 7
- Allow Java actions to throw ParserError? HOT 1
- Incorrect call to java.lang.String#substring() HOT 2
- Don't throw a bounds exception when reporting Java errors at EOF. HOT 1
- Add -h and --help flags to the command line tool
- Pass cross-references / named nodes to actions HOT 1
- Question about Improving a grammar HOT 2
- Develop/debug grammar online HOT 2
- Discarding white-space (or any other matched rule) by not a creating node. HOT 3
- When running canopy I get TypeError: mkdirp is not a function HOT 6
- TypeError: mkdirp is not a function HOT 1
- Question about character classes HOT 8
- Ordered choices failed to switch to the next choice HOT 1
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 canopy.