Java 类org.antlr.runtime.tree.ParseTree 实例源码

项目:orphanblack    文件:TreeProducer.java   
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+">");
}
项目:orphanblack    文件:TreeProducer.java   
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 ();
}
项目:mclab-core    文件:Interpreter.java   
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();
}
项目:mclab-core    文件:Interp.java   
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());
}
项目:mclab-core    文件:TestInterpretedParsing.java   
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);
}
项目:mclab-core    文件:TestInterpretedParsing.java   
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);
}
项目:mclab-core    文件:TestInterpretedParsing.java   
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);
}
项目:mclab-core    文件:TestInterpretedParsing.java   
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);
}
项目:jtcc    文件:ParseTreeBuilder.java   
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);
}
项目:jtcc    文件:ParseTreeBuilder.java   
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();        
}
项目:jtcc    文件:ParseTreeBuilder.java   
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);
}
项目:VectorAttackScanner    文件:ParseTreeBuilder.java   
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);
}
项目:VectorAttackScanner    文件:ParseTreeBuilder.java   
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();        
}
项目:VectorAttackScanner    文件:ParseTreeBuilder.java   
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);
}
项目:jFuzzyLogic    文件:Interpreter.java   
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();
}
项目:jFuzzyLogic    文件:TestInterpretedParsing.java   
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);
}
项目:jFuzzyLogic    文件:TestInterpretedParsing.java   
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);
}
项目:jFuzzyLogic    文件:TestInterpretedParsing.java   
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);
}
项目:chipster    文件:StringValidator.java   
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");
}
项目:mclab    文件:Interpreter.java   
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();
}
项目:mclab    文件:Interp.java   
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());
}
项目:mclab    文件:TestInterpretedParsing.java   
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);
}
项目:mclab    文件:TestInterpretedParsing.java   
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);
}
项目:mclab    文件:TestInterpretedParsing.java   
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);
}
项目:mclab    文件:TestInterpretedParsing.java   
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);
}
项目:mclab-core    文件:Interpreter.java   
public ParseTree parse(String startRule)
    throws RecognitionException
{
    return parse(startRule, null);
}
项目:mclab-core    文件:ParseTreeBuilder.java   
public ParseTreeBuilder(String grammarName) {
    ParseTree root = create("<grammar "+grammarName+">");
    callStack.push(root);
}
项目:mclab-core    文件:ParseTreeBuilder.java   
public ParseTree getTree() {
    return (ParseTree)callStack.elementAt(0);
}
项目:mclab-core    文件:ParseTreeBuilder.java   
/**  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);
}
项目:mclab-core    文件:ParseTreeBuilder.java   
public void enterRule(String ruleName) {
    ParseTree parentRuleNode = (ParseTree)callStack.peek();
    ParseTree ruleNode = create(ruleName);
    parentRuleNode.addChild(ruleNode);
    callStack.push(ruleNode);
}
项目:mclab-core    文件:ParseTreeBuilder.java   
public void consumeToken(Token token) {
    ParseTree ruleNode = (ParseTree)callStack.peek();
    ParseTree elementNode = create(token);
    ruleNode.addChild(elementNode);
}
项目:mclab-core    文件:ParseTreeBuilder.java   
public void recognitionException(RecognitionException e) {
    ParseTree ruleNode = (ParseTree)callStack.peek();
    ParseTree errorNode = create(e);
    ruleNode.addChild(errorNode);
}
项目:jtcc    文件:ParseTreeBuilder.java   
public ParseTreeBuilder(String grammarName) {
    ParseTree root = create("<grammar "+grammarName+">");
    callStack.push(root);
}
项目:jtcc    文件:ParseTreeBuilder.java   
public ParseTree getTree() {
    return (ParseTree)callStack.elementAt(0);
}
项目:jtcc    文件:ParseTreeBuilder.java   
/**  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);
}
项目:jtcc    文件:ParseTreeBuilder.java   
public ParseTree epsilonNode() {
    return create(EPSILON_PAYLOAD);
}
项目:jtcc    文件:ParseTreeBuilder.java   
public void recognitionException(RecognitionException e) {
    if ( backtracking>0 ) return;
    ParseTree ruleNode = (ParseTree)callStack.peek();
    ParseTree errorNode = create(e);
    ruleNode.addChild(errorNode);
}
项目:VectorAttackScanner    文件:ParseTreeBuilder.java   
public ParseTreeBuilder(String grammarName) {
    ParseTree root = create("<grammar "+grammarName+">");
    callStack.push(root);
}
项目:VectorAttackScanner    文件:ParseTreeBuilder.java   
public ParseTree getTree() {
    return (ParseTree)callStack.elementAt(0);
}