1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
|
'\"
'\" Generated from file 'peg\&.man' by tcllib/doctools with format 'nroff'
'\" Copyright (c) 2005 Andreas Kupries <andreas_kupries@users\&.sourceforge\&.net>
'\"
.TH "grammar::peg" n 0\&.1 tcllib "Grammar operations and usage"
.\" The -*- nroff -*- definitions below are for supplemental macros used
.\" in Tcl/Tk manual entries.
.\"
.\" .AP type name in/out ?indent?
.\" Start paragraph describing an argument to a library procedure.
.\" type is type of argument (int, etc.), in/out is either "in", "out",
.\" or "in/out" to describe whether procedure reads or modifies arg,
.\" and indent is equivalent to second arg of .IP (shouldn't ever be
.\" needed; use .AS below instead)
.\"
.\" .AS ?type? ?name?
.\" Give maximum sizes of arguments for setting tab stops. Type and
.\" name are examples of largest possible arguments that will be passed
.\" to .AP later. If args are omitted, default tab stops are used.
.\"
.\" .BS
.\" Start box enclosure. From here until next .BE, everything will be
.\" enclosed in one large box.
.\"
.\" .BE
.\" End of box enclosure.
.\"
.\" .CS
.\" Begin code excerpt.
.\"
.\" .CE
.\" End code excerpt.
.\"
.\" .VS ?version? ?br?
.\" Begin vertical sidebar, for use in marking newly-changed parts
.\" of man pages. The first argument is ignored and used for recording
.\" the version when the .VS was added, so that the sidebars can be
.\" found and removed when they reach a certain age. If another argument
.\" is present, then a line break is forced before starting the sidebar.
.\"
.\" .VE
.\" End of vertical sidebar.
.\"
.\" .DS
.\" Begin an indented unfilled display.
.\"
.\" .DE
.\" End of indented unfilled display.
.\"
.\" .SO ?manpage?
.\" Start of list of standard options for a Tk widget. The manpage
.\" argument defines where to look up the standard options; if
.\" omitted, defaults to "options". The options follow on successive
.\" lines, in three columns separated by tabs.
.\"
.\" .SE
.\" End of list of standard options for a Tk widget.
.\"
.\" .OP cmdName dbName dbClass
.\" Start of description of a specific option. cmdName gives the
.\" option's name as specified in the class command, dbName gives
.\" the option's name in the option database, and dbClass gives
.\" the option's class in the option database.
.\"
.\" .UL arg1 arg2
.\" Print arg1 underlined, then print arg2 normally.
.\"
.\" .QW arg1 ?arg2?
.\" Print arg1 in quotes, then arg2 normally (for trailing punctuation).
.\"
.\" .PQ arg1 ?arg2?
.\" Print an open parenthesis, arg1 in quotes, then arg2 normally
.\" (for trailing punctuation) and then a closing parenthesis.
.\"
.\" # Set up traps and other miscellaneous stuff for Tcl/Tk man pages.
.if t .wh -1.3i ^B
.nr ^l \n(.l
.ad b
.\" # Start an argument description
.de AP
.ie !"\\$4"" .TP \\$4
.el \{\
. ie !"\\$2"" .TP \\n()Cu
. el .TP 15
.\}
.ta \\n()Au \\n()Bu
.ie !"\\$3"" \{\
\&\\$1 \\fI\\$2\\fP (\\$3)
.\".b
.\}
.el \{\
.br
.ie !"\\$2"" \{\
\&\\$1 \\fI\\$2\\fP
.\}
.el \{\
\&\\fI\\$1\\fP
.\}
.\}
..
.\" # define tabbing values for .AP
.de AS
.nr )A 10n
.if !"\\$1"" .nr )A \\w'\\$1'u+3n
.nr )B \\n()Au+15n
.\"
.if !"\\$2"" .nr )B \\w'\\$2'u+\\n()Au+3n
.nr )C \\n()Bu+\\w'(in/out)'u+2n
..
.AS Tcl_Interp Tcl_CreateInterp in/out
.\" # BS - start boxed text
.\" # ^y = starting y location
.\" # ^b = 1
.de BS
.br
.mk ^y
.nr ^b 1u
.if n .nf
.if n .ti 0
.if n \l'\\n(.lu\(ul'
.if n .fi
..
.\" # BE - end boxed text (draw box now)
.de BE
.nf
.ti 0
.mk ^t
.ie n \l'\\n(^lu\(ul'
.el \{\
.\" Draw four-sided box normally, but don't draw top of
.\" box if the box started on an earlier page.
.ie !\\n(^b-1 \{\
\h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
.\}
.el \}\
\h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
.\}
.\}
.fi
.br
.nr ^b 0
..
.\" # VS - start vertical sidebar
.\" # ^Y = starting y location
.\" # ^v = 1 (for troff; for nroff this doesn't matter)
.de VS
.if !"\\$2"" .br
.mk ^Y
.ie n 'mc \s12\(br\s0
.el .nr ^v 1u
..
.\" # VE - end of vertical sidebar
.de VE
.ie n 'mc
.el \{\
.ev 2
.nf
.ti 0
.mk ^t
\h'|\\n(^lu+3n'\L'|\\n(^Yu-1v\(bv'\v'\\n(^tu+1v-\\n(^Yu'\h'-|\\n(^lu+3n'
.sp -1
.fi
.ev
.\}
.nr ^v 0
..
.\" # Special macro to handle page bottom: finish off current
.\" # box/sidebar if in box/sidebar mode, then invoked standard
.\" # page bottom macro.
.de ^B
.ev 2
'ti 0
'nf
.mk ^t
.if \\n(^b \{\
.\" Draw three-sided box if this is the box's first page,
.\" draw two sides but no top otherwise.
.ie !\\n(^b-1 \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
.el \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
.\}
.if \\n(^v \{\
.nr ^x \\n(^tu+1v-\\n(^Yu
\kx\h'-\\nxu'\h'|\\n(^lu+3n'\ky\L'-\\n(^xu'\v'\\n(^xu'\h'|0u'\c
.\}
.bp
'fi
.ev
.if \\n(^b \{\
.mk ^y
.nr ^b 2
.\}
.if \\n(^v \{\
.mk ^Y
.\}
..
.\" # DS - begin display
.de DS
.RS
.nf
.sp
..
.\" # DE - end display
.de DE
.fi
.RE
.sp
..
.\" # SO - start of list of standard options
.de SO
'ie '\\$1'' .ds So \\fBoptions\\fR
'el .ds So \\fB\\$1\\fR
.SH "STANDARD OPTIONS"
.LP
.nf
.ta 5.5c 11c
.ft B
..
.\" # SE - end of list of standard options
.de SE
.fi
.ft R
.LP
See the \\*(So manual entry for details on the standard options.
..
.\" # OP - start of full description for a single option
.de OP
.LP
.nf
.ta 4c
Command-Line Name: \\fB\\$1\\fR
Database Name: \\fB\\$2\\fR
Database Class: \\fB\\$3\\fR
.fi
.IP
..
.\" # CS - begin code excerpt
.de CS
.RS
.nf
.ta .25i .5i .75i 1i
..
.\" # CE - end code excerpt
.de CE
.fi
.RE
..
.\" # UL - underline word
.de UL
\\$1\l'|0\(ul'\\$2
..
.\" # QW - apply quotation marks to word
.de QW
.ie '\\*(lq'"' ``\\$1''\\$2
.\"" fix emacs highlighting
.el \\*(lq\\$1\\*(rq\\$2
..
.\" # PQ - apply parens and quotation marks to word
.de PQ
.ie '\\*(lq'"' (``\\$1''\\$2)\\$3
.\"" fix emacs highlighting
.el (\\*(lq\\$1\\*(rq\\$2)\\$3
..
.\" # QR - quoted range
.de QR
.ie '\\*(lq'"' ``\\$1''\\-``\\$2''\\$3
.\"" fix emacs highlighting
.el \\*(lq\\$1\\*(rq\\-\\*(lq\\$2\\*(rq\\$3
..
.\" # MT - "empty" string
.de MT
.QW ""
..
.BS
.SH NAME
grammar::peg \- Create and manipulate parsing expression grammars
.SH SYNOPSIS
package require \fBTcl 8\&.4\fR
.sp
package require \fBsnit \fR
.sp
package require \fBgrammar::peg ?0\&.1?\fR
.sp
\fB::grammar::peg\fR \fIpegName\fR ?\fB=\fR|\fB:=\fR|\fB<--\fR|\fBas\fR|\fBdeserialize\fR \fIsrc\fR?
.sp
\fIpegName\fR \fBdestroy\fR
.sp
\fIpegName\fR \fBclear\fR
.sp
\fIpegName\fR \fB=\fR \fIsrcPEG\fR
.sp
\fIpegName\fR \fB-->\fR \fIdstPEG\fR
.sp
\fIpegName\fR \fBserialize\fR
.sp
\fIpegName\fR \fBdeserialize\fR \fIserialization\fR
.sp
\fIpegName\fR \fBis valid\fR
.sp
\fIpegName\fR \fBstart\fR ?\fIpe\fR?
.sp
\fIpegName\fR \fBnonterminals\fR
.sp
\fIpegName\fR \fBnonterminal add\fR \fInt\fR \fIpe\fR
.sp
\fIpegName\fR \fBnonterminal delete\fR \fInt1\fR ?\fInt2\fR \&.\&.\&.?
.sp
\fIpegName\fR \fBnonterminal exists\fR \fInt\fR
.sp
\fIpegName\fR \fBnonterminal rename\fR \fInt\fR \fIntnew\fR
.sp
\fIpegName\fR \fBnonterminal mode\fR \fInt\fR ?\fImode\fR?
.sp
\fIpegName\fR \fBnonterminal rule\fR \fInt\fR
.sp
\fIpegName\fR \fBunknown nonterminals\fR
.sp
.BE
.SH DESCRIPTION
.PP
This package provides a container class for
\fIparsing expression grammars\fR (Short: PEG)\&.
It allows the incremental definition of the grammar, its manipulation
and querying of the definition\&.
The package neither provides complex operations on the grammar, nor has
it the ability to execute a grammar definition for a stream of symbols\&.
Two packages related to this one are \fBgrammar::mengine\fR and
\fBgrammar::peg::interpreter\fR\&. The first of them defines a
general virtual machine for the matching of a character stream, and
the second implements an interpreter for parsing expression grammars
on top of that virtual machine\&.
.SS "TERMS & CONCEPTS"
PEGs are similar to context-free grammars, but not equivalent; in some
cases PEGs are strictly more powerful than context-free grammars (there
exist PEGs for some non-context-free languages)\&.
The formal mathematical definition of parsing expressions and parsing
expression grammars can be found in section
\fBPARSING EXPRESSION GRAMMARS\fR\&.
.PP
In short, we have \fIterminal symbols\fR, which are the most basic
building blocks for \fIsentences\fR, and \fInonterminal symbols\fR
with associated \fIparsing expressions\fR, defining the grammatical
structure of the sentences\&. The two sets of symbols are distinctive,
and do not overlap\&. When speaking about symbols the word "symbol" is
often left out\&. The union of the sets of terminal and nonterminal
symbols is called the set of \fIsymbols\fR\&.
.PP
Here the set of \fIterminal symbols\fR is not explicitly managed,
but implicitly defined as the set of all characters\&. Note that this
means that we inherit from Tcl the ability to handle all of Unicode\&.
.PP
A pair of \fInonterminal\fR and \fIparsing expression\fR is also
called a \fIgrammatical rule\fR, or \fIrule\fR for short\&. In the
context of a rule the nonterminal is often called the left-hand-side
(LHS), and the parsing expression the right-hand-side (RHS)\&.
.PP
The \fIstart expression\fR of a grammar is a parsing expression
from which all the sentences contained in the language specified by
the grammar are \fIderived\fR\&.
To make the understanding of this term easier let us assume for a
moment that the RHS of each rule, and the start expression, is either
a sequence of symbols, or a series of alternate parsing expressions\&.
In the latter case the rule can be seen as a set of rules, each
providing one alternative for the nonterminal\&.
A parsing expression A' is now a derivation of a parsing expression A
if we pick one of the nonterminals N in the expression, and one of the
alternative rules R for N, and then replace the nonterminal in A with
the RHS of the chosen rule\&. Here we can see why the terminal symbols
are called such\&. They cannot be expanded any further, thus terminate
the process of deriving new expressions\&.
An example
.PP
.CS
Rules
(1) A <- a B c
(2a) B <- d B
(2b) B <- e
Some derivations, using starting expression A\&.
A -/1/-> a B c -/2a/-> a d B c -/2b/-> a d e c
.CE
.PP
A derived expression containing only terminal symbols is a
\fIsentence\fR\&. The set of all sentences which can be derived from
the start expression is the \fIlanguage\fR of the grammar\&.
.PP
Some definitions for nonterminals and expressions:
.IP [1]
A nonterminal A is called \fIreachable\fR if it is possible to derive
a parsing expression from the start expression which contains A\&.
.IP [2]
A nonterminal A is called \fIuseful\fR if it is possible to derive a
sentence from it\&.
.IP [3]
A nonterminal A is called \fIrecursive\fR if it is possible to derive
a parsing expression from it which contains A, again\&.
.IP [4]
The \fIFIRST set\fR of a nonterminal A contains all the symbols which
can occur of as the leftmost symbol in a parsing expression derived from
A\&. If the FIRST set contains A itself then that nonterminal is called
\fIleft-recursive\fR\&.
.IP [5]
The \fILAST set\fR of a nonterminal A contains all the symbols which
can occur of as the rightmost symbol in a parsing expression derived from
A\&. If the LAST set contains A itself then that nonterminal is called
\fIright-recursive\fR\&.
.IP [6]
The \fIFOLLOW set\fR of a nonterminal A contains all the symbols which
can occur after A in a parsing expression derived from the start
expression\&.
.IP [7]
A nonterminal (or parsing expression) is called \fInullable\fR if the
empty sentence can be derived from it\&.
.PP
.PP
And based on the above definitions for grammars:
.IP [1]
A grammar G is \fIrecursive\fR if and only if it contains a nonterminal
A which is recursive\&. The terms \fIleft-\fR and \fIright-recursive\fR,
and \fIuseful\fR are analogously defined\&.
.IP [2]
A grammar is \fIminimal\fR if it contains only \fIreachable\fR and
\fIuseful\fR nonterminals\&.
.IP [3]
A grammar is \fIwellformed\fR if it is not left-recursive\&. Such
grammars are also \fIcomplete\fR, which means that they always succeed
or fail on all input sentences\&. For an incomplete grammar on the
other hand input sentences exist for which an attempt to match them
against the grammar will not terminate\&.
.IP [4]
As we wish to allow ourselves to build a grammar incrementally in a
container object we will encounter stages where the RHS of one or more
rules reference symbols which are not yet known to the container\&. Such
a grammar we call \fIinvalid\fR\&.
We cannot use the term \fIincomplete\fR as this term is already
taken, see the last item\&.
.PP
.PP
.SS "CONTAINER CLASS API"
The package exports the API described here\&.
.TP
\fB::grammar::peg\fR \fIpegName\fR ?\fB=\fR|\fB:=\fR|\fB<--\fR|\fBas\fR|\fBdeserialize\fR \fIsrc\fR?
The command creates a new container object for a parsing expression
grammar and returns the fully qualified name of the object command as
its result\&. The API the returned command is following is described in
the section \fBCONTAINER OBJECT API\fR\&. It may be used to invoke
various operations on the container and the grammar within\&.
.sp
The new container, i\&.e\&. grammar will be empty if no \fIsrc\fR is
specified\&. Otherwise it will contain a copy of the grammar contained
in the \fIsrc\fR\&.
The \fIsrc\fR has to be a container object reference for all operators
except \fBdeserialize\fR\&.
The \fBdeserialize\fR operator requires \fIsrc\fR to be the
serialization of a parsing expression grammar instead\&.
.sp
An empty grammar has no nonterminal symbols, and the start expression
is the empty expression, i\&.e\&. epsilon\&. It is \fIvalid\fR, but not
\fIuseful\fR\&.
.PP
.SS "CONTAINER OBJECT API"
.PP
All grammar container objects provide the following methods for the
manipulation of their contents:
.TP
\fIpegName\fR \fBdestroy\fR
Destroys the grammar, including its storage space and associated
command\&.
.TP
\fIpegName\fR \fBclear\fR
Clears out the definition of the grammar contained in \fIpegName\fR,
but does \fInot\fR destroy the object\&.
.TP
\fIpegName\fR \fB=\fR \fIsrcPEG\fR
Assigns the contents of the grammar contained in \fIsrcPEG\fR to
\fIpegName\fR, overwriting any existing definition\&.
This is the assignment operator for grammars\&. It copies the grammar
contained in the grammar object \fIsrcPEG\fR over the grammar
definition in \fIpegName\fR\&. The old contents of \fIpegName\fR are
deleted by this operation\&.
.sp
This operation is in effect equivalent to
.sp
.CS
\fIpegName\fR \fBdeserialize\fR [\fIsrcPEG\fR \fBserialize\fR]
.CE
.TP
\fIpegName\fR \fB-->\fR \fIdstPEG\fR
This is the reverse assignment operator for grammars\&. It copies the
automation contained in the object \fIpegName\fR over the grammar
definition in the object \fIdstPEG\fR\&.
The old contents of \fIdstPEG\fR are deleted by this operation\&.
.sp
This operation is in effect equivalent to
.sp
.CS
\fIdstPEG\fR \fBdeserialize\fR [\fIpegName\fR \fBserialize\fR]
.CE
.TP
\fIpegName\fR \fBserialize\fR
This method serializes the grammar stored in \fIpegName\fR\&. In other
words it returns a tcl \fIvalue\fR completely describing that
grammar\&.
This allows, for example, the transfer of grammars over arbitrary
channels, persistence, etc\&.
This method is also the basis for both the copy constructor and the
assignment operator\&.
.sp
The result of this method has to be semantically identical over all
implementations of the \fBgrammar::peg\fR interface\&. This is what
will enable us to copy grammars between different implementations of
the same interface\&.
.sp
The result is a list of four elements with the following structure:
.RS
.IP [1]
The constant string \fBgrammar::peg\fR\&.
.IP [2]
A dictionary\&. Its keys are the names of all known nonterminal symbols,
and their associated values are the parsing expressions describing
their sentennial structure\&.
.IP [3]
A dictionary\&. Its keys are the names of all known nonterminal symbols,
and their associated values hints to a matcher regarding the semantic
values produced by the symbol\&.
.IP [4]
The last item is a parsing expression, the \fIstart expression\fR
of the grammar\&.
.RE
.sp
Assuming the following PEG for simple mathematical expressions
.sp
.CS
Digit <- '0'/'1'/'2'/'3'/'4'/'5'/'6'/'7'/'8'/'9'
Sign <- '+' / '-'
Number <- Sign? Digit+
Expression <- '(' Expression ')' / (Factor (MulOp Factor)*)
MulOp <- '*' / '/'
Factor <- Term (AddOp Term)*
AddOp <- '+'/'-'
Term <- Number
.CE
.sp
a possible serialization is
.sp
.CS
grammar::peg \\
{Expression {/ {x ( Expression )} {x Factor {* {x MulOp Factor}}}} \\
Factor {x Term {* {x AddOp Term}}} \\
Term Number \\
MulOp {/ * /} \\
AddOp {/ + -} \\
Number {x {? Sign} {+ Digit}} \\
Sign {/ + -} \\
Digit {/ 0 1 2 3 4 5 6 7 8 9} \\
} \\
{Expression value Factor value \\
Term value MulOp value \\
AddOp value Number value \\
Sign value Digit value \\
}
Expression
.CE
.sp
A possible one, because the order of the nonterminals in the
dictionary is not relevant\&.
.TP
\fIpegName\fR \fBdeserialize\fR \fIserialization\fR
This is the complement to \fBserialize\fR\&. It replaces the grammar
definition in \fIpegName\fR with the grammar described by the
\fIserialization\fR value\&. The old contents of \fIpegName\fR are
deleted by this operation\&.
.TP
\fIpegName\fR \fBis valid\fR
A predicate\&. It tests whether the PEG in \fIpegName\fR is \fIvalid\fR\&.
See section \fBTERMS & CONCEPTS\fR for the definition of this
grammar property\&.
The result is a boolean value\&. It will be set to \fBtrue\fR if
the PEG has the tested property, and \fBfalse\fR otherwise\&.
.TP
\fIpegName\fR \fBstart\fR ?\fIpe\fR?
This method defines the \fIstart expression\fR of the grammar\&. It
replaces the previously defined start expression with the parsing
expression \fIpe\fR\&.
The method fails and throws an error if \fIpe\fR does not contain a
valid parsing expression as specified in the section
\fBPARSING EXPRESSIONS\fR\&. In that case the existing start
expression is not changed\&.
The method returns the empty string as its result\&.
.sp
If the method is called without an argument it will return the currently
defined start expression\&.
.TP
\fIpegName\fR \fBnonterminals\fR
Returns the set of all nonterminal symbols known to the grammar\&.
.TP
\fIpegName\fR \fBnonterminal add\fR \fInt\fR \fIpe\fR
This method adds the nonterminal \fInt\fR and its associated parsing
expression \fIpe\fR to the set of nonterminal symbols and rules of the
PEG contained in the object \fIpegName\fR\&.
The method fails and throws an error if either the string \fInt\fR is
already known as a symbol of the grammar, or if \fIpe\fR does not
contain a valid parsing expression as specified in the section
\fBPARSING EXPRESSIONS\fR\&. In that case the current set of
nonterminal symbols and rules is not changed\&.
The method returns the empty string as its result\&.
.TP
\fIpegName\fR \fBnonterminal delete\fR \fInt1\fR ?\fInt2\fR \&.\&.\&.?
This method removes the named symbols \fInt1\fR, \fInt2\fR from the
set of nonterminal symbols of the PEG contained in the object
\fIpegName\fR\&.
The method fails and throws an error if any of the strings is not
known as a nonterminal symbol\&. In that case the current set of
nonterminal symbols is not changed\&.
The method returns the empty string as its result\&.
.sp
The stored grammar becomes invalid if the deleted nonterminals are
referenced by the RHS of still-known rules\&.
.TP
\fIpegName\fR \fBnonterminal exists\fR \fInt\fR
A predicate\&. It tests whether the nonterminal symbol \fInt\fR is known
to the PEG in \fIpegName\fR\&.
The result is a boolean value\&. It will be set to \fBtrue\fR if the
symbol \fInt\fR is known, and \fBfalse\fR otherwise\&.
.TP
\fIpegName\fR \fBnonterminal rename\fR \fInt\fR \fIntnew\fR
This method renames the nonterminal symbol \fInt\fR to \fIntnew\fR\&.
The method fails and throws an error if either \fInt\fR is not known
as a nonterminal, or if \fIntnew\fR is a known symbol\&.
The method returns the empty string as its result\&.
.TP
\fIpegName\fR \fBnonterminal mode\fR \fInt\fR ?\fImode\fR?
This mode returns or sets the semantic mode associated with the
nonterminal symbol \fInt\fR\&. If no \fImode\fR is specified the
current mode of the nonterminal is returned\&. Otherwise the current
mode is set to \fImode\fR\&.
The method fails and throws an error if \fInt\fR is not known as a
nonterminal\&.
The grammar interpreter implemented by the package
\fBgrammar::peg::interpreter\fR recognizes the
following modes:
.RS
.TP
value
The semantic value of the nonterminal is the abstract syntax tree
created from the AST's of the RHS and a node for the nonterminal
itself\&.
.TP
match
The semantic value of the nonterminal is an the abstract syntax tree
consisting of single a node for the string matched by the RHS\&. The ASTs
generated by the RHS are discarded\&.
.TP
leaf
The semantic value of the nonterminal is an the abstract syntax tree
consisting of single a node for the nonterminal itself\&. The ASTs
generated by the RHS are discarded\&.
.TP
discard
The nonterminal has no semantic value\&. The ASTs generated by the RHS
are discarded (as well)\&.
.RE
.TP
\fIpegName\fR \fBnonterminal rule\fR \fInt\fR
This method returns the parsing expression associated with the
nonterminal \fInt\fR\&.
The method fails and throws an error if \fInt\fR is not known as a
nonterminal\&.
.TP
\fIpegName\fR \fBunknown nonterminals\fR
This method returns a list containing the names of all nonterminal
symbols which are referenced on the RHS of a grammatical rule, but
have no rule definining their structure\&. In other words, a list of
the nonterminal symbols which make the grammar invalid\&. The grammar
is valid if this list is empty\&.
.PP
.PP
.SS "PARSING EXPRESSIONS"
.PP
Various methods of PEG container objects expect a parsing expression
as their argument, or will return such\&. This section specifies the
format such parsing expressions are in\&.
.PP
.IP [1]
The string \fBepsilon\fR is an atomic parsing expression\&. It matches
the empty string\&.
.IP [2]
The string \fBalnum\fR is an atomic parsing expression\&. It matches
any alphanumeric character\&.
.IP [3]
The string \fBalpha\fR is an atomic parsing expression\&. It matches
any alphabetical character\&.
.IP [4]
The string \fBdot\fR is an atomic parsing expression\&. It matches
any character\&.
.IP [5]
The expression
[list t \fBx\fR]
is an atomic parsing expression\&. It matches the terminal string \fBx\fR\&.
.IP [6]
The expression
[list n \fBA\fR]
is an atomic parsing expression\&. It matches the nonterminal \fBA\fR\&.
.IP [7]
For parsing expressions \fBe1\fR, \fBe2\fR, \&.\&.\&. the result of
[list / \fBe1\fR \fBe2\fR \&.\&.\&. ]
is a parsing expression as well\&.
This is the \fIordered choice\fR, aka \fIprioritized choice\fR\&.
.IP [8]
For parsing expressions \fBe1\fR, \fBe2\fR, \&.\&.\&. the result of
[list x \fBe1\fR \fBe2\fR \&.\&.\&. ]
is a parsing expression as well\&.
This is the \fIsequence\fR\&.
.IP [9]
For a parsing expression \fBe\fR the result of
[list * \fBe\fR]
is a parsing expression as well\&.
This is the \fIkleene closure\fR, describing zero or more
repetitions\&.
.IP [10]
For a parsing expression \fBe\fR the result of
[list + \fBe\fR]
is a parsing expression as well\&.
This is the \fIpositive kleene closure\fR, describing one or more
repetitions\&.
.IP [11]
For a parsing expression \fBe\fR the result of
[list & \fBe\fR]
is a parsing expression as well\&.
This is the \fIand lookahead predicate\fR\&.
.IP [12]
For a parsing expression \fBe\fR the result of
[list ! \fBe\fR]
is a parsing expression as well\&.
This is the \fInot lookahead predicate\fR\&.
.IP [13]
For a parsing expression \fBe\fR the result of
[list ? \fBe\fR]
is a parsing expression as well\&.
This is the \fIoptional input\fR\&.
.PP
.PP
Examples of parsing expressions where already shown, in the
description of the method \fBserialize\fR\&.
.SH "PARSING EXPRESSION GRAMMARS"
.PP
For the mathematically inclined, a PEG is a 4-tuple (VN,VT,R,eS) where
.IP \(bu
VN is a set of \fInonterminal symbols\fR,
.IP \(bu
VT is a set of \fIterminal symbols\fR,
.IP \(bu
R is a finite set of rules, where each rule is a pair (A,e), A in VN,
and \fIe\fR a \fIparsing expression\fR\&.
.IP \(bu
eS is a parsing expression, the \fIstart expression\fR\&.
.PP
.PP
Further constraints are
.IP \(bu
The intersection of VN and VT is empty\&.
.IP \(bu
For all A in VT exists exactly one pair (A,e) in R\&. In other words, R
is a function from nonterminal symbols to parsing expressions\&.
.PP
.PP
Parsing expression are inductively defined via
.IP \(bu
The empty string (epsilon) is a parsing expression\&.
.IP \(bu
A terminal symbol \fIa\fR is a parsing expression\&.
.IP \(bu
A nonterminal symbol \fIA\fR is a parsing expression\&.
.IP \(bu
\fIe1\fR\fIe2\fR is a parsing expression for parsing expressions
\fIe1\fR and \fI2\fR\&. This is called \fIsequence\fR\&.
.IP \(bu
\fIe1\fR/\fIe2\fR is a parsing expression for parsing expressions
\fIe1\fR and \fI2\fR\&. This is called \fIordered choice\fR\&.
.IP \(bu
\fIe\fR* is a parsing expression for parsing expression
\fIe\fR\&. This is called \fIzero-or-more repetitions\fR, also known
as \fIkleene closure\fR\&.
.IP \(bu
\fIe\fR+ is a parsing expression for parsing expression
\fIe\fR\&. This is called \fIone-or-more repetitions\fR, also known
as \fIpositive kleene closure\fR\&.
.IP \(bu
!\fIe\fR is a parsing expression for parsing expression
\fIe1\fR\&. This is called a \fInot lookahead predicate\fR\&.
.IP \(bu
&\fIe\fR is a parsing expression for parsing expression
\fIe1\fR\&. This is called an \fIand lookahead predicate\fR\&.
.PP
.PP
.PP
PEGs are used to define a grammatical structure for streams of symbols
over VT\&. They are a modern phrasing of older formalisms invented by
Alexander Birham\&. These formalisms were called TS (TMG recognition
scheme), and gTS (generalized TS)\&. Later they were renamed to TPDL
(Top-Down Parsing Languages) and gTPDL (generalized TPDL)\&.
.PP
They can be easily implemented by recursive descent parsers with
backtracking\&. This makes them relatives of LL(k) Context-Free
Grammars\&.
.SH REFERENCES
.IP [1]
\fIThe Packrat Parsing and Parsing Expression Grammars Page\fR [http://www\&.pdos\&.lcs\&.mit\&.edu/~baford/packrat/],
by Bryan Ford, Massachusetts Institute of Technology\&. This is the main
entry page to PEGs, and their realization through Packrat Parsers\&.
.IP [2]
\fIParsing Techniques - A Practical Guide \fR [http://www\&.cs\&.vu\&.nl/~dick/PTAPG\&.html], an online book
offering a clear, accessible, and thorough discussion of many
different parsing techniques with their interrelations and
applicabilities, including error recovery techniques\&.
.IP [3]
\fICompilers and Compiler Generators\fR [http://scifac\&.ru\&.ac\&.za/compilers/], an online book using
CoCo/R, a generator for recursive descent parsers\&.
.PP
.SH "BUGS, IDEAS, FEEDBACK"
This document, and the package it describes, will undoubtedly contain
bugs and other problems\&.
Please report such in the category \fIgrammar_peg\fR of the
\fITcllib Trackers\fR [http://core\&.tcl\&.tk/tcllib/reportlist]\&.
Please also report any ideas for enhancements you may have for either
package and/or documentation\&.
.PP
When proposing code changes, please provide \fIunified diffs\fR,
i\&.e the output of \fBdiff -u\fR\&.
.PP
Note further that \fIattachments\fR are strongly preferred over
inlined patches\&. Attachments can be made by going to the \fBEdit\fR
form of the ticket immediately after its creation, and then using the
left-most button in the secondary navigation bar\&.
.SH KEYWORDS
LL(k), TDPL, context-free languages, expression, grammar, parsing, parsing expression, parsing expression grammar, push down automaton, recursive descent, state, top-down parsing languages, transducer
.SH CATEGORY
Grammars and finite automata
.SH COPYRIGHT
.nf
Copyright (c) 2005 Andreas Kupries <andreas_kupries@users\&.sourceforge\&.net>
.fi
|