Question

I have this (working) LALR grammar for SABLECC:

Package org.univpm.grail.sable;

Helpers
    digit = [ '0' .. '9' ];
    letter = [ [ 'a' .. 'z' ] + [ 'A' .. 'Z' ] ];
    any_character = [ 0 .. 0xfffff ] ;

States
    normal, complex;

Tokens
    newline = ( 13 | 10 | 13 10 ) ; 
    blank = 32+ ;
    dot = '.' ;
    comma = ',' ;
    element = 'v' | 'V' | 'e' | 'E' | 'all' | 'ALL' ;
    cop = '>' | '<' | '>=' | '<=' | 'like' | 'LIKE' | '==' | '!=' ;
    number = digit+ | digit+ '.' digit digit? ;
    l_par = '(' ;
    r_par = ')' ;
    logic_and = 'and' | 'AND' ;
    logic_or = 'or' | 'OR' ;
    logic_not = 'not' | 'NOT' ;
    id = ( 95 | letter ) ( letter | digit )+ ;
    line_comment = '/' '/' [ any_character - [ 10 + 13 ] ]* ( 13 | 10 | 10 13 ) ;
    string = '"' letter* '"' ;

Ignored Tokens
    blank;

Productions
    phrase =
        {instruction} instr |
        {complex_instruction} instr newline+ phrase? ;

    instr = command query ;

    command =
        {identifier} id |
        {complex_identifier} id l_par parlist r_par ;

    parlist = 
        {complex_parlist} par comma parlist |
        {simple_parlist} par ;

    par = 
        {numero} number | 
        {stringa} string |
        {idpar} id ;

    query = 
        {query_or} query logic_or term | 
        {query_term} term ;

    term =
        {term_and} term logic_and factor |
        {term_factor} factor ;

    factor = 
        {atop} attroperator |
        {query_not} logic_not attroperator |
        {query_par} l_par query r_par ;

    attroperator =
        {simple_element} element |
        {complex_element} element dot id cop par ;

I was trying to convert it for XText that uses ANTLR (LL parser generator). I'm having trouble converting this two left-recursive rules:

query = 
    {query_or} query logic_or term | 
    {query_term} term ;

term =
    {term_and} term logic_and factor |
    {term_factor} factor ;

How should I do it? I think I should work with operator precedence but rigth now I can't just think in a LL way.

Was it helpful?

Solution

Well, I finally did it with this guide:

http://javadude.com/articles/lalrtoll.html

I had to solve left recursion

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top