options { LOOKAHEAD=1; } PARSER_BEGIN(Q1) public class Q1 { /* Variables and methods are static for efficiency... */ /*** Keeping track of start of paragraph/sentence ***/ static boolean new_sent = true; static boolean new_para = true; static void mkNewPara() { new_sent=true; new_para=true; } static void mkNewSent() { new_sent=true; new_para=false; } static void mkMidLine() { new_sent=false; new_para=false; } /*** A list of the proper nouns ***/ static StringBuffer prop_nouns = new StringBuffer(); static void addNoun(String noun) { // Check if there already before adding... if (prop_nouns.toString().indexOf(noun) == -1) prop_nouns.append(" "+noun); } /*** Main routine - call parser, print proper nouns ***/ public static void main(String args[]) throws ParseException { Q1 parser = new Q1(System.in); try { parser.essay(); System.out.println("Proper Nouns: " + prop_nouns.toString()); } catch (ParseException x) { System.out.println("Exiting due to parse error."); throw x; } } } PARSER_END(Q1) SKIP : /*** Ignoring spaces/tabs ***/ { " " | "\t" } TOKEN : /*** Nouns and Pronouns ***/ { < NOUN : ["a" - "z"]()* > | < PNOUN : ["A" - "Z"]()* > | < #LETTER : ["a" - "z", "A" - "Z"] > } /*************************/ /***** GRAMMAR RULES *****/ /*************************/ void essay() : {} { ( tok() { System.out.print(token.image); } )+ } void tok() : {} { ( | ) { if (!new_para) { System.out.print(" "); if (new_sent) System.out.print(" "); else if (token.kind == PNOUN) addNoun(token.image); } mkMidLine(); } | "." { mkNewSent(); } | ( ";" | "," | ":" ) { mkMidLine(); } | ( "\n" | "\r") { mkNewPara(); } }