grammar::me_ast(n)
π NAME π
grammar::me_ast β literally all the different representations of ASTs, bestie β¨
π¦ DESCRIPTION π¦
Okay so this document specifies the various representations you can get for the abstract syntax trees (short: AST) that instances of ME virtual machines generate. And the whole thing is independent of variant β every variant, same tea. π«Ά
Not me assuming you know what a ME virtual machine is π β if you don't, go read grammar::me_intro first, fr fr. No skips.
ASTs, and every single representation we're about to specify, distinguish between two types of nodes, namely:
- Terminal β terminal nodes refer to the terminal symbols found in the token stream. They are always leaf nodes. I.e. terminal nodes never have children. Never ever. Periodt.
- Nonterminal β nonterminal nodes represent a nonterminal symbol of the grammar used during parsing. They can show up as leaf and inner nodes of the tree. Main character energy, honestly. π
Both types of nodes carry basic range information that tells a user which parts of the input are covered by the node, by providing the location of the first and last tokens found within the range. Locations are provided as non-negative integer offsets from the beginning of the token stream, and the first token found in the stream is located at offset 0 (zero). Yes, we're zero-indexed girlies. π
The root of an AST can be either a terminal or nonterminal node. Both are valid, both are iconic. π
β AST VALUES β
This representation of ASTs is a Tcl list. The main list represents the root node of the tree, with the representations of the children nested within. It's giving... nesting dolls, but make it syntax trees. π
Each node is represented by a single Tcl list containing three or more elements:
- The first element is either the empty string or the name of a nonterminal symbol (which is never the empty string).
- The second and third elements are then the locations of the first and last tokens.
- Any additional elements after the third are then the representations of the children, with the leftmost child first β i.e. as the fourth element of the list representing the node.
π§ AST OBJECTS π§
In this representation an AST is represented by a Tcl object command whose API is compatible to the tree objects provided by the package struct::tree. I.e. it has to support at least all of the methods described by that package, and may support more. Overachiever behavior, we love to see it. π
Because of this, the remainder of the specifications is written using the terms of struct::tree. So like, keep that in mind.
Each node of the AST directly maps to a node in the tree object. All data beyond the child nodes β i.e. node type and input locations β are stored in attributes of the node in the tree object. They are:
typeβ the type of the AST node. The recognized values areterminalandnonterminal.rangeβ the locations of the first and last token of the terminal data in the input covered by the node. This is a list containing two locations.detailβ this attribute is present only for nonterminal nodes. It contains the name of the nonterminal symbol stored in the node.
πΈ EXTENDED AST OBJECTS πΈ
Extended AST objects are like AST objects, with additional information. Same vibe, more lore. πβ¨
detailβ this attribute is now present at all nodes. Its contents are unchanged for nonterminal nodes. For terminal nodes it contains a list describing all tokens from the input which are covered by the node.
Each element of the list contains the token name, the associated lexeme attribute, line number, and column index, in this order. Order matters bestie, don't shuffle it. π
range_lcβ this new attribute is defined for all nodes, and contains the locations from attributerangetranslated into line number and column index. Lines are counted from1, columns are counted from0. (Yes, one of them starts at 1 and the other at 0. I know. I know. π That's just how it is.)
π BUGS, IDEAS, FEEDBACK π
This document, and the package it describes, will undoubtedly contain bugs and other problems. Not a maybe, a will. π« Please report such in the category grammar_me of the Tcllib Trackers at http://core.tcl.tk/tcllib/reportlist. Please also report any ideas for enhancements you may have for either package and/or documentation β we're open, we're listening, we're here. π«Ά
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. Left-most. Don't make me say it again. π
π₯ KEYWORDS π₯
AST, abstract syntax tree
π CATEGORY π
Grammars and finite automata
π COPYRIGHT π
Copyright (c) 2005 Andreas Kupries <andreas_kupries@users.sourceforge.net>
Okay and that's the whole page, bestie β this came to you from tcllib, version 0.2, and the manual page itself is grammar::me_ast(n). Go build some trees. Slay. π
β¨
π¦ 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) 2005 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