Package: mccode-antlr
Monte Carlo Particle Ray Tracing compiler via ANTLR4Implementing themccode-3language and targeting themcstasandmcxtraceruntimes.Language parsing with ANTLR4The traditionalMcCodelex|flextokenizer andyacc|bisonparser
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 theMcCodelanguagesusingANTLR4which both tokenizes and parses the language into a recursive descent parse tree.ANTLRcan include extra in-rule parsing code, but since it can produce output
suited for multiple languages (and the extra code must beinthe targeted language)
this feature is not implemented in this project.Other benefits ofANTLR4include 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 languagesTraditionally,McCodeidentifies as a single language able to read, parse, and construct
programs to perform single particle statistical ray tracing.
WhileMcCode-3uses a singlelanguage.landlanguage.yfile pair for lexing and parsing,
it actually implementedat least tworelated languages: one for component definitions in.compfiles,
one for instrument definitions in.instrfiles,
plus arguably more for other specialised tasks.This project makes use ofANTLR's language dependency feature to separate the languages
intoMcCompfor components andMcInstrfor instruments, with common language features
defined in aMcCommongrammar.Language translationFor use with theMcCoderuntimes (McStasandMcXtrace), the input languages must be
translated toCfollowing theC99standard.
This translation was previously performedinCsince thelex|flex,yacc|bisonworkflow produced programs written inC.
TheCprogramming language is a very good choice where execution speed is important,
as in theMcCoderuntimes, but less so if speed is not the main goal and memory safety
or cross-platform development is important.
TheMcCode-3translators 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.ANTLR4is aJavaprogram, but produces parse-trees in multiple languages.
This project uses thePythontarget 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.InstallationInstall the latest development version from GitHub with$python-mpipinstallgit+https://github.com/McStasMcXtrace/mccode-antlr.gitor the latest release from PyPI with$python-ppipinstallmccode_antlrUseThemccode-antlrpackage provides a command-line interface to theMcCodelanguage parsers
and translators. To avoid shadowing theMcCode-3translators, the command-line interface
are suffixed with-antlr.$mcstas-antlr--help$mcxtrace-antlr--help