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).