Quantcast

Parsing something like "term1 OR term2 anotherterm"

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

Parsing something like "term1 OR term2 anotherterm"

brendan
Hi,

I've posted this over at SO as well: http://stackoverflow.com/questions/33029437/matching-or-expression-using-grappa-java-peg-parser so please excuse the double post.

I'm new to PEG parsing and trying to write a simple parser to parse out an expression like: "term1 OR term2 anotherterm" ideally into an AST that would look something like:

              OR
    -----------|---------
    |                    |
    "term1"            "term2 anotherterm"

I'm currently using Grappa (https://github.com/fge/grappa) but it's not matching even the more basic expression "term1 OR term2". This is what I have:

    package grappa;
   
    import com.github.fge.grappa.annotations.Label;
    import com.github.fge.grappa.parsers.BaseParser;
    import com.github.fge.grappa.rules.Rule;
   
    public class ExprParser extends BaseParser {
   
      @Label("expr")
      Rule expr() {
        return sequence(terms(), wsp(), string("OR"), wsp(), terms(), push(match()));
      }
     
      @Label("terms")
      Rule terms() {
        return sequence(whiteSpaces(),
            join(term()).using(wsp()).min(0),
            whiteSpaces());
      }
   
      @Label("term")
      Rule term() {
        return sequence(oneOrMore(character()), push(match()));
      }
   
      Rule character() {
        return anyOf(
            "0123456789" +
            "abcdefghijklmnopqrstuvwxyz" +
            "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
            "-_");
      }
     
      @Label("whiteSpaces")
      Rule whiteSpaces() {
        return join(zeroOrMore(wsp())).using(sequence(optional(cr()), lf())).min(0);
      }
   
    }

Can anyone point me in the right direction?

Thanks
Brendan
Loading...