pt::pgen(3tcl)

πŸ“¦
version 2.0 Β· 2 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’– pt::pgen β€” the Parser Generator, bestie edition πŸ’–

okay so you found the manual page for pt::pgen from the tcllib crew (it lives in section 3tcl of the manual, iykyk) β€” and honestly? it's giving machine that writes parsers for you. let's get into it πŸ¦‹


✨ SYNOPSIS ✨

package require Tcl 8.5 9

package require pt::pgen ?1.2?

::pt::pgen inputformat text resultformat ?options...?

like. three lines. and one of them is the whole entire vibe. slay πŸ’…


🌸 DESCRIPTION 🌸

Are you lost? Are you, like, actually struggling to understand this document? no shame, bestie, it happens 😭 in that case please go read the overview provided by the Introduction to Parser Tools. that document is literally the entrypoint to the whole system that this package is a part of, so start there fr fr.

This package provides a command implementing a parser generator that takes parsing expression grammars as input. yes. a whole generator. for parsers. iconic behaviour honestly πŸ‘‘

It is the implementation of method generate of pt, the Parser Tools Application.

Which means β€” and this is the important part β€” the intended audience of this document are people wishing to modify and/or extend this part of pt's functionality. If you're just a user of pt, you are hereby refered to the application's manpage, i.e. Parser Tools Application. not me gatekeeping, just pointing you to the right doc 🫢

It resides in the User Package Layer of Parser Tools.

IMAGE: arch_user_pkg


πŸŽ€ API πŸŽ€

πŸ’œ ::pt::pgen inputformat text resultformat ?options...?

This command takes the parsing expression grammar in text (in the format specified by inputformat), and returns the same grammar in the format resultformat as the result of the command. yes bestie, it's a glow-up machine for grammars ✨

input formats (the way in πŸ¦‹)

The two known input formats are peg and json. Introductions to them β€” including their formal specifications β€” can be found in the PEG Language Tutorial and The JSON Grammar Exchange Format. The packages used to parse these formats are:

  • peg β€” pt::peg::from::peg
  • json β€” pt::peg::from::json

output formats (the way out πŸ‘‘)

On the output side, the known formats, and the packages used to generate them, are:

  • c β€” pt::peg::to::cparam
  • container β€” pt::peg::to::container
  • critcl β€” pt::peg::to::cparam + pt::cparam::configuration::critcl
  • json β€” pt::peg::to::json
  • oo β€” pt::peg::to::tclparam + pt::tclparam::configuration::tcloo
  • peg β€” pt::peg::to::peg
  • snit β€” pt::peg::to::tclparam + pt::tclparam::configuration::snit

The options supported by each of these formats are documented with their respective packages. so like, go read those pages for the flag drama, this one isn't holding them for you πŸ’–


🌈 EXAMPLE 🌈

In this section we are working a complete example, starting with a PEG grammar and ending with running the parser generated from it over some input, following the outline shown in the figure below:

IMAGE: flow

Our grammar, assumed to the stored in the file "calculator.peg", is

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;

From this we create a snit-based parser using the script "gen"

package require Tcl 8.5
package require fileutil
package require pt::pgen

lassign $argv name
set grammar [fileutil::cat $name.peg]
set pclass  [pt::pgen peg $gr snit -class $name -file  $name.peg -name  $name]
fileutil::writeFile $name.tcl $pclass
exit 0

calling it like

 tclsh8.5 gen calculator

which leaves us with the parser package and class written to the file "calculator.tcl". Assuming that this package is then properly installed in a place where Tcl can find it we can now use this class via a script like

   package require calculator

   lassign $argv input
   set channel [open $input r]

   set parser [calculator]
   set ast [$parser parse $channel]
   $parser destroy
   close $channel

   ... now process the returned abstract syntax tree ...

where the abstract syntax tree stored in the variable will look like

set ast {Expression 0 4
    {Factor 0 4
        {Term 0 2
            {Number 0 2
                {Digit 0 0}
                {Digit 1 1}
                {Digit 2 2}
            }
        }
        {AddOp 3 3}
        {Term 4 4
            {Number 4 4
                {Digit 4 4}
            }
        }
    }
}

assuming that the input file and channel contained the text

 120+5

A more graphical representation of the tree would be

.nf +- Digit 0 0 | 1 |            | +- Term 0 2 --- Number 0 2 -+- Digit
1 1 | 2 |                           |            | |
+- Digit 2 2 | 0 |                                        | Expression 0
4 --- Factor 0 4 -+----------------------------- AddOp 3 3 | + |
| +- Term 4 4 --- Number 4 4 --- Digit 4 4 | 5 .fi

Regardless, at this point it is the user's responsibility to work with the tree to reach whatever goal she desires. I.e. analyze it, transform it, etc. The package pt::ast should be of help here, providing commands to walk such ASTs structures in various ways. she's got you 🫢

One important thing to note is that the parsers used here return a data structure representing the structure of the input per the grammar underlying the parser. There are no callbacks during the parsing process, i.e. no parsing actions, as most other parsers will have. no side quests, just tree 🌳

Going back to the last snippet of code, the execution of the parser for some input β€” note how the parser instance follows the specified Parser API. periodt.


😭 BUGS, IDEAS, FEEDBACK 😭

This document, and the package it describes, will undoubtedly contain bugs and other problems. like, obviously, it's software πŸ’€ 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. we love ideas here, no cap.

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. (yes, the left-most one. the icon. you got this πŸ’…)


⭐ KEYWORDS ⭐

EBNF, LL(k), PEG, TDPL, context-free languages, expression, grammar, matching, parser, parsing expression, parsing expression grammar, push down automaton, recursive descent, state, top-down parsing languages, transducer


πŸ‘‘ CATEGORY πŸ‘‘

Parsing and Grammars


Copyright (c) 2009 Andreas Kupries <andreas_kupries@users.sourceforge.net>


and that's the whole page, bestie!! πŸ’– brought to you by tcllib, version 1.2, living its best life as pt::pgen(3tcl). go generate some parsers, you absolute icon πŸ¦‹βœ¨

πŸ“¦ where this page comes from