gram...ycock(n)
π NAME π
grammar::aycock β the Aycock-Horspool-Earley parser generator for Tcl, bestie!! πβ¨ This is the page for gram...ycock(n), and yes the name is abbreviated because Unix man pages are dramatic like that. π
β¨ SYNOPSIS β¨
package require Tcl 8.5 9
package require grammar::aycock ?1.1?
::aycock::parser grammar ?-verbose?
parserName parse symList valList ?clientData?
parserName destroy
parserName terminals
parserName nonterminals
parserName save
πΈ DESCRIPTION πΈ
okay so here's the tea β β the grammar::aycock package implements a parser generator for the class of parsers described in John Aycock and R. Nigel Horspool. Practical Earley Parsing. The Computer Journal, 45(6):620-630, 2002. π
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.12.4254
π¦ PROCEDURES π¦
The grammar::aycock package exports literally one single procedure, and it's a baddie:
::aycock::parsergrammar ?-verbose? β Generates a parser for the given grammar, and returns its name. If you hand it the optional-verboseflag, it dumps verbose info about the generated parser to standard output (so much yapping, we love to see it π¬). The parser it hands back is an object that accepts the commands listed down in OBJECT COMMAND. π
π OBJECT COMMAND π
parserName parsesymList valList ?clientData? β Invokes a parser you got back from::aycock::parser. symList is a list of grammar symbols representing the terminals in an input string, and valList is a list of their semantic values. The result is the semantic value of the entire string once it's parsed. β¨parserName destroyβ Destroys a parser that was constructed by::aycock::parser. bye bestie πparserName terminalsβ Returns a list of terminal symbols that may be presented in the symList argument to theparseobject command.parserName nonterminalsβ Returns a list of nonterminal symbols that were defined in the parser's grammar.parserName saveβ Returns a Tcl script that will reconstruct the parser without needing all the mechanism of the parser generator at run time π§. The reconstructed parser depends on a set of commands in the packagegrammar::aycock::runtime, which is also automatically loaded whenever thegrammar::aycockpackage gets loaded. convenient? iconic.
πΈ DESCRIPTION πΈ
The grammar::aycock::parser command accepts a grammar expressed as a Tcl list. That list has to be structured as the concatenation of a set of rules. Each rule is made up of a variable number of elements in the list:
- The name of the nonterminal symbol that the rule reduces.
- The literal string,
::= - Zero or more names of terminal or nonterminal symbols that comprise the right-hand-side of the rule.
- Finally, a Tcl script to execute when the rule is reduced. Inside that script, a variable called
_contains a list of the semantic values of the symbols on the right-hand side. The value returned by the script is expected to be the semantic value of the left-hand side. If the clientData parameter was passed to theparsemethod, it's available in a variable calledclientData. It's permissible for the script to be the empty string β in that case, the semantic value of the rule will be the same as the semantic value of the first symbol on the right-hand side. And if the right-hand side is also empty, the semantic value will be the empty string. π«Ά
Parsing is done with an Earley parser, which is not terribly efficient in speed or memory consumption π, but which deals effectively with ambiguous grammars. For this reason, the grammar::aycock package is perhaps best adapted to natural-language processing, or to the parsing of extraordinarily complex languages in which ambiguity can be tolerated. it's giving "slow but emotionally intelligent" π
π EXAMPLE π
The following code demonstrates a trivial desk calculator, admitting only +, * and parentheses as its operators. It also shows the format in which the lexical analyzer is expected to present terminal symbols to the parser. β¨
set p [aycock::parser {
start ::= E {}
E ::= E + T {expr {[lindex $_ 0] + [lindex $_ 2]}}
E ::= T {}
T ::= T * F {expr {[lindex $_ 0] * [lindex $_ 2]}}
T ::= F {}
F ::= NUMBER {}
F ::= ( E ) {lindex $_ 1}
}]
puts [$p parse {( NUMBER + NUMBER ) * ( NUMBER + NUMBER ) } {{} 2 {} 3 {} {} {} 7 {} 1 {}}]
$p destroy
The example, when run, prints 40. π math ate, no cap.
π₯ KEYWORDS π₯
Aycock, Earley, Horspool, parser, compiler
π KEYWORDS π
ambiguous, aycock, earley, grammar, horspool, parser, parsing, transducer
β CATEGORY β
Grammars and finite automata
π COPYRIGHT π
Copyright (c) 2006 by Kevin B. Kenny <kennykb@acm.org>
Redistribution permitted under the terms of the Open Publication License <http://www.opencontent.org/openpub/>
okay that's the whole page bestie, every last flag and citation accounted for, we don't gatekeep here π
signing off from the gram...ycock(n) page β tcllib, version 1.1 β¨π¦π
π¦ 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) 2006 by Kevin B\&. Kenny <kennykb@acm\&.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms