module xtc.lang.lojban; //import xtc.lang.JavaIdentifier(xtc.lang.JavaSymbol, xtc.util.Spacing); //instantiate xtc.util.Symbol(xtc.util.Spacing); //import xtc.lang.JavaSymbol(xtc.util.Symbol); //import xtc.util.Spacing; header { import java.io.Reader; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.StringReader; import java.io.InputStreamReader; import xtc.util.Pair; import xtc.tree.GNode; import xtc.tree.Printer; import xtc.util.Utilities; import xtc.tree.Attribute; import xtc.tree.Printer; import gnu.getopt.Getopt; // Numerical data loading. import java.util.HashMap; import java.io.StreamTokenizer; import java.util.Vector; // These are from VFormat.java import java.util.ArrayList; import java.util.regex.Pattern; import java.util.regex.Matcher; } body { // Used for ZOI public static String startString; public static String startString2; // Different types of output. public static boolean parserParens = false; public static boolean morphology = false; public static boolean morphprint = false; public static boolean whitespace = false; public static boolean blanks = false; public static boolean pretty = false; public static boolean text = false; public static boolean terml = false; public static boolean verbose = false; public static boolean latex = false; public static HashMap mex_data; public static void loadMexData() { try { FileReader f = new FileReader( "/home/rlpowell/www/hobbies/lojban/grammar/rats/mex.tsv" ); Reader r = new BufferedReader( f ); StreamTokenizer st = new StreamTokenizer(r); // Tokenizer setup. st.eolIsSignificant( true ); st.wordChars( ' ', '~' ); st.whitespaceChars( ' ', ' ' ); st.quoteChar( '\"' ); mex_data = new HashMap(); int ttype; ttype = st.nextToken(); while( ttype != java.io.StreamTokenizer.TT_EOF ) { String key; String[] values; values = new String[3]; int i = 0; if( st.sval == null ) { key = Integer.toString( (int) st.nval ); } else { key = st.sval; } ttype = st.nextToken(); while( ttype != java.io.StreamTokenizer.TT_EOL && ttype != java.io.StreamTokenizer.TT_EOF ) { if( st.sval == null ) { values[i] = Integer.toString( (int) st.nval ); } else { values[i] = st.sval; } System.out.println("mex val: " + values[i] ); i++; ttype = st.nextToken(); } System.out.println("mex data: " + key + "," + values[0] + "," + values[1] + "," + values[2] ); mex_data.put( key, values ); ttype = st.nextToken(); } } catch( java.io.FileNotFoundException e ) { System.out.println("mex data file not found.\n"); } catch( java.io.IOException e ) { System.out.println("mex data file io problems.\n"); } } public static boolean myAssign2 (String obj1) { startString2 = obj1; //System.out.println("ssmyAssign;" + startString + ";"); return true; } public static boolean myCompare (String obj1, String obj2) { /* System.out.println("obj1;" + obj1 + ";"); System.out.println("obj2;" + obj2 + ";"); System.out.println("comp;" + obj1.equals( obj2 ) + ";"); */ return obj1.equals( obj2 ); } public static boolean myAssign (String obj1) { startString = obj1; //System.out.println("ssmyAssign;" + startString + ";"); return true; } public static String nonVerbose( String pre, String main, boolean outermost ) { String retval; //System.out.println( "nv pre: "+pre); //System.out.println( "nv pre: "+main); // We mark the first increase in nesting. If we get back to // nest == 0 and see another increase, we have more than one // child. int nest=0; boolean single = true; boolean none = true; boolean found_child = false; for (int i=0; i 0) { // recursively create the parse tree ParseNode l_ParseNode = new ParseNode(lS_input); // create the viewable representation of the parse tree lSb_output = displayNode(l_ParseNode, ab_collapse, "", "", "", false); } return lSb_output.toString(); } private static StringBuffer displayNode( ParseNode a_ParseNode, boolean ab_collapse, String aS_indentBase, String aS_indentFirst, String aS_indentRest, boolean ab_collapsing) { // these are the indent strings final String INDENT = " "; final String INDENT_V = "| "; final String INDENT_V_DASH = "|- "; if (a_ParseNode.getNumChild() == 0) { // if the node has no children, display it return new StringBuffer(aS_indentBase + aS_indentFirst + a_ParseNode.getValue() + "\n"); } else if (ab_collapse && !ab_collapsing && a_ParseNode.getNumChild() == 1 && ParseUtil.removeNumerals(a_ParseNode.getValue()).equals(ParseUtil.removeNumerals(a_ParseNode.getChild(0).getValue()))) { // the node has only one child, and the value of the node is the same as the child (without numerals) // and this is the top level of this structure StringBuffer lSb_display; if (a_ParseNode.getChild(0).getNumChild() == 1 && ParseUtil.removeNumerals(a_ParseNode.getChild(0).getValue()).equals(ParseUtil.removeNumerals(a_ParseNode.getChild(0).getChild(0).getValue()))) { // if the same applies to the child include ellipses lSb_display = new StringBuffer(aS_indentBase + aS_indentFirst + a_ParseNode.getValue() + " ...\n"); } else { lSb_display = new StringBuffer(aS_indentBase + aS_indentFirst + a_ParseNode.getValue() + "\n"); } lSb_display.append(displayNode(a_ParseNode.getChild(0), ab_collapse, aS_indentBase + aS_indentRest, INDENT, INDENT, true)); return lSb_display; } else if (ab_collapse && ab_collapsing && a_ParseNode.getNumChild() == 1 && ParseUtil.removeNumerals(a_ParseNode.getValue()).equals(ParseUtil.removeNumerals(a_ParseNode.getChild(0).getValue()))) { // if the node has only one child, and the value of the node is the same as the child, display only the child return displayNode(a_ParseNode.getChild(0), ab_collapse, aS_indentBase, aS_indentFirst, aS_indentRest, true); } else if (a_ParseNode.getNumChild() == 1 && a_ParseNode.getChild(0).isTerminal()) { // if the node has only one child, and the child is a terminal, display both on the same line return new StringBuffer(aS_indentBase + aS_indentFirst + a_ParseNode.getValue() + ": " + a_ParseNode.getChild(0).getValue() + "\n"); } else { // otherwise display each of the children on a separate line StringBuffer lSb_display = new StringBuffer(aS_indentBase + aS_indentFirst + a_ParseNode.getValue() + "\n"); if (a_ParseNode.getNumChild() == 1) { // only one child lSb_display.append(displayNode(a_ParseNode.getChild(0), ab_collapse, aS_indentBase + aS_indentRest, INDENT, INDENT, false)); } else { for (int i = 0; i < a_ParseNode.getNumChild(); i++) { if (i == 0) { // first child lSb_display.append(displayNode(a_ParseNode.getChild(i), ab_collapse, aS_indentBase + aS_indentRest, INDENT_V_DASH, INDENT_V, false)); } else if (i == a_ParseNode.getNumChild() - 1) { // last child lSb_display.append(displayNode(a_ParseNode.getChild(i), ab_collapse, aS_indentBase + aS_indentRest, INDENT_V_DASH, INDENT, false)); } else { lSb_display.append(displayNode(a_ParseNode.getChild(i), ab_collapse, aS_indentBase + aS_indentRest, INDENT_V_DASH, INDENT_V, false)); } } } return lSb_display; } } static class ParseNode { String iS_value; boolean ib_terminal; int ii_numChild; ArrayList iLi_child; // constructor public ParseNode() { iS_value = ""; ib_terminal = true; ii_numChild = 0; iLi_child = new ArrayList(); } // constructor public ParseNode(String aS_text) { ii_numChild = 0; iLi_child = new ArrayList(); // find the end of the node String lS_text = aS_text.substring(0, ParseUtil.endOfNode(aS_text) + 1); if (ParseUtil.isTerminal(lS_text)) { // this is a terminal node iS_value = lS_text; ib_terminal = true; } else { // this node has sub-nodes iS_value = ParseUtil.getToken(ParseUtil.getWord(lS_text)); ib_terminal = false; // add the sub-nodes lS_text = ParseUtil.getTail(lS_text); while (!lS_text.equals(")")) { addChild(lS_text.substring(0, ParseUtil.endOfNode(lS_text) + 1)); lS_text = lS_text.substring(ParseUtil.endOfNode(lS_text) + 1).trim(); } } } // access methods public String getValue() { return iS_value; } public boolean isTerminal() { return ib_terminal; } public int getNumChild() { return ii_numChild; } public ParseNode addChild(String aS_value) { iLi_child.add(ii_numChild, new ParseNode(aS_value)); return (ParseNode) iLi_child.get(ii_numChild++); } public ParseNode getChild(int ai_index) { return (ParseNode) iLi_child.get(ai_index); } } // end of class ParseNode static class ParseUtil { // return the first word of a string public static String getWord(String aS_text) { String lS_text = aS_text.trim(); int li_spaceLoc = lS_text.indexOf(" "); if (li_spaceLoc == -1) { return lS_text; } else { return lS_text.substring(0, lS_text.indexOf(" ")); } } // return the string, excluding the first word public static String getTail(String aS_text) { String lS_text = aS_text.trim(); int li_spaceLoc = lS_text.indexOf(" "); if (li_spaceLoc == -1) { return ""; } else { return lS_text.substring(li_spaceLoc + 1); } } // return true if the node is a terminal node public static boolean isTerminal(String aS_text) { if (getWord(aS_text).endsWith("=(")) { return false; } else { return true; } } // return the string, excluding the trailing "=(" public static String getToken(String aS_text) { Pattern l_Pattern = Pattern.compile(" *([a-zA-Z]*[0-9]*)(\\=\\()? *"); Matcher l_Matcher = l_Pattern.matcher(aS_text); if (l_Matcher.matches()) { return l_Matcher.group(1); } else { return ""; } } // return the string, excluding the trailing numerals if present public static String removeNumerals(String aS_text) { Pattern l_Pattern = Pattern.compile(" *([a-zA-Z]*)([0-9]*) *"); Matcher l_Matcher = l_Pattern.matcher(aS_text); if (l_Matcher.matches()) { return l_Matcher.group(1); } else { return ""; } } // find the index of the end of the node (the closing right paren) public static int endOfNode(String aS_text) { int li_endIndex = -1; if (isTerminal(aS_text)) { // this is a terminal node int li_spaceLoc = aS_text.indexOf(" "); if (li_spaceLoc == -1) { li_endIndex = aS_text.length() - 1; } else { li_endIndex = aS_text.indexOf(" ") - 1; } } else { int li_parenCount = 0; boolean lb_seenParen = false; for (int i = 0; i < aS_text.length(); i++) { if (aS_text.charAt(i) == '(') { li_parenCount++; lb_seenParen = true; } if (aS_text.charAt(i) == ')') { li_parenCount--; } if (lb_seenParen && li_parenCount == 0) { li_endIndex = i; break; } } if (li_endIndex == -1) { throw new IndexOutOfBoundsException(); } } return li_endIndex; } } // end of class ParseUtil } //option withLocation, constant, parser(xtc.lang.JavaPackratParser); //option location,debug; //top text, morphology;