Quantcast

Recursion, grouping question

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Recursion, grouping question

da-z
Hi.

I am new to parboiled and would like to match something nested, like "(a(b(c))d)". I'd like to have (a(b(c))d) matched as a group, then (b(c)) as another group, then (c) and finally c.

My rule is

Rule SubSelect() {
      return FirstOf(Sequence("(", SubSelect(), ")"), OneOrMore(NoneOf("()")));
}

however it does not seem to work. Am I doing it wrong?

Is it maybe something along the lines of:

Rule SubSelect() {
      return FirstOf(Sequence("(", SubSelect(), ")"), OneOrMore(TestNot(SubSelect()), ANY));
}

However I'm getting StackOverflow..

Thank you very much.
fge
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Recursion, grouping question

fge
Try this (note that it uses grappa which is a fork of parboiled):

public class RecursiveParensParser
    extends EventBusParser<Void>
{
    Rule exprContent()
    {
        return oneOrMore(noneOf("()"));
    }

    Rule expression()
    {
        return sequence(
            '(',
            exprContent(),
            zeroOrMore(expression()),
            optional(exprContent()),
            ')'
        );
    }

    public static void main(final String... args)
    {
        final RecursiveParensParser parser
            = Parboiled.createParser(RecursiveParensParser.class);
        final ParseRunner<Void> runner =
            new BasicParseRunner<>(parser.expression());
        System.out.println(runner.run("(a(b(c))d)").isSuccess());
    }
}
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[SOLVED] Re: Recursion, grouping question

da-z
It works. Thank you very much!! :)
Loading...