title: How to get the full user-written statements (including the spaces) in ANTLR
body: <p>I have a "statement" definition from the Java language definition as follows.</p>
  
  <pre><code>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
  ;
  </code></pre>
  
  <p>When doing the parser, i want to print the full user-written statements also (inculding the spaces in the statements),  such as:</p>
  
  <pre><code>Object o = Ma.addToObj(r1);
  if(h.isFull() &amp;&amp; !h.contains(true)) h.update(o);
  </code></pre>
  
  <p>But when i use the function "getText()" in "exitStatement", i can only get the statements with all the spaces been deleted, such as:</p>
  
  <pre><code>Objecto=Ma.addToObj(r1);
  if(h.isFull()&amp;&amp;!h.contains(true))h.update(o);
  </code></pre>
  
  <p>How can i get the full user-written statements (inculding the spaces in the statements) in a easy way? Thanks a lot!</p>
  
  <p>The full codes as follows:</p>
  
  <pre><code>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&gt;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());
  }
  }
  </code></pre>