r/Compilers • • Jul 07 '26

Building a Parser Generator!

Hi, I am creating a Parser Generator. It will have it's own unique syntax for grammar definition. This is what I am working on currently. I am sharing a draft of the syntax and asking if anyone is interested in sharing their feedbacks in the comments.

Is this syntax:

  • easy to read?
  • easy to understand?
  • sparks interest in you?
  • what stands out?
  • do you suggests any changes or additions?

UPDATED

I am settling on a syntax for the language. Here is a snippet:

# BASIC LANG
# ---------
# A 100
# B  25

parser {
    # action      ->  ast: (parent(children))
    (name number) -> (name(number)); # generate name with number as child of name
    (spc | nl) -> (); # no AST generated
}

name = <A:Z>+; # character sequence
number = <0> | <1:9> <0:9>+; # character sequence

# spc is built-in rule for space
# nl is built-in rule for nl

Full syntax is being worked on here: https://github.com/Algodal/Algodal_Text_Parser_Generator_Manual

I will be streaming code implementation of the parser generator on youtube: https://www.youtube.com/@RevnantRicko

8 Upvotes

21 comments sorted by

View all comments

Show parent comment

1

u/Blueglyph Jul 12 '26

It's here, but ANTLR is a more mature tool. For ANTLR, you can find a lot of grammar examples here.

I started with LL; the LR code is still only in a branch, and I haven't documented that part yet, but it follows the same principle and is more advanced, which is why I gave you the link to that branch. You can find some examples in the "examples" directory: gen_{x} is where the grammar is, and the code is generated in {x}, like gen_microcalc and microcalc.

The downside of the interface approach is, when you modify the grammar, you have to adapt any implementation you've already written. It's the same problem with ANTLR, and, to be fair, it's likely the same problem when a part of the code is in the grammar, like with Yacc/Bison.

To help with this, I made an option to generate a template of empty implementation (e.g. examples/microcalc/src/templates.txt). A simple diff of those templates shows what's changed, but perhaps there's a more elegant solution. It also helps write the initial implementation.

ANTLR allows to name the productions and put them in separate methods of the interface, but I'm not convinced it's always beneficial. That tools, being much more mature than mine, also offers other features like the ability to enable/disable rules and so on. It's worth a look at the documentation.

1

u/Commercial-Drawer881 Jul 12 '26

Thank you. I'll take a look.