![]() Recommend namespacing pragmas and annotations |
||
---|---|---|
.. | ||
openqasm_reference_parser | ||
tests | ||
.gitignore | ||
README.md | ||
index.rst | ||
pyproject.toml | ||
qasm3Lexer.g4 | ||
qasm3Parser.g4 | ||
requirements-dev.txt | ||
requirements.txt | ||
setup.cfg |
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
- 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 likelyantlr4
orantlr
. If you followed the "Getting Started" instructions, it is likely just theantlr4
alias, or it might bejava -jar <path/to/antlr.jar>
. - Install the Python package with
pip install -e .
.
Run the Tests
- Make sure the Python parser is built and available on the Python path.
- Install the testing requirements with
pip install -e .[tests]
orpip install -r requirements-dev.txt
. - Run
pytest
.