openqasm/source/grammar
Simon Cross 58481e1bef
Merge pull request #566 from hodgestar/pragma-namespaces
Recommend namespacing pragmas and annotations
2024-12-11 18:02:16 +01:00
..
openqasm_reference_parser Clarify documentation of reference parser 2021-08-23 15:12:03 +01:00
tests Correct integer division in `pow` modifiers (#529) 2024-04-30 17:33:40 +01:00
.gitignore Split lexer and parser ANTLR grammars 2022-04-15 15:13:06 -04:00
README.md Replace reference to grammar file to lexer and parser files 2022-12-14 13:51:09 -05:00
index.rst OpenQasm -> OpenQASM 2024-11-05 14:18:48 +01:00
pyproject.toml Create Python package for reference parser 2021-08-20 13:31:08 +01:00
qasm3Lexer.g4 Allow annotations to include multiple dotted identifiers. 2024-11-12 16:04:02 +01:00
qasm3Parser.g4 Update parser per Jake's comment 2024-09-18 12:03:25 -04:00
requirements-dev.txt Add CI tests for grammar via ANTLR parser 2021-08-11 19:28:53 +01:00
requirements.txt Add CI tests for grammar via ANTLR parser 2021-08-11 19:28:53 +01:00
setup.cfg Update other references to the OpenQASM GitHub repository. 2023-11-21 11:28:59 +01:00

README.md

OpenQASM 3.0 Grammar Reference

The reference grammar is written in ANTLR. The lexer rules are in the file qasm3Lexer.g4, and the parser rules are in the file qasm3Parser.g4.

This directory also contains a very basic Python parser, which is simply built from the reference grammar and used to test against the examples.

Requisites

Building the grammar only requires ANTLR 4. You can likely get a copy of ANTLR using your system package manager if you are on Unix, or from brew if you are on macOS. You could also follow these instructions.

Running the Python version of the parser also requires the ANTLR Python runtime. You can install this with pip by

pip install antlr4-python3-runtime==<version>

where <version> should exactly match the version of ANTLR 4 you installed. If you let pip do this automatically when it installs the reference parser, it will likely pull the wrong version, and produce errors during use.

Building the Python Parser

  1. Build the grammar files into the package directory with
    <antlr command> -o openqasm_reference_parser -Dlanguage=Python3 -visitor qasm3Lexer.g4 qasm3Parser.g4
    
    <antlr command> should be replaced with however you invoke ANTLR on your system. If you used a package manager, it is likely antlr4 or antlr. If you followed the "Getting Started" instructions, it is likely just the antlr4 alias, or it might be java -jar <path/to/antlr.jar>.
  2. Install the Python package with pip install -e ..

Run the Tests

  1. Make sure the Python parser is built and available on the Python path.
  2. Install the testing requirements with pip install -e .[tests] or pip install -r requirements-dev.txt.
  3. Run pytest.