r/Compilers • u/Commercial-Drawer881 • 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
2
u/Key_River7180 Jul 07 '26
So like, it mixes the lexer and parser? I'll stick with lex and yacc for now. I really don't like how it doesn't resemble EBNF at all.
1
u/Commercial-Drawer881 Jul 08 '26
based on the json specification: https://datatracker.ietf.org/doc/html/rfc8259
I wrote a JSON parser in the language:
Based on earlier feedback, I do agree with some challenges in the current syntax:
I am reviewing potential improvements to the syntax.
1
u/Commercial-Drawer881 Jul 10 '26
Latest update
1
u/Commercial-Drawer881 Aug 08 '26 edited Aug 09 '26
After doing some research and experimenting, I have settled on mostly on the following syntax design:
1
u/GiveMe30Dollars Jul 07 '26
Hi there, have been thinking of trying my hand at a parser generator too! Just a few things that stood out to me:
raccmay be of interest as it sidesteps this entirely by proc-macro expanding to Rust code, though even full-blown parser generators likebisontend to use a syntax that can be transpiled to Rust relatively easily. Do you have a specific host language in mind?Overall, I find the syntax pretty easy to understand and it serves its purpose well. You could definitely use it as a specification format as is.
1
u/Blueglyph Jul 09 '26 edited Jul 09 '26
There already exist parser generators, so I suppose you're either trying to solve a specific problem with the existing ones or making one for a language that isn't covered yet, but I don't see which it is.
Personally, I've always found that mixing the target source code with the lexicon and the grammar made it difficult to read, not to mention the problems with refactoring tools and the inability to share the grammar between several parsers, but it might not be a problem for others. I'm more in favour of a tool that generates a listener, like ANTLR does. So an interface, a virtual class or a trait, depending on the target language, that the user can implement and that the parser will call back each time a nonterminal is derived (and optionally before, too, if you're generating a top-down parser). Same with the initializations / exits you've put in your grammar, though I'm not sure why the user should deal with those.
I'm not sure that the one/two pass is something that the parser generator—or the parser itself—should be aware of, but perhaps I'm misunderstanding what exactly it generates. Ideally, the parser should do the minimum and let the user build an AST or whatever they need to do. If another pass is needed after the AST is built, for instance, the parser isn't required any more. If an initial pass is required to gather the declarations, which is quite typical, then the user can call the parser twice with two different objects implementing the interface.
As a counter-example, ANTLR builds the entire parse tree before calling the developer's callbacks; it allows them to walk into the whole structure, but it's often unnecessary and impactful on the performances. I often thought it was a drawback of that generator.
Same remark about the error messages. From experience, the user will need more than formatting the messages; they'll need to act on them depending on the situation. The best is to let them optionally intercept any log output by the lexer and the parser and return a decision (resynchronize, abort, ...) or possibly let them call recovery methods.
I think the definition of the tokens and the nonterminals is perhaps a little too overloaded. A simple regular-expression-like expression for each token should be enough, with the optional actions associated with it (change of mode for complicated syntaxes or island sub-languages, output channels, discarding of the comments and spaces, etc.).
Wouldn't this be clearer?
Less syntactic noise, and a more familiar syntax in both the lexicon and the grammar.
For the rare cases where you need to deal with special syntaxes, like Python, it's easier to provide the user with a callback that emits a token based on the scanned text. That avoids a complicated syntax for all the other cases.
For example, if you catch a newline followed by a number of spaces, provide an action that creates an entry in the listener for that token:
and something like this, which defines the default behaviour and lets you redefine it. For Python, you'll want to count the number of spaces to issue INDENT / DEDENT tokens on top of the NEWLINE, so that your grammar can define what a compound statement is.
1
u/kendomino Jul 10 '26
Not sure I like your AST construction syntax. Take a look at Antlr3 tree rewrite operators for AST construction. Parr removed it based on "separation of concerns," but replaced it with target-specific parser listeners. Can't stand writing listeners and visitors in a target language. It's a hassle to translate into other languages, even with Claude. Threw out the baby with the bathwater. Also, I suggest you study the large corpus of Antlr4 grammars at https://github.com/antlr/grammars-v4, e.g., the C grammar. Most grammars for popular languages require some semantics for making the right choices in parsing!
1
u/Current-Scheme-9896 19d ago
I think separating lexical rules from parser rules is a good design choice. It makes the grammar easier to understand because each part has a specific responsibility. The lexical rules define how tokens are recognized, while the parser rules describe how those tokens can be combined into valid expressions. This separation also makes the grammar easier to maintain and extend