Java Code Examples for org.antlr.runtime.tree.ParseTree#toStringTree()

The following examples show how to use org.antlr.runtime.tree.ParseTree#toStringTree() . 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: TestInterpretedParsing.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 2
Source File: TestInterpretedParsing.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 3
Source File: TestInterpretedParsing.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 4
Source File: TestInterpretedParsing.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
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);
}