grammar neq;
WS
: [ \t\r\n]+ -> skip
;
root: aGTb | bGTa | eq;
aGTb: 'a'
| eq aGTb
| aGTb eq
| aGTb aGTb
;
bGTa: 'b'
| eq bGTa
| bGTa eq
| bGTa bGTa
;
eq: 'a' 'b'
| 'b' 'a'
| 'a' eq 'b'
| 'b' eq 'a'
| eq eq
;
This version is too slow, however, tests:
Random strings length 20 literals: Avg time =14 ms, Min time =0, Max time =78
Length 40: Avg time =32, Min time =0, Max time =231
Length 60: Avg time =83, Min time =16, Max time =416
Length 80: Avg time =365, Min time =37, Max time =11935
Length 100: Avg time =1442, Min time =67, Max time =25317
Length 120: Avg time =21861, Min time =135, Max time =924769
The input for the above 924 sec parse time:
a b b a a a b b a a b a b a a a a b a a a a b b b a a a a b a a b
a a a b b b a a a b b a b b b a a b b a b a b a b a b a a a a b b
b a a b b b b a a b a b a b a a a b a b b b b b a a a b a a a b b
a b a b a a b b b a a b b a b a a a a a a
Can the grammar be rewritten to faster version? (Be careful, changing say eq
into
eq: 'a' 'b'
| 'b' 'a'
| eq 'a' 'b'
| eq 'b' 'a'
| 'a' eq 'b'
| 'b' eq 'a'
| 'a' 'b' eq
| 'b' 'a' eq
;
is not equivalent rule rewriting, as it will not recognize a a b b b b a a
).
root : ('a' | 'b')+;
. – kaby76eq: ('a'|'b')+ { /* validate # of children % 2 == 0, # of children > 1, equal # of 'a's and 'b's */ }?;
. Is this a CS class question? – kaby76