Skip to content

Guide · Open source

How to use grammars-v4: clone the repo and pick an ANTLR grammar

Updated · written with AI from the project's own documentation and checked against our catalogue data

grammars-v4 is a collection of formal grammars written for ANTLR v4, a parser generator tool. A grammar describes the structure of a language or file format so a parser can read code or data written in it. This guide is for developers, students and tool builders who want a starting grammar instead of writing one from scratch.

grammars-v4antlr/grammars-v4 · ★ 11.1k · MIT

Quick start

  1. 1Install ANTLR v4. Install the ANTLR v4 tool, since the grammars need it to produce a parser.
  2. 2Clone the repository. Run git clone https://github.com/antlr/grammars-v4 to download the grammar collection.
  3. 3Open the language folder. Find the top-level folder named after your language or file format, such as java or cpp.
  4. 4Review the grammar. Read the grammar file in that folder and check it covers the syntax you need.
  5. 5Generate and test the parser. Use ANTLR v4 to generate a parser from the grammar, then test it on your own sample input.

What grammars-v4 contains

The repository holds grammar files, not a finished program. Each top-level folder is named after the language or file format it covers, such as java, cpp or csharp, so you can find a grammar by name.

The project's README says the root directory name is the all-lowercase name of the language or file format parsed by the grammar, with examples including java, cpp, csharp and c.

Install the prerequisites and clone the repository

The install method is clone-and-build, and the main prerequisite is the ANTLR v4 tool. Clone the repository with the command below, then open the folder for the language you need.

The README does not describe a separate installer for the grammars themselves.

  • Install the ANTLR v4 tool first, since the grammars need ANTLR to be used.
  • Clone the repository to a folder on your machine.
git clone https://github.com/antlr/grammars-v4

Find the grammar you need

Browse the top-level folders and pick the one named after your language or format. The README's performance table is a sortable page that lists grammars, which can help you compare them.

The README links a grammar performance table and points to the project wiki for further questions.

  • Look for a folder named after your language, for example java.
  • Check the performance table to compare grammars by the columns it lists.
  • Read the wiki for project FAQ content.

Generate a parser with ANTLR

Once you have a grammar file, you use the ANTLR v4 tool to turn it into parser code. The source README does not give the exact generation command, so follow the ANTLR v4 documentation for that step.

The project expects grammars to be free of actions, so check the grammar you pick before you rely on it in your own build.

Know the limits before you rely on a grammar

The README does not describe per-grammar testing guarantees. Treat each grammar as a starting point and test it against your own sample input.

Use grammars-v4 for parser, linter and code analysis work, or for learning how formal grammars describe real languages. It is not an application you can run on its own.

  • Test each grammar on real files before using it in production tools.
  • Expect to write your own code around the generated parser.

What people use it for

  • Starting a Java parser for a linter or code analysis tool using the java folder
  • Building a parser for a file format that has a grammar in the collection
  • Studying how a real language such as C or C# is described in a formal grammar
  • Comparing grammars for the same language to see which one fits your project

Frequently asked questions

Is grammars-v4 free to use?

grammars-v4 is licensed under the MIT licence. The README names the licence, so check the repository's licence file for the full terms.

What do I need to use grammars-v4?

You need the ANTLR v4 tool, because the grammars are written for ANTLR v4 and need it to be used. You also need to clone the repository to get the grammar files.

How do I install grammars-v4?

The install method is clone-and-build. Run git clone https://github.com/antlr/grammars-v4 and install the ANTLR v4 tool separately.

Is grammars-v4 a program I can run?

No. It is a collection of grammar files. You need ANTLR to turn a grammar into a parser that you can use in your own code.

Are the grammars tested and guaranteed to be correct?

The README does not describe per-grammar testing guarantees, so do not assume every grammar is fully verified. Test each one on your own input before relying on it.

What are the alternatives to grammars-v4?

Comparable projects include grammars from the ANTLR project itself and Tree-sitter grammars. Compare the language coverage and tooling you need before choosing.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

grammars-v4 suits developers, students and tool builders who want a ready grammar for a language or file format and already work with ANTLR. It is not a finished application, and people who do not use parsers will get little from it. Check each grammar against your own input, and consider the ANTLR project's grammars or Tree-sitter grammars if you want a different option.

Install options & downloads