/* The grammar below is correct, but you're never going to get rid * of the ambiguities... * The best bet would be to code the whole thing directly in Java. */ options { LOOKAHEAD=1; } PARSER_BEGIN(Q2) public class Q2 { /*** Main routine - just call parser ***/ public static void main(String args[]) throws ParseException { Q2 parser = new Q2(System.in); try { parser.inp_string(); System.out.println("Is a palindrome!"); } catch (ParseException x) { System.out.println("Is not a palindrome"); // Let's not throw x; } } } PARSER_END(Q2) SKIP : /*** Ignoring spaces/tabs ***/ { " " | "\t" } /*************************/ /***** GRAMMAR RULES *****/ /*************************/ void inp_string() : {} { pal() ( | "\n" | "\r") } void pal() : {} { "a" [ pal() "a" ] | "b" [ pal() "b" ] | "c" }