pt::peg::to::cparam(n)
pt::peg::to::cparam β Parser Tools π
okay bestie, this is the manual page for pt::peg::to::cparam, and it's a big one so buckle up β part 1 of 3, we're not even done yet. β¨
πΈ NAME πΈ
pt::peg::to::cparam β PEG Conversion. Write CPARAM format
π SYNOPSIS π
package require Tcl 8.5 9
package require pt::peg::to::cparam ?1.1.3?
pt::peg::to::cparam reset
pt::peg::to::cparam configure
pt::peg::to::cparam configure option
pt::peg::to::cparam configure option value...
pt::peg::to::cparam convert serial
β¨ DESCRIPTION β¨
Are you lost? π Having trouble understanding this document? fr fr, if that's you, go read the overview in the Introduction to Parser Tools first. That doc is literally the entrypoint to the whole system this package lives in. iykyk. π«Ά
So. This package implements the converter that goes from parsing expression grammars β CPARAM markup. π
It lives in the Export section of the Core Layer of Parser Tools, and you can use it two ways:
- directly, alongside the other packages in that layer, or
- indirectly, through the export manager that
pt::peg::exportgives you β which is the one you want for untrusted environments, and it works via the matching export pluginpt::peg::export::cparamsitting between converter and export manager. π§
IMAGE: arch_core_eplugins
π API π
The API this package serves up satisfies the spec of the Converter API from the Parser Tools Export API specification. no cap.
pt::peg::to::cparam resetβ resets the package configuration back to its default settings. factory reset energy, but make it cute. πpt::peg::to::cparam configureβ hands you back a dictionary holding the current configuration of the package.pt::peg::to::cparam configure optionβ gives you the current value of that one specified configuration option. wanna know which options are legal? go read the Options section below, bestie. πpt::peg::to::cparam configure option value...β sets the given configuration options of the package to the values you specify. legal options? again, Options section. πpt::peg::to::cparam convert serialβ takes the canonical serialization of a parsing expression grammar (as specified in the PEG serialization format section β that's part of the bigger docs, not this page) sitting insideserial, and generates CPARAM markup that encodes the grammar, using whatever the current package configuration is. The created string is what gets returned as the result of the command. slay. β¨
π OPTIONS π
The converter to C code recognizes the following configuration variables, and it changes its behaviour exactly as they say. periodt.
-filestring β the name of the file (or other entity) the grammar came from, the one this command is being run for. default isunknown.-namestring β the name of the grammar we're processing. default isa_pe_grammar.-userstring β the name of the user this command is being run for. default isunknown.-templatestring β a string that the generated text and the other configuration settings get dropped into. The various spots for user-data are expected to be marked with the placeholders listed below. default is"@code@".@user@β replaced with the value of the-useroption.@format@β replaced with the constantC/PARAM.@file@β replaced with the value of the-fileoption.@name@β replaced with the value of the-nameoption.@code@β replaced with the generated Tcl code.- The following options are special, in that they show up inside the generated code and get replaced there too:
@statedecl@β replaced with the value of thestate-decloption.@stateref@β replaced with the value of thestate-refoption.@strings@β replaced with the value of thestring-varnameoption.@self@β replaced with the value of theself-commandoption.@def@β replaced with the value of thefun-qualifieroption.@ns@β replaced with the value of thenamespaceoption.@main@β replaced with the value of themainoption.@prelude@β replaced with the value of thepreludeoption.
-state-declstring β a C string for the argument declaration used in the generated parsing functions to refer to the parsing state. in essence: type and argument name. default is the stringRDE_PARAM p.-state-refstring β a C string for the argument name used in the generated parsing functions to refer to the parsing state. default is the stringp.-self-commandstring β a C string for the reference needed to call the generated parser function (methods ...) from another parser fonction, per the chosen framework (template). default is the empty string.-fun-qualifierstring β a C string holding the attributes to give the generated functions (methods ...), per the chosen framework (template). default isstatic.-namespacestring β the name of the C namespace the parser functions (methods, ...) shall live in, or a general prefix to slap onto the function names. default is the empty string.-mainstring β the name of the main function (method, ...) that the chosen framework (template) calls to start parsing input. default is__main.-string-varnamestring β the name of the variable used for the table of strings the generated parser uses, i.e. error messages, symbol names, etc. default isp_string.-preludestring β a snippet of code inserted at the head of each generated parsing function. default is the empty string.-indentinteger β how many characters to indent each line of the generated code by. default is0.-commentsboolean β a flag controlling whether code comments get generated containing the original parsing expression a parsing function is for. default ison.
ngl, this converter is highkey parameterizable β look at that pile of options. For the advanced user that's a whole advantage, letting her customize the converter output however she needs. But a novice user will likely not see the forest for the trees. π
To help those latter users, an adjunct package is provided, containing a canned configuration that generates immediately useful full parsers. It is:
pt::cparam::configuration::critclβ generated parsers are embedded into a Critcl-based framework. π₯
π C/PARAM CODE REPRESENTATION OF PARSING EXPRESSION GRAMMARS π
The c format is executable code β a parser for the grammar. The parser implementation is written in C and can be tweaked to the users' needs through a multitude of options. π
The critcl format, for example, is implemented as a canned configuration of these options on top of the generator for c.
The bulk of such a framework has to be specified through the -template option. The additional options
-fun-qualifierstring-mainstring-namespacestring-preludestring-self-commandstring-state-declstring-state-refstring-string-varnamestring
provide code snippets that help glue framework and generated code together. Their placeholders are in the generated code. Further, the options
-indentinteger-commentsboolean
allow for customizing the code indent (default: none), and whether to generate comments showing the parsing expressions a function is for (default: on).
π¦ EXAMPLE π¦
We are forgoing an example of this representation, with apologies. π It would be way too large for this document.
β¨ PEG SERIALIZATION FORMAT β¨
okay bestie, this section is where we spill the tea on the format that Parser Tools uses to serialize Parsing Expression Grammars into immutable values β like, for transport, for comparison, for all of that π
We gotta distinguish between regular</> and canonical serializations, no cap. A PEG can have more than one regular serialization, but only exactly one of them will ever be canonical. periodt.
π regular serialization π
- [1] The serialization of any PEG is a nested Tcl dictionary. it's giving dictionary-ception fr fr π«Ά
- [2] This dictionary holds a single key,
pt::grammar::peg, and its value. That value holds the contents of the grammar. one key, main character energy π - [3] The contents of the grammar are a Tcl dictionary holding the set of nonterminal symbols and the starting expression. The relevant keys and their values are:
rulesβ The value is a Tcl dictionary whose keys are the names of the nonterminal symbols known to the grammar.- [1] Each nonterminal symbol may occur only once. no duplicates bestie, we don't do that here π
- [2] The empty string is not a legal nonterminal symbol. like, at all. it's giving nothing to work with π
- [3] The value for each symbol is a Tcl dictionary itself. The relevant keys and their values in this dictionary are:
isβ The value is the serialization of the parsing expression describing the symbols sentennial structure, as specified in the section PE serialization format. that's the whole shape of it β¨modeβ The value can be one of three values specifying how a parser should handle the semantic value produced by the symbol. three vibes, pick one:valueβ The semantic value of the nonterminal symbol is an abstract syntax tree consisting of a single node node for the nonterminal itself, which has the ASTs of the symbol's right hand side as its children. full tree, all the kids included π³leafβ The semantic value of the nonterminal symbol is an abstract syntax tree consisting of a single node node for the nonterminal, without any children. Any ASTs generated by the symbol's right hand side are discarded. bye bye little subtrees πvoidβ The nonterminal has no semantic value. Any ASTs generated by the symbol's right hand side are discarded (as well). truly nothing. it's giving silence π³οΈ
startβ The value is the serialization of the start parsing expression of the grammar, as specified in the section PE serialization format. this is where the whole thing kicks off π«
- [4] The terminal symbols of the grammar are specified implicitly as the set of all terminal symbols used in the start expression and on the RHS of the grammar rules. like, they're just there, you don't declare them, you find them. lowkey mystical π¦
πΈ canonical serialization πΈ
The canonical serialization of a grammar has the format as specified in the previous item, and then additionally satisfies the constraints below β which make it unique among all the possible serializations of this grammar. one true version, and that's on periodt π
- [1] The keys found in all the nested Tcl dictionaries are sorted in ascending dictionary order, as generated by Tcl's builtin command
lsort -increasing -dict. alphabetical, bestie, always π - [2] The string representation of the value is the canonical representation of a Tcl dictionary. I.e. it does not contain superfluous whitespace. no extra spaces, no crumbs left behind π§Ή
π EXAMPLE π
Assuming the following PEG for simple mathematical expressions:
PEG calculator (Expression)
Digit <- '0'/'1'/'2'/'3'/'4'/'5'/'6'/'7'/'8'/'9' ;
Sign <- '-' / '+' ;
Number <- Sign? Digit+ ;
Expression <- Term (AddOp Term)* ;
MulOp <- '*' / '/' ;
Term <- Factor (MulOp Factor)* ;
AddOp <- '+'/'-' ;
Factor <- '(' Expression ')' / Number ;
END;
then its canonical serialization (except for whitespace) is:
pt::grammar::peg {
rules {
AddOp {is {/ {t -} {t +}} mode value}
Digit {is {/ {t 0} {t 1} {t 2} {t 3} {t 4} {t 5} {t 6} {t 7} {t 8} {t 9}} mode value}
Expression {is {x {n Term} {* {x {n AddOp} {n Term}}}} mode value}
Factor {is {/ {x {t (} {n Expression} {t )}} {n Number}} mode value}
MulOp {is {/ {t *} {t /}} mode value}
Number {is {x {? {n Sign}} {+ {n Digit}}} mode value}
Sign {is {/ {t -} {t +}} mode value}
Term {is {x {n Factor} {* {x {n MulOp} {n Factor}}}} mode value}
}
start {n Expression}
}
like, look at that. every rule in alphabetical order, every value in value mode, the whole thing squeaky clean. she ate and left no crumbs πβ¨
okay so here's the whole β¨teaβ¨ on the format the Parser Tools use to serialize Parsing Expressions as immutable values β for transport, comparison, and honestly just so we can compare them without drama π
Big distinction bestie: regular serializations vs canonical ones. A parsing expression can have more than one regular serialization, but exactly one of them will be canonical. One. Single. π
πΈ Regular serialization πΈ
π§ Atomic Parsing Expressions π§
- [1] The string
epsilonis an atomic parsing expression. It matches the empty string. It's giving absolutely nothing and that's the whole point π«Ά - [2] The string
dotis an atomic parsing expression. It matches any character. literally any one π - [3] The string
alnumis an atomic parsing expression. It matches any Unicode alphabet or digit character. This is a custom extension of PEs based on Tcl's builtin commandstring is⨠- [4] The string
alphais an atomic parsing expression. It matches any Unicode alphabet character. This is a custom extension of PEs based on Tcl's builtin commandstring is⨠- [5] The string
asciiis an atomic parsing expression. It matches any Unicode character belowU0080. This is a custom extension of PEs based on Tcl's builtin commandstring is⨠- [6] The string
controlis an atomic parsing expression. It matches any Unicode control character. This is a custom extension of PEs based on Tcl's builtin commandstring is⨠- [7] The string
digitis an atomic parsing expression. It matches any Unicode digit character. Heads up though: this includes characters outside the[0..9]range π. This is a custom extension of PEs based on Tcl's builtin commandstring isβ¨ - [8] The string
graphis an atomic parsing expression. It matches any Unicode printing character, except for space. This is a custom extension of PEs based on Tcl's builtin commandstring is⨠- [9] The string
loweris an atomic parsing expression. It matches any Unicode lower-case alphabet character. This is a custom extension of PEs based on Tcl's builtin commandstring is⨠- [10] The string
printis an atomic parsing expression. It matches any Unicode printing character, including space. This is a custom extension of PEs based on Tcl's builtin commandstring is⨠- [11] The string
punctis an atomic parsing expression. It matches any Unicode punctuation character. This is a custom extension of PEs based on Tcl's builtin commandstring is⨠- [12] The string
spaceis an atomic parsing expression. It matches any Unicode space character. This is a custom extension of PEs based on Tcl's builtin commandstring is⨠- [13] The string
upperis an atomic parsing expression. It matches any Unicode upper-case alphabet character. This is a custom extension of PEs based on Tcl's builtin commandstring is⨠- [14] The string
wordcharis an atomic parsing expression. It matches any Unicode word character β that's any alphanumeric character (seealnum), plus any connector punctuation characters (e.g. underscore). This is a custom extension of PEs based on Tcl's builtin commandstring isβ¨ - [15] The string
xdigitis an atomic parsing expression. It matches any hexadecimal digit character. This is a custom extension of PEs based on Tcl's builtin commandstring is⨠- [16] The string
ddigitis an atomic parsing expression. It matches any decimal digit character. This is a custom extension of PEs based on Tcl's builtin commandregexp⨠- [17] The expression
[list t x]is an atomic parsing expression. It matches the terminal string x. - [18] The expression
[list n A]is an atomic parsing expression. It matches the nonterminal A.
π₯ Combined Parsing Expressions π₯
- [1] For parsing expressions e1, e2, ..., the result of
[list / e1 e2 ... ]is a parsing expression as well. This is the ordered choice, aka prioritized choice. Main character energy fr π - [2] For parsing expressions e1, e2, ..., the result of
[list x e1 e2 ... ]is a parsing expression as well. This is the sequence. - [3] For a parsing expression e, the result of
[list * e]is a parsing expression as well. This is the kleene closure, describing zero or more repetitions. - [4] For a parsing expression e, the result of
[list + e]is a parsing expression as well. This is the positive kleene closure, describing one or more repetitions. - [5] For a parsing expression e, the result of
[list & e]is a parsing expression as well. This is the and lookahead predicate. - [6] For a parsing expression e, the result of
[list ! e]is a parsing expression as well. This is the not lookahead predicate. - [7] For a parsing expression e, the result of
[list ? e]is a parsing expression as well. This is the optional input.
π¦ Canonical serialization π¦
The canonical serialization of a parsing expression has the format as specified in the previous item, and then additionally satisfies the constraints below β which is what makes it unique among all the possible serializations of this parsing expression. It's the one that ate, no cap π
- [1] The string representation of the value is the canonical representation of a pure Tcl list. I.e. it does not contain superfluous whitespace.
- [2] Terminals are not encoded as ranges (where start and end of the range are identical).
π EXAMPLE π
Say the parsing expression is the one shown on the right-hand side of the rule
Expression <- Term (AddOp Term)*
then its canonical serialization (except for whitespace) is
{x {n Term} {* {x {n AddOp} {n Term}}}}
look at her. iconic. β¨
π BUGS, IDEAS, FEEDBACK π
Not to be dramatic but this document, and the package it describes, will undoubtedly contain bugs and other problems π. Please report such in the category pt of the Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist]. Also pls pls pls report any ideas for enhancements you may have for either the package and/or the documentation π«Ά
When proposing code changes, please provide unified diffs, i.e the output of diff -u.
Also note: attachments are strongly preferred over inlined patches. How do you attach? Go to the Edit form of the ticket immediately after its creation, and then use the left-most button in the secondary navigation bar. iykyk πΈ
π KEYWORDS π
- CPARAM
- EBNF
- LL(k)
- PEG
- TDPL
- context-free languages
- conversion
- expression
- format conversion
- grammar
- matching
- parser
- parsing expression
- parsing expression grammar
- push down automaton
- recursive descent
- serialization
- state
- top-down parsing languages
- transducer
π CATEGORY π
Parsing and Grammars
Β© COPYRIGHT Β©
Copyright (c) 2009 Andreas Kupries <andreas_kupries@users.sourceforge.net>
and that's the whole page bestie π this one came to you straight from tcllib, version 1.1.3 π go forth and serialize your parsing expressions canonically, you absolute queen π
π¦ where this page comes from
- fedora 43: tcllib 2.0-3.fc43 Β· TCL
- fedora 44: tcllib 2.0-4.fc44 Β· TCL
- nixos 26.05: tcllib 2.0
- nixpkgs unstable: tcllib 2.0
- Copyright (c) 2009 Andreas Kupries <andreas_kupries@users\&.sourceforge\&.net>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms