pt::pegrammar(n)
π pt::pegrammar β Introduction to Parsing Expression Grammars π
okay bestie, so this is pt::pegrammar from the Parser Tools family, and it's basically the β¨lore dropβ¨ for Parsing Expression Grammars. pull up a chair, grab a boba, we're doing theory today π§
πΈ SYNOPSIS πΈ
package require Tcl 8.5 9
β¨ DESCRIPTION β¨
Are you lost? π Is this document giving you a headache? Do you have trouble understanding it? girl, same, but if that's you, pls go read the overview in the Introduction to Parser Tools first. This doc here is the entrypoint to the whole system that this package is part of. door = open πͺβ¨
Welcome to the intro to Parsing Expression Grammars (short: PEG), the formalism the Parser Tools use. Now β heads up β this assumes you already know a lil something about parsing theory, i.e. Context-Free Grammars (short: CFG), languages, and the whole vocab squad: LL(k), LR(k), terminal and nonterminal symbols, etc. We are not recapping that. We're not re-explaining useful, reachable, (left/right) recursive, nullable, first/last/follow sets, none of it π. If you need that background, scroll to the References at the end, they got you covered with places and books. π
PEGs are formally very similar to CFGs β terminal and nonterminal symbols, start symbol, and rules defining the structure of each nonterminal symbol. The main difference is in the choice (sic!) of choice operators. Where CFGs use an unordered choice to represent alternatives, PEGs use prioritized choice. Which is a fancy way of saying: the parser has to try the first alternative first, and can try the other alternatives only if it fails for the first, and so on. one at a time bestie, no skipping the line π¦
On the CFG side this gives rise to LL(k) and LR(k) for making the choice deterministic with a bounded lookahead of k terminal symbols, where LL is in essence topdown aka recursive descent parsing, and LR is bottomup aka shift reduce parsing.
On the PEG side, we can parse input with recursive descent and backtracking of failed choices β and that backtracking thing amounts to unlimited lookahead π. By additionally recording the success or failure of nonterminals at the specific locations they were tried at, and reusing this info after backtracking, we can avoid the exponential blowup of running time that's usually associated with backtracking, and keep the parsing linear. The memory requirements are of course higher thanks to that cache, because we are literally trading space for time. π no free lunch, periodt.
This is the basic concept behind packrat parsers. πβ¨ iconic name, honestly.
A limitation pure PEGs share with LL(k) CFGs is that left-recursive grammars cannot be parsed, and the associated recursive descent parser just enters an infinite recursion ππ΅π«. This limitation is usually overcome by extending pure PEGs with explicit operators to specify repetition β zero or more, and one or more β or, formally spoken, for the kleene closure and positive kleene closure. That is what the Parser Tools are doing. slay.
Another extension, specific to Parser Tools, is a set of operators which map more or less directly to various character classes built into Tcl, i.e. the classes reachable via string is. π
The rest of this document is: the formal definition of PEGs for the mathematically inclined, and an appendix listing references to places with more info on PEGs specifically, and parsing in general. π¦
π FORMAL DEFINITION π
For the mathematically inclined π, a Parsing Expression Grammar is a 4-tuple (VN,VT,R,eS) where:
VNis a set of nonterminal symbols,VTis a set of terminal symbols,Ris a finite set of rules, where each rule is a pair(A,e),AinVN, andea parsing expression.eSis a parsing expression, the start expression.
Further constraints are:
- The intersection of
VNandVTis empty. - For all
AinVTexists exactly one pair(A,e)inR. In other words,Ris a function from nonterminal symbols to parsing expressions.
Parsing expressions are inductively defined via:
- The empty string (epsilon) is a parsing expression.
- A terminal symbol
ais a parsing expression. - A nonterminal symbol
Ais a parsing expression. e1e2is a parsing expression for parsing expressionse1and2. This is called sequence.e1/e2is a parsing expression for parsing expressionse1and2. This is called ordered choice.e*is a parsing expression for parsing expressione. This is called zero-or-more repetitions, also known as kleene closure.e+is a parsing expression for parsing expressione. This is called one-or-more repetitions, also known as positive kleene closure.!eis a parsing expression for parsing expressione1. This is called a not lookahead predicate.&eis a parsing expression for parsing expressione1. This is called an and lookahead predicate.
PEGs are used to define a grammatical structure for streams of symbols over VT. They are a modern phrasing of older formalisms invented by Alexander Birham. These formalisms were called TS (TMG recognition scheme), and gTS (generalized TS). Later they were renamed to TPDL (Top-Down Parsing Languages) and gTPDL (generalized TPDL). history lesson unlocked ποΈβ¨
They can be easily implemented by recursive descent parsers with backtracking. This makes them relatives of LL(k) Context-Free Grammars. family reunion π«Ά
π REFERENCES π
- The Packrat Parsing and Parsing Expression Grammars Page [
http://www.pdos.lcs.mit.edu/~baford/packrat/], by Bryan Ford, Massachusetts Institute of Technology. This is the main entry page to PEGs, and their realization through Packrat Parsers. π - [
http://en.wikipedia.org/wiki/Parsing_expression_grammar] β Wikipedia's entry about Parsing Expression Grammars. - Parsing Techniques - A Practical Guide [
http://www.cs.vu.nl/~dick/PTAPG.html], an online book offering a clear, accessible, and thorough discussion of many different parsing techniques with their interrelations and applicabilities, including error recovery techniques. π - Compilers and Compiler Generators [
http://scifac.ru.ac.za/compilers/], an online book using CoCo/R, a generator for recursive descent parsers.
π BUGS, IDEAS, FEEDBACK π
This document, and the package it describes, will undoubtedly contain bugs and other problems. π not me admitting it but okay, honesty is hot. Please report such in the category pt of the Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist]. Please also report any ideas for enhancements you may have for either package and/or documentation. we want your thoughts bestie π
When proposing code changes, please provide unified diffs, i.e the output of diff -u.
Note further that attachments are strongly preferred over inlined patches. Attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar. π±οΈβ¨
π KEYWORDS π
EBNF, LL(k), PEG, TDPL, context-free languages, expression, grammar, matching, parser, parsing expression, parsing expression grammar, push down automaton, recursive descent, state, top-down parsing languages, transducer
π CATEGORY π
Parsing and Grammars
π COPYRIGHT π
Copyright (c) 2009 Andreas Kupries <andreas_kupries@users.sourceforge.net>
and that's the whole thing, bestie π this was pt::pegrammar(n) from tcllib 1.1 β go forth and parse, you absolute main character π¦π
β¨
π¦ where this page comes from
- fedora 43: tcllib 2.0-3.fc43 Β· TCL
- fedora 44: tcllib 2.0-4.fc44 Β· TCL
- nixos 26.05: tcllib 2.0
- nixpkgs unstable: tcllib 2.0
- Copyright (c) 2009 Andreas Kupries <andreas_kupries@users\&.sourceforge\&.net>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms