Replies: 1 comment
-
What I would suggest is taking one of the expression parsing examples and modifying it to do what you want. That will give you a functioning starting point.
It's not really idiomatic one way or the other, it's more about the requirements of your language - if your language is ambiguous if keywords are used as identifiers, then you should have separate tokens. This seems to be the case in your language, take "bar AND foo" as an example - if "foo" was renamed to "not" you would have an ambiguous parse, as the parser wouldn't know if it was beginning a negation expression or was an identifier. |
Beta Was this translation helpful? Give feedback.
-
Hi all.
Apologies in advance: this isn't a bug report; it's a request for help. I'm hoping that some kind soul can point me in the right direction, much as happened for the user who posted issue 178.
I'm experimenting with replacing a Java-based parser using ANTLR with one using
participle
, but I'm having trouble getting quite a simple parser working, and I think there must be something fundamental that I've misunderstood.The task at hand is to have arbitrary (and arbitrarily nested) boolean combinations of "node identifers", of the form "<string.number>". The strings ultimately evaluate to an application-specific boolean, but how that happens is unrelated to the actual parsing.
Here's my example code, in full:
The first few example expressions (with node expressions, parentheses and NOTs) work, but the moment I try to enable the AND and OR expressions, I cause infinite recursion.
Things I've tried, in no particular order:
participle.Union
.UseLookahead()
(which is 100% pure cargo-cult).AndExpression
, have a 3-fieldAndExpression
where theAndToken
gets consumed into anOperator
field (which is unnecessary, since the struct type determine the operator anyway, but I tried in case my 2-argument grammar constructs were faulty).TopLevelExpression
struct with aHead *Expression
followed by aTail []*Expression
.The last of these was pure desperation; it shouldn't even be needed, since we aren't parsing something like a computer program, where there's "one or more" things to be parsed; the entire expression should be parseable into a tree with a single root node.
I'm sure that the solution is just a few keystrokes' worth of fixes, but after 2 days of trying, I'm out of ideas.
Finally, is it considered more idiomatic to create lexer tokens for keywords, as I've done above, or is it considered more idiomatic to simply quote the keywords in the grammar annotations?
Beta Was this translation helpful? Give feedback.
All reactions