0

I need to write a java parser for expressions like this:

x - true, false, unknown

EXAMPLE:
or(x, x, and(x, x, x, or(not(x), x, x)), atleast(3, x, x, x, x, x, x, x, x)))

I figured out, that I could use antlr4, but I'm a total newbie. I was struggling with this problem for 3 days, I managed to grasp some of the concepts of antlr and run some simple examples, but I have no code that I could show as an attempt to solve this problem. As I really couldn't come up with anything that would be promising I started to wonder if this problem is even solvable with antlr? Could someone more experienced answer me this question and, possibly, if it is possible, give me some hints how to approach to solve the problem?

EDIT: I think I mostly got this. This is a sketch (no three valued logic implemented, not completed):

grammar grmmr;



start : operand {System.out.println($operand.value);};

operand returns [boolean value]

    : OR    LBR(
                    (   rsval {$value = $rsval.value;  }
                    |   op = operand {$value = $op.value;  }
                    )

                    (   CM rsval {$value = $value || $rsval.value;  }
                    |   CM op = operand {$value = $value || $op.value; }
                    )*
            )RBR

    | AND   LBR(
                    (   rsval {$value = $rsval.value;}
                    |   op = operand {$value = $op.value;}
                    )

                    (   CM rsval {$value = $value && $rsval.value;}
                    |   CM op=operand {$value = $value && $op.value;}
                    )*
            )RBR

    | rsval {$value = $rsval.value;};






rsval returns [boolean value]

    : rs = RS   {    if ( ( Integer.parseInt($rs.getText().substring(3, $rs.getText().length())) & 1 ) == 0 ) {
                        $value = true;
                    } else {
                        $value = false;
                    }
                 };


OR : 'or' ;
AND : 'and' ;
ATLEAST : 'atleast(' DIGIT CM;


LBR : '(';
RBR : ')';
CM : ',' ;

RS : 'rs'(DIGIT)+ ;

DIGIT : ('0'..'9') ;

WS : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ { skip(); };

However, this approach requires ALL rsval expressions to be evaluated, while 'lazy' calculating would be desired ( example: no point in evaluating y in or(x,y,z), if x was already evaluated and is true).

ltw
  • 265
  • 2
  • 17
  • 1
    Yes, it is possible to write such a parser. The programming LISP uses exactly such parser for its entire language. The first step is to write a grammar for your language. Show us that first. – Ira Baxter Aug 25 '16 at 08:47
  • The LDAP search filter syntax is another example. – user207421 Aug 25 '16 at 08:59
  • Okay guys, I will try to deliver, but it may take a while. – ltw Aug 25 '16 at 09:17
  • Define 'not exactly working'. – user207421 Aug 25 '16 at 10:35
  • 1
    For lazy evaluation, you need to defer evaluation till after the parse step. That means you have to build a syntax tree while parsing and then evaluate separately. – user207421 Aug 25 '16 at 12:20
  • Could you give me a simple example or a direction to a simple example on how to achieve this? – ltw Aug 25 '16 at 12:35
  • 1
    Not for ANTLR, but talks about parsing simple languages like yours, and discusses how to build trees as your parse, and evaluate them later: http://stackoverflow.com/questions/2245962/is-there-an-alternative-for-flex-bison-that-is-usable-on-8-bit-embedded-systems/2336769#2336769 You can either switch to that kind of parser (not much work) or use the understanding of how that works to make similar changes to your ANTLR parser. And of course, there's always the ANTLR documentation, that will tell you how to build trees as you parse. – Ira Baxter Aug 25 '16 at 12:48

0 Answers0