pt::param(n) | Parser Tools | pt::param(n) |
pt::param - PackRat Machine Specification
package require Tcl 8.5
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.
Welcome to the PackRat Machine (short: PARAM), a virtual machine geared towards the support of recursive descent parsers, especially packrat parsers. Towards this end it has features like the caching and reuse of partial results, the caching of the encountered input, and the ability to backtrack in both input and AST creation.
This document specifies the machine in terms of its architectural state and instruction set.
Any PARAM implementation has to manage at least the following state:
This part of the machine's state is used and modified by the instructions defined in the section Input Handling.
This part of the machine's state is used and modified by the instructions defined in the section Character Processing.
This part of the machine's state is used and modified by the instructions defined in the sections Character Processing, Location Handling, and Nonterminal Execution.
This part of the machine's state is used and modified by the instructions defined in the sections Character Processing, Location Handling, and Nonterminal Execution.
This part of the machine's state is used and modified by the instructions defined in the sections Status Control, Character Processing, and Nonterminal Execution.
This part of the machine's state is used and modified by the instructions defined in the sections Value Construction, and AST Construction.
This part of the machine's state is used and modified by the instructions defined in the sections Value Construction, and AST Construction.
This part of the machine's state is used and modified by the instructions defined in the sections Value Construction, and AST Construction.
Note that this part of the machine's state can be set even if the last test of the current character was successful. For example, the *-operator (matching a sub-expression zero or more times) in a PEG is always successful, even if it encounters a problem further in the input and has to backtrack. Such problems must not be forgotten when continuing the parsing.
This part of the machine's state is used and modified by the instructions defined in the sections Error Handling, Character Processing, and Nonterminal Execution.
This part of the machine's state is used and modified by the instructions defined in the sections Error Handling, Character Processing, and Nonterminal Execution.
The key location is where machine started the attempt to match the named nonterminal symbol, and the location in the saved 4-tuple is where machine ended up after the attempt completed, independent of the success of the attempt.
This part of the machine's state is used and modified by the instructions defined in the section Nonterminal Execution.
This part of the machine's state is used and modified by the instructions defined in the section Input Handling.
With the machine's architectural state specified it is now possible to specify the instruction set operating on that state and to be implemented by any realization of the PARAM. The 37 instructions are grouped roughly by the state they influence and/or query during their execution.
The instructions in this section mainly access IN, pulling the characters to process into the machine.
The operation may read the character from IN if the next character is not yet known to TC. If successful the new character is stored in TC, with its location (line, column), and the operation otherwise behaves as specified above. Future reads from the same location, possible due to backtracking, will then be satisfied from TC instead of IN.
If, on the other hand, the end of IN was reached, the operation is recorded as failed in ST, CL is left unchanged, and the pair of CL and msg becomes the new ES.
The instructions in this section mainly access CC, testing it against character classes, ranges, and individual characters.
Success and failure of the test are both recorded directly in ST. Success further clears ES, wheras 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.
Success and failure of the test are both recorded directly in ST. Success further clears ES, wheras 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.
Success and failure of the test are both recorded directly in ST. Success further clears ES, wheras 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.
Success and failure of the test are both recorded directly in ST. Success further clears ES, wheras 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.
Success and failure of the test are both recorded directly in ST. Success further clears ES, wheras 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.
Success and failure of the test are both recorded directly in ST. Success further clears ES, wheras 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.
Success and failure of the test are both recorded directly in ST. Success further clears ES, wheras 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.
Success and failure of the test are both recorded directly in ST. Success further clears ES, wheras 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.
Success and failure of the test are both recorded directly in ST. Success further clears ES, wheras 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.
Success and failure of the test are both recorded directly in ST. Success further clears ES, wheras 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.
Success and failure of the test are both recorded directly in ST. Success further clears ES, wheras 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.
Success and failure of the test are both recorded directly in ST. Success further clears ES, wheras 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.
Success and failure of the test are both recorded directly in ST. Success further clears ES, wheras 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.
Success and failure of the test are both recorded directly in ST. Success further clears ES, wheras 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.
Success and failure of the test are both recorded directly in ST. Success further clears ES, wheras 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.
The instructions in this section mainly access ER and ES.
The merge is governed by four rules, with the merge result
Note: In the above "just past" means "that location plus one", or also "the location of the next character after that location".
The instructions in this section directly manipulate ST.
The instructions in this section access CL and LS.
The instructions in this section access and manipulate NC.
The method with which the instruction's result is transformed into control flow is left undefined and the responsibility of the implementation.
The instructions in this section manipulate SV.
The instructions in this section manipulate ARS and AS.
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.
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).
Instruction Inputs Outputs ======================= ======================= ==================== ast_pop_discard AS -> AS ast_pop_rewind AS -> AS, ARS ast_push ARS, AS -> AS ast_value_push SV, ARS -> ARS ======================= ======================= ==================== error_clear - -> ER error_nonterminal sym ER, LS -> ER error_pop_merge ES, ER -> ER error_push ES, ER -> ES ======================= ======================= ==================== input_next msg IN -> TC, CL, CC, ST, ER ======================= ======================= ==================== loc_pop_discard LS -> LS loc_pop_rewind LS -> LS, CL loc_push CL, LS -> LS ======================= ======================= ==================== status_fail - -> ST status_negate ST -> ST status_ok - -> ST ======================= ======================= ==================== symbol_restore sym NC -> CL, ST, ER, SV symbol_save sym CL, ST, ER, SV LS -> NC ======================= ======================= ==================== 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 chare CC -> ST, ER test_space CC -> ST, ER test_upper CC -> ST, ER test_wordchar CC -> ST, ER test_xdigit CC -> ST, ER ======================= ======================= ==================== value_clear - -> SV value_leaf symbol LS, CL -> SV value_reduce symbol ARS, LS, CL -> SV ======================= ======================= ====================
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, virtual machine
Parsing and Grammars
Copyright (c) 2009 Andreas Kupries <andreas_kupries@users.sourceforge.net>
1 | pt |