1grammar::peg::interp(n) Grammar operations and usage grammar::peg::interp(n)
2
3
4
5______________________________________________________________________________
6
8 grammar::peg::interp - Interpreter for parsing expression grammars
9
11 package require Tcl 8.4
12
13 package require grammar::mengine ?0.1?
14
15 package require grammar::peg::interp ?0.1?
16
17 ::grammar::peg::interp::setup peg
18
19 ::grammar::peg::interp::parse nextcmd errorvar astvar
20
21_________________________________________________________________
22
24 This package provides commands for the controlled matching of a charac‐
25 ter stream via a parsing expression grammar and the creation of an
26 abstract syntax tree for the stream and partials.
27
28 It is built on top of the virtual machine provided by the package gram‐
29 mar::mengine and directly interprets the parsing expression grammar
30 given to it. In other words, the grammar is not pre-compiled but used
31 as is.
32
33 The grammar to be interpreted is taken from a container object follow‐
34 ing the interface specified by the package grammar::peg::container.
35 Only the relevant parts are copied into the state of this package.
36
37 It should be noted that the package provides exactly one instance of
38 the interpreter, and interpreting a second grammar requires the user to
39 either abort or complete a running interpretation, or to put them into
40 different Tcl interpreters.
41
42 Also of note is that the implementation assumes a pull-type handling of
43 the input. In other words, the interpreter pulls characters from the
44 input stream as it needs them. For usage in a push environment, i.e.
45 where the environment pushes new characters as they come we have to put
46 the engine into its own thread.
47
49 The package exports the following API
50
51 ::grammar::peg::interp::setup peg
52 This command (re)initializes the interpreter. It returns the
53 empty string. This command has to be invoked first, before any
54 matching run.
55
56 Its argument peg is the handle of an object containing the pars‐
57 ing expression grammar to interpret. This grammar has to be
58 valid, or an error will be thrown.
59
60 ::grammar::peg::interp::parse nextcmd errorvar astvar
61 This command interprets the loaded grammar and tries to match it
62 against the stream of characters represented by the command pre‐
63 fix nextcmd. Its result is a boolean value indicating whether
64 the matching process was successful (true), or not (false). In
65 case of a match failure error information will be stored into
66 the variable referenced by errorvar. The variable referenced by
67 astvar will always contain the generated abstract syntax tree,
68 however in the case of an error it will be only partial and pos‐
69 sibly malformed.
70
71 The abstract syntax tree is represented by a nested list, as
72 described in section ABSTRACT SYNTAX TREE of package gram‐
73 mar::mengine.
74
76 LL(k), TDPL, context-free languages, expression, grammar, matching,
77 parsing, parsing expression, parsing expression grammar, push down au‐
78 tomaton, recursive descent, state, top-down parsing languages, trans‐
79 ducer, virtual machine
80
82 Copyright (c) 2005 Andreas Kupries <andreas_kupries@users.sourceforge.net>
83
84
85
86
87grammar_peg 0.1 grammar::peg::interp(n)