Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the full user-written statements (including the spaces) in ANTLR

Tags:

antlr

I have a "statement" definition from the Java language definition as follows.

statement
: block
|   ASSERT expression (':' expression)? ';'
|   'if' parExpression statement ('else' statement)?
|   'for' '(' forControl ')' statement
|   'while' parExpression statement
|   'do' statement 'while' parExpression ';'
|   'try' block
    ( catches 'finally' block
    | catches
    | 'finally' block
    )
|   'switch' parExpression switchBlock
|   'synchronized' parExpression block
|   'return' expression? ';'
|   'throw' expression ';'
|   'break' Identifier? ';'
|   'continue' Identifier? ';'
|   ';'
|   statementExpression ';'
|   Identifier ':' statement
;

When doing the parser, i want to print the full user-written statements also (inculding the spaces in the statements), such as:

Object o = Ma.addToObj(r1);
if(h.isFull() && !h.contains(true)) h.update(o);

But when i use the function "getText()" in "exitStatement", i can only get the statements with all the spaces been deleted, such as:

Objecto=Ma.addToObj(r1);
if(h.isFull()&&!h.contains(true))h.update(o);

How can i get the full user-written statements (inculding the spaces in the statements) in a easy way? Thanks a lot!

The full codes as follows:

public class PrintStatements {
public static class GetStatements extends sdlParserBaseListener {
    StringBuilder statements = new StringBuilder();
     public void exitStatement(sdlParserParser.StatementContext ctx){               
            statements.append(ctx.getText());
            statements.append("\n");                        
        }
}


public static void main(String[] args) throws Exception{

String inputFile = null;
if ( args.length>0 ) inputFile = args[0];
InputStream is = System.in;
if ( inputFile!=null ) {
    is = new FileInputStream(inputFile);
}
ANTLRInputStream input = new ANTLRInputStream(is);
sdlParserLexer lexer = new sdlParserLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
sdlParserParser parser = new sdlParserParser(tokens);
ParseTree tree = parser.s();

// create a standard ANTLR parse tree walker
ParseTreeWalker walker = new ParseTreeWalker();
// create listener then feed to walker
GetStatements loader = new GetStatements();
walker.walk(loader, tree);        // walk parse tree   

System.out.println(loader.statements.toString());
}
}
like image 981
Lisa Avatar asked Sep 23 '13 18:09

Lisa


1 Answers

I've solved this problem by using tokens.getText() in the upper level of the statement, like this:

public void exitE(sdlParserParser.EContext ctx) {
    TokenStream tokens = parser.getTokenStream();
    String Stmt = null;
    Stmt = tokens.getText(ctx.statement());
                ...

}
like image 113
Lisa Avatar answered Oct 06 '22 20:10

Lisa