public static ErrorQueue antlrOnString(String workdir,
										   String targetName,
										   String grammarFileName,
										   String grammarStr,
										   boolean defaultListener,
										   String... extraOptions)
	{
		FileUtils.mkdir(workdir);
		writeFile(workdir, grammarFileName, grammarStr);
		return antlrOnString(workdir, targetName, grammarFileName, defaultListener, extraOptions);
	}
Write a grammar to tmpdir and run antlr
runtime-testsuite/test/org/antlr/v4/test/runtime/Generator.java