public static void printTree (ParseTree tree, PrintWriter outputStream, String indent) { // String xml_node_name = (tree.is_statement?"statement_node":"node"); String xml_node_name = "node"; int lineno; if ( tree.payload instanceof Token ) { Token t = (Token)tree.payload; lineno = t.getLine(); } else { lineno = 0; } outputStream.println (indent + "<" + xml_node_name + " name=\"" + forXML ("" + tree) + "\"" + " line_number=\"" + lineno + "\" " + ">"); for (int i = 0; i < tree.getChildCount (); i += 1) { printTree ((ParseTree )tree.getChild (i), outputStream, indent + " "); } outputStream.println (indent + "</"+xml_node_name+">"); }
public static void main (String[]args) throws Exception { ANTLRFileStream input = new ANTLRFileStream (args[0]); LuaLexer lexer = new LuaLexer (input); CommonTokenStream tokens = new CommonTokenStream (lexer); ParseTreeBuilder builder = new ParseTreeBuilder("chunk"); LuaParser parser = new LuaParser (tokens, builder); parser.chunk(); ParseTree tree = builder.getTree(); PrintWriter outputStream = new PrintWriter (new FileWriter (args[1], false)); outputStream.println ("<?xml version=\"1.0\" ?>"); printTree (tree, outputStream, ""); outputStream.close (); }
public ParseTree parse(String startRule, List visitedStates) throws RecognitionException { ParseTreeBuilder actions = new ParseTreeBuilder(grammar.name); try { parse(startRule, actions, visitedStates); } catch (RecognitionException re) { // Errors are tracked via the ANTLRDebugInterface // Exceptions are used just to blast out of the parse engine // The error will be in the parse tree. } return actions.getTree(); }
public static void main(String[] args) throws Exception { if ( args.length!=4 ) { System.err.println("java Interp file.g tokens-to-ignore start-rule input-file"); return; } String grammarFileName = args[0]; String ignoreTokens = args[1]; String startRule = args[2]; String inputFileName = args[3]; Grammar parser = new Grammar(null, grammarFileName, new BufferedReader(new FileReader(grammarFileName))); String lexerGrammarText = parser.getLexerGrammar(); Grammar lexer = new Grammar(); lexer.importTokenVocabulary(parser); lexer.setGrammarContent(lexerGrammarText); CharStream input = new ANTLRFileStream(inputFileName); Interpreter lexEngine = new Interpreter(lexer, input); CommonTokenStream tokens = new CommonTokenStream(lexEngine); StringTokenizer tk = new StringTokenizer(ignoreTokens, " "); while ( tk.hasMoreTokens() ) { String tokenName = tk.nextToken(); tokens.setTokenTypeChannel(lexer.getTokenType(tokenName), 99); } if ( parser.getRule(startRule)==null ) { System.err.println("Rule "+startRule+" does not exist in "+grammarFileName); return; } Interpreter parseEngine = new Interpreter(parser, tokens); ParseTree t = parseEngine.parse(startRule); System.out.println(t.toStringTree()); }
public void testSimpleParse() throws Exception { Grammar pg = new Grammar( "parser grammar p;\n"+ "prog : WHILE ID LCURLY (assign)* RCURLY EOF;\n" + "assign : ID ASSIGN expr SEMI ;\n" + "expr : INT | FLOAT | ID ;\n"); Grammar g = new Grammar(); g.importTokenVocabulary(pg); g.setFileName(Grammar.IGNORE_STRING_IN_GRAMMAR_FILE_NAME +"string"); g.setGrammarContent( "lexer grammar t;\n"+ "WHILE : 'while';\n"+ "LCURLY : '{';\n"+ "RCURLY : '}';\n"+ "ASSIGN : '=';\n"+ "SEMI : ';';\n"+ "ID : ('a'..'z')+ ;\n"+ "INT : (DIGIT)+ ;\n"+ "FLOAT : (DIGIT)+ '.' (DIGIT)* ;\n"+ "fragment DIGIT : '0'..'9';\n" + "WS : (' ')+ ;\n"); CharStream input = new ANTLRStringStream("while x { i=1; y=3.42; z=y; }"); Interpreter lexEngine = new Interpreter(g, input); CommonTokenStream tokens = new CommonTokenStream(lexEngine); tokens.setTokenTypeChannel(g.getTokenType("WS"), 99); //System.out.println("tokens="+tokens.toString()); Interpreter parseEngine = new Interpreter(pg, tokens); ParseTree t = parseEngine.parse("prog"); String result = t.toStringTree(); String expecting = "(<grammar p> (prog while x { (assign i = (expr 1) ;) (assign y = (expr 3.42) ;) (assign z = (expr y) ;) } <EOF>))"; assertEquals(expecting, result); }
public void testMismatchedTokenError() throws Exception { Grammar pg = new Grammar( "parser grammar p;\n"+ "prog : WHILE ID LCURLY (assign)* RCURLY;\n" + "assign : ID ASSIGN expr SEMI ;\n" + "expr : INT | FLOAT | ID ;\n"); Grammar g = new Grammar(); g.setFileName(Grammar.IGNORE_STRING_IN_GRAMMAR_FILE_NAME +"string"); g.importTokenVocabulary(pg); g.setGrammarContent( "lexer grammar t;\n"+ "WHILE : 'while';\n"+ "LCURLY : '{';\n"+ "RCURLY : '}';\n"+ "ASSIGN : '=';\n"+ "SEMI : ';';\n"+ "ID : ('a'..'z')+ ;\n"+ "INT : (DIGIT)+ ;\n"+ "FLOAT : (DIGIT)+ '.' (DIGIT)* ;\n"+ "fragment DIGIT : '0'..'9';\n" + "WS : (' ')+ ;\n"); CharStream input = new ANTLRStringStream("while x { i=1 y=3.42; z=y; }"); Interpreter lexEngine = new Interpreter(g, input); CommonTokenStream tokens = new CommonTokenStream(lexEngine); tokens.setTokenTypeChannel(g.getTokenType("WS"), 99); //System.out.println("tokens="+tokens.toString()); Interpreter parseEngine = new Interpreter(pg, tokens); ParseTree t = parseEngine.parse("prog"); String result = t.toStringTree(); String expecting = "(<grammar p> (prog while x { (assign i = (expr 1) MismatchedTokenException(5!=9))))"; assertEquals(expecting, result); }
public void testMismatchedSetError() throws Exception { Grammar pg = new Grammar( "parser grammar p;\n"+ "prog : WHILE ID LCURLY (assign)* RCURLY;\n" + "assign : ID ASSIGN expr SEMI ;\n" + "expr : INT | FLOAT | ID ;\n"); Grammar g = new Grammar(); g.importTokenVocabulary(pg); g.setFileName("<string>"); g.setGrammarContent( "lexer grammar t;\n"+ "WHILE : 'while';\n"+ "LCURLY : '{';\n"+ "RCURLY : '}';\n"+ "ASSIGN : '=';\n"+ "SEMI : ';';\n"+ "ID : ('a'..'z')+ ;\n"+ "INT : (DIGIT)+ ;\n"+ "FLOAT : (DIGIT)+ '.' (DIGIT)* ;\n"+ "fragment DIGIT : '0'..'9';\n" + "WS : (' ')+ ;\n"); CharStream input = new ANTLRStringStream("while x { i=; y=3.42; z=y; }"); Interpreter lexEngine = new Interpreter(g, input); CommonTokenStream tokens = new CommonTokenStream(lexEngine); tokens.setTokenTypeChannel(g.getTokenType("WS"), 99); //System.out.println("tokens="+tokens.toString()); Interpreter parseEngine = new Interpreter(pg, tokens); ParseTree t = parseEngine.parse("prog"); String result = t.toStringTree(); String expecting = "(<grammar p> (prog while x { (assign i = (expr MismatchedSetException(9!={5,10,11})))))"; assertEquals(expecting, result); }
public void testNoViableAltError() throws Exception { Grammar pg = new Grammar( "parser grammar p;\n"+ "prog : WHILE ID LCURLY (assign)* RCURLY;\n" + "assign : ID ASSIGN expr SEMI ;\n" + "expr : {;}INT | FLOAT | ID ;\n"); Grammar g = new Grammar(); g.importTokenVocabulary(pg); g.setFileName("<string>"); g.setGrammarContent( "lexer grammar t;\n"+ "WHILE : 'while';\n"+ "LCURLY : '{';\n"+ "RCURLY : '}';\n"+ "ASSIGN : '=';\n"+ "SEMI : ';';\n"+ "ID : ('a'..'z')+ ;\n"+ "INT : (DIGIT)+ ;\n"+ "FLOAT : (DIGIT)+ '.' (DIGIT)* ;\n"+ "fragment DIGIT : '0'..'9';\n" + "WS : (' ')+ ;\n"); CharStream input = new ANTLRStringStream("while x { i=; y=3.42; z=y; }"); Interpreter lexEngine = new Interpreter(g, input); CommonTokenStream tokens = new CommonTokenStream(lexEngine); tokens.setTokenTypeChannel(g.getTokenType("WS"), 99); //System.out.println("tokens="+tokens.toString()); Interpreter parseEngine = new Interpreter(pg, tokens); ParseTree t = parseEngine.parse("prog"); String result = t.toStringTree(); String expecting = "(<grammar p> (prog while x { (assign i = (expr NoViableAltException(9!=[4:1: expr : ( INT | FLOAT | ID );])))))"; assertEquals(expecting, result); }
public void enterRule(String filename, String ruleName) { if ( backtracking>0 ) return; ParseTree parentRuleNode = (ParseTree)callStack.peek(); ParseTree ruleNode = create(ruleName); parentRuleNode.addChild(ruleNode); callStack.push(ruleNode); }
public void exitRule(String filename, String ruleName) { if ( backtracking>0 ) return; ParseTree ruleNode = (ParseTree)callStack.peek(); if ( ruleNode.getChildCount()==0 ) { ruleNode.addChild(epsilonNode()); } callStack.pop(); }
public void consumeToken(Token token) { if ( backtracking>0 ) return; ParseTree ruleNode = (ParseTree)callStack.peek(); ParseTree elementNode = create(token); elementNode.hiddenTokens = this.hiddenTokens; this.hiddenTokens = new ArrayList(); ruleNode.addChild(elementNode); }
public boolean validate(String string, String startingRule) throws RecognitionException { CharStream input = new ANTLRStringStream(string); Interpreter lexEngine = new Interpreter(g, input); ParseTree tree; CommonTokenStream tokens = new CommonTokenStream(lexEngine); Interpreter parseEngine = new Interpreter(pg, tokens); tree = parseEngine.parse(startingRule); System.out.println(tree.toStringTree()); return !tree.toStringTree().contains("Exception"); }
public ParseTree parse(String startRule) throws RecognitionException { return parse(startRule, null); }
public ParseTreeBuilder(String grammarName) { ParseTree root = create("<grammar "+grammarName+">"); callStack.push(root); }
public ParseTree getTree() { return (ParseTree)callStack.elementAt(0); }
/** What kind of node to create. You might want to override * so I factored out creation here. */ public ParseTree create(Object payload) { return new ParseTree(payload); }
public void enterRule(String ruleName) { ParseTree parentRuleNode = (ParseTree)callStack.peek(); ParseTree ruleNode = create(ruleName); parentRuleNode.addChild(ruleNode); callStack.push(ruleNode); }
public void consumeToken(Token token) { ParseTree ruleNode = (ParseTree)callStack.peek(); ParseTree elementNode = create(token); ruleNode.addChild(elementNode); }
public void recognitionException(RecognitionException e) { ParseTree ruleNode = (ParseTree)callStack.peek(); ParseTree errorNode = create(e); ruleNode.addChild(errorNode); }
public ParseTree epsilonNode() { return create(EPSILON_PAYLOAD); }
public void recognitionException(RecognitionException e) { if ( backtracking>0 ) return; ParseTree ruleNode = (ParseTree)callStack.peek(); ParseTree errorNode = create(e); ruleNode.addChild(errorNode); }