grammar::me::tcl(n) | Grammar operations and usage | grammar::me::tcl(n) |
grammar::me::tcl - Virtual machine implementation I for parsing token streams
package require Tcl 8.4
package require grammar::me::tcl ?0.1?
::grammar::me::tcl cmd ...
::grammar::me::tcl init nextcmd ?tokmap?
::grammar::me::tcl lc location
::grammar::me::tcl tok from ?to?
::grammar::me::tcl tokens
::grammar::me::tcl sv
::grammar::me::tcl ast
::grammar::me::tcl astall
::grammar::me::tcl ctok
::grammar::me::tcl nc
::grammar::me::tcl next
::grammar::me::tcl ord
::grammar::me::tcl::ict_advance message
::grammar::me::tcl::ict_match_token tok message
::grammar::me::tcl::ict_match_tokrange tokbegin tokend message
::grammar::me::tcl::ict_match_tokclass code message
::grammar::me::tcl::inc_restore nt
::grammar::me::tcl::inc_save nt startlocation
::grammar::me::tcl::iok_ok
::grammar::me::tcl::iok_fail
::grammar::me::tcl::iok_negate
::grammar::me::tcl::icl_get
::grammar::me::tcl::icl_rewind oldlocation
::grammar::me::tcl::ier_get
::grammar::me::tcl::ier_clear
::grammar::me::tcl::ier_nonterminal message location
::grammar::me::tcl::ier_merge olderror
::grammar::me::tcl::isv_clear
::grammar::me::tcl::isv_terminal
::grammar::me::tcl::isv_nonterminal_leaf nt startlocation
::grammar::me::tcl::isv_nonterminal_range nt startlocation
::grammar::me::tcl::isv_nonterminal_reduce nt startlocation ?marker?
::grammar::me::tcl::ias_push
::grammar::me::tcl::ias_mark
::grammar::me::tcl::ias_pop2mark marker
This package provides an implementation of the ME virtual machine. Please go and read the document grammar::me_intro first if you do not know what a ME virtual machine is.
This implementation is tied very strongly to Tcl. All the stacks in the machine state are handled through the Tcl stack, all control flow is handled by Tcl commands, and the remaining machine instructions are directly mapped to Tcl commands. Especially the matching of nonterminal symbols is handled by Tcl procedures as well, essentially extending the machine implementation with custom instructions.
Further on the implementation handles only a single machine which is uninteruptible during execution and hardwired for pull operation. I.e. it explicitly requests each new token through a callback, pulling them into its state.
A related package is grammar::peg::interp which provides a generic interpreter / parser for parsing expression grammars (PEGs), implemented on top of this implementation of the ME virtual machine.
The commands documented in this section do not implement any of the instructions of the ME virtual machine. They provide the facilities for the initialization of the machine and the retrieval of important information.
The command prefix nextcmd represents the input stream of characters and is invoked by the machine whenever the a new character from the stream is required. The instruction for handling this is ict_advance. The callback has to return either the empty list, or a list of 4 elements containing the token, its lexeme attribute, and its location as line number and column index, in this order. The empty list is the signal that the end of the input stream has been reached. The lexeme attribute is stored in the terminal cache, but otherwise not used by the machine.
The optional dictionary tokmap maps from tokens to integer numbers. If present the numbers impose an order on the tokens, which is subsequently used by ict_match_tokrange to determine if a token is in the specified range or not. If no token map is specified the lexicographic order of th token names will be used instead. This choice is especially asensible when using characters as tokens.
Note that the command is not able to convert locations which have not been reached by the machine yet. In other words, if the machine has read 7 tokens the command is able to convert the offsets 0 to 6, but nothing beyond that. This also shows that it is not possible to convert offsets which refer to locations before the beginning of the stream.
After a call of init the state used for the conversion is cleared, making further conversions impossible until the machine has read tokens again.
Each element of the returned list is a list of four elements, the token, its associated lexeme, line number, and column index, in this order. In other words, each element has the same structure as the result of the nextcmd callback given to ::grammar::me::tcl::init
This command places the same restrictions on its location arguments as ::grammar::me::tcl::lc.
Please go and read the document grammar::me_vm first for a specification of the basic ME virtual machine and its state.
This implementation manages the state described in that document, except for the stacks minus the AST stack. In other words, location stack, error stack, return stack, and ast marker stack are implicitly managed through standard Tcl scoping, i.e. Tcl variables in procedures, outside of this implementation.
Please go and read the document grammar::me_vm first for a specification of the basic ME virtual machine and its instruction set.
This implementation maps all instructions to Tcl commands in the namespace "::grammar::me::tcl", except for the stack related commands, nonterminal symbols and control flow. Here we simply list the commands and explain the differences to the specified instructions, if there are any. For their semantics see the aforementioned specification. The machine commands are not reachable through the ensemble command ::grammar::me::tcl.
This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category grammar_me 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.
grammar, parsing, virtual machine
Grammars and finite automata
Copyright (c) 2005 Andreas Kupries <andreas_kupries@users.sourceforge.net>
0.1 | grammar_me |