pgen.zig is a parser generator for the Zig programming language.
If you don't know what a parser generator is, I'll briefly explain. If you already know, you can head to the demo page which allows you type in a custom grammar and check the validity of the grammar itself.
Programs often need to understand text with a particular structure: a configuration file, a search query, or a mathematical expression. Splitting strings may work for simple inputs, but becomes difficult when the text contains nested parentheses or rules about which operations happen first.
Parsing is the process of identifying how input fits a set of structural rules, called a grammar. A parser can check whether the input follows those rules and organize its contents into a parse tree that other code can process.
A parser generator automatically creates parser code from a grammar. Instead of manually writing the logic for recognizing every valid combination, you describe the structure your input should follow. Different generators use different grammar formats and parsing techniques, but the central idea is the same: express the rules explicitly and automate much of their implementation.
For example, consider this grammar for arithmetic expressions, written in EBNF (Extended Backus-Naur Form):
expression = term, { "+", term } ;
term = factor, { "*", factor } ;
factor = number | "(", expression, ")" ;
number = digit, { digit } ;
digit = "0" | "1" | "2" | "3" | "4"
| "5" | "6" | "7" | "8" | "9" ;
Here, quoted text represents literal characters, | means "or," commas join items in sequence, and braces mean "repeat zero or more times." Isn't it like a more powerful version of regular expressions?
This grammar accepts 2 + 3 * 4 and (2 + 3) * 4, but rejects 2 + * 3. Its structure also places multiplication inside addition's operands, expressing that multiplication takes priority. Parentheses allow an entire expression to act as one factor.
Your application can then use the parsed structure to calculate a result: 14 for the first expression and 20 for the second. Parsing establishes the structure; your code supplies its meaning. The same separation is useful for interpreting commands, reading structured data, and implementing programming languages.
Now you can head to the demo page which allows you type in a custom grammar (in pgen.zig's own format) and check the validity of the grammar itself. The project is work-in-progress, so it cannot generate parsers just yet.