Changeset 10
- Timestamp:
- 07/07/08 18:14:06 (6 months ago)
- Location:
- trunk/src/xtc/lang/stats
- Files:
-
- 3 added
- 7 modified
-
Makefile (modified) (1 diff)
-
Stats.rats (modified) (1 diff)
-
StatsCore.rats (modified) (1 diff)
-
StatsDriver.java (modified) (2 diffs)
-
StatsParser.java (modified) (19 diffs)
-
StatsReader.java (modified) (19 diffs)
-
StatsReserved.rats (added)
-
StatsSpacing.rats (modified) (1 diff)
-
StatsSymbol.rats (added)
-
test_rewrite.cpp (added)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/xtc/lang/stats/Makefile
r9 r10 69 69 $(JAVAC) $(JAVAC_OPTIONS) StatsParser.java 70 70 71 $(RATS) -option 'parser(xtc.lang.stats.StatsReader)' -option withParseTree -option verbose Stats.rats72 $(JAVAC) $(JAVAC_OPTIONS) StatsReader.java73 71 74 72 factories : -
trunk/src/xtc/lang/stats/Stats.rats
r9 r10 18 18 */ 19 19 20 /**21 * A complete C grammar.22 *23 * @author Robert Grimm24 * @version $Revision: 1.20 $25 */26 20 module xtc.lang.stats.Stats; 27 21 28 22 instantiate xtc.lang.CConstant(xtc.lang.stats.StatsSpacing); 29 instantiate xtc.lang.CIdentifier(xtc.lang. CReserved, xtc.lang.stats.StatsSpacing);23 instantiate xtc.lang.CIdentifier(xtc.lang.stats.StatsReserved, xtc.lang.stats.StatsSpacing); 30 24 instantiate xtc.util.Symbol(xtc.lang.stats.StatsSpacing); 31 25 instantiate xtc.lang.CSymbol(xtc.util.Symbol); 26 instantiate xtc.lang.stats.StatsSymbol(xtc.lang.CSymbol); 32 27 instantiate xtc.lang.stats.StatsSpacing(xtc.lang.CState); 33 28 instantiate xtc.lang.CContext(xtc.lang.CState); 34 29 35 30 modify xtc.lang.stats.StatsCore(xtc.lang.CContext, xtc.lang.CConstant, 36 xtc.lang.CIdentifier, xtc.lang. CSymbol,31 xtc.lang.CIdentifier, xtc.lang.stats.StatsSymbol, 37 32 xtc.lang.stats.StatsSpacing); 38 33 -
trunk/src/xtc/lang/stats/StatsCore.rats
r9 r10 31 31 import Identifier; 32 32 import Symbol; 33 import xtc.lang.stats.StatsSpacing; 33 import Spacing; 34 34 35 35 36 36 37 modify xtc.lang.CCore(Context, Constant, Identifier, Symbol, Spacing); 37 38 38 39 40 Node ExternalDeclaration += 41 <PragmaExt> PragmaStatement 42 / <Declaration> ... 43 ; 44 39 45 Node Statement += 40 <Conditional> ...41 / < Pragma> PragmaStatement46 <PragmaIn> PragmaStatement 47 / <Expression> ... 42 48 ; 43 44 45 inline Node PragmaStatement =46 <PIfElse> IfElsePStatement47 / <PIf> IfPStatement48 / <PIfDefElse> IfDefElsePStatement49 / <PIfDef> IfDefPStatement50 ;51 52 49 53 inline generic IfDefElsePStatement = 54 void:"#":Symbol Space* void:"pragma ifdef ":Keyword Expression &LineTerminator 55 Statement 56 void:"#":Symbol Space* void:"pragma else ":Keyword Space* &LineTerminator 57 Statement 58 void:"#":Symbol Space* void:"pragma endif ":Keyword Space* &LineTerminator 59 / void:"#":Symbol Space* void:"pragma ifdef ":Keyword Expression &LineTerminator 60 PragmaElif+ 61 void:"#":Symbol Space* void:"pragma endif ":Keyword Space* &LineTerminator 50 generic PragmaStatement = 51 <PIfDef> PragmaIfDef 52 ; 53 54 55 generic PragmaIfDef = 56 void:"#pragma":Symbol void:"ifdef":Keyword Expression LineTerminator* 57 Statement+ 58 PragmaAlternation* 59 EndPragmaIf 60 ; 61 62 void EndPragmaIf = 63 void:"#pragma":Symbol Space* void:"endif":Keyword LineTerminator* 64 ; 65 66 67 generic PragmaAlternation = 68 <PElse> PragmaElse 69 / <PElif> PragmaElIf 70 ; 71 72 73 generic PragmaElse = 74 void:"#pragma":Symbol void:"else":Keyword LineTerminator* 75 Statement+ 62 76 ; 63 77 64 inline generic IfElsePStatement = 65 void:"#":Symbol Space* void:"pragma if ":Keyword Expression &LineTerminator 66 Statement 67 void:"#":Symbol Space* void:"pragma else ":Keyword Space* &LineTerminator 68 Statement 69 void:"#":Symbol Space* void:"pragma endif ":Keyword Space* &LineTerminator 70 / void:"#":Symbol Space* void:"pragma if ":Keyword Expression &LineTerminator 71 PragmaElif+ 72 void:"#":Symbol Space* void:"pragma endif ":Keyword Space* &LineTerminator 73 ; 74 75 inline generic PragmaElif = 76 void:"#":Symbol Space* void:"pragma elif ":Keyword Expression &LineTerminator 77 Statement 78 ; 79 80 inline generic IfDefPStatement = 81 void:"#":Symbol Space* void:"pragma ifdef ":Keyword Expression &LineTerminator 82 Statement 83 void:"#":Symbol Space* void:"pragma endif ":Keyword Space* &LineTerminator 84 ; 85 86 87 inline generic IfPStatement = 88 void:"#":Symbol Space* void:"pragma if ":Keyword Expression &LineTerminator 89 Statement 90 void:"#":Symbol Space* void:"pragma endif ":Keyword Space* &LineTerminator 91 ; 92 78 generic PragmaElIf = 79 void:"#pragma":Symbol void:"elif":Keyword Expression LineTerminator* 80 Statement+ 81 ; -
trunk/src/xtc/lang/stats/StatsDriver.java
r9 r10 19 19 package xtc.lang.stats; 20 20 21 import java.io.File; 22 import java.io.IOException; 23 import java.io.Reader; 24 21 25 import xtc.lang.C; 26 import xtc.lang.CParser; 22 27 import xtc.lang.CPragmaAnalyzer; 28 import xtc.lang.CReader; 23 29 import xtc.lang.PragmaSymbolTable; 30 import xtc.lang.c4.C4; 31 import xtc.parser.ParseException; 32 import xtc.parser.Result; 33 import xtc.tree.Formatting; 34 import xtc.tree.GNode; 35 import xtc.tree.Location; 24 36 import xtc.tree.Node; 25 37 import xtc.tree.Visitor; … … 27 39 28 40 public class StatsDriver extends C { 29 30 public String getName() { 31 return "Stats Analysis"; 41 42 public String getName() { 43 return "Stats Analysis"; 44 } 45 46 public void init() { 47 super.init(); 48 runtime.bool("pragmaSymbol", "pragmaSymbolAnalysis", false, "Build a symbol table based also on pragma directives"); 49 } 50 51 public void process(Node node) { 52 53 // For pragma color analysis 54 // So far only print an enhanced version of the Symbol Table 55 if (runtime.test("pragmaSymbolAnalysis")) { 56 System.out.println("PRAGMA SYMBOL"); 57 PragmaSymbolTable pragmaTable = new PragmaSymbolTable(); 58 new CPragmaAnalyzer(runtime).analyze(node, pragmaTable); 59 // Save the registered visitor. 60 Visitor visitor = runtime.console().visitor(); 61 // Note that the type printer's constructor registers the just 62 // created printer with the console. 63 new TypePrinter(runtime.console()); 64 try { 65 if( pragmaTable != null ); 66 //pragmaTable.root().dump(runtime.console()); 67 } finally { 68 // Restore the previously registered visitor. 69 runtime.console().register(visitor); 70 } 71 runtime.console().flush(); 72 } 73 } 74 75 /** 76 * Print memoization information. 77 * 78 * @param parser 79 * The parser. 80 * @param file 81 * The file. 82 */ 83 private void printMemoInfo(Object parser, File file) { 84 if (runtime.test("printMemoProfile")) { 85 try { 86 profile.invoke(parser, new Object[] { runtime.console() }); 87 } catch (Exception x) { 88 runtime.error(file + ": " + x.getMessage()); 89 } 90 runtime.console().pln().flush(); 91 } 92 93 if (runtime.test("printMemoTable")) { 94 try { 95 dump.invoke(parser, new Object[] { runtime.console() }); 96 } catch (Exception x) { 97 runtime.error(file + ": " + x.getMessage()); 98 } 99 runtime.console().flush(); 100 } 101 } 102 public Node parse(Reader in, File file) throws IOException, ParseException { 103 if (runtime.test("optionParseTree")) { // ======================== Reader 104 if (runtime.test("optionNoIncr")) { 105 StatsReader parser = new StatsReader(in, file.toString(), (int)file.length()); 106 Result result = parser.pTranslationUnit(0); 107 printMemoInfo(parser, file); 108 return (Node)parser.value(result); 109 } else { 110 StatsReader parser = new StatsReader(in, file.getName()); 111 GNode unit = GNode.create("TranslationUnit"); 112 unit.setLocation(new Location(file.toString(), 1, 0)); 113 Node root = unit; 114 boolean first = true; 115 116 while (! parser.isEOF(0)) { 117 Result result = 118 first ? parser.pPrelude(0) : parser.pExternalDeclaration(0); 119 printMemoInfo(parser, file); 120 if (! result.hasValue()) parser.signal(result.parseError()); 121 122 if (first) { 123 root = Formatting.before1(result.semanticValue(), unit); 124 first = false; 125 } else { 126 unit.add(result.semanticValue()); 127 } 128 parser.resetTo(result.index); 129 } 130 131 // Grab any trailing annotations. 132 Result result = parser.pAnnotations(0); 133 if (! result.hasValue()) parser.signal(result.parseError()); 134 unit.add(result.semanticValue()); 135 136 return root; 137 } 138 139 } else if (runtime.test("optionNoIncr")) { // ==================== Parser 140 StatsParser parser = new StatsParser(in, file.toString(), (int)file.length()); 141 Result result = parser.pTranslationUnit(0); 142 printMemoInfo(parser, file); 143 return (Node)parser.value(result); 144 145 } else { 146 StatsParser parser = new StatsParser(in, file.getName()); 147 GNode root = GNode.create("TranslationUnit"); 148 boolean first = true; 149 150 while (! parser.isEOF(0)) { 151 Result result = 152 first ? parser.pPrelude(0) : parser.pExternalDeclaration(0); 153 printMemoInfo(parser, file); 154 if (! result.hasValue()) parser.signal(result.parseError()); 155 156 if (first) { 157 first = false; 158 } else { 159 root.add(result.semanticValue()); 160 } 161 parser.resetTo(result.index); 162 } 163 164 // Grab any trailing annotations. 165 Result result = parser.pAnnotations(0); 166 if (! result.hasValue()) parser.signal(result.parseError()); 167 root.add(result.semanticValue()); 168 169 return root; 170 } 171 } 172 173 174 /** 175 * Run the tool with the specified command line arguments. 176 * 177 * @param args 178 * The command line arguments. 179 */ 180 public static void main(String[] args) { 181 new StatsDriver().run(args); 32 182 } 33 34 public void init() { 35 super.init(); 36 runtime.bool("pragmaSymbol", "pragmaSymbolAnalysis", false, "Build a symbol table based also on pragma directives"); 37 } 38 39 public void process(Node node) { 40 super.process(node); 41 // For pragma color analysis 42 // So far only print an enhanced version of the Symbol Table 43 if (runtime.test("pragmaSymbol")) { 44 System.out.println("PRAGMA SYMBOL"); 45 PragmaSymbolTable pragmaTable = new PragmaSymbolTable(); 46 new CPragmaAnalyzer(runtime).analyze(node, pragmaTable); 47 // Save the registered visitor. 48 Visitor visitor = runtime.console().visitor(); 49 // Note that the type printer's constructor registers the just 50 // created printer with the console. 51 new TypePrinter(runtime.console()); 52 try { 53 if( pragmaTable != null ); 54 //pragmaTable.root().dump(runtime.console()); 55 } finally { 56 // Restore the previously registered visitor. 57 runtime.console().register(visitor); 58 } 59 runtime.console().flush(); 60 } 61 } 183 62 184 } -
trunk/src/xtc/lang/stats/StatsParser.java
r9 r10 3 3 // Rats! Parser Generator, version 1.13.3, 4 4 // (C) 2004-2008 Robert Grimm, 5 // on Thursday, July 3, 2008 at 11:32:18PM.5 // on Monday, July 7, 2008 at 9:56:36 PM. 6 6 // Edit at your own risk. 7 7 // =========================================================================== … … 41 41 public static final Set<String> C_KEYWORDS = new HashSet<String>(); 42 42 43 /** The STATS flag. */ 44 public static final boolean STATS = true; 45 46 /** The RELOCATE flag. */ 47 public static final boolean RELOCATE = true; 48 43 49 /** The C99 flag. */ 44 50 public static final boolean C99 = true; 45 46 /** The RELOCATE flag. */47 public static final boolean RELOCATE = true;48 51 49 52 // ========================================================================= … … 87 90 Result fAssemblyOperand; 88 91 Result fAsmKeyword; 92 Result fPragmaStatement; 89 93 Result fExponent; 90 Result f$$Shared2;91 94 } 92 95 93 96 /** Chunk 4 of memoized results. */ 94 97 static final class Chunk4 { 98 Result f$$Shared2; 95 99 Result fBinaryExponent; 96 100 Result fCharacterLiteral; … … 102 106 Result fIdentifier; 103 107 Result fKeyword; 104 Result fWord;105 108 } 106 109 107 110 /** Chunk 5 of memoized results. */ 108 111 static final class Chunk5 { 112 Result fWord; 109 113 Result fSymbol; 110 114 } … … 273 277 // Start a state modification. 274 278 yyState.start(); 279 280 // Alternative <PragmaExt>. 281 282 yyResult = pPragmaStatement(yyStart); 283 yyError = yyResult.select(yyError); 284 if (yyResult.hasValue()) { 285 yyValue = yyResult.semanticValue(); 286 287 // Commit the state modification. 288 yyState.commit(); 289 290 return yyResult.createValue(yyValue, yyError); 291 } 275 292 276 293 // Alternative <Declaration>. … … 5208 5225 private Result pStatement$1(final int yyStart) throws IOException { 5209 5226 Result yyResult; 5210 Result yyPredResult;5211 5227 int yyBase; 5212 int yyRepetition1;5213 boolean yyRepeated1;5214 Pair<Node> yyRepValue1;5215 5228 int yyOption1; 5216 5229 Node yyOpValue1; … … 5218 5231 ParseError yyError = ParseError.DUMMY; 5219 5232 5220 // Alternative 1. 5233 // Alternative <PragmaIn>. 5234 5235 yyResult = pPragmaStatement(yyStart); 5236 yyError = yyResult.select(yyError); 5237 if (yyResult.hasValue()) { 5238 yyValue = yyResult.semanticValue(); 5239 5240 return yyResult.createValue(yyValue, yyError); 5241 } 5242 5243 // Alternative 2. 5221 5244 5222 5245 yyResult = pCommaExpression(yyStart); … … 5249 5272 } 5250 5273 5251 // Alternative 3.5274 // Alternative 4. 5252 5275 5253 5276 yyResult = pKeyword(yyStart); … … 5311 5334 } else { 5312 5335 yyError = yyError.select("\"(\" expected", yyBase); 5313 }5314 }5315 5316 // Alternative 4.5317 5318 yyResult = pSymbol(yyStart);5319 yyError = yyResult.select(yyError);5320 if (yyResult.hasValue("#")) {5321 5322 yyRepetition1 = yyResult.index;5323 while (true) {5324 5325 yyResult = pSpace(yyRepetition1);5326 yyError = yyResult.select(yyError);5327 if (yyResult.hasValue()) {5328 5329 yyRepetition1 = yyResult.index;5330 continue;5331 }5332 break;5333 }5334 5335 final int yyChoice1 = yyRepetition1;5336 5337 // Nested alternative 1.5338 5339 yyBase = yyChoice1;5340 yyResult = pKeyword(yyBase);5341 yyError = yyResult.select(yyError);5342 if (yyResult.hasValue("pragma if ")) {5343 5344 yyResult = pCommaExpression(yyResult.index);5345 yyError = yyResult.select(yyError);5346 if (yyResult.hasValue()) {5347 final Node v$g$1 = yyResult.semanticValue();5348 5349 yyPredResult = pLineTerminator(yyResult.index);5350 yyError = yyPredResult.select(yyError);5351 if (yyPredResult.hasValue()) {5352 5353 final int yyChoice2 = yyResult.index;5354 5355 // Nested alternative 1.5356 5357 yyResult = pStatement(yyChoice2);5358 yyError = yyResult.select(yyError);5359 if (yyResult.hasValue()) {5360 final Node v$g$2 = yyResult.semanticValue();5361 5362 yyBase = yyResult.index;5363 yyResult = pSymbol(yyBase);5364 yyError = yyResult.select(yyError);5365 if (yyResult.hasValue("#")) {5366 5367 yyRepetition1 = yyResult.index;5368 while (true) {5369 5370 yyResult = pSpace(yyRepetition1);5371 yyError = yyResult.select(yyError);5372 if (yyResult.hasValue()) {5373 5374 yyRepetition1 = yyResult.index;5375 continue;5376 }5377 break;5378 }5379 5380 yyBase = yyRepetition1;5381 yyResult = pKeyword(yyBase);5382 yyError = yyResult.select(yyError);5383 if (yyResult.hasValue("pragma else ")) {5384 5385 yyRepetition1 = yyResult.index;5386 while (true) {5387 5388 yyResult = pSpace(yyRepetition1);5389 yyError = yyResult.select(yyError);5390 if (yyResult.hasValue()) {5391 5392 yyRepetition1 = yyResult.index;5393 continue;5394 }5395 break;5396 }5397 5398 yyPredResult = pLineTerminator(yyRepetition1);5399 yyError = yyPredResult.select(yyError);5400 if (yyPredResult.hasValue()) {5401 5402 yyResult = pStatement(yyRepetition1);5403 yyError = yyResult.select(yyError);5404 if (yyResult.hasValue()) {5405 final Node v$g$3 = yyResult.semanticValue();5406 5407 yyBase = yyResult.index;5408 yyResult = pSymbol(yyBase);5409 yyError = yyResult.select(yyError);5410 if (yyResult.hasValue("#")) {5411 5412 yyRepetition1 = yyResult.index;5413 while (true) {5414 5415 yyResult = pSpace(yyRepetition1);5416 yyError = yyResult.select(yyError);5417 if (yyResult.hasValue()) {5418 5419 yyRepetition1 = yyResult.index;5420 continue;5421 }5422 break;5423 }5424 5425 yyBase = yyRepetition1;5426 yyResult = pKeyword(yyBase);5427 yyError = yyResult.select(yyError);5428 if (yyResult.hasValue("pragma endif ")) {5429 5430 yyRepetition1 = yyResult.index;5431 while (true) {5432 5433 yyResult = pSpace(yyRepetition1);5434 yyError = yyResult.select(yyError);5435 if (yyResult.hasValue()) {5436 5437 yyRepetition1 = yyResult.index;5438 continue;5439 }5440 break;5441 }5442 5443 yyPredResult = pLineTerminator(yyRepetition1);5444 yyError = yyPredResult.select(yyError);5445 if (yyPredResult.hasValue()) {5446 5447 yyValue = GNode.create("IfElsePStatement", v$g$1, v$g$2, v$g$3);5448 yyValue.setLocation(location(yyStart));5449 5450 return new SemanticValue(yyValue, yyRepetition1, yyError);5451 }5452 } else {5453 yyError = yyError.select("\"pragma endif \" expected", yyBase);5454 }5455 } else {5456 yyError = yyError.select("\"#\" expected", yyBase);5457 }5458 }5459 }5460 } else {5461 yyError = yyError.select("\"pragma else \" expected", yyBase);5462 }5463 } else {5464 yyError = yyError.select("\"#\" expected", yyBase);5465 }5466 }5467 5468 // Nested alternative 2.5469 5470 yyRepetition1 = yyChoice2;5471 yyRepeated1 = false;5472 yyRepValue1 = Pair.empty();5473 while (true) {5474 5475 yyResult = pPragmaElif(yyRepetition1);5476 yyError = yyResult.select(yyError);5477 if (yyResult.hasValue()) {5478 final Node v$el$1 = yyResult.semanticValue();5479 5480 yyRepetition1 = yyResult.index;5481 yyRepeated1 = true;5482 yyRepValue1 = new Pair<Node>(v$el$1, yyRepValue1);5483 continue;5484 }5485 break;5486 }5487 5488 if (yyRepeated1) {5489 final Pair<Node> v$g$4 = yyRepValue1.reverse();5490 5491 yyBase = yyRepetition1;5492 yyResult = pSymbol(yyBase);5493 yyError = yyResult.select(yyError);5494 if (yyResult.hasValue("#")) {5495 5496 yyRepetition1 = yyResult.index;5497 while (true) {5498 5499 yyResult = pSpace(yyRepetition1);5500 yyError = yyResult.select(yyError);5501 if (yyResult.hasValue()) {5502 5503 yyRepetition1 = yyResult.index;5504 continue;5505 }5506 break;5507 }5508 5509 yyBase = yyRepetition1;5510 yyResult = pKeyword(yyBase);5511 yyError = yyResult.select(yyError);5512 if (yyResult.hasValue("pragma endif ")) {5513 5514 yyRepetition1 = yyResult.index;5515 while (true) {5516 5517 yyResult = pSpace(yyRepetition1);5518 yyError = yyResult.select(yyError);5519 if (yyResult.hasValue()) {5520 5521 yyRepetition1 = yyResult.index;5522 continue;5523 &nb
