pt::ast(3tcl)
π pt::ast β Abstract Syntax Tree Serialization π
okay so real talk: this is the manual page for pt::ast, which lives in section 3tcl of the Parser Tools docs. it's the AST-serialization bestie. let's go π¦
β¨ NAME β¨
pt::ast β Abstract Syntax Tree Serialization
πΈ SYNOPSIS πΈ
package require Tcl 8.5 9
package require pt::ast ?1.2?
::pt::ast verify serial ?canonvar?
::pt::ast verify-as-canonical serial
::pt::ast canonicalize serial
::pt::ast print serial
::pt::ast bottomup cmdprefix ast
cmdprefix ast
::pt::ast topdown cmdprefix pe
::pt::ast equal seriala serialb
::pt::ast new0 s loc ?child...?
::pt::ast new s start end ?child...?
π«Ά DESCRIPTION π«Ά
bestie. are you lost ? π are you having trouble understanding this document ? in that case pls pls pls go read the overview that the Introduction to Parser Tools hands you. that document is literally the entrypoint to the whole entire system that this package is a part of. it's giving front door energy.
this package gives you the commands to work with the serializations of abstract syntax trees as managed by the Parser Tools, and as specified in the section AST serialization format (yes, that one, the big one further down π).
this is a supporting package in the Core Layer of Parser Tools. lowkey iconic of it.
there's an image here in the original, named arch_core_support β pure vibes, no text, we move.
π API π
::pt::ast verify serial ?canonvar?
this command verifies that whatever's inside serial is a valid serialization of an abstract syntax tree, and it will throw an error if that is not the case. and what does it give you back when it's happy ? the empty string. that's it. that's the reward. π
if you pass the argument canonvar, it gets interpreted as the name of a variable in the calling context. that variable gets written to if and only if serial is a valid regular serialization. its value will be a boolean: True means the serialization is not only valid but also canonical (slay), and False gets written for a valid but non-canonical serialization.
for the full spec of serializations, go see the section AST serialization format.
::pt::ast verify-as-canonical serial
this one verifies that the content of serial is a valid canonical serialization of an abstract syntax tree, and it will throw an error if that's not the case. result is the empty string. same deal, no notes.
for the specification of canonical serializations, see the section AST serialization format.
::pt::ast canonicalize serial
this command assumes the content of serial is a valid regular serialization of an abstract syntax, and it will throw an error if that's not the case. no cap, it will.
then it converts the input into the canonical serialization of the contained tree and returns that as its result. and if the input is already canonical ? it just hands it straight back to you unchanged. queen behavior.
for the spec of regular and canonical serializations, see the section AST serialization format.
::pt::ast print serial
this command assumes the argument serial holds a valid serialization of an abstract syntax tree, and it returns a string containing that tree in a human readable form. pretty printer energy β¨
the exact format of that human readable form is not specified and you cannot rely on it for parsing or for any other machine-based activity. it's for your eyeballs only, bestie. do not parse it.
for the specification of serializations, see the section AST serialization format.
::pt::ast bottomup cmdprefix ast
this command walks the abstract syntax tree ast from the bottom up to the root, invoking the command prefix cmdprefix for each node. this implies that the children of a node N get handled before N itself. leaves first, main character last. π±β‘οΈπ
the command prefix has the signature
cmdprefix ast
i.e. it's invoked with the ast node the walk is currently sitting at.
the result returned by the command prefix replaces ast in the node it was a child of β so yes, you can transform the tree as you go. glow-up on the way up. π
this also means that for all inner nodes, the contents of the children elements are the results of the command prefix invoked for the children of that node.
::pt::ast topdown cmdprefix pe
this command walks the abstract syntax tree ast from the root down to the leaves, invoking the command prefix cmdprefix for each node. this implies that the children of a node N get handled after N. top first, then everybody below. π
the command prefix has the same signature as for bottomup, see above.
the result returned by the command prefix is ignored. no takesies, no givebacks. π
::pt::ast equal seriala serialb
this command tests the two abstract syntax trees seriala and serialb for structural equality. the result is a boolean value: it's set to true if the trees are identical, and false otherwise.
string equality is usable only if we can assume that the two trees are pure Tcl lists. otherwise, do not come for string equality, ok ?
::pt::ast new0 s loc ?child...?
this command constructs the ast for a nonterminal node referring to the symbol s at position loc in the input, plus the set of child nodes child ..., from left to right. those children may be empty. the constructed node is returned as the result of the command.
the end position is loc-1, i.e. one character before the start. this type of node is possible for rules containing optional parts. yes it's a weird little gremlin node, that's the point. π
::pt::ast new s start end ?child...?
this command constructs the ast for a nonterminal node referring to the symbol s covering the range of positions start to end in the input, plus the set of child nodes child ..., from left to right. those children may be empty. the constructed node is returned as the result of the command.
π AST SERIALIZATION FORMAT π
here we specify the format used by the Parser Tools to serialize Abstract Syntax Trees (ASTs) as immutable values for transport, comparison, etc. π§
each node in an AST represents a nonterminal symbol of a grammar, and the range of tokens/characters in the input covered by it. ASTs do not contain terminal symbols, i.e. tokens/characters. those can be recovered from the input given a symbol's location.
we distinguish between regular and canonical serializations. and while a tree may have more than one regular serialization, only exactly one of them will be canonical. one of one. periodt. π
Regular serialization
- The serialization of any AST is the serialization of its root node.
- The serialization of any node is a Tcl list containing at least three elements.
- The first element is the name of the nonterminal symbol stored in the node.
- The second and third element are the locations of the first and last token in the token stream the node represents (covers).
- Locations are provided as non-negative integer offsets from the beginning of the token stream, with the first token found in the stream located at offset
0(zero). - The end location has to be equal to or larger than the start location.
- Locations are provided as non-negative integer offsets from the beginning of the token stream, with the first token found in the stream located at offset
- All elements after the first three represent the children of the node, which are themselves nodes. This means that the serializations of nodes without children, i.e. leaf nodes, have exactly three elements. The children are stored in the list with the leftmost child first, and the rightmost child last.
Canonical serialization
The canonical serialization of an abstract syntax tree has the format as specified in the previous item, and then additionally satisfies the constraints below, which make it unique among all the possible serializations of this tree.
- The string representation of the value is the canonical representation of a pure Tcl list. I.e. it does not contain superfluous whitespace.
EXAMPLE
assuming the parsing expression grammar below π
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;
and the input string
120+5
then a parser should deliver the abstract syntax tree below (except for whitespace)
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}
}
}
}
}
or, more graphical π³
.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
π BUGS, IDEAS, FEEDBACK π
this document, and the package it describes, will undoubtedly contain bugs and other problems. ngl, it's basically guaranteed. π
pls report such things in the category pt of the Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist]. and pls also report any ideas for enhancements you may have for either the package and/or the documentation. we want the ideas.
when proposing code changes, pls 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.
ok bestie that's part 1 π we are not done, more is coming β don't go anywhere π
π KEYWORDS π
okay bestie, here's the vibe-tag cloud for this whole page β every single one, no skipping, because they're all part of the lore π
- 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
like, it's giving "everything you'd ever need to search for when you're deep in a parsing rabbit hole" π³οΈβ¨ no cap.
π CATEGORY π
Parsing and Grammars
that's the shelf it lives on. iconic placement honestly π
π COPYRIGHT π
Copyright (c) 2009 Andreas Kupries <andreas.kupries@users.sourceforge.net>
shoutout to Andreas, the actual main character, dropping this in 2009 and we're still out here reading it fr fr π«Ά
and that's a wrap on the whole page, bestie!! you made it all the way through ππ
this came to you from tcllib, version 1.2, and it's the manpage for pt::ast(3tcl) β go forth and parse something gorgeous πΈπ¦β
π¦ 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