# \[ANN\] PikaMacros.jl: Declarative grammars via PikaParser.jl

**URL:** <https://discourse.julialang.org/t/ann-pikamacros-jl-declarative-grammars-via-pikaparser-jl/138743>\
**Category:** Package Announcements\
**Created:** [August 11, 2026, 1:39pm UTC](https://discourse.julialang.org/t/ann-pikamacros-jl-declarative-grammars-via-pikaparser-jl/138743 "2026-08-11T13:39:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![impact-basin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impact-basin/32/220810_2.png) [@impact-basin](https://discourse.julialang.org/u/impact-basin)\
**Post date:** [August 11, 2026, 1:39pm UTC](https://discourse.julialang.org/t/ann-pikamacros-jl-declarative-grammars-via-pikaparser-jl/138743/1 "2026-08-11T13:39:26Z")

</div>

Hi all,

This is a small package meant to provide a front-end syntax to the excellent PikaParser.jl package.

This package can be used to parse arbitrary strings, or for implementing DSLs via `str` and `cmd` macros.

For an example, here is a simple arithmetic evaluator:

```julia
syn = @syntax :top begin
    :ws => many(satisfy(isspace))
    :number => some(:digit => satisfy(isdigit))
    :plus => seq(:pexpr, :ws, token('+'), :ws, :pexpr, :ws)
    :minus => seq(:pexpr, :ws, token('-'), :ws, :pexpr, :ws)
    :times => seq(:pexpr, :ws, token('*'), :ws, :pexpr, :ws)
    :divby => seq(:pexpr, :ws, token('/'), :ws, :pexpr, :ws)
    :paren => seq(token('('), :ws, :expr, :ws, token(')'), :ws)
    :expr => first(:times, :divby, :plus, :minus, :number)
    :pexpr => first(:paren, :expr)
    :top => seq(:ws, :pexpr)
end

sem = @semantics :top m v begin
    :number => parse(Int, m.view)
    :expr => v[1]
    :plus => v[1] + v[5]
    :minus => v[1] - v[5]
    :times => v[1] * v[5]
    :divby => v[1] / v[5]
    :paren => v[3]
    :expr => v[1]
    :pexpr => v[1]
    :top => v[2]
end

calc(x) = x |> syn |> sem
calc("3 + 2 + 8") # => 13
calc("3 * (2 + 16)") # => 54
calc("3 + 5 * (2 + 16)") # => 93

```

A neat trick is that the PikaParser.jl grammar is built at macro-expansion time, if possible. This should allow for aggressive optimisation during precompilation, if I understand the Julia internals right.

No AI was harmed in the making of this package 🙂.

Feel free to raise issues and PRs! I’ll do the best I can to solve them, but life is a bit hectic at the moment.

[The repo is here on GitHub.](https://github.com/impact-basin/PikaMacros.jl)

Cheers all!

---

<div class="post-metadata">

**Author:** ![sinisterMage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sinistermage/32/222897_2.png) [@sinisterMage](https://discourse.julialang.org/u/sinisterMage)\
**Post date:** [August 11, 2026, 1:47pm UTC](https://discourse.julialang.org/t/ann-pikamacros-jl-declarative-grammars-via-pikaparser-jl/138743/2 "2026-08-11T13:47:15Z")

</div>

very cool 🙂  
not sure i can provide much feedback as i am not an expert in this topic, but from what ive seen it looks like it could be useful for people, so great job! earned yourself a star too if that helps 😁

---

<div class="post-metadata">

**Author:** ![impact-basin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impact-basin/32/220810_2.png) [@impact-basin](https://discourse.julialang.org/u/impact-basin)\
**Post date:** [August 11, 2026, 2:41pm UTC](https://discourse.julialang.org/t/ann-pikamacros-jl-declarative-grammars-via-pikaparser-jl/138743/3 "2026-08-11T14:41:44Z")

</div>

Thanks! I’m not much of an expert in this topic either, alas 😃 just happened to need to write a parser whilst I was fooling around with metaprogramming!
