Skip to main content

ANTLR4 grammars for McStas and McXtrace

Project description

Monte Carlo Particle Ray Tracing compiler via ANTLR4

Implementing the mccode-3 language and targeting the mcstas and mcxtrace runtimes.

Language parsing with ANTLR4

The traditional McCode lex|flex tokenizer and yacc|bison parser included in-rule code to implement some language features and called the code-generator to construct the intermediate instrument source file. The mixture of language parsing and multiple layers of generated functionality made understanding the program operation, and debugging introduced errors, difficult. Worst of all, there is no easy-to-use tooling available to help the programmer identify syntax errors on-the-fly.

This project reimplements the McCode languages using ANTLR4 which both tokenizes and parses the language into a recursive descent parse tree. ANTLR can include extra in-rule parsing code, but since it can produce output suited for multiple languages (and the extra code must be in the targeted language) this feature is not implemented in this project.

Other benefits of ANTLR4 include integration with Integrated Development Environments, including the freely available Community edition of PyCharm from JetBrains. IDE integration can identify syntax mistakes in the language grammar files, plus help to understand and debug language parsing.

McCode languages

Traditionally, McCode identifies as a single language able to read, parse, and construct programs to perform single particle statistical ray tracing. While McCode-3 uses a single language.l and language.y file pair for lexing and parsing, it actually implemented at least two related languages: one for component definitions in .comp files, one for instrument definitions in .instr files, plus arguably more for other specialised tasks.

This project makes use of ANTLR's language dependency feature to separate the languages into McComp for components and McInstr for instruments, with common language features defined in a McCommon grammar.

Language translation

For use with the McCode runtimes (McStas and McXtrace), the input languages must be translated to C following the C99 standard. This translation was previously performed in C since the lex|flex, yacc|bison workflow produced programs written in C. The C programming language is a very good choice where execution speed is important, as in the McCode runtimes, but less so if speed is not the main goal and memory safety or cross-platform development is important. The McCode-3 translators do not always deallocate memory used in their runtime, and newly developed features are likely to introduce unallocated, out-of-bounds, or double-free memory errors which are then difficult to track down.

ANTLR4 is a Java program, but produces parse-trees in multiple languages. This project uses the Python target so that language-translation can proceed in a language which is well suited to new-feature development, while removing memory handling issues and making cross-platform development significantly easier.

Installation

Install the latest development version from GitHub with

$ python -m pip install git+https://github.com/McStasMcXtrace/mccode-antlr.git

or the latest release from PyPI with

$ python -p pip install mccode_antlr

Use

The mccode-antlr package provides a command-line interface to the McCode language parsers and translators. To avoid shadowing the McCode-3 translators, the command-line interface are suffixed with -antlr.

$ mcstas-antlr --help
$ mcxtrace-antlr --help

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mccode_antlr-0.8.2.tar.gz (288.2 kB view details)

Uploaded Source

Built Distribution

mccode_antlr-0.8.2-py3-none-any.whl (255.8 kB view details)

Uploaded Python 3

File details

Details for the file mccode_antlr-0.8.2.tar.gz.

File metadata

  • Download URL: mccode_antlr-0.8.2.tar.gz
  • Upload date:
  • Size: 288.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for mccode_antlr-0.8.2.tar.gz
Algorithm Hash digest
SHA256 e2591d2ae6bed32591ddb07433e8b9f23129f9a80bd3de57f626b9b1c8a3e747
MD5 4dc3bae2a0a41bb19d2244b795229cae
BLAKE2b-256 fe33b14c401878f8f46ab5d0209178b3639f68b1203ac0c6c0e5ca7a7045da95

See more details on using hashes here.

File details

Details for the file mccode_antlr-0.8.2-py3-none-any.whl.

File metadata

  • Download URL: mccode_antlr-0.8.2-py3-none-any.whl
  • Upload date:
  • Size: 255.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for mccode_antlr-0.8.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c67427078cadadfd7950ce4d3e9c34a2f665f3cb7dc9d3bc53623d51e5a9bc67
MD5 804bdd4cdb8641c7fd65757433593e52
BLAKE2b-256 6e9fe607435b5745a9e3aa1451ad0ad59ee37adb0499f679fd6d12b5d2d56299

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page