pt::ast(n) | Parser Tools | pt::ast(n) |
pt::ast - Abstract Syntax Tree Serialization
package require Tcl 8.5
package require pt::ast ?1.1?
::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 terminal loc
::pt::ast nonterminal s start end ?child...?
Are you lost ? Do you have trouble understanding this document ? In that case please read the overview provided by the Introduction to Parser Tools. This document is the entrypoint to the whole system the current package is a part of.
This package provides commands to work with the serializations of abstract syntax trees as managed by the Parser Tools, and specified in section AST serialization format.
This is a supporting package in the Core Layer of Parser Tools.
IMAGE: arch_core_support
If the argument canonvar is specified it is interpreted as the name of a variable in the calling context. This variable will be written to if and only if serial is a valid regular serialization. Its value will be a boolean, with True indicating that the serialization is not only valid, but also canonical. False will be written for a valid, but non-canonical serialization.
For the specification of serializations see the section AST serialization format.
For the specification of canonical serializations see the section AST serialization format.
It will then convert the input into the canonical serialization of the contained tree and return it as its result. If the input is already canonical it will be returned unchanged.
For the specification of regular and canonical serializations see the section AST serialization format.
The exact format of this form is not specified and cannot be relied on for parsing or other machine-based activities.
For the specification of serializations see the section AST serialization format.
The command prefix has the signature
The result returned by the command prefix replaces ast in the node it was a child of, allowing transformations of the tree.
This also means that for all inner node the contents of the children elements are the results of the command prefix invoked for the children of this node.
The command prefix has the same signature as for bottomup, see above.
The result returned by the command prefix is ignored.
String equality is usable only if we can assume that the two trees are pure Tcl lists.
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. These can be recovered from the input given a symbol's location.
We distinguish between regular and canonical serializations. While a tree may have more than one regular serialization only exactly one of them will be canonical.
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 <- '(' Expression ')' / (Factor (MulOp Factor)*) ;
MulOp <- '*' / '/' ;
Factor <- Term (AddOp Term)* ;
AddOp <- '+'/'-' ;
Term <- Number ; END;
and the input string
then a parser should deliver the abstract syntax tree below (except for whitespace)
120+5
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
IMAGE: expr_ast
This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category pt of the Tcllib SF Trackers [http://sourceforge.net/tracker/?group_id=12883]. Please also report any ideas for enhancements you may have for either package and/or documentation.
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
Parsing and Grammars
Copyright (c) 2009 Andreas Kupries <andreas_kupries@users.sourceforge.net>
1.1 | pt |