pt::pegrammar(3tcl)
π pt::pegrammar β the Parser Tools manual, PEG edition π
okay bestie, this is a page from Parser Tools (the manual is pt::pegrammar, section 3tcl), and it's literally the intro to Parsing Expression Grammars. grab a snack, we're doing theory but make it cute π
β¨ SYNOPSIS β¨
package require Tcl 8.5 9
that's it. that's the whole synopsis. iconic behavior honestly π
πΈ DESCRIPTION πΈ
Are you lost? Do you have trouble understanding this document? π fr if that's you, go read the overview provided by the Introduction to Parser Tools first β that one is the entrypoint to the whole system this package is part of.
So. Welcome to the intro to Parsing Expression Grammars (short: PEG), the formalism the Parser Tools use. Heads up though: this page assumes you already have a basic knowledge of parsing theory, like Context-Free Grammars (short: CFG), languages, and all the associated terms β LL(k), LR(k), terminal and nonterminal symbols, etc. We are not recapping those basic definitions or terms like useful, reachable, (left/right) recursive, nullable, first/last/follow sets, etc. Nope! If you need that background, go check the References at the end of this page, they'll point you at places and books that have it. π
PEGs are formally very similar to CFGs β terminal and nonterminal symbols, start symbol, and rules defining the structure of each nonterminal symbol, all of it. The main difference lies 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 that a parser has to try the first alternative first and can try the other alternatives only if it fails for the first, and so on.
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 the backtracking part amounts to unlimited lookahead. β¨ By additionally recording the success or failure of nonterminals at the specific locations they were tried at, and reusing this information after backtracking, we can avoid the exponential blowup of running time usually associated with backtracking and keep the parsing linear. The memory requirements are of course higher because of this cache β we're trading space for time, bestie, that's the deal. π€
This is the basic concept behind packrat parsers.
A limitation pure PEGs share with LL(k) CFGs is that left-recursive grammars cannot be parsed, with the associated recursive descent parser entering 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. This is what the Parser Tools are doing. π«Ά
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 remainder of this document consists of the formal definition of PEGs for the mathematically inclined, plus an appendix listing references to places with more information 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).
They can be easily implemented by recursive descent parsers with backtracking. This makes them relatives of LL(k) Context-Free Grammars. π
π 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 usingCoCo/R, a generator for recursive descent parsers.
π BUGS, IDEAS, FEEDBACK π
ngl, this document and the package it describes will undoubtedly contain bugs and other problems. π 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 the package and/or the documentation. We want them!
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 vibe check, bestie β this page came from tcllib, version 1.1, and it's serving pt::pegrammar(3tcl). go parse something gorgeous, you absolute main character ππ¦β¨
π¦ where this page comes from
- debian trixie: tcllib 2.0+dfsg-4 Β· Tcl
- ubuntu questing: tcllib 2.0+dfsg-4
- ubuntu resolute: tcllib 2.0+dfsg-5 Β· Tcl
- ubuntu stonking: tcllib 2.0+dfsg-6 Β· Tcl
- 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