PARSER_BEGIN(Q3) public class Q3 { public static void main(String args[]) throws ParseException { Q3 parser = new Q3(System.in); try { String c_prog = parser.s(); if (c_prog != null) System.out.println(c_prog); } catch (ParseException x) { System.out.println("Exiting."); throw x; } } } PARSER_END(Q3) /*** Ignoring whitespace ***/ SKIP : { " " | "\n" | "\r" | "\f" | "\t" } /******************/ /***** TOKENS *****/ /******************/ TOKEN : /* Keywords NB - this must be placed before defn of identifiers */ { < RECORD : "record" > | < END : "end" > } TOKEN : { < IDENT : ()+ > | < #LETTER : ["a" - "z", "A" - "Z"] > } TOKEN : /* Punctuation */ { < SEMI : ";" > | < COLON : ":" > | < COMMA : "," > } /*************************/ /***** GRAMMAR RULES *****/ /*************************/ String s() : { String fieldlist=null; } { fieldlist=fieldlist() { return "struct { " + fieldlist + "}"; } } String fieldlist() : { String field=null, rest=null; } { field=field() [ rest=fieldlist() ] { if (rest==null) return field; else return field + rest; } } String field() : { Token id; String idlist; } { idlist=idlist() id= { return id.image + " " + idlist + "; "; } } String idlist() : { Token id; String idlist = null; } { id= [ idlist=idlist()] { if (idlist == null) return id.image; else return id.image + "," + idlist; } }