1pt::peg::export::peg(n) Parser Tools pt::peg::export::peg(n)
2
3
4
5______________________________________________________________________________
6
8 pt::peg::export::peg - PEG Export Plugin. Write PEG format
9
11 package require Tcl 8.5
12
13 package require pt::peg::export::peg ?1?
14
15 package require pt::peg::to::peg
16
17 export serial configuration
18
19______________________________________________________________________________
20
22 Are you lost ? Do you have trouble understanding this document ? In
23 that case please read the overview provided by the Introduction to
24 Parser Tools. This document is the entrypoint to the whole system the
25 current package is a part of.
26
27 This package implements the parsing expression grammar export plugin
28 for the generation of PEG markup.
29
30 It resides in the Export section of the Core Layer of Parser Tools and
31 is intended to be used by pt::peg::export, the export manager, sitting
32 between it and the corresponding core conversion functionality provided
33 by pt::peg::to::peg.
34
35 IMAGE: arch_core_eplugins
36
37 While the direct use of this package with a regular interpreter is pos‐
38 sible, this is strongly disrecommended and requires a number of contor‐
39 tions to provide the expected environment. The proper way to use this
40 functionality depends on the situation:
41
42 [1] In an untrusted environment the proper access is through the
43 package pt::peg::export and the export manager objects it pro‐
44 vides.
45
46 [2] In a trusted environment however simply use the package
47 pt::peg::to::peg and access the core conversion functionality
48 directly.
49
51 The API provided by this package satisfies the specification of the
52 Plugin API found in the Parser Tools Export API specification.
53
54 export serial configuration
55 This command takes the canonical serialization of a parsing
56 expression grammar, as specified in section PEG serialization
57 format, and contained in serial, the configuration, a dictio‐
58 nary, and generates PEG markup encoding the grammar. The cre‐
59 ated string is then returned as the result of the command.
60
62 The PEG export plugin recognizes the following configuration variables
63 and changes its behaviour as they specify.
64
65 string template
66 If this configuration variable is set it is assumed to contain a
67 string into which to put the generated text and other configura‐
68 tion data. The various locations are expected to be specified
69 with the following placeholders:
70
71 @user@ To be replaced with the value of the configuration vari‐
72 able user.
73
74 @format@
75 To be replaced with the the constant PEG.
76
77 @file@ To be replaced with the value of the configuration vari‐
78 able file.
79
80 @name@ To be replaced with the value of the configuration vari‐
81 able name.
82
83 @code@ To be replaced with the generated text.
84
85 If this configuration variable is not set, or empty, then the plugin
86 falls back to a standard template, which is defined as "@code@".
87
88 Note that this plugin may ignore the standard configuration variables
89 user, format, file, and their values, depending on the chosen template.
90
91 The content of the standard configuration variable name, if set, is
92 used as name of the grammar in the output. Otherwise the plugin falls
93 back to the default name a_pe_grammar.
94
96 peg, a language for the specification of parsing expression grammars is
97 meant to be human readable, and writable as well, yet strict enough to
98 allow its processing by machine. Like any computer language. It was
99 defined to make writing the specification of a grammar easy, something
100 the other formats found in the Parser Tools do not lend themselves too.
101
102 It is formally specified by the grammar shown below, written in itself.
103 For a tutorial / introduction to the language please go and read the
104 PEG Language Tutorial.
105
106 PEG pe-grammar-for-peg (Grammar)
107
108 # --------------------------------------------------------------------
109 # Syntactical constructs
110
111 Grammar <- WHITESPACE Header Definition* Final EOF ;
112
113 Header <- PEG Identifier StartExpr ;
114 Definition <- Attribute? Identifier IS Expression SEMICOLON ;
115 Attribute <- (VOID / LEAF) COLON ;
116 Expression <- Sequence (SLASH Sequence)* ;
117 Sequence <- Prefix+ ;
118 Prefix <- (AND / NOT)? Suffix ;
119 Suffix <- Primary (QUESTION / STAR / PLUS)? ;
120 Primary <- ALNUM / ALPHA / ASCII / CONTROL / DDIGIT / DIGIT
121 / GRAPH / LOWER / PRINTABLE / PUNCT / SPACE / UPPER
122 / WORDCHAR / XDIGIT
123 / Identifier
124 / OPEN Expression CLOSE
125 / Literal
126 / Class
127 / DOT
128 ;
129 Literal <- APOSTROPH (!APOSTROPH Char)* APOSTROPH WHITESPACE
130 / DAPOSTROPH (!DAPOSTROPH Char)* DAPOSTROPH WHITESPACE ;
131 Class <- OPENB (!CLOSEB Range)* CLOSEB WHITESPACE ;
132 Range <- Char TO Char / Char ;
133
134 StartExpr <- OPEN Expression CLOSE ;
135 void: Final <- "END" WHITESPACE SEMICOLON WHITESPACE ;
136
137 # --------------------------------------------------------------------
138 # Lexing constructs
139
140 Identifier <- Ident WHITESPACE ;
141 leaf: Ident <- ([_:] / <alpha>) ([_:] / <alnum>)* ;
142 Char <- CharSpecial / CharOctalFull / CharOctalPart
143 / CharUnicode / CharUnescaped
144 ;
145
146 leaf: CharSpecial <- "\\" [nrt'"\[\]\\] ;
147 leaf: CharOctalFull <- "\\" [0-2][0-7][0-7] ;
148 leaf: CharOctalPart <- "\\" [0-7][0-7]? ;
149 leaf: CharUnicode <- "\\" 'u' HexDigit (HexDigit (HexDigit HexDigit?)?)? ;
150 leaf: CharUnescaped <- !"\\" . ;
151
152 void: HexDigit <- [0-9a-fA-F] ;
153
154 void: TO <- '-' ;
155 void: OPENB <- "[" ;
156 void: CLOSEB <- "]" ;
157 void: APOSTROPH <- "'" ;
158 void: DAPOSTROPH <- '"' ;
159 void: PEG <- "PEG" !([_:] / <alnum>) WHITESPACE ;
160 void: IS <- "<-" WHITESPACE ;
161 leaf: VOID <- "void" WHITESPACE ; # Implies that definition has no semantic value.
162 leaf: LEAF <- "leaf" WHITESPACE ; # Implies that definition has no terminals.
163 void: SEMICOLON <- ";" WHITESPACE ;
164 void: COLON <- ":" WHITESPACE ;
165 void: SLASH <- "/" WHITESPACE ;
166 leaf: AND <- "&" WHITESPACE ;
167 leaf: NOT <- "!" WHITESPACE ;
168 leaf: QUESTION <- "?" WHITESPACE ;
169 leaf: STAR <- "*" WHITESPACE ;
170 leaf: PLUS <- "+" WHITESPACE ;
171 void: OPEN <- "(" WHITESPACE ;
172 void: CLOSE <- ")" WHITESPACE ;
173 leaf: DOT <- "." WHITESPACE ;
174
175 leaf: ALNUM <- "<alnum>" WHITESPACE ;
176 leaf: ALPHA <- "<alpha>" WHITESPACE ;
177 leaf: ASCII <- "<ascii>" WHITESPACE ;
178 leaf: CONTROL <- "<control>" WHITESPACE ;
179 leaf: DDIGIT <- "<ddigit>" WHITESPACE ;
180 leaf: DIGIT <- "<digit>" WHITESPACE ;
181 leaf: GRAPH <- "<graph>" WHITESPACE ;
182 leaf: LOWER <- "<lower>" WHITESPACE ;
183 leaf: PRINTABLE <- "<print>" WHITESPACE ;
184 leaf: PUNCT <- "<punct>" WHITESPACE ;
185 leaf: SPACE <- "<space>" WHITESPACE ;
186 leaf: UPPER <- "<upper>" WHITESPACE ;
187 leaf: WORDCHAR <- "<wordchar>" WHITESPACE ;
188 leaf: XDIGIT <- "<xdigit>" WHITESPACE ;
189
190 void: WHITESPACE <- (" " / "\t" / EOL / COMMENT)* ;
191 void: COMMENT <- '#' (!EOL .)* EOL ;
192 void: EOL <- "\n\r" / "\n" / "\r" ;
193 void: EOF <- !. ;
194
195 # --------------------------------------------------------------------
196 END;
197
198
199 EXAMPLE
200 Our example specifies the grammar for a basic 4-operation calculator.
201
202 PEG calculator (Expression)
203 Digit <- '0'/'1'/'2'/'3'/'4'/'5'/'6'/'7'/'8'/'9' ;
204 Sign <- '-' / '+' ;
205 Number <- Sign? Digit+ ;
206 Expression <- Term (AddOp Term)* ;
207 MulOp <- '*' / '/' ;
208 Term <- Factor (MulOp Factor)* ;
209 AddOp <- '+'/'-' ;
210 Factor <- '(' Expression ')' / Number ;
211 END;
212
213
214 Using higher-level features of the notation, i.e. the character classes
215 (predefined and custom), this example can be rewritten as
216
217 PEG calculator (Expression)
218 Sign <- [-+] ;
219 Number <- Sign? <ddigit>+;
220 Expression <- '(' Expression ')' / (Factor (MulOp Factor)*);
221 MulOp <- [*/];
222 Factor <- Term (AddOp Term)*;
223 AddOp <- [-+];
224 Term <- Number;
225 END;
226
227
229 Here we specify the format used by the Parser Tools to serialize Pars‐
230 ing Expression Grammars as immutable values for transport, comparison,
231 etc.
232
233 We distinguish between regular and canonical serializations. While a
234 PEG may have more than one regular serialization only exactly one of
235 them will be canonical.
236
237 regular serialization
238
239 [1] The serialization of any PEG is a nested Tcl dictionary.
240
241 [2] This dictionary holds a single key, pt::grammar::peg, and
242 its value. This value holds the contents of the grammar.
243
244 [3] The contents of the grammar are a Tcl dictionary holding
245 the set of nonterminal symbols and the starting expres‐
246 sion. The relevant keys and their values are
247
248 rules The value is a Tcl dictionary whose keys are the
249 names of the nonterminal symbols known to the
250 grammar.
251
252 [1] Each nonterminal symbol may occur only
253 once.
254
255 [2] The empty string is not a legal nonterminal
256 symbol.
257
258 [3] The value for each symbol is a Tcl dictio‐
259 nary itself. The relevant keys and their
260 values in this dictionary are
261
262 is The value is the serialization of
263 the parsing expression describing
264 the symbols sentennial structure, as
265 specified in the section PE serial‐
266 ization format.
267
268 mode The value can be one of three values
269 specifying how a parser should han‐
270 dle the semantic value produced by
271 the symbol.
272
273 value The semantic value of the
274 nonterminal symbol is an
275 abstract syntax tree consist‐
276 ing of a single node node for
277 the nonterminal itself, which
278 has the ASTs of the symbol's
279 right hand side as its chil‐
280 dren.
281
282 leaf The semantic value of the
283 nonterminal symbol is an
284 abstract syntax tree consist‐
285 ing of a single node node for
286 the nonterminal, without any
287 children. Any ASTs generated
288 by the symbol's right hand
289 side are discarded.
290
291 void The nonterminal has no seman‐
292 tic value. Any ASTs generated
293 by the symbol's right hand
294 side are discarded (as well).
295
296 start The value is the serialization of the start pars‐
297 ing expression of the grammar, as specified in the
298 section PE serialization format.
299
300 [4] The terminal symbols of the grammar are specified implic‐
301 itly as the set of all terminal symbols used in the start
302 expression and on the RHS of the grammar rules.
303
304 canonical serialization
305 The canonical serialization of a grammar has the format as spec‐
306 ified in the previous item, and then additionally satisfies the
307 constraints below, which make it unique among all the possible
308 serializations of this grammar.
309
310 [1] The keys found in all the nested Tcl dictionaries are
311 sorted in ascending dictionary order, as generated by
312 Tcl's builtin command lsort -increasing -dict.
313
314 [2] The string representation of the value is the canonical
315 representation of a Tcl dictionary. I.e. it does not con‐
316 tain superfluous whitespace.
317
318 EXAMPLE
319 Assuming the following PEG for simple mathematical expressions
320
321 PEG calculator (Expression)
322 Digit <- '0'/'1'/'2'/'3'/'4'/'5'/'6'/'7'/'8'/'9' ;
323 Sign <- '-' / '+' ;
324 Number <- Sign? Digit+ ;
325 Expression <- Term (AddOp Term)* ;
326 MulOp <- '*' / '/' ;
327 Term <- Factor (MulOp Factor)* ;
328 AddOp <- '+'/'-' ;
329 Factor <- '(' Expression ')' / Number ;
330 END;
331
332
333 then its canonical serialization (except for whitespace) is
334
335 pt::grammar::peg {
336 rules {
337 AddOp {is {/ {t -} {t +}} mode value}
338 Digit {is {/ {t 0} {t 1} {t 2} {t 3} {t 4} {t 5} {t 6} {t 7} {t 8} {t 9}} mode value}
339 Expression {is {x {n Term} {* {x {n AddOp} {n Term}}}} mode value}
340 Factor {is {/ {x {t (} {n Expression} {t )}} {n Number}} mode value}
341 MulOp {is {/ {t *} {t /}} mode value}
342 Number {is {x {? {n Sign}} {+ {n Digit}}} mode value}
343 Sign {is {/ {t -} {t +}} mode value}
344 Term {is {x {n Factor} {* {x {n MulOp} {n Factor}}}} mode value}
345 }
346 start {n Expression}
347 }
348
349
351 Here we specify the format used by the Parser Tools to serialize Pars‐
352 ing Expressions as immutable values for transport, comparison, etc.
353
354 We distinguish between regular and canonical serializations. While a
355 parsing expression may have more than one regular serialization only
356 exactly one of them will be canonical.
357
358 Regular serialization
359
360 Atomic Parsing Expressions
361
362 [1] The string epsilon is an atomic parsing expres‐
363 sion. It matches the empty string.
364
365 [2] The string dot is an atomic parsing expression. It
366 matches any character.
367
368 [3] The string alnum is an atomic parsing expression.
369 It matches any Unicode alphabet or digit charac‐
370 ter. This is a custom extension of PEs based on
371 Tcl's builtin command string is.
372
373 [4] The string alpha is an atomic parsing expression.
374 It matches any Unicode alphabet character. This is
375 a custom extension of PEs based on Tcl's builtin
376 command string is.
377
378 [5] The string ascii is an atomic parsing expression.
379 It matches any Unicode character below U0080. This
380 is a custom extension of PEs based on Tcl's
381 builtin command string is.
382
383 [6] The string control is an atomic parsing expres‐
384 sion. It matches any Unicode control character.
385 This is a custom extension of PEs based on Tcl's
386 builtin command string is.
387
388 [7] The string digit is an atomic parsing expression.
389 It matches any Unicode digit character. Note that
390 this includes characters outside of the [0..9]
391 range. This is a custom extension of PEs based on
392 Tcl's builtin command string is.
393
394 [8] The string graph is an atomic parsing expression.
395 It matches any Unicode printing character, except
396 for space. This is a custom extension of PEs based
397 on Tcl's builtin command string is.
398
399 [9] The string lower is an atomic parsing expression.
400 It matches any Unicode lower-case alphabet charac‐
401 ter. This is a custom extension of PEs based on
402 Tcl's builtin command string is.
403
404 [10] The string print is an atomic parsing expression.
405 It matches any Unicode printing character, includ‐
406 ing space. This is a custom extension of PEs based
407 on Tcl's builtin command string is.
408
409 [11] The string punct is an atomic parsing expression.
410 It matches any Unicode punctuation character. This
411 is a custom extension of PEs based on Tcl's
412 builtin command string is.
413
414 [12] The string space is an atomic parsing expression.
415 It matches any Unicode space character. This is a
416 custom extension of PEs based on Tcl's builtin
417 command string is.
418
419 [13] The string upper is an atomic parsing expression.
420 It matches any Unicode upper-case alphabet charac‐
421 ter. This is a custom extension of PEs based on
422 Tcl's builtin command string is.
423
424 [14] The string wordchar is an atomic parsing expres‐
425 sion. It matches any Unicode word character. This
426 is any alphanumeric character (see alnum), and any
427 connector punctuation characters (e.g. under‐
428 score). This is a custom extension of PEs based on
429 Tcl's builtin command string is.
430
431 [15] The string xdigit is an atomic parsing expression.
432 It matches any hexadecimal digit character. This
433 is a custom extension of PEs based on Tcl's
434 builtin command string is.
435
436 [16] The string ddigit is an atomic parsing expression.
437 It matches any decimal digit character. This is a
438 custom extension of PEs based on Tcl's builtin
439 command regexp.
440
441 [17] The expression [list t x] is an atomic parsing
442 expression. It matches the terminal string x.
443
444 [18] The expression [list n A] is an atomic parsing
445 expression. It matches the nonterminal A.
446
447 Combined Parsing Expressions
448
449 [1] For parsing expressions e1, e2, ... the result of
450 [list / e1 e2 ... ] is a parsing expression as
451 well. This is the ordered choice, aka prioritized
452 choice.
453
454 [2] For parsing expressions e1, e2, ... the result of
455 [list x e1 e2 ... ] is a parsing expression as
456 well. This is the sequence.
457
458 [3] For a parsing expression e the result of [list *
459 e] is a parsing expression as well. This is the
460 kleene closure, describing zero or more repeti‐
461 tions.
462
463 [4] For a parsing expression e the result of [list +
464 e] is a parsing expression as well. This is the
465 positive kleene closure, describing one or more
466 repetitions.
467
468 [5] For a parsing expression e the result of [list &
469 e] is a parsing expression as well. This is the
470 and lookahead predicate.
471
472 [6] For a parsing expression e the result of [list !
473 e] is a parsing expression as well. This is the
474 not lookahead predicate.
475
476 [7] For a parsing expression e the result of [list ?
477 e] is a parsing expression as well. This is the
478 optional input.
479
480 Canonical serialization
481 The canonical serialization of a parsing expression has the for‐
482 mat as specified in the previous item, and then additionally
483 satisfies the constraints below, which make it unique among all
484 the possible serializations of this parsing expression.
485
486 [1] The string representation of the value is the canonical
487 representation of a pure Tcl list. I.e. it does not con‐
488 tain superfluous whitespace.
489
490 [2] Terminals are not encoded as ranges (where start and end
491 of the range are identical).
492
493 EXAMPLE
494 Assuming the parsing expression shown on the right-hand side of the
495 rule
496
497 Expression <- Term (AddOp Term)*
498
499
500 then its canonical serialization (except for whitespace) is
501
502 {x {n Term} {* {x {n AddOp} {n Term}}}}
503
504
506 This document, and the package it describes, will undoubtedly contain
507 bugs and other problems. Please report such in the category pt of the
508 Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist]. Please also
509 report any ideas for enhancements you may have for either package
510 and/or documentation.
511
512 When proposing code changes, please provide unified diffs, i.e the out‐
513 put of diff -u.
514
515 Note further that attachments are strongly preferred over inlined
516 patches. Attachments can be made by going to the Edit form of the
517 ticket immediately after its creation, and then using the left-most
518 button in the secondary navigation bar.
519
521 EBNF, LL(k), PEG, TDPL, context-free languages, export, expression,
522 grammar, matching, parser, parsing expression, parsing expression gram‐
523 mar, plugin, push down automaton, recursive descent, serialization,
524 state, top-down parsing languages, transducer
525
527 Parsing and Grammars
528
530 Copyright (c) 2009 Andreas Kupries <andreas_kupries@users.sourceforge.net>
531
532
533
534
535tcllib 1 pt::peg::export::peg(n)