I have a definition of "statement" from a 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 you execute the parser, I also want to print full user statements (by entering spaces in the statements), for example:
Object o = Ma.addToObj(r1); if(h.isFull() && !h.contains(true)) h.update(o);
But when I use the "getText ()" function in "exitStatement", I can only get instructions with all spaces removed, for example:
Objecto=Ma.addToObj(r1); if(h.isFull()&&!h.contains(true))h.update(o);
How can I get full user statements (embedding spaces in statements) in a simple way? Many thanks!
Full codes:
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();
source share