org.antlr.runtime.tree.ParseTree Java Examples
The following examples show how to use
org.antlr.runtime.tree.ParseTree.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Interpreter.java From jFuzzyLogic with GNU Lesser General Public License v3.0 | 5 votes |
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(); }
Example #2
Source File: TestInterpretedParsing.java From jFuzzyLogic with GNU Lesser General Public License v3.0 | 5 votes |
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); }
Example #3
Source File: TestInterpretedParsing.java From jFuzzyLogic with GNU Lesser General Public License v3.0 | 5 votes |
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); }
Example #4
Source File: TestInterpretedParsing.java From jFuzzyLogic with GNU Lesser General Public License v3.0 | 5 votes |
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); }
Example #5
Source File: TestInterpretedParsing.java From jFuzzyLogic with GNU Lesser General Public License v3.0 | 5 votes |
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); }
Example #6
Source File: StringValidator.java From chipster with MIT License | 5 votes |
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"); }
Example #7
Source File: Interpreter.java From jFuzzyLogic with GNU Lesser General Public License v3.0 | 4 votes |
public ParseTree parse(String startRule) throws RecognitionException { return parse(startRule, null); }
Example #8
Source File: Interp.java From jFuzzyLogic with GNU Lesser General Public License v3.0 | 4 votes |
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]; // TODO: using wrong constructor now Tool tool = new Tool(); CompositeGrammar composite = new CompositeGrammar(); Grammar parser = new Grammar(tool, grammarFileName, composite); composite.setDelegationRoot(parser); FileReader fr = new FileReader(grammarFileName); BufferedReader br = new BufferedReader(fr); parser.parseAndBuildAST(br); br.close(); parser.composite.assignTokenTypes(); parser.composite.defineGrammarSymbols(); parser.composite.createNFAs(); List leftRecursiveRules = parser.checkAllRulesForLeftRecursion(); if ( leftRecursiveRules.size()>0 ) { return; } if ( parser.getRule(startRule)==null ) { System.out.println("undefined start rule "+startRule); return; } String lexerGrammarText = parser.getLexerGrammar(); Grammar lexer = new Grammar(); lexer.importTokenVocabulary(parser); lexer.fileName = grammarFileName; lexer.setTool(tool); if ( lexerGrammarText!=null ) { lexer.setGrammarContent(lexerGrammarText); } else { System.err.println("no lexer grammar found in "+grammarFileName); } lexer.composite.createNFAs(); 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()); }