pt::param(n)

๐Ÿ“ฆ
version 2.0 ยท 2 versions
, in fedora 43, fedora 44, nixos 26.05, nixpkgs unstable

๐Ÿฆ‹ pt::param(n) โ€” Parser Tools ๐Ÿฆ‹

okay bestie, welcome to pt::param(n), aka the PackRat Machine Specification. This is part 1, and we're about to vibe-check a whole virtual machine. No cap, every state and instruction is staying. ๐Ÿ’–

โœจ NAME โœจ

pt::param โ€” PackRat Machine Specification


๐ŸŒธ SYNOPSIS ๐ŸŒธ

package require Tcl 8.5 9

๐Ÿ’… DESCRIPTION ๐Ÿ’…

Are you lost? Is this doc giving you trouble? Then bestie, go read the overview in the Introduction to Parser Tools. This page is the entrypoint to the whole system this package lives in.

Welcome to the PackRat Machine (short: PARAM) โ€” a virtual machine built to support recursive descent parsers, especially packrat parsers. It's lowkey iconic: it caches and reuses partial results, caches the input it encounters, and can backtrack in both input and AST creation. The way it just does all that? Obsessed.

This document lays out the machine's architectural state and its instruction set.


๐Ÿ‘‘ ARCHITECTURAL STATE ๐Ÿ‘‘

Any PARAM implementation has to manage at least the following state:

Input (IN)

This is the channel we pull characters from when we need to process them.

The instructions in Input Handling are the ones that use and modify this part of the machine's state.

Current Character (CC)

This is the character from the input that's currently getting tested against all its possible alternatives.

The instructions in Character Processing are the ones that use and modify this part of the machine's state.

Current Location (CL)

This is where the current character sits in the input, measured as an offset from the start of the input. Character offsets start at 0.

The instructions in Character Processing, Location Handling, and Nonterminal Execution are the ones that use and modify this part of the machine's state.

Location Stack (LS)

A stack of input locations, saved so we can backtrack later.

The instructions in Character Processing, Location Handling, and Nonterminal Execution are the ones that use and modify this part of the machine's state.

Status (ST)

This is the result of the last attempt to test the input โ€” either success or failure.

The instructions in Status Control, Character Processing, and Nonterminal Execution are the ones that use and modify this part of the machine's state.

Semantic Value (SV)

The current semantic value. It's either empty, or it's a node for an AST built from the input.

The instructions in Value Construction, and AST Construction are the ones that use and modify this part of the machine's state.

AST Reduction Stack (ARS)

A stack of partial ASTs that get built while nonterminal symbols are being processed.

The instructions in Value Construction, and AST Construction are the ones that use and modify this part of the machine's state.

AST Stack (AS)

A stack of reduction stacks, saved so we can backtrack if we need to.

The instructions in Value Construction, and AST Construction are the ones that use and modify this part of the machine's state.

Error Status (ER)

What the machine currently knows about errors. It's either empty, or it's a pair: a location in the input plus the set of messages for that location.

Heads up: this part of the state can be set even when the last test of the current character succeeded. Like, the *-operator (matching a sub-expression zero or more times) in a PEG is always successful, even if it runs into a problem further in the input and has to backtrack. Those problems can't just be forgotten when parsing continues.

The instructions in Error Handling, Character Processing, and Nonterminal Execution are the ones that use and modify this part of the machine's state.

Error Stack (ES)

A stack of error stati, saved for backtracking. It lets the machine merge current and older error stati when it backtracks in choices after a failed match.

The instructions in Error Handling, Character Processing, and Nonterminal Execution are the ones that use and modify this part of the machine's state.

Nonterminal Cache (NC)

A cache of machine states, keyed by pairs of nonterminal symbol name and input location. Each pair (N, L) gets a 4-tuple: the values to use for CL, ST, SV, and ER after nonterminal N was parsed starting from location L. It's a performance boost for backtracking parsers, so they can skip expensive reparsing of complex nonterminal symbols if they've already been seen at a given location.

The key location is where the machine started trying to match the named nonterminal symbol, and the location inside the saved 4-tuple is where the machine ended up after the attempt finished โ€” doesn't matter if the attempt succeeded or not.

The instructions in Nonterminal Execution are the ones that use and modify this part of the machine's state.

Terminal Cache (TC)

A cache of characters read from IN, with their location in IN as a pair of line and column, keyed by the location in IN โ€” this time as a character offset from the beginning of IN. It's a performance aid for backtracking parsers: it avoids a possibly expensive reread of characters from IN, and it can even enable backtracking when IN isn't randomly seekable, i.e.

The instructions in Input Handling are the ones that use and modify this part of the machine's state.


๐Ÿ”ฅ INSTRUCTION SET ๐Ÿ”ฅ

Now that the machine's architectural state is on the table, we can talk about the instruction set that operates on it โ€” every PARAM realization has to implement these. There are 37 instructions, grouped roughly by the state they influence and/or query while they run.

๐Ÿงš INPUT HANDLING ๐Ÿงš

These instructions mostly touch IN, pulling characters into the machine for processing.

input_next msg

Reads the next character from IN โ€” that's the character after CL. If it works, that character becomes CC, CL moves forward by one, ES gets cleared, and the operation is marked as a success in ST.

If the next character isn't already known to TC, the operation might read it from IN. When that works, the new character gets stored in TC along with its location (line, column), and everything else behaves like above. Later reads from the same location โ€” which can happen because of backtracking โ€” will then be satisfied from TC instead of IN.

If we hit the end of IN instead, the operation is marked as failed in ST, CL stays exactly where it was, and the pair of CL and msg becomes the new ES.

๐Ÿ“ CHARACTER PROCESSING ๐Ÿ“

These instructions mostly access CC, testing it against character classes, ranges, and individual characters.

test_alnum

This instruction implements the special PE operator "alnum". It checks whether CC falls into the character class of the same name or not.

Both success and failure get recorded straight into ST. On success, ES also gets cleared. On failure, the pair of CL and the expected input (encoded as a leaf parsing expression) becomes the new ES, and then CL rewinds by one character โ€” that gets the machine ready for another parse attempt by a possible alternative.

test_alpha

This instruction implements the special PE operator "alpha". It checks whether CC falls into the character class of the same name or not.

Both success and failure get recorded straight into ST. On success, ES also gets cleared. On failure, the pair of CL and the expected input (encoded as a leaf parsing expression) becomes the new ES, and then CL rewinds by one character โ€” that gets the machine ready for another parse attempt by a possible alternative.

test_ascii

This instruction implements the special PE operator "ascii". It checks whether CC falls into the character class of the same name or not.

Both success and failure get recorded straight into ST. On success, ES also gets cleared. On failure, the pair of CL and the expected input (encoded as a leaf parsing expression) becomes the new ES, and then CL rewinds by one character โ€” that gets the machine ready for another parse attempt by a possible alternative.

test_char char

This instruction implements the character matching operator, i.e. it checks whether CC is char.

Both success and failure get recorded straight into ST. On success, ES also gets cleared. On failure, the pair of CL and the expected input (encoded as a leaf parsing expression) becomes the new ES, and then CL rewinds by one character โ€” that gets the machine ready for another parse attempt by a possible alternative.

test_ddigit

This instruction implements the special PE operator "ddigit". It checks whether CC falls into the character class of the same name or not.

Both success and failure get recorded straight into ST. On success, ES also gets cleared. On failure, the pair of CL and the expected input (encoded as a leaf parsing expression) becomes the new ES, and then CL rewinds by one character โ€” that gets the machine ready for another parse attempt by a possible alternative.

test_digit

This instruction implements the special PE operator "digit". It checks whether CC falls into the character class of the same name or not.

Both success and failure get recorded straight into ST. On success, ES also gets cleared. On failure, the pair of CL and the expected input (encoded as a leaf parsing expression) becomes the new ES, and then CL rewinds by one character โ€” that gets the machine ready for another parse attempt by a possible alternative.

test_graph

This instruction implements the special PE operator "graph". It checks whether CC falls into the character class of the same name or not.

okay so picking right back up where we left off โ€” that whole success/failure thing? it lands directly in ST, no detours. โœจ and if it succeeds, ES gets cleared, cute. if it fails, the pair of CL and the expected input (encoded as a leaf parsing expression) becomes the new ES, and then CL gets rewound by one character โ€” which is the machine's way of going "okay bet, let's try again with a possible alternative." ๐ŸŽ€

test_lower

this one implements the special PE operator lower โ€” it checks whether CC falls into the character class of the same name, or not. that's it, that's the vibe check. ๐Ÿ’…

success and failure of the test are both recorded directly in ST. success further clears ES, whereas failure sets the pair of CL and expected input (encoded as a leaf parsing expression) as the new ES and then rewinds CL by one character, preparing the machine for another parse attempt by a possible alternative. ๐ŸŒธ

test_print

implements the special PE operator print, checking if CC falls into the character class of the same name, or not. printable bestie check, love that for us. ๐Ÿ–จ๏ธ

success and failure of the test are both recorded directly in ST. success further clears ES, whereas failure sets the pair of CL and expected input (encoded as a leaf parsing expression) as the new ES and then rewinds CL by one character, preparing the machine for another parse attempt by a possible alternative. โœจ

test_punct

implements the special PE operator punct โ€” is CC in that character class or not, that's the whole question. punctuation girlies rise ๐ŸŽ€

success and failure of the test are both recorded directly in ST. success further clears ES, whereas failure sets the pair of CL and expected input (encoded as a leaf parsing expression) as the new ES and then rewinds CL by one character, preparing the machine for another parse attempt by a possible alternative.

test_range chars chare

this instruction implements the range matching operator, i.e. it checks if CC falls into the interval of characters spanned up by the two characters from chars to chare, both inclusive. so like, chars โ‰ค CC โ‰ค chare, no cap. ๐ŸŒˆ

success and failure of the test are both recorded directly in ST. success further clears ES, whereas failure sets the pair of CL and expected input (encoded as a leaf parsing expression) as the new ES and then rewinds CL by one character, preparing the machine for another parse attempt by a possible alternative. ๐Ÿ”ฅ

test_space

implements the special PE operator space, which checks if CC falls into the character class of the same name, or not. yes bestie, whitespace counts as a character class, iconic behaviour honestly. ๐Ÿฆ‹

success and failure of the test are both recorded directly in ST. success further clears ES, whereas failure sets the pair of CL and expected input (encoded as a leaf parsing expression) as the new ES and then rewinds CL by one character, preparing the machine for another parse attempt by a possible alternative. ๐ŸŒธ

test_upper

implements the special PE operator upper, checking whether CC falls into the character class of the same name, or not. UPPERCASE ONLY, periodt. ๐Ÿ‘‘

success and failure of the test are both recorded directly in ST. success further clears ES, whereas failure sets the pair of CL and expected input (encoded as a leaf parsing expression) as the new ES and then rewinds CL by one character, preparing the machine for another parse attempt by a possible alternative. ๐Ÿ’œ

test_wordchar

implements the special PE operator wordchar, which checks if CC falls into the character class of the same name, or not. word characters stay winning fr fr. ๐Ÿซถ

success and failure of the test are both recorded directly in ST. success further clears ES, whereas failure sets the pair of CL and expected input (encoded as a leaf parsing expression) as the new ES and then rewinds CL by one character, preparing the machine for another parse attempt by a possible alternative. โœจ

test_xdigit

implements the special PE operator xdigit โ€” checks if CC falls into the character class of the same name, or not. hex digits, we love a base-16 moment. ๐ŸŒˆ

success and failure of the test are both recorded directly in ST. success further clears ES, whereas failure sets the pair of CL and expected input (encoded as a leaf parsing expression) as the new ES and then rewinds CL by one character, preparing the machine for another parse attempt by a possible alternative. ๐Ÿ’–


๐Ÿšจ ERROR HANDLING ๐Ÿšจ

the instructions in this section mainly access ER and ES. yeah, they're the drama-adjacent registers, we love them anyway. ๐Ÿ’€

error_clear

this instruction clears ER. just wipes it, gone, bye. ๐Ÿงš

error_push

this instruction makes a copy of ER and pushes it on ES. a lil souvenir of the error state, kept safe on the stack. ๐ŸŽ€

error_pop_merge

this instruction takes the topmost entry of ES and merges the error status it contains with ES, making the result the new ES.

the merge is governed by four rules, with the merge result:

  1. Empty if both states are empty.
  2. The non-empty state if only one of the two states is non-empty.
  3. The state with the larger location, if the two states specify different locations.
  4. The pair of the location shared by the two states, and the set-union of their messages for states at the same location.

like okay the merge is doing the most but it's also being extremely fair about it. โš–๏ธ

error_nonterminal symbol

this is a guarded instruction. it does nothing if either ES is empty, or if the location in ES is not just past the last location saved in LS. otherwise it sets the pair of that location and the nonterminal symbol as the new ES. ๐Ÿ™ˆ

Note: in the above, "just past" means "that location plus one", or also "the location of the next character after that location". yes bestie, they said it twice to be sure, and honestly? valid.

๐Ÿ’– STATUS CONTROL ๐Ÿ’–

the instructions in this section directly manipulate ST. no middlemen, no vibes, just straight up status edits. ๐Ÿ’…

status_ok

this instruction sets ST to true, recording a success. we love a win. ๐Ÿ†

status_fail

this instruction sets ST to false, recording a failure. it happens, we move. ๐Ÿ˜ญ

status_negate

this instruction negates ST, turning a failure into a success and vice versa. flip the narrative, main character energy. ๐Ÿ‘‘


๐Ÿ“ LOCATION HANDLING ๐Ÿ“

the instructions in this section access CL and LS. yes, the position girlies. ๐Ÿฆ‹

loc_push

this instruction makes a copy of CL and pushes it on LS. save point, bestie. ๐Ÿ’พ

loc_pop_discard

this instruction pops the last saved location from LS. and then it's justโ€ฆ gone. no takebacks. ๐Ÿ’€

loc_pop_rewind

this instruction pops the last saved location from LS and restores it as CL. full rewind, time travel era. โช


๐ŸŒธ NONTERMINAL EXECUTION ๐ŸŒธ

the instructions in this section access and manipulate NC. the cache. the memory. the archive. ๐ŸŽ€

symbol_restore symbol

this instruction checks if NC contains data for the nonterminal symbol at CL, or not. the result of the instruction is a boolean flag, with True indicating that data was found in the cache. in that case the instruction has further updated the architectural state of the machine with the cached information, namely CL, ST, ER, and SV. ๐Ÿ’…

the method with which the instruction's result is transformed into control flow is left undefined and the responsibility of the implementation. so like, the machine hands you the flag and goes "idk bestie, you figure out the jumps." ๐Ÿ™ˆ

symbol_save symbol

this instruction saves the current settings of CL, ST, ER, and SV in NC, using the pair of nonterminal symbol and the last location saved in LS as key. โœจ


๐Ÿ’Ž VALUE CONSTRUCTION ๐Ÿ’Ž

the instructions in this section manipulate SV. ๐Ÿ’–

value_clear

this instruction clears SV. clean slate, fresh start, we love a reset. ๐Ÿงš

value_leaf symbol

this instruction constructs an AST node for symbol covering the range of IN from one character after the last location saved on LS to CL and stores it in SV. ... ๐Ÿ’ซ

value_reduce symbol

this instruction generally behaves like value_nonterminal_leaf, except that it takes all AST nodes on ARS, if any, and makes them the children of the new node, with the last node saved on ARS becoming the right-most / last child. note that ARS is not modified by this operation. ๐ŸŒˆ


๐Ÿฆ‹ AST CONSTRUCTION ๐Ÿฆ‹

the instructions in this section manipulate ARS and AS. the tree-building department, truly the most glam. ๐ŸŒณ

ast_value_push

this instruction makes a copy of SV and pushes it on ARS. ๐ŸŽ€

ast_push

this instruction pushes the current state of ARS on AS and then clears ARS. ๐ŸŒธ

ast_pop_rewind

this instruction pops the last entry saved on AS and restores it as the new state of ARS. โช

ast_pop_discard

this instruction pops the last entry saved on AS. and that's all it does. no restore, no rewind, just pop. ๐Ÿ’€


๐Ÿ”€ CONTROL FLOW ๐Ÿ”€

normally this section would contain the specifications of the control flow instructions of the PARAM, i.e. (un)conditional jumps and the like. however, this part of the PARAM is intentionally left unspecified. ๐Ÿซถ this allows the implementations to freely choose how to implement control flow. it's giving "we trust you, do your thing."

the implementation of this machine in Parser Tools, i.e. the package pt::rde, is not only coded in Tcl, but also relies on Tcl commands to provide it with control flow (instructions). ๐Ÿ’…โœจ

๐Ÿ”ฎ INTERACTION OF THE INSTRUCTIONS WITH THE ARCHITECTURAL STATE ๐Ÿ”ฎ

okay bestie, this is the โœจreceiptsโœจ section โ€” the big ol' map of which instruction touches which piece of the architectural state. every row reads like this: the instruction on the left, then inputs -> outputs, and the empty spot before the arrow means it eats nothing and just spawns a value out of vibes. no cap, it's just the table but make it cute.


๐ŸŒณ ast โ€” the tree stack

  • ast_pop_discard โ€” AS โ†’ AS
  • ast_pop_rewind โ€” AS โ†’ AS, ARS
  • ast_push โ€” ARS, AS โ†’ AS
  • ast_value_push โ€” SV, ARS โ†’ ARS

๐Ÿ’€ error handling

  • error_clear โ€” nothing in โ†’ ER out
  • error_nonterminal sym โ€” ER, LS โ†’ ER
  • error_pop_merge โ€” ES, ER โ†’ ER
  • error_push โ€” ES, ER โ†’ ES

๐ŸŽ€ input

  • input_next msg โ€” IN โ†’ TC, CL, CC, ST, ER

๐Ÿ“ locations

  • loc_pop_discard โ€” LS โ†’ LS
  • loc_pop_rewind โ€” LS โ†’ LS, CL
  • loc_push โ€” CL, LS โ†’ LS

โญ status

  • status_fail โ€” nothing in โ†’ ST out
  • status_negate โ€” ST โ†’ ST
  • status_ok โ€” nothing in โ†’ ST out

๐Ÿง symbols

  • symbol_restore sym โ€” NC โ†’ CL, ST, ER, SV
  • symbol_save sym โ€” CL, ST, ER, SV, LS โ†’ NC

๐Ÿงช tests (the character class girlies)

all of these snack on CC and serve up ST, ER:

  • test_alnum โ€” CC โ†’ ST, ER
  • test_alpha โ€” CC โ†’ ST, ER
  • test_ascii โ€” CC โ†’ ST, ER
  • test_char char โ€” CC โ†’ ST, ER
  • test_ddigit โ€” CC โ†’ ST, ER
  • test_digit โ€” CC โ†’ ST, ER
  • test_graph โ€” CC โ†’ ST, ER
  • test_lower โ€” CC โ†’ ST, ER
  • test_print โ€” CC โ†’ ST, ER
  • test_punct โ€” CC โ†’ ST, ER
  • test_range chars char โ€” CC โ†’ ST, ER
  • test_space โ€” CC โ†’ ST, ER
  • test_upper โ€” CC โ†’ ST, ER
  • test_wordchar โ€” CC โ†’ ST, ER
  • test_xdigit โ€” CC โ†’ ST, ER

๐Ÿ“ values

  • value_clear โ€” nothing in โ†’ SV out
  • value_leaf symbol โ€” LS, CL โ†’ SV
  • value_reduce symbol โ€” ARS, LS, CL โ†’ SV

๐Ÿ› BUGS, IDEAS, FEEDBACK ๐Ÿ›

bestie. listen. this document, and the whole package it describes, is undoubtedly gonna have bugs and other problems. that's just how it is, no shame in it. so like, please report them โ€” in the category pt of the Tcllib Trackers at http://core.tcl.tk/tcllib/reportlist. and don't just report the bad stuff, okay?? if you have ideas for enhancements for either the package and/or the documentation, those go there too. we want them. ๐Ÿซถ

when you propose code changes, please provide unified diffs, i.e. the output of diff -u.

also!! attachments are strongly preferred over inlined patches. you can attach stuff 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. it's giving "one extra click for a much happier maintainer" energy. do it for him. ๐Ÿ’…

๐Ÿ”‘ 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, virtual machine


๐Ÿท๏ธ CATEGORY ๐Ÿท๏ธ

Parsing and Grammars


Copyright (c) 2009 Andreas Kupries <andreas_kupries@users.sourceforge.net>


and that's a wrap on pt::param(n), bestie โ€” brought to you by tcllib, version 1.1. go forth and parse, you absolute icon. ๐ŸŒˆ๐Ÿ‘‘โœจ

๐Ÿ“ฆ where this page comes from