1 package fri.patterns.interpreter.parsergenerator.syntax.builder; 2 3 import java.util.*; 4 import fri.patterns.interpreter.parsergenerator.Token; 5 import fri.patterns.interpreter.parsergenerator.util.SymbolToName; 6 7 38 39 class ArtificialRule 40 { 41 private List rules; 42 private String nonterminal; 43 44 51 public ArtificialRule(List sentencesInParenthesis, String catSym) { StringBuffer sb = new StringBuffer (); 53 parenthesisContentsToString(sentencesInParenthesis, sb, catSym); 54 nonterminal = ensureUnderscore(sb.toString()); 55 rules = createRule(nonterminal, sentencesInParenthesis); 56 } 57 58 private void parenthesisContentsToString(List sentences, StringBuffer sb, String catSym) { 59 for (int i = 0; i < sentences.size(); i++) { 60 Object o = sentences.get(i); 61 62 if (o instanceof List) { 63 parenthesisContentsToString((List)o, sb, ""); } 65 else { 66 if (o instanceof String ) 67 o = SymbolToName.makeIdentifier((String )o, ""); sb.append(o.toString()); } 70 71 if (i < sentences.size() - 1) 72 sb.append((catSym.length() > 0 ? "_" + catSym : "") + "_"); 73 } 74 } 75 76 77 82 public ArtificialRule(Object token, String quantifier) { nonterminal = token.toString(); 84 85 String listNonterm = null; 87 if (quantifier.equals("+") || quantifier.equals("*")) { 88 List sentences = new ArrayList(); 89 90 listNonterm = ensureUnderscore(nonterminal)+"_LIST"; 91 92 List sentence = new ArrayList(); sentence.add(listNonterm); sentence.add(token.toString()); sentences.add(sentence); 97 98 sentence = new ArrayList(); 99 sentence.add(token.toString()); sentences.add(sentence); 101 102 rules = createRule(listNonterm, sentences); 104 if (quantifier.equals("+")) 105 nonterminal = listNonterm; } 107 108 if (quantifier.equals("*") || quantifier.equals("?")) { 109 List sentences = new ArrayList(); 110 111 nonterminal = ensureUnderscore(nonterminal)+(quantifier.equals("*") ? "_OPTLIST" : "_OPT"); 112 113 String nonterm = quantifier.equals("*") ? listNonterm : token.toString(); 114 115 List sentence = new ArrayList(); sentence.add(nonterm); sentences.add(sentence); 118 119 sentences.add(new ArrayList()); 121 List mandatoryList = quantifier.equals("*") ? rules : null; 122 123 rules = createRule(nonterminal, sentences); 124 125 if (mandatoryList != null) 126 rules.addAll(mandatoryList); 127 } 128 129 if (token instanceof ArtificialRule) rules.addAll(((ArtificialRule)token).getRules()); 131 } 132 133 134 private String ensureUnderscore(String nonterminal) { 135 if (nonterminal.startsWith(Token.ARTIFICIAL_NONTERMINAL_START_CHARACTER) == false) 136 nonterminal = Token.ARTIFICIAL_NONTERMINAL_START_CHARACTER + nonterminal; 137 return nonterminal; 138 } 139 140 private List createRule(String nonterminal, List sentences) { 141 for (int i = 0; i < sentences.size(); i++) { 142 List deep = (List) sentences.get(i); 143 List flat = flattenLists(deep, new ArrayList()); 144 flat.add(0, nonterminal); 145 sentences.set(i, flat); 146 } 147 return sentences; 148 } 149 150 151 158 public static List flattenLists(List deep, List flat) { 159 for (int i = 0; i < deep.size(); i++) { 160 Object o = deep.get(i); 161 162 if (o instanceof List) 163 flattenLists((List)o, flat); 164 else 165 flat.add(o); 166 } 167 return flat; 168 } 169 170 171 172 176 public String toString() { 177 return nonterminal; 178 } 179 180 183 public List getRules() { 184 return rules; 185 } 186 187 188 194 public static void resolveArtificialRules(List rules, List resultSyntax) { 195 for (int j = 0; j < rules.size(); j++) { 196 List rule = (List) rules.get(j); 197 198 resultSyntax.add(rule); 200 for (int k = 1; k < rule.size(); k++) { Object symbol = rule.get(k); 202 203 if (symbol instanceof ArtificialRule) { rule.set(k, symbol.toString()); 206 ArtificialRule ar = (ArtificialRule)symbol; 207 resolveArtificialRules(ar.getRules(), resultSyntax); } 209 } 210 } 211 } 212 213 214 public static void main(String [] args) { 215 List ruleA = new ArrayList(); 216 List ruleB = new ArrayList(); 217 List parenthesisRule = new ArrayList(); 218 ruleA.add("a1"); ruleA.add("a2"); 219 ruleB.add("b1"); 220 parenthesisRule.add(ruleA); parenthesisRule.add(ruleB); 221 ArtificialRule afr = new ArtificialRule(parenthesisRule, "OR"); 222 System.err.println(afr.toString()+" ::= "+afr.getRules()); 223 224 ArtificialRule afr2 = new ArtificialRule(afr, "*"); 225 System.err.println(afr2.toString()+" ::= "+afr2.getRules()); 226 } 227 228 } | Popular Tags |