pt::pgen(3tcl)
π pt::pgen β the Parser Generator, bestie edition π
okay so you found the manual page for pt::pgen from the tcllib crew (it lives in section 3tcl of the manual, iykyk) β and honestly? it's giving machine that writes parsers for you. let's get into it π¦
β¨ SYNOPSIS β¨
package require Tcl 8.5 9
package require pt::pgen ?1.2?
::pt::pgen inputformat text resultformat ?options...?
like. three lines. and one of them is the whole entire vibe. slay π
πΈ DESCRIPTION πΈ
Are you lost? Are you, like, actually struggling to understand this document? no shame, bestie, it happens π in that case please go read the overview provided by the Introduction to Parser Tools. that document is literally the entrypoint to the whole system that this package is a part of, so start there fr fr.
This package provides a command implementing a parser generator that takes parsing expression grammars as input. yes. a whole generator. for parsers. iconic behaviour honestly π
It is the implementation of method generate of pt, the Parser Tools Application.
Which means β and this is the important part β the intended audience of this document are people wishing to modify and/or extend this part of pt's functionality. If you're just a user of pt, you are hereby refered to the application's manpage, i.e. Parser Tools Application. not me gatekeeping, just pointing you to the right doc π«Ά
It resides in the User Package Layer of Parser Tools.
IMAGE: arch_user_pkg
π API π
π ::pt::pgen inputformat text resultformat ?options...?
This command takes the parsing expression grammar in text (in the format specified by inputformat), and returns the same grammar in the format resultformat as the result of the command. yes bestie, it's a glow-up machine for grammars β¨
input formats (the way in π¦)
The two known input formats are peg and json. Introductions to them β including their formal specifications β can be found in the PEG Language Tutorial and The JSON Grammar Exchange Format. The packages used to parse these formats are:
pegβpt::peg::from::pegjsonβpt::peg::from::json
output formats (the way out π)
On the output side, the known formats, and the packages used to generate them, are:
cβpt::peg::to::cparamcontainerβpt::peg::to::containercritclβpt::peg::to::cparam+pt::cparam::configuration::critcljsonβpt::peg::to::jsonooβpt::peg::to::tclparam+pt::tclparam::configuration::tcloopegβpt::peg::to::pegsnitβpt::peg::to::tclparam+pt::tclparam::configuration::snit
The options supported by each of these formats are documented with their respective packages. so like, go read those pages for the flag drama, this one isn't holding them for you π
π EXAMPLE π
In this section we are working a complete example, starting with a PEG grammar and ending with running the parser generated from it over some input, following the outline shown in the figure below:
IMAGE: flow
Our grammar, assumed to the stored in the file "calculator.peg", is
PEG calculator (Expression)
Digit <- '0'/'1'/'2'/'3'/'4'/'5'/'6'/'7'/'8'/'9' ;
Sign <- '-' / '+' ;
Number <- Sign? Digit+ ;
Expression <- Term (AddOp Term)* ;
MulOp <- '*' / '/' ;
Term <- Factor (MulOp Factor)* ;
AddOp <- '+'/'-' ;
Factor <- '(' Expression ')' / Number ;
END;
From this we create a snit-based parser using the script "gen"
package require Tcl 8.5
package require fileutil
package require pt::pgen
lassign $argv name
set grammar [fileutil::cat $name.peg]
set pclass [pt::pgen peg $gr snit -class $name -file $name.peg -name $name]
fileutil::writeFile $name.tcl $pclass
exit 0
calling it like
tclsh8.5 gen calculator
which leaves us with the parser package and class written to the file "calculator.tcl". Assuming that this package is then properly installed in a place where Tcl can find it we can now use this class via a script like
package require calculator
lassign $argv input
set channel [open $input r]
set parser [calculator]
set ast [$parser parse $channel]
$parser destroy
close $channel
... now process the returned abstract syntax tree ...
where the abstract syntax tree stored in the variable will look like
set ast {Expression 0 4
{Factor 0 4
{Term 0 2
{Number 0 2
{Digit 0 0}
{Digit 1 1}
{Digit 2 2}
}
}
{AddOp 3 3}
{Term 4 4
{Number 4 4
{Digit 4 4}
}
}
}
}
assuming that the input file and channel contained the text
120+5
A more graphical representation of the tree would be
.nf +- Digit 0 0 | 1 | | +- Term 0 2 --- Number 0 2 -+- Digit
1 1 | 2 | | | |
+- Digit 2 2 | 0 | | Expression 0
4 --- Factor 0 4 -+----------------------------- AddOp 3 3 | + |
| +- Term 4 4 --- Number 4 4 --- Digit 4 4 | 5 .fi
Regardless, at this point it is the user's responsibility to work with the tree to reach whatever goal she desires. I.e. analyze it, transform it, etc. The package pt::ast should be of help here, providing commands to walk such ASTs structures in various ways. she's got you π«Ά
One important thing to note is that the parsers used here return a data structure representing the structure of the input per the grammar underlying the parser. There are no callbacks during the parsing process, i.e. no parsing actions, as most other parsers will have. no side quests, just tree π³
Going back to the last snippet of code, the execution of the parser for some input β note how the parser instance follows the specified Parser API. periodt.
π BUGS, IDEAS, FEEDBACK π
This document, and the package it describes, will undoubtedly contain bugs and other problems. like, obviously, it's software π 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 love ideas here, no cap.
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. (yes, the left-most one. the icon. you got this π )
β 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 page, bestie!! π brought to you by tcllib, version 1.2, living its best life as pt::pgen(3tcl). go generate some parsers, you absolute icon π¦β¨
π¦ 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