protected void parse(final String query, final ParserRule rule, final boolean expectFailures) {

        try {
            final CharStream in = CharStreams.fromString(query);
            final GremlinLexer lexer = new GremlinLexer(in);
            lexer.removeErrorListeners();
            lexer.addErrorListener(new GremlinErrorListener());

            final CommonTokenStream tokens = new CommonTokenStream(lexer);

            // Setup error handler on parser
            final GremlinParser parser = new GremlinParser(tokens);
            parser.setErrorHandler(new BailErrorStrategy());
            parser.getInterpreter().setPredictionMode(PredictionMode.SLL);

            // Visit based on the specified rule
            try {
                final GremlinBaseVisitor<?> visitor = new GremlinBaseVisitor<>();

                switch (rule) {
                    case QUERY_LIST:
                        visitor.visit(parser.queryList());
                        break;
                }
            } catch (Exception ex) {
                // Retry parsing with LL prediction mode
                tokens.seek(0);
                lexer.reset();
                parser.reset();
                parser.getInterpreter().setPredictionMode(PredictionMode.LL);

                final GremlinBaseVisitor<?> visitor = new GremlinBaseVisitor<>();

                switch (rule) {
                    case QUERY_LIST:
                        visitor.visit(parser.queryList());
                        break;
                }
            }

            if (expectFailures) {
                fail("Query parsing should have failed for " + query);
            }

        } catch (Exception e) {
            if (!expectFailures) {
                final Throwable t = ExceptionUtils.getRootCause(e);
                if (t instanceof LexerNoViableAltException) {
                    fail(String.format("Error parsing: %s - [%s] - failed at index: %s",
                            query, t.toString(), ((LexerNoViableAltException) t).getStartIndex()));
                } else if (t instanceof NoViableAltException) {
                    fail(String.format("Error parsing: %s - [%s]", query, ((NoViableAltException) t).getStartToken().toString()));
                } else {
                    fail(String.format("Error parsing: %s - [%s]", query, t.getMessage()));
                }
            }
        }
    }
Allows control of the type of parsing to do for a particular test line.
gremlin-language/src/test/java/org/apache/tinkerpop/gremlin/language/grammar/AbstractGrammarTest.java