1pt::peg::to::cparam(n) Parser Tools pt::peg::to::cparam(n)
2
3
4
5______________________________________________________________________________
6
8 pt::peg::to::cparam - PEG Conversion. Write CPARAM format
9
11 package require Tcl 8.5
12
13 package require pt::peg::to::cparam ?1.1.2?
14
15 pt::peg::to::cparam reset
16
17 pt::peg::to::cparam configure
18
19 pt::peg::to::cparam configure option
20
21 pt::peg::to::cparam configure option value...
22
23 pt::peg::to::cparam convert serial
24
25______________________________________________________________________________
26
28 Are you lost ? Do you have trouble understanding this document ? In
29 that case please read the overview provided by the Introduction to
30 Parser Tools. This document is the entrypoint to the whole system the
31 current package is a part of.
32
33 This package implements the converter from parsing expression grammars
34 to CPARAM markup.
35
36 It resides in the Export section of the Core Layer of Parser Tools, and
37 can be used either directly with the other packages of this layer, or
38 indirectly through the export manager provided by pt::peg::export. The
39 latter is intented for use in untrusted environments and done through
40 the corresponding export plugin pt::peg::export::cparam sitting between
41 converter and export manager.
42
43 IMAGE: arch_core_eplugins
44
46 The API provided by this package satisfies the specification of the
47 Converter API found in the Parser Tools Export API specification.
48
49 pt::peg::to::cparam reset
50 This command resets the configuration of the package to its de‐
51 fault settings.
52
53 pt::peg::to::cparam configure
54 This command returns a dictionary containing the current config‐
55 uration of the package.
56
57 pt::peg::to::cparam configure option
58 This command returns the current value of the specified configu‐
59 ration option of the package. For the set of legal options,
60 please read the section Options.
61
62 pt::peg::to::cparam configure option value...
63 This command sets the given configuration options of the pack‐
64 age, to the specified values. For the set of legal options,
65 please read the section Options.
66
67 pt::peg::to::cparam convert serial
68 This command takes the canonical serialization of a parsing ex‐
69 pression grammar, as specified in section PEG serialization for‐
70 mat, and contained in serial, and generates CPARAM markup encod‐
71 ing the grammar, per the current package configuration. The
72 created string is then returned as the result of the command.
73
75 The converter to C code recognizes the following configuration vari‐
76 ables and changes its behaviour as they specify.
77
78 -file string
79 The value of this option is the name of the file or other entity
80 from which the grammar came, for which the command is run. The
81 default value is unknown.
82
83 -name string
84 The value of this option is the name of the grammar we are pro‐
85 cessing. The default value is a_pe_grammar.
86
87 -user string
88 The value of this option is the name of the user for which the
89 command is run. The default value is unknown.
90
91 -template string
92 The value of this option is a string into which to put the gen‐
93 erated text and the other configuration settings. The various
94 locations for user-data are expected to be specified with the
95 placeholders listed below. The default value is "@code@".
96
97 @user@ To be replaced with the value of the option -user.
98
99 @format@
100 To be replaced with the the constant C/PARAM.
101
102 @file@ To be replaced with the value of the option -file.
103
104 @name@ To be replaced with the value of the option -name.
105
106 @code@ To be replaced with the generated Tcl code.
107
108 The following options are special, in that they will occur
109 within the generated code, and are replaced there as well.
110
111 @statedecl@
112 To be replaced with the value of the option state-decl.
113
114 @stateref@
115 To be replaced with the value of the option state-ref.
116
117 @strings@
118 To be replaced with the value of the option string-var‐
119 name.
120
121 @self@ To be replaced with the value of the option self-command.
122
123 @def@ To be replaced with the value of the option fun-quali‐
124 fier.
125
126 @ns@ To be replaced with the value of the option namespace.
127
128 @main@ To be replaced with the value of the option main.
129
130 @prelude@
131 To be replaced with the value of the option prelude.
132
133 -state-decl string
134 A C string representing the argument declaration to use in the
135 generated parsing functions to refer to the parsing state. In
136 essence type and argument name. The default value is the string
137 RDE_PARAM p.
138
139 -state-ref string
140 A C string representing the argument named used in the generated
141 parsing functions to refer to the parsing state. The default
142 value is the string p.
143
144 -self-command string
145 A C string representing the reference needed to call the gener‐
146 ated parser function (methods ...) from another parser fonction,
147 per the chosen framework (template). The default value is the
148 empty string.
149
150 -fun-qualifier string
151 A C string containing the attributes to give to the generated
152 functions (methods ...), per the chosen framework (template).
153 The default value is static.
154
155 -namespace string
156 The name of the C namespace the parser functions (methods, ...)
157 shall reside in, or a general prefix to add to the function
158 names. The default value is the empty string.
159
160 -main string
161 The name of the main function (method, ...) to be called by the
162 chosen framework (template) to start parsing input. The default
163 value is __main.
164
165 -string-varname string
166 The name of the variable used for the table of strings used by
167 the generated parser, i.e. error messages, symbol names, etc.
168 The default value is p_string.
169
170 -prelude string
171 A snippet of code to be inserted at the head of each generated
172 parsing function. The default value is the empty string.
173
174 -indent integer
175 The number of characters to indent each line of the generated
176 code by. The default value is 0.
177
178 -comments boolean
179 A flag controlling the generation of code comments containing
180 the original parsing expression a parsing function is for. The
181 default value is on.
182
183 While the high parameterizability of this converter, as shown by the
184 multitude of options it supports, is an advantage to the advanced user,
185 allowing her to customize the output of the converter as needed, a
186 novice user will likely not see the forest for the trees.
187
188 To help these latter users an adjunct package is provided, containing a
189 canned configuration which will generate immediately useful full
190 parsers. It is
191
192 pt::cparam::configuration::critcl
193 Generated parsers are embedded into a Critcl-based framework.
194
196 The c format is executable code, a parser for the grammar. The parser
197 implementation is written in C and can be tweaked to the users' needs
198 through a multitude of options.
199
200 The critcl format, for example, is implemented as a canned configura‐
201 tion of these options on top of the generator for c.
202
203 The bulk of such a framework has to be specified through the option
204 -template. The additional options
205
206 -fun-qualifier string
207
208 -main string
209
210 -namespace string
211
212 -prelude string
213
214 -self-command string
215
216 -state-decl string
217
218 -state-ref string
219
220 -string-varname string
221
222 provide code snippets which help to glue framework and generated code
223 together. Their placeholders are in the generated code. Further the
224 options
225
226 -indent integer
227
228 -comments boolean
229
230 allow for the customization of the code indent (default none), and
231 whether to generate comments showing the parsing expressions a function
232 is for (default on).
233
234 EXAMPLE
235 We are forgoing an example of this representation, with apologies. It
236 would be way to large for this document.
237
239 Here we specify the format used by the Parser Tools to serialize Pars‐
240 ing Expression Grammars as immutable values for transport, comparison,
241 etc.
242
243 We distinguish between regular and canonical serializations. While a
244 PEG may have more than one regular serialization only exactly one of
245 them will be canonical.
246
247 regular serialization
248
249 [1] The serialization of any PEG is a nested Tcl dictionary.
250
251 [2] This dictionary holds a single key, pt::grammar::peg, and
252 its value. This value holds the contents of the grammar.
253
254 [3] The contents of the grammar are a Tcl dictionary holding
255 the set of nonterminal symbols and the starting expres‐
256 sion. The relevant keys and their values are
257
258 rules The value is a Tcl dictionary whose keys are the
259 names of the nonterminal symbols known to the
260 grammar.
261
262 [1] Each nonterminal symbol may occur only
263 once.
264
265 [2] The empty string is not a legal nonterminal
266 symbol.
267
268 [3] The value for each symbol is a Tcl dictio‐
269 nary itself. The relevant keys and their
270 values in this dictionary are
271
272 is The value is the serialization of
273 the parsing expression describing
274 the symbols sentennial structure, as
275 specified in the section PE serial‐
276 ization format.
277
278 mode The value can be one of three values
279 specifying how a parser should han‐
280 dle the semantic value produced by
281 the symbol.
282
283 value The semantic value of the
284 nonterminal symbol is an ab‐
285 stract syntax tree consisting
286 of a single node node for the
287 nonterminal itself, which has
288 the ASTs of the symbol's
289 right hand side as its chil‐
290 dren.
291
292 leaf The semantic value of the
293 nonterminal symbol is an ab‐
294 stract syntax tree consisting
295 of a single node node for the
296 nonterminal, without any
297 children. Any ASTs generated
298 by the symbol's right hand
299 side are discarded.
300
301 void The nonterminal has no seman‐
302 tic value. Any ASTs generated
303 by the symbol's right hand
304 side are discarded (as well).
305
306 start The value is the serialization of the start pars‐
307 ing expression of the grammar, as specified in the
308 section PE serialization format.
309
310 [4] The terminal symbols of the grammar are specified implic‐
311 itly as the set of all terminal symbols used in the start
312 expression and on the RHS of the grammar rules.
313
314 canonical serialization
315 The canonical serialization of a grammar has the format as spec‐
316 ified in the previous item, and then additionally satisfies the
317 constraints below, which make it unique among all the possible
318 serializations of this grammar.
319
320 [1] The keys found in all the nested Tcl dictionaries are
321 sorted in ascending dictionary order, as generated by
322 Tcl's builtin command lsort -increasing -dict.
323
324 [2] The string representation of the value is the canonical
325 representation of a Tcl dictionary. I.e. it does not con‐
326 tain superfluous whitespace.
327
328 EXAMPLE
329 Assuming the following PEG for simple mathematical expressions
330
331 PEG calculator (Expression)
332 Digit <- '0'/'1'/'2'/'3'/'4'/'5'/'6'/'7'/'8'/'9' ;
333 Sign <- '-' / '+' ;
334 Number <- Sign? Digit+ ;
335 Expression <- Term (AddOp Term)* ;
336 MulOp <- '*' / '/' ;
337 Term <- Factor (MulOp Factor)* ;
338 AddOp <- '+'/'-' ;
339 Factor <- '(' Expression ')' / Number ;
340 END;
341
342
343 then its canonical serialization (except for whitespace) is
344
345 pt::grammar::peg {
346 rules {
347 AddOp {is {/ {t -} {t +}} mode value}
348 Digit {is {/ {t 0} {t 1} {t 2} {t 3} {t 4} {t 5} {t 6} {t 7} {t 8} {t 9}} mode value}
349 Expression {is {x {n Term} {* {x {n AddOp} {n Term}}}} mode value}
350 Factor {is {/ {x {t (} {n Expression} {t )}} {n Number}} mode value}
351 MulOp {is {/ {t *} {t /}} mode value}
352 Number {is {x {? {n Sign}} {+ {n Digit}}} mode value}
353 Sign {is {/ {t -} {t +}} mode value}
354 Term {is {x {n Factor} {* {x {n MulOp} {n Factor}}}} mode value}
355 }
356 start {n Expression}
357 }
358
359
361 Here we specify the format used by the Parser Tools to serialize Pars‐
362 ing Expressions as immutable values for transport, comparison, etc.
363
364 We distinguish between regular and canonical serializations. While a
365 parsing expression may have more than one regular serialization only
366 exactly one of them will be canonical.
367
368 Regular serialization
369
370 Atomic Parsing Expressions
371
372 [1] The string epsilon is an atomic parsing expres‐
373 sion. It matches the empty string.
374
375 [2] The string dot is an atomic parsing expression. It
376 matches any character.
377
378 [3] The string alnum is an atomic parsing expression.
379 It matches any Unicode alphabet or digit charac‐
380 ter. This is a custom extension of PEs based on
381 Tcl's builtin command string is.
382
383 [4] The string alpha is an atomic parsing expression.
384 It matches any Unicode alphabet character. This is
385 a custom extension of PEs based on Tcl's builtin
386 command string is.
387
388 [5] The string ascii is an atomic parsing expression.
389 It matches any Unicode character below U0080. This
390 is a custom extension of PEs based on Tcl's
391 builtin command string is.
392
393 [6] The string control is an atomic parsing expres‐
394 sion. It matches any Unicode control character.
395 This is a custom extension of PEs based on Tcl's
396 builtin command string is.
397
398 [7] The string digit is an atomic parsing expression.
399 It matches any Unicode digit character. Note that
400 this includes characters outside of the [0..9]
401 range. This is a custom extension of PEs based on
402 Tcl's builtin command string is.
403
404 [8] The string graph is an atomic parsing expression.
405 It matches any Unicode printing character, except
406 for space. This is a custom extension of PEs based
407 on Tcl's builtin command string is.
408
409 [9] The string lower is an atomic parsing expression.
410 It matches any Unicode lower-case alphabet charac‐
411 ter. This is a custom extension of PEs based on
412 Tcl's builtin command string is.
413
414 [10] The string print is an atomic parsing expression.
415 It matches any Unicode printing character, includ‐
416 ing space. This is a custom extension of PEs based
417 on Tcl's builtin command string is.
418
419 [11] The string punct is an atomic parsing expression.
420 It matches any Unicode punctuation character. This
421 is a custom extension of PEs based on Tcl's
422 builtin command string is.
423
424 [12] The string space is an atomic parsing expression.
425 It matches any Unicode space character. This is a
426 custom extension of PEs based on Tcl's builtin
427 command string is.
428
429 [13] The string upper is an atomic parsing expression.
430 It matches any Unicode upper-case alphabet charac‐
431 ter. This is a custom extension of PEs based on
432 Tcl's builtin command string is.
433
434 [14] The string wordchar is an atomic parsing expres‐
435 sion. It matches any Unicode word character. This
436 is any alphanumeric character (see alnum), and any
437 connector punctuation characters (e.g. under‐
438 score). This is a custom extension of PEs based on
439 Tcl's builtin command string is.
440
441 [15] The string xdigit is an atomic parsing expression.
442 It matches any hexadecimal digit character. This
443 is a custom extension of PEs based on Tcl's
444 builtin command string is.
445
446 [16] The string ddigit is an atomic parsing expression.
447 It matches any decimal digit character. This is a
448 custom extension of PEs based on Tcl's builtin
449 command regexp.
450
451 [17] The expression [list t x] is an atomic parsing ex‐
452 pression. It matches the terminal string x.
453
454 [18] The expression [list n A] is an atomic parsing ex‐
455 pression. It matches the nonterminal A.
456
457 Combined Parsing Expressions
458
459 [1] For parsing expressions e1, e2, ... the result of
460 [list / e1 e2 ... ] is a parsing expression as
461 well. This is the ordered choice, aka prioritized
462 choice.
463
464 [2] For parsing expressions e1, e2, ... the result of
465 [list x e1 e2 ... ] is a parsing expression as
466 well. This is the sequence.
467
468 [3] For a parsing expression e the result of [list *
469 e] is a parsing expression as well. This is the
470 kleene closure, describing zero or more repeti‐
471 tions.
472
473 [4] For a parsing expression e the result of [list +
474 e] is a parsing expression as well. This is the
475 positive kleene closure, describing one or more
476 repetitions.
477
478 [5] For a parsing expression e the result of [list &
479 e] is a parsing expression as well. This is the
480 and lookahead predicate.
481
482 [6] For a parsing expression e the result of [list !
483 e] is a parsing expression as well. This is the
484 not lookahead predicate.
485
486 [7] For a parsing expression e the result of [list ?
487 e] is a parsing expression as well. This is the
488 optional input.
489
490 Canonical serialization
491 The canonical serialization of a parsing expression has the for‐
492 mat as specified in the previous item, and then additionally
493 satisfies the constraints below, which make it unique among all
494 the possible serializations of this parsing expression.
495
496 [1] The string representation of the value is the canonical
497 representation of a pure Tcl list. I.e. it does not con‐
498 tain superfluous whitespace.
499
500 [2] Terminals are not encoded as ranges (where start and end
501 of the range are identical).
502
503 EXAMPLE
504 Assuming the parsing expression shown on the right-hand side of the
505 rule
506
507 Expression <- Term (AddOp Term)*
508
509
510 then its canonical serialization (except for whitespace) is
511
512 {x {n Term} {* {x {n AddOp} {n Term}}}}
513
514
516 This document, and the package it describes, will undoubtedly contain
517 bugs and other problems. Please report such in the category pt of the
518 Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist]. Please also
519 report any ideas for enhancements you may have for either package
520 and/or documentation.
521
522 When proposing code changes, please provide unified diffs, i.e the out‐
523 put of diff -u.
524
525 Note further that attachments are strongly preferred over inlined
526 patches. Attachments can be made by going to the Edit form of the
527 ticket immediately after its creation, and then using the left-most
528 button in the secondary navigation bar.
529
531 CPARAM, EBNF, LL(k), PEG, TDPL, context-free languages, conversion, ex‐
532 pression, format conversion, grammar, matching, parser, parsing expres‐
533 sion, parsing expression grammar, push down automaton, recursive de‐
534 scent, serialization, state, top-down parsing languages, transducer
535
537 Parsing and Grammars
538
540 Copyright (c) 2009 Andreas Kupries <andreas_kupries@users.sourceforge.net>
541
542
543
544
545tcllib 1.1.2 pt::peg::to::cparam(n)