mccode-antlr¶
ANTLR4-based compiler and Python API for the McStas and McXtrace Monte Carlo ray-tracing languages.
mccode-antlr provides:
- A faithful re-implementation of the McStas/McXtrace instrument languages using ANTLR4 grammars
- Command-line tools (
mcstas-antlr,mcxtrace-antlr, …) that replace the traditional lex/yacc translators - A Python API for programmatically building, reading, and manipulating instruments
Quick start¶
from mccode_antlr import Flavor
from mccode_antlr.assembler import Assembler
a = Assembler("BrillouinSpectrometer", flavor=Flavor.MCSTAS)
a.parameter("double E_i = 5.0") # meV
a.parameter("int nmon = 1")
origin = a.component("Origin", "Progress_bar", at=(0, 0, 0))
source = a.component("Source", "Source_simple",
at=(0, 0, 0),
parameters={"radius": 0.05, "E0": "E_i", "dE": 0.1})
instr = a.instrument
print(instr) # or display in Jupyter: just put `instr` on the last line
Why ANTLR4?¶
The traditional McCode translators mix language parsing and code generation
inside lex/yacc rules — making them hard to debug and impossible to
tool-integrate. ANTLR4 cleanly separates grammar from behaviour and integrates
with IDEs (including the free PyCharm Community edition) for live syntax
checking of .comp and .instr files.
See Core Concepts for a fuller explanation.
Installation¶
Optional pip extras: