Parsec - how to implement recursive grammar? #1004
-
Hello, I hope that someone can help me. I'm trying to implement a simple (but recursive) grammar: E = ( E ) | term But I cannot figure out how to recursively call itself: E = ( E ) I tried the following but it fails on NullPointerException: ` Parser ex = null;
Any help is appreciated, thank you in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
So, here's a few tips: 1. Declare
|
Beta Was this translation helpful? Give feedback.
So, here's a few tips:
1. Declare
ex
before-hand asnull
2. The first line of the LINQ expression can't refer to
ex
Because the right-hand-side of the first
in
is evaluated immediately,ex
will benull
. And so, yourbetween
parser won't be particularly useful.You can fix this (in general), by using
lazyp(() => ex)
, which will evaluateex
later, when it's got a value.Or, move the recursion to the second line of the parser, this can be done by c…