@Test
	public void testVisitErrorNode() {
		String input = "";
		VisitorBasicLexer lexer = new VisitorBasicLexer(new ANTLRInputStream(input));
		VisitorBasicParser parser = new VisitorBasicParser(new CommonTokenStream(lexer));

		final List<String> errors = new ArrayList<>();
		parser.removeErrorListeners();
		parser.addErrorListener(new BaseErrorListener() {
			@Override
			public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
				errors.add("line " + line + ":" + charPositionInLine + " " + msg);
			}
		});

		VisitorBasicParser.SContext context = parser.s();
		assertEquals("(s <missing 'A'> <EOF>)", context.toStringTree(parser));
		assertEquals(1, errors.size());
		assertEquals("line 1:0 missing 'A' at '<EOF>'", errors.get(0));

		VisitorBasicVisitor<String> listener = new VisitorBasicBaseVisitor<String>() {
			@Override
			public String visitErrorNode(ErrorNode node) {
				return "Error encountered: " + node.getSymbol();
			}

			@Override
			protected String defaultResult() {
				return "";
			}

			@Override
			protected String aggregateResult(String aggregate, String nextResult) {
				return aggregate + nextResult;
			}
		};

		String result = listener.visit(context);
		String expected = "Error encountered: [@-1,-1:-1='<missing 'A'>',<1>,1:0]";
		assertEquals(expected, result);
	}
This test verifies the basic behavior of visitors, with an emphasis on
{@link AbstractParseTreeVisitor#visitErrorNode}.
runtime-testsuite/test/org/antlr/v4/test/runtime/java/api/TestVisitors.java