grammar Example_4_4_solution; /* partially solved left factorization and leftmost nonterminal expansion used check the grammar by menu Grammar -> Check Grammar (CTRL+r) */ options { k=1; } init : a '\n'; a : 'a' a_after_a | 'b' a_after_b ; a_after_a : 'b' b | 'c' a_after_ac ; a_after_ac : c | 'd' b ; a_after_b : 'b' a_after_bb | 'c' b c 'b' ; a_after_bb : 'a' a | 'b' a_after_bbb ; a_after_bbb : 'a' a_after_bbba | //epsilon ; a_after_bbba : b 'b'| 'd' c 'b' ; b : 'b' 'b' 'a' b_after_bba | 'c' b c ; b_after_bba : b | 'd' c ; c : 'a' a | 'b' ;