| Refresh | Home EGTry.com

gated semantic predicate



Example 1

give a sequence of digits, take first sub-sequence whose sum of elements are no larger then 10.

antlr grammar

prog
@init {
  int sum=0;
}
: 

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

  (d2=DIGIT {System.out.println("Remaining Digit: "+$d2.text);})* 
  
  '\r'? '\n'
;

DIGIT: '0' .. '9';


input example

123491


Output

Remaining Digit: 9
Remaining Digit: 1



Example 2

give a sequence of digits, the first digit states how many digits to take next.

antlr grammar

@init {
  int len=0;
  int count=0;
}
: 
  d1=DIGIT {len=Integer.parseInt($d1.text); System.out.println("size of the following digits: "+len);} 
  ( { count< len }?=> d2=DIGIT {count++;System.out.println("element: "+$d2.text);}  )+ 

  (d3=DIGIT {System.out.println("Remaining Digit: "+$d3.text);})* 
  
  '\r'? '\n'
;


DIGIT: '0' .. '9';


input example

3123888


Output

size of the following digits: 3
element: 1
element: 2
element: 3
Remaining Digit: 8
Remaining Digit: 8
Remaining Digit: 8