grammar LineCounter; @members { public int cnt=0; public static void main(String[] args) throws Exception { LineCounterLexer lex = new LineCounterLexer(new ANTLRFileStream(args[0])); CommonTokenStream tokens = new CommonTokenStream(lex); LineCounterParser parser = new LineCounterParser(tokens); try { parser.s(); } catch (RecognitionException e) { e.printStackTrace(); } } } /*------------------------------------------------------------------ * LEXER RULES *------------------------------------------------------------------*/ NONLCHARS : (NONLCHAR)* ; fragment NONLCHAR : ~('\n') ; NL : '\n' '\r'? ; /*------------------------------------------------------------------ * PARSER RULES *------------------------------------------------------------------*/ s : (line) + {System.out.println(cnt);}; line : NONLCHARS NL {cnt++;};