r/ProgrammingLanguages • u/LegendaryMauricius • Aug 08 '26
A new grammar generation language
Hi everybody. I'm happy to share a small project I've been working on lately. I call it MGFF (Macro grammar functional form), and its specification can be found here: https://github.com/LMauricius/py-perg-mgff/blob/main/Docs/mgff-specification.md . It's related to a post that I made ages ago ( here ). After re-reading that version (called just MGF back then) when I wasn't tired I realized what monstrosity I made. MGFF is far more elegant. Here is an example:
# A tiny calculator language.
t Lex (
d Digit = 0-9
d Alpha = a-z|A-Z
d AlNum = a-z|A-Z|0-9
d Int = (Digit)+
> class(Int) push(tokens)
d Number = Int ( . (Digit)+ )?
> class(Number) push(tokens)
d Ident = Alpha (AlNum)*
> class(Ident) push(tokens)
# length-based: "<=" (the two-item "< =") takes precedence over "<"
d Op = < =
| <
| =
| +
| -
| *
| /
> push(tokens) string
d Space = ( _|\t|\n )+
d LParen = \(
> class(\() push(tokens)
d RParen = \)
> class(\)) push(tokens)
d Token = Number
/ Ident
/ Op
/ Space
/ LParen
/ RParen
d File = (Token)*
)
# mixfix macro: an R, then zero or more (S R)
d sep(R)by(S) = R (S R)*
t Parse (
# `Lex` runs first; the terminals here are still characters.
> post(Lex) over(tokens)
# order-based: the first alternative that succeeds is the match
d Expr = Term + Expr
/ Term - Expr
/ Term
d Term = Factor * Term
/ Factor / Term
# the second / on the line above is an ordinary item, not a marker
/ Factor
d Factor = Number
/ Ident
/ \( Expr \)
d Signed = ( (+)/(-) )? Number
d AssignList = sep(Ident = Expr)by(,)
)It can also serve as a replacement for regexes:
# A grammar matching a "key = value" setting line
d Space = ( _|\t )*
d Word = ( a-z|A-Z|_ )+
# right-linear recursion: the same as ( 0-9 )+
d Digits = 0-9 Digits
/ 0-9
d Value = Digits
/ Word
# The field a match ends up in belongs to the rule, not to the place it is used,
# so the two sides of the line are productions of their own.
d Key = Word
> store(key)
d Val = Value
> store(value)
d Match = Space Key Space = Space Val SpaceI'm sharing the MGFF spec rather than the generator using it because the generator is very much WIP and needs a lot of testing and refactoring. Still, since I've got a bunch of projects I love working on more, I'd like to know what's the interest for parser generator tools in the wider community.
Actually I doubt that I will link the generator itself here because I would risk a perma-ban. It's not vibe-coded, but it wouldn't be welcomed. Most of it was quickly prototyped with LLM. Still, it generates quite nice TextMate and Pandoc syntax highlighting grammars.
MGFF itself is of course manually defined by me. I just figured I like to work on languages themselves and parser algorithms than on CLI tools and understanding existing niche specifications 🤷♂️.
4
u/vmxdev Aug 08 '26
This isn't a rant, I'm just curious — I constantly see lexical analyzer generators that don't take Unicode into account.
At the same time, quite a few languages now use all sorts of cool mathematical symbols and Greek letters like π, ∀, Σ, and so on.
Yeah, I know, these symbols are difficult to type on a regular keyboard.
However, even if you don't plan to use these symbols, Unicode already has so-called "character properties," so you don't have to write "Alpha = a-z|A-Z" or list the mathematical symbols every time
3
u/sreekotay Aug 08 '26
Intreesting! Have you ever looked at Rebol's parse dialect? It's pretty cool
Here's a (pretty) complete CSS parser, e.g.
https://github.com/Oldes/Rebol-CSS/blob/main/css.reb
2
u/tiger-56 Aug 08 '26
I like this. I’m currently working on a PEG parser generator myself. Does it generate source code or does it run as an interpreter?
2
u/PitifulTheme411 ... Aug 09 '26
What is the wackiest grammar you can make with it?
1
u/david-1-1 Aug 11 '26
I would use | instead of /. Tokens that are punctuation or special characters should be quoted.
This grammar language is a bit too compact. And the semantics that happen for each production recognition are too obscure or entirely omitted.
Unnecessary learning hurdles.
2
u/--predecrement Aug 08 '26 edited Aug 08 '26
For reference, here's the same grammar/parser as your simplest example, using
grammar, Raku's built in EBNF grammar/parsing/regex construct:In the above I elided some of the structure you appear to prefer but also showed enough that you can hopefully see how it can still be done if a dev wants that (cf the "nodes can be renamed" comment).
Here's the same parsing task, but approached as a Rakoon might write it as a regex (i.e. throwing things together with less structure on a YAGNI basis, but then again randomly throwing in a renaming of a captured piece of the match so it can be pulled it out easily with a different name):
It's all the same underlying EBNF slang (slang = sub-language, with its own grammar) for the body of the
tokensubroutines, and it can get terser still, but the above is hopefully both similar enough (so you can grok it based on the grammar code above) and different enough (so you can see how the terser form can be appropriate for small tasks).----
From a theoretic perspective the parsing class for Raku's "rules" (grammar / regex features) is Unrestricted grammar ("No restrictions are made on the productions ...".)
From a practical perspective, the Rakudo compiler provides an industrial strength example. It compiles Raku code by compiling several modules that declare
grammars that define the standard Raku grammar, which declarations/modules automatically generate corresponding parsers that the compiler then combines and uses to compile Raku code, including itself.(Any user code can use the same trick, so the upshot is that Raku naturally covers the same "Languages and Libraries" tricks as Racket/Rhombus. That said, Racket is decades ahead in building this out using a macro led approach, but my impression is that errors can get pretty obscure. Raku is decades ahead in building this out using a grammar led approach, but it's a long hard slog to arrive at the point where Rakoons can begin to talk amongst themselves about the decades old dream of sensibly choosing between, and easily refactoring between, using the grammar construct approach and the AST macro approach.)
0
u/EggplantExtra4946 Aug 08 '26
Why no character classes? This is biggest thing that is lacking. It's a lot more practical AND readable to write a single character class with everything you want rather than combining alpha, digit, etc.. A character class (for a single byte) is also going to be evaluate a lot faster with the usual 16 bytse/32 bytes bitset rather with however you would implement it otherwise (DFA, NFA, backtracking).
With that you have less of a need for length-based alternatives, because those are parctically only useful for lexing and at the same time it's going to be a pain in the ass to implement the full generality of your parser generator, when there is arbitrary recursion used inside the alternative and when the sub rules that are called also contained length-based alternatives. How do you implement to implement it?
It's going to be hard to reason about a parser that does that and at the same time I don't see another use case other lexing. Raku's grammar did that and it is its biggest flaw IMO, not that it matters because it is too slow to be usable. Also like Raku you chose the good and natural operator "|" for length-based alternatives and the bad one "/" for the natural order-based alternatives.
If I made a parser generator, I would allow the parser interpreter to call an external lexer function to do the lexing, since it's easy to write and modify and it's going to one of the bottlenecks in performance. Otherwise for a completely bultin solution, I would put a "lexer" declaration that contains a set of regexes or rule name containing regexes, they rule would probably have to declared as "token" or "regex" like in Raku and in those I guess the alteratives would all be automatically length-based given that those rules are meant to be used by the lexer, not in themselves individually. Those rules (integer, identifier, string, keyword, etc..) would have some restrictions: keep the constructs regular expressions (in case you support lookadhead, backtracking control, etc..), allow rule call as long as they are not recursive/mutually recursive, and maybe limit the internal capturing you can do inside and arbitrary code that can run inside. All these rules so that you can implement the lexer efficiently but in practice this wouldn't really be limiting your expressivitiy, given the nature of lexing and what it is used for. Of course, having the possibility to call an external lexer and/or with a lexer builtin there wouldn't be a length-based alternative for the general parser generator, not needed and potentially harmful.
In your spec there is no mention of when ordered-alternatives and quantifiers can backtrack or if they can backtrack at all, you need to specify it otherwise we can't reason about how your parser generator is going to parse.
Your mixfix macro is interesting though, with a useful example.
-1
u/EggplantExtra4946 Aug 10 '26
AI slop.
Using AI to generate source code is one thing, but using AI to write a """spec""" if you can call it that is next level damage.
0
u/[deleted] Aug 13 '26
[deleted] — view deleted comment
11
u/[deleted] Aug 08 '26
[removed] — view removed comment