pt::peg::to::cparam(3tcl)
π pt::peg::to::cparam β PEG Conversion. Write CPARAM format π
okay bestie, so this is the manual page for pt::peg::to::cparam, part of Parser Tools (that's the 3tcl section of the manual, iykyk). It's giving converter, it's giving C code generation, and honestly? It's a lot. But we're gonna get through it together. π«Ά
β¨ 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 ? Do you have trouble understanding this document ? π In that case please read the overview provided by the Introduction to Parser Tools. That document is literally the entrypoint to the whole system this package is a part of. No cap.
This package implements the converter from parsing expression grammars to CPARAM markup. π
It resides in the Export section of the Core Layer of Parser Tools, and it can be used either:
- directly with the other packages of this layer, or
- indirectly through the export manager provided by
pt::peg::export.
That indirect route is intended for use in untrusted environments, and it happens through the corresponding export plugin pt::peg::export::cparam sitting between converter and export manager. It's the middle-manager of the pipeline, basically. π
IMAGE: arch_core_eplugins
π¦ API π¦
The API provided by this package satisfies the specification of the Converter API found in the Parser Tools Export API specification.
pt::peg::to::cparam resetβ This command resets the configuration of the package to its default settings. Fresh start, clean slate, main character energy. β¨pt::peg::to::cparam configureβ This command returns a dictionary containing the current configuration of the package.pt::peg::to::cparam configure optionβ This command returns the current value of the specified configuration option of the package. For the set of legal options, please read the section Options.pt::peg::to::cparam configure option value...β This command sets the given configuration options of the package, to the specified values. For the set of legal options, please read the section Options.pt::peg::to::cparam convert serialβ This command takes the canonical serialization of a parsing expression grammar, as specified in section PEG serialization format, and contained in serial, and generates CPARAM markup encoding the grammar, per the current package configuration. The created string is then returned as the result of the command. π
π OPTIONS π
The converter to C code recognizes the following configuration variables and changes its behaviour as they specify. Buckle up, there's a bunch. πΈ
-file stringβ The value of this option is the name of the file or other entity from which the grammar came, for which the command is run. The default value isunknown.-name stringβ The value of this option is the name of the grammar we are processing. The default value isa_pe_grammar.-user stringβ The value of this option is the name of the user for which the command is run. The default value isunknown.-template stringβ The value of this option is a string into which to put the generated text and the other configuration settings. The various locations for user-data are expected to be specified with the placeholders listed below. The default value is"@code@".@user@β To be replaced with the value of the option-user.@format@β To be replaced with the constantC/PARAM.@file@β To be replaced with the value of the option-file.@name@β To be replaced with the value of the option-name.@code@β To be replaced with the generated Tcl code.
The following options are special, in that they will occur within the generated code, and are replaced there as well. π
@statedecl@β To be replaced with the value of the optionstate-decl.@stateref@β To be replaced with the value of the optionstate-ref.@strings@β To be replaced with the value of the optionstring-varname.@self@β To be replaced with the value of the optionself-command.@def@β To be replaced with the value of the optionfun-qualifier.@ns@β To be replaced with the value of the optionnamespace.@main@β To be replaced with the value of the optionmain.@prelude@β To be replaced with the value of the optionprelude.
-state-decl stringβ A C string representing the argument declaration to use in the generated parsing functions to refer to the parsing state. In essence type and argument name. The default value is the stringRDE_PARAM p.-state-ref stringβ A C string representing the argument named used in the generated parsing functions to refer to the parsing state. The default value is the stringp.-self-command stringβ A C string representing the reference needed to call the generated parser function (methods ...) from another parser fonction, per the chosen framework (template). The default value is the empty string.-fun-qualifier stringβ A C string containing the attributes to give to the generated functions (methods ...), per the chosen framework (template). The default value isstatic.-namespace stringβ The name of the C namespace the parser functions (methods, ...) shall reside in, or a general prefix to add to the function names. The default value is the empty string.-main stringβ The name of the main function (method, ...) to be called by the chosen framework (template) to start parsing input. The default value is__main.-string-varname stringβ The name of the variable used for the table of strings used by the generated parser, i.e. error messages, symbol names, etc. The default value isp_string.-prelude stringβ A snippet of code to be inserted at the head of each generated parsing function. The default value is the empty string.-indent integerβ The number of characters to indent each line of the generated code by. The default value is0.-comments booleanβ A flag controlling the generation of code comments containing the original parsing expression a parsing function is for. The default value ison.
While the high parameterizability of this converter, as shown by the multitude of options it supports, is an advantage to the advanced user, allowing her to customize the output of the converter as needed, a novice user will likely not see the forest for the trees. π²π΅π« fr.
To help these latter users an adjunct package is provided, containing a canned configuration which will generate 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. It's giving bespoke. π
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 option -template. The additional options:
-fun-qualifier string-main string-namespace string-prelude string-self-command string-state-decl string-state-ref string-string-varname string
provide code snippets which help to glue framework and generated code together. Their placeholders are in the generated code. Further the options:
-indent integer-comments boolean
allow for the customization of 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 to large for this document. π iconic of it honestly.
β¨ PEG SERIALIZATION FORMAT β¨
Okay bestie, gather round, because this is where we spill the tea on how Parser Tools writes a Parsing Expression Grammar down into a value you can actually ship around, compare, whatever. π
So, here's the format the Parser Tools use to serialize PEGs as immutable values for transport, comparison, etc. π¦
We got two flavors going on here: regular and canonical. A PEG can have more than one regular serialization, but only exactly one of them is canonical. No cap. π
πΈ regular serialization πΈ
- The serialization of any PEG is a nested Tcl dictionary. πͺ
- This dictionary holds a single key,
pt::grammar::peg, and its value. This value holds the contents of the grammar. - 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.- Each nonterminal symbol may occur only once. π
- The empty string is not a legal nonterminal symbol.
- 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.modeβ the value can be one of three values specifying how a parser should handle the semantic value produced by the symbol.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. π³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. πvoidβ the nonterminal has no semantic value. Any ASTs generated by the symbol's right hand side are discarded (as well). π³οΈ
startβ the value is the serialization of the start parsing expression of the grammar, as specified in the section PE serialization format.
- 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.
π 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. Iconic behavior honestly. β¨
- 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. - The string representation of the value is the canonical representation of a Tcl dictionary. I.e. it does not contain superfluous whitespace. π§Ό
π§ 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}
}
See how every key is sitting in its sorted little spot? That's the canonical glow-up right there. π πΈ
okay so bestie, here's the tea on how Parser Tools serializes Parsing Expressions into immutable values for transport, comparison, etc. π We're distinguishing between regular and canonical serializations. A parsing expression can have more than one regular serialization, but only exactly one of them will be canonical. no cap. β¨
Regular serialization
Atomic Parsing Expressions
[1]β the stringepsilonis an atomic parsing expression. It matches the empty string. π«Ά[2]β the stringdotis an atomic parsing expression. It matches any character. π[3]β the stringalnumis 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 stringalphais 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 stringasciiis 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 stringcontrolis 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 stringdigitis an atomic parsing expression. It matches any Unicode digit character. Note that this includes characters outside of the[0..9]range. This is a custom extension of PEs based on Tcl's builtin commandstring is. π’[8]β the stringgraphis 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 stringloweris 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 stringprintis 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 stringpunctis 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 stringspaceis 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 stringupperis 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 stringwordcharis an atomic parsing expression. It matches any Unicode word character. This is any alphanumeric character (seealnum), and any connector punctuation characters (e.g. underscore). This is a custom extension of PEs based on Tcl's builtin commandstring is. π€[15]β the stringxdigitis 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 stringddigitis 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 stringx. π¬[18]β the expression[list n A]is an atomic parsing expression. It matches the nonterminalA. π·οΈ
Combined Parsing Expressions
[1]β For parsing expressionse1,e2, ... the result of[list / e1 e2 ... ]is a parsing expression as well. This is the ordered choice, aka prioritized choice. π[2]β For parsing expressionse1,e2, ... the result of[list x e1 e2 ... ]is a parsing expression as well. This is the sequence. β¨[3]β For a parsing expressionethe result of[list * e]is a parsing expression as well. This is the kleene closure, describing zero or more repetitions. π[4]β For a parsing expressionethe 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 expressionethe result of[list & e]is a parsing expression as well. This is the and lookahead predicate. π[6]β For a parsing expressionethe result of[list ! e]is a parsing expression as well. This is the not lookahead predicate. π«[7]β For a parsing expressionethe 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 make it unique among all the possible serializations of this parsing expression.
[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
Assuming the parsing expression 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}}}}
like, that's the whole vibe right there. π
BUGS, IDEAS, FEEDBACK
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]. Please also report any ideas for enhancements you may have for either package and/or documentation. π«Ά
When proposing code changes, please provide unified diffs, i.e the output of diff -u. π
Note further that attachments are strongly preferred over inlined patches. Attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar. π
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 tea, bestie! π this was tcllib 1.1.3, pt::peg::to::cparam(3tcl). periodt. β¨
π¦ where this page comes from
- debian trixie: tcllib 2.0+dfsg-4 Β· Tcl
- ubuntu questing: tcllib 2.0+dfsg-4
- ubuntu resolute: tcllib 2.0+dfsg-5 Β· Tcl
- ubuntu stonking: tcllib 2.0+dfsg-6 Β· Tcl
- 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