1 package fri.patterns.interpreter.parsergenerator.syntax.builder; 2 3 import java.util.*; 4 import java.io.IOException ; 5 import fri.patterns.interpreter.parsergenerator.*; 6 import fri.patterns.interpreter.parsergenerator.lexer.*; 7 import fri.patterns.interpreter.parsergenerator.syntax.*; 8 import fri.patterns.interpreter.parsergenerator.parsertables.LALRParserTables; 9 import fri.patterns.interpreter.parsergenerator.parsertables.ParserBuildException; 10 11 36 37 public class SyntaxBuilder 38 { 39 private Syntax syntax; 40 private Syntax lexerSyntax, parserSyntax; 41 private Lexer lexer; 42 private List tokenSymbols, ignoredSymbols; 43 private List initialNonterminals; 44 45 51 public SyntaxBuilder(Object syntaxInput) 52 throws SyntaxException, LexerException, ParserBuildException, IOException 53 { 54 56 SyntaxSeparation.DEBUG = false; SyntaxSeparation separation = new SyntaxSeparation(new Syntax(StandardLexerRules.lexerSyntax)); 58 SyntaxSeparation.DEBUG = true; 59 60 LexerBuilder builder = new LexerBuilder(separation.getLexerSyntax(), separation.getIgnoredSymbols()); 61 Lexer lexer = builder.getLexer(); 62 lexer.setInput(syntaxInput); 63 64 66 ParserTables parserTables = new SyntaxBuilderParserTables(); 69 70 Parser parser = new Parser(parserTables); 72 initialNonterminals = new ArrayList(64); 73 boolean ok = parser.parse(lexer, new SyntaxBuilderSemantic(initialNonterminals)); 74 if (ok == false) 75 throw new SyntaxException("Failed building Syntax from "+syntaxInput); 76 77 List result = (List) parser.getResult(); List rules = new ArrayList(); ArtificialRule.resolveArtificialRules(result, rules); 80 this.syntax = new Syntax(rules); 81 } 83 84 private void ensureSeparation() 85 throws SyntaxException 86 { 87 if (tokenSymbols == null) { 88 SyntaxSeparation separation = new SyntaxSeparation(syntax); 89 this.tokenSymbols = separation.getTokenSymbols(); 90 this.ignoredSymbols = separation.getIgnoredSymbols(); 91 this.parserSyntax = separation.getParserSyntax(); 92 this.lexerSyntax = separation.getLexerSyntax(); 93 } 94 } 95 96 97 98 public Lexer getLexer() 99 throws LexerException, SyntaxException 100 { 101 if (lexer == null) { 102 ensureSeparation(); 103 LexerBuilder builder = new LexerBuilder(lexerSyntax, ignoredSymbols); 104 this.lexer = builder.getLexer(); 105 } 106 return this.lexer; 107 } 108 109 110 public Syntax getParserSyntax() 111 throws SyntaxException 112 { 113 ensureSeparation(); 114 return this.parserSyntax; 115 } 116 117 125 126 public Syntax getSyntax() { 127 return syntax; 128 } 129 130 134 public List getInitialNonterminals() { 135 return initialNonterminals; 136 } 137 138 142 public Syntax resolveSingulars() { 143 getSyntax().resolveSingulars(); 144 return getSyntax(); 145 } 146 147 148 149 150 public static void main(String [] args) { 151 try { 152 new LALRParserTables(new Syntax(SyntaxBuilderSemantic.syntax)).toSourceFile( 153 "fri.patterns.interpreter.parsergenerator.syntax.builder.SyntaxBuilderParserTables"); 154 } 155 catch (Exception e) { 156 e.printStackTrace(); 157 } 158 } 159 160 } 161 | Popular Tags |