Skip to content
Ken Domino edited this page Feb 17, 2023 · 29 revisions

Welcome to the grammars-v4

Grammars-v4 is a collection of antrl4 grammars contributed by authors around the world. Grammars-v4 uses antlr4test-maven-plugin, a number of scripts in the _scripts directory, and Github Actions to ensure that all grammars on the tree build and parse input files properly with Antlr4. Each grammar has a directory of examples, which contains input files, and the expected output from the parse (parse errors contained in .errors files; parse tree of the input contained in .tree files). Testing is performed across: Ubuntu, macOS, and Windows operating systems; Cpp (C++), CSharp (C#), Dart (Dart2), Go, Java, JavaScript, PHP, and Python3 targets; Bash and Powershell environments.

A core value of grammars-v4 is that any grammar downloaded from grammars-v4 will compile properly with Antlr4, and has been validated against some example inputs.

FAQ

What are the licensing terms for Grammars-v4?

There is no single license for the grammars, each grammar has its own license. Check inside the grammar files for licensing terms

There is no grammar for the language or file format I need. What do I do?

You are welcome to submit an issue ticket, and contributions to the grammars tree are welcome also.

My PR was rejected! Why?!

If your PR breaks the existing tests, it will be rejected. Additionally, we ask that any incremental changes made to grammar files have examples contributed to the /examples directory for that grammar to ensure that future changes to the grammars don't introduce regressions.

Are there examples of how to use the grammars?

Look here

Is there a coding standard for Antlr4 grammars?

There is no official standard. However, there is a formatter that most grammars use: Antlr4Formatter

Typically, grammar files use 3 spaces for indents.

How can I use Antlr to parse binary files?

There is an example at /tcpheader/

How can I download grammars from the github page in a maven build?

Use download-maven-plugin

<plugin>
	<groupId>com.googlecode.maven-download-plugin</groupId>
	<artifactId>download-maven-plugin</artifactId>
	<version>1.4.0</version>
	<executions>
		<execution>
			<phase>generate-sources</phase>
			<goals>
				<goal>wget</goal>
			</goals>
			<configuration>
				<url>https://raw.githubusercontent.com/antlr/grammars-v4/master/arithmetic/arithmetic.g4</url>
				<outputFileName>arithmetic.g4</outputFileName>
				<outputDirectory>src/main/antlr4/com/khubla/antlr4example/</outputDirectory>
			</configuration>
		</execution>
	</executions>
</plugin>

How do I test the grammars?

Using Maven

  1. Clone grammars-v4. git clone https://github.com/antlr/grammars-v4.git
  2. Make sure you have Maven installed. See the documentation.
  3. cd grammars-v4 (the root directory), or to a grammar cd grammars-v4/java/java.
  4. Execute mvn clean test.

Using Trgen

  1. Clone grammars-v4. git clone https://github.com/antlr/grammars-v4.git
  2. Make sure you have the Trash toolkit installed. See the documentation.
  3. cd grammars-v4 (the root directory), or to a grammar cd grammars-v4/java/java.
  4. trgen -t _target_, where _target_ is Antlr4cs, Cpp, CSharp, Dart, Go, Java, JavaScript, PHP, Python3, or TypeScript.
  5. In a Bash prompt, type make; make test. Or, in a Powershell prompt, type pwsh build.sh; pwsh test.ps1. The scripts create temporary files used in the build. Use git clean -f to remove these files.
  6. Tests create .errors and .tree files automatically. If you want, you can check these in for testing across targets and OSes.
Clone this wiki locally