All pastes #2056527 Raw Edit

Anonymous

public java v1 · immutable
#2056527 ·published 2011-05-11 01:53 UTC
rendered paste body
options {STATIC = false ;IGNORE_CASE = true ;}PARSER_BEGIN(Assignment)class Assignment {	public static void main( String[] args )	throws ParseException, TokenMgrError {		Assignment parser = new Assignment( System.in ) ;		parser.Start() ; }}PARSER_END(Assignment)SKIP : 	{ " " | "," | "." }TOKEN : { <EOL : "\n" | "\r" | "\r\n"> }TOKEN : { <AND : "and"> }	//AND keyword TOKEN : { <THEN : "then"> }		//THEN keywordTOKEN : { <ANDALSO : "and also"> }		//AND ALSO keywordTOKEN : { <FINALLY : "finally"> }		//FINALLY keywordTOKEN : { <AFINALLY : "and finally"> }	//AND FINALLY keywordTOKEN : { <AAFINALLY : "and also finally"> }	//AND ALSO FINALLY keywordTOKEN : { <TFINALLY : "then finally"> }			// THEN FINALLY keywordTOKEN : { <TASK : (["a"-"z"])+ | "'"(["a"-"z", ",", " "])*"'"> }	//TASK tokenvoid Start() :{Token w;  String s = "";} {		(w=<TASK> {s=s+" "+w.image; System.out.print(w.image+" ");})+ {System.out.print("\b;");}	(( w=<AND> | w=<THEN> | w=<ANDALSO> ) {s=s+" _"+w.image+"_";} (w=<TASK> {s=s+" "+w.image;System.out.print(w.image+" ");})+ {System.out.print("\b;");} )* (w=<FINALLY> | w=<AFINALLY> | w=<AAFINALLY> | w=<TFINALLY> ) {s=s+" _"+w.image+"_";} (w=<TASK> {s=s+" "+w.image; System.out.print(w.image+" ");})+ {System.out.print("\b;");} <EOL>	//{System.out.print("tasks: ");}	{System.out.println("\n" + "min-time: " + mintime(s));}	{System.out.println("\n" + "max-time: " + maxtime(s));}	{System.out.println("\n" + "min-width: " + minwidth(s));}		}					int mintime(String x) :	{int count = 1; int idx = 0; x = x.replaceAll("_then finally_", "_then_"); x = x.replaceAll("_finally_", "_then_");}{		{ if (x.indexOf("_then_") > -1) 		//if the input string contains THEN keyword		while ((idx = x.indexOf("_then_", idx)) != -1)  //count the occurences of the THEN keyword 		{        idx++;        count++;		}		return count;	}	{return count;}}				int maxtime(String x) :	{int count = 1; int idx = 0; x = x.replaceAll("_and finally_", "_and_");}{	{ if (x.indexOf("_and_") > -1) 		//if the input string contains AND keyword		while ((idx = x.indexOf("_and_", idx)) != -1)  //count the occurences of the AND keyword 		{        idx++;        count++;		}		return count+mintime(x)-1;	}	{return count+mintime(x)-1;}}int minwidth(String x) :{int count = 0; int max = 0; /* x = x.replaceAll("_and also finally_", "_and also_"); */}{	  	 	{ if (x.contains("_and also_")) {   		//if the input string contains AND ALSO		System.out.println("in if statement");		for (int i=0; i<x.length(); i++) {			if (x.indexOf("_and also_") == i){				count++;				System.out.println("counted");}			else if	(((x.indexOf("_and_") == i) | (x.indexOf("_then_") == i)) | ((x.indexOf("_finally_") == i) | (x.indexOf("_and finally_") == i))) {				max = Math.max(count, max);				count = 0;				System.out.println("max assigned" + max);			}			 		}		return max+1;	 }	}	{return 1;}}