| Refresh | Home EGTry.com

validating semantic predicates


enter a sequence of digits the constraints that the sum of all digits can't be more than 10.

antlr grammar

prog
@init {
  int sum=0;
}
: 

  (DIGIT {sum +=Integer.parseInt($DIGIT.text);}  )+ 
  { sum <=10}?

  '\r'? '\n'
;


DIGIT: '0' .. '9';


valid input

1234


invalid input

12345


exception for the above invalid input

line 1:5 rule prog failed predicate: { sum <=10}?