Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | import yeti 0.4.2 |
---|---|
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
b2a387058e022c73b6801061d9b14573 |
User & Date: | chw 2019-11-10 17:59:35.897 |
Context
2019-11-10
| ||
18:08 | add tcllib upstream changes check-in: e0308f0c75 user: chw tags: trunk | |
17:59 | import yeti 0.4.2 check-in: b2a387058e user: chw tags: trunk | |
2019-11-09
| ||
10:56 | correct an error path in [56ed33b76a] check-in: cb738b7de0 user: chw tags: trunk | |
Changes
Changes to assets/INVENTORY.
︙ | ︙ | |||
95 96 97 98 99 100 101 102 103 | {opt assets/VecTcLab*} {opt assets/vfs* {libs/$arch/libtclvfs.so jni/tclvfs}} {opt assets/vnc* {libs/$arch/libtkvnc.so jni/tkvnc}} {opt assets/vqtcl* {libs/$arch/libvqtcl.so jni/tclkit}} {opt assets/vu* {libs/$arch/libvu.so jni/vu}} {opt assets/wibble*} {opt assets/xotcl* {libs/$arch/libxotcl.so jni/xotcl}} {opt assets/zbar* {libs/$arch/libzbar.so jni/ZBar}} {opt assets/zint* {libs/$arch/libzint.so jni/zint}} | > | 95 96 97 98 99 100 101 102 103 104 | {opt assets/VecTcLab*} {opt assets/vfs* {libs/$arch/libtclvfs.so jni/tclvfs}} {opt assets/vnc* {libs/$arch/libtkvnc.so jni/tkvnc}} {opt assets/vqtcl* {libs/$arch/libvqtcl.so jni/tclkit}} {opt assets/vu* {libs/$arch/libvu.so jni/vu}} {opt assets/wibble*} {opt assets/xotcl* {libs/$arch/libxotcl.so jni/xotcl}} {opt assets/yeti*} {opt assets/zbar* {libs/$arch/libzbar.so jni/ZBar}} {opt assets/zint* {libs/$arch/libzint.so jni/zint}} |
Added assets/yeti0.4.2/README.
> > > > > > > > > | 1 2 3 4 5 6 7 8 9 | This directory contains yeti and ylex, version 4c010893cd39. Yeti is released under BSD license without any warranties. Copyright (c) 2004 Frank Pilhofer, yeti@fpx.de Copyright (c) 2013 Steve Havelka, steve@arieslabs.com Original but vanished source: https://bitbucket.org/smh377/yeti https://bitbucket.org/smh377/yeti/get/4c010893cd39.zip |
Added assets/yeti0.4.2/pkgIndex.tcl.
> > | 1 2 | package ifneeded ylex 0.4.2 [list source [file join $dir ylex.tcl]] package ifneeded yeti 0.4.2 [list source [file join $dir yeti.tcl]] |
Added assets/yeti0.4.2/yeti.n.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | '\"" '\" Copyright (c) 2001 Frank Pilhofer" '\"" '\".so man.macros" .TH YETI n 0.4 yeti "Yet another Tcl Interpreter" .BS .SH NAME Yeti \- Yet another Tcl Interpreter .SH SYNOPSIS .PD 0 \fBpackage require yeti ?0.4?\fR .PD .PP .PD 0 \fByeti::yeti\fR \fIname\fR \fR?\fIoptions\fR?\fR .PP \fIname\fR \fBadd\fR \fIlhs\fR \fIrhs\fR \fR?\fIscript\fR?\fR .PP \fIname\fR \fBadd\fR \fIargs\fR .PP \fIname\fR \fBcode\fR \fItoken\fR \fIscript\fR .PP \fIname\fR \fBdump\fR .PP \fIname\fR \fBconfigure\fR \fB-name\fR \fR?\fIvalue\fR?\fR .PP \fIname\fR \fBconfigure\fR \fB-start\fR \fR?\fIvalue\fR?\fR .PP \fIname\fR \fBconfigure\fR \fB-verbose\fR \fR?\fIvalue\fR?\fR .PP \fIname\fR \fBconfigure\fR \fB-verbout\fR \fR?\fIvalue\fR?\fR .PD .BE .SH PARSER SYNOPSIS .PP .PD 0 \fIparserName\fR \fIobjName\fR \fR?\fIoptions\fR?\fR .PP \fIobjName\fR \fBparse\fR .PP \fIobjName\fR \fBstep\fR .PP \fIobjName\fR \fBreset\fR .PP \fIobjName\fR \fBconfigure\fR \fB-scanner\fR \fR?\fIobject\fR?\fR .PP \fIobjName\fR \fBconfigure\fR \fB-verbose\fR \fR?\fIvalue\fR?\fR .PD .SH DESCRIPTION .PP This manual page describes \fByeti\fR, a parser generator that is modeled after the standard \fByacc\fR package (and its incarnation as GNU \fBbison\fR), which is used to create parsers in the C programming language. .PP Parsers are used to parse an input (a stream of terminals) according to a \fIgrammar\fR. This grammar is defined by a number of \fIrules\fR. \fByeti\fR supports a subclass of context-free grammars named LALR(1), which is the class of context-free grammars that can be parsed using a single lookahead token. .PP Rules in \fByeti\fR are written similar to the Backus-Naur Form (BNF). Rules have a \fInon-terminal\fR as left-hand side (LHS) and a list of non-terminals and \fIterminals\fR on the right-hand side (RHS). Non-terminals are parsed according to these rules, while terminals are read from the input. This way, all non-terminals are ultimately decomposed into sequences of terminals. There may be multiple rules with the same non-terminal on the LHS but different RHSs. The RHS may be empty. .PP \fByeti\fR does not do the parsing by itself, rather it is used, quite like \fByacc\fR, to generate parsers (by way of the \fBdump\fR method). Generated parsers are \fB[incr Tcl]\fR classes that act independently of \fByeti\fR, see the \fBParser\fR section below. These parsers can be customized; you can use the \fBcode\fR method to add user variables and methods to the class. .SH COMMANDS .TP \fByeti::yeti\fR \fIname\fR \fR?\fIoptions\fR?\fR Creates a new parser generator by the name of \fIname\fR. The new parser generator has an empty set of rules. \fIoptions\fR can be used to configure the parser generator's public variables, see the \fIvariables\fR section below. .SH METHODS .TP \fIname\fR \fBadd\fR \fIlhs\fR \fIrhs\fR \fR?\fIscript\fR?\fR .PD 0 .TP \fIname\fR \fBadd\fR \fIargs\fR .PD Adds new rules to the parser. In the first form, \fIlhs\fR is a non-terminal, \fIrhs\fR is a (possibly empty) list of terminals and non-terminals. If this rule is matched, \fIscript\fR is executed. In the second form, \fIargs\fR is a list consisting of triplets, so the number of elements must be divisible by three. Each triplet represents a rule. The first element of each triplet is the \fIlhs\fR, the second element is the \fIrhs\fR, and the third element is a \fIscript\fR, as above. In the second and following triplets, \fB|\fR (vertical bar, pipe symbol) can be used as \fIlhs\fR, referring to the \fIlhs\fR of the previous rule. The minus sign \fB-\fR can be used for \fIcode\fR to indicate that no script is associated with this rule. If a rule is matched, its corresponding script is executed, see the \fBscripts\fR section below. .TP \fIname\fR \fBcode\fR \fItoken\fR \fIscript\fR Adds custom user code to the generated parser. \fItoken\fR must be one of .RS .IP \fBconstructor\fR Defines the class's constructor. The \fIscript\fR may have any of the formats allowed by \fB[incr Tcl]\fR, without the \fBconstructor\fR keyword. .IP \fBdestructor\fR Defines the body of the class's destructor. .IP \fBerror\fR Defines the body of the error handler that is called whenever an error occurs, e.g. parse errors or errors executing a rule's script. \fIscript\fR has access to the \fByyerrmsg\fR parameter, which contains a string with a description of the error and its cause. \fIscript\fR is supposed to inform the user of the problem. The default implementations prints the message to the channel set in the \fBverbout\fR variable. \fIscript\fR is expected to return normally; the parser then returns from the current invocation with the original error. .IP \fBreset\fR The \fIscript\fR is added to the body of the parser's \fIreset\fR method. .IP \fBreturndefault\fR The \fIscript\fR is executed by the parser whenever the code associated with a non-terminal is omitted or does not execute a return. The result or return value of \fIscript\fR is used as the value of the non-terminal. The default \fBreturndefault\fR script returns the leftmost item on the RHS (\fB$1\fR). If \fIscript\fR is set to empty string, the result of the code associated with a non-terminal is used as its value even if return was not executed. .IP \fBpublic\fR .PD 0 .IP \fBprotected\fR .IP \fBprivate\fR Defines public, protected or private class members. The \fIscript\fR may contain many different member definitions, like the respective keywords in an \fB[incr Tcl]\fR class definition. .PD .RE .TP \fIname\fR \fBdump\fR Returns a script containing the generated parser. This method is called after all configuration options have been set and all rules have been added; the parser generator object is usually deleted after this method has been called. The returned script can be passed to \fBeval\fR for instant usage, or saved to a file, from where it can later be sourced without involvement of the parser generator. .SH VARIABLES .TP \fIname\fR \fBconfigure\fR \fB-name\fR \fR?\fIvalue\fR?\fR Defines the class name of the generated parser class. The default value is \fBunnamed\fR. .TP \fIname\fR \fBconfigure\fR \fB-start\fR \fR?\fIvalue\fR?\fR Defines the starting non-terminal for the parser. There must be at least one rule with this starting non-terminal as the left hand side. The default value is \fBstart\fR. .TP \fIname\fR \fBconfigure\fR \fB-verbose\fR \fR?\fIvalue\fR?\fR If the value of the \fBverbose\fR variable is non-zero, the parser prints some debug information about the generated parser, like the state machines that are generated from the rule set. .TP \fIname\fR \fBconfigure\fR \fB-verbout\fR \fR?\fIvalue\fR?\fR Sets the target channel for debug information. The default value is \fBstderr\fR. .SH NOTES .PP No checks are done whether all non-terminals are reachable. Also, no checks are done to ensure that all non-terminals are defined, i.e. there is at least one rule with the non-terminal as LHS. .PP Using uppercase names for terminals and lowercase names for non-terminals is a common convention that is not enforced, and \fByeti\fR assumes all undefined tokens to be terminals. .PP Set the \fIverbose\fR variable to a positive value to see warnings about reduce/reduce conflicts. Shift/reduce conflicts are not reported; \fByeti\fR always prefers shifts over reductions. .SH PARSER USAGE .PP Parsers are independent of \fByeti\fR, their only dependency is on \fB[incr Tcl]\fR. .PP Parsers read terminals from a \fIscanner\fR and try to match them against the rule set. Starting from the \fBstart\fR non-terminal, the parser looks for a rule that accepts the terminal. Whenever a rule is matched, the script associated with the rule is executed. The values for each element on the RHS are made available to the script in the variables \fB$\fI<i>\fR, where \fI<i>\fR is the index of the item. The script can use these values to compute its own result. .PP Values for terminals are read from the \fIscanner\fR, values for non-terminals are the return values of the code associated with a rule that produced the non-terminal. If the code does not execute a \fBreturn\fR, or if there is no code associated with a rule, the \fBreturndefault\fR script is executed to obtain the value of the non-terminal. If the user did not override \fBreturndefault\fR, the value of the leftmost item on the RHS (\fB$1\fR) is used as result (see the example below). .PP Parsers are \fB[incr Tcl]\fR objects, so its usual rules of object handling (e.g. deletion of parsers) apply. .SH PARSER COMMANDS .TP \fIparserName\fR \fIobjName\fR \fR?\fIoptions\fR?\fR Creates a new parser instance by the name of \fIobjName\fR. \fIoptions\fR can be used to configure the parser's public variables, see the \fIparser variables\fR section below. .SH PARSER METHODS .TP \fIobjName\fR \fBparse\fR Runs the parser. Tokens are read from the scanner as necessary and matched against terminals. The parsing process runs to completion, i.e. until a rule for the \fBstart\fR token has been executed and the end of input has been reached. The value returned from the last rule is returned as result of \fBparse\fR, but it is also very common for parsers to leave data in variables rather than returning a result. .TP \fIobjName\fR \fBstep\fR Single-steps the parser. One step is either the shifting of a token or a reduction. This method may be useful e.g. to do other work in parallel to the parsing. The method returns a list of length two; the first element is the token just shifted or reduced, and the second element is the associated data. Parsing is finished if the token \fB__init__\fR has been reduced. .TP \fIobjName\fR \fBreset\fR Resets the parser to the initial state, e.g. to start parsing new input. The scanner must be reset or replaced separately. .SH PARSER VARIABLES .TP \fIobjName\fR \fBconfigure\fR \fB-scanner\fR \fR?\fIobject\fR?\fR The \fBscanner\fR variable holds the scanner from which terminals are read. The scanner must be configured before parsing can begin. By default, the scanner is not deleted; if desired, this can be done in custom destructor code (see the parser generator's \fBcode\fR method). .RS .PP \fIobject\fR must be a command; the parser calls this command with \fBnext\fR as parameter whenever a new terminal needs to be read. The value returned by the command is expected to be a list of length one or two. The first item of this list is considered to be a terminal, and the second item is considered to be the value associated with this terminal. If the list has only one element, the value is assumed to be the empty string. At the end of input, an empty string shall be returned as terminal. .PP The \fBylex\fR package is designed to provide appropriate scanners for parsing, but it is possible to use custom scanners as long as the above rules are satisfied. .RE .TP \fIobjName\fR \fBconfigure\fR \fB-verbose\fR \fR?\fIvalue\fR?\fR If the value of the \fBverbose\fR variable is non-zero, the parser prints debug information about the read tokens and matched rules to standard output. .TP \fIobjName\fR \fBconfigure\fR \fB-verbout\fR \fR?\fIvalue\fR?\fR Sets the target channel for debug information. The default value is \fBstderr\fR. .SH SCRIPTS .PP If a rule is matched, its corresponding script is executed in the context of the parser. Scripts have access to the variables \fB$\fI<i>\fR, which are set to the values associated with each item on the RHS, where \fI<i>\fR is the index of the item. Numbering items is left to right, starting with 1. .PP Scripts can execute \fBreturn\fR to set their return value, which will then be associated with the rule's LHS. If a script does not execute \fBreturn\fR, the value associated with the the leftmost item on the RHS is used as the rule's value. If the RHS is empty, an empty string is used. .PP \fByeti\fR reserves names with the \fByy\fR prefix, so scripts should not use or access variables with this prefix. Also, a parser's public methods and variables as seen above must be respected. .SH EXAMPLE .PP Here's one simple but complete example of a parser that can parse mathematical expressions, albeit limited to multiplications. The scanner is not shown, but is expected to return the terminals written in uppercase. .PP .PD 0 set pgen [yeti::yeti #auto -name MathParser] .PP $pgen add start {mult} { .RS .PP puts "Result is $1" .RE .PP } .PP $pgen add mult {number MULTIPLY number} { .RS .PP return [expr {$1 * $3}] .RE .PP } .PP $pgen add mult {number} .PP $pgen add number {INTEGER} .PP $pgen add number {OPEN mult CLOSE} { .RS .PP return $2 .RE .PP } .PP eval [$pgen dump] .PP set mp [MathParser #auto -scanner $scanner] .PP puts "The result is [$mp parse]" .PD .PP .SH KEYWORDS parser, scanner |
Added assets/yeti0.4.2/yeti.tcl.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 | # # ====================================================================== # # YETI -- Yet anothEr Tcl Interpreter # # A yacc/bison like parser for Tcl # # Copyright (c) Frank Pilhofer, yeti@fpx.de # # ====================================================================== # # CVS Version Tag: $Id: yeti.tcl,v 1.28 2004/07/06 00:11:03 fp Exp $ # package require Tcl 8.0 package require struct 2.0 package require Itcl package provide yeti 0.4.2 # # ---------------------------------------------------------------------- # The Yeti parser # ---------------------------------------------------------------------- # namespace eval yeti { namespace import -force ::itcl::* class yeti { public variable verbose public variable verbout # # production rules # public variable start private variable productions private variable rules private variable codes private variable ruleno # # NFSM data # private variable nfsm private variable nfsminitstate private variable nfsmendstate private variable nfsmstateno # # DFSM data # private variable dfsm private variable dfsminitstate private variable dfsmendstate private variable dfsmstateno # # Mapping bettween the three # private variable nfsmstatetont private variable nfsmstatetodfsmstate private variable dfsmstatetonfsmstate # # We store the terminals that may follow a non-terminal. This # knowledge is useful to solve reduce/reduce conflicts by # looking at the lookahead. # private variable ntpostterminals # # cache # private variable equivalentstates private variable canntbeempty # # user code to be dumped # public variable name private variable usercode private variable userconstrcode private variable userdestrcode private variable usererrorcode private variable userresetcode private variable userreturndefaultcode {set yyretdata [append 1 {}]} # # ============================================================ # Constructor and Destructor # ============================================================ # constructor {args} { set verbose 0 set ruleno 1 set dfsm "" set nfsm "" set verbout stderr set name "unnamed" set start "start" eval $this configure $args } destructor { if {$nfsm != ""} { catch {$nfsm destroy} } if {$dfsm != ""} { catch {$dfsm destroy} } } public method add {args} { if {[llength $args] == 1} { set args [lindex $args 0] } if {([llength $args] == 1) || \ ([llength $args] != 2 && ([llength $args] % 3) != 0)} { error "usage: $this add lhs rhs ?script?" } if {[llength $args] == 2} { set lhs [lindex $args 0] set rhs [lindex $args 1] lappend productions($lhs) [incr ruleno] set rules($ruleno) [list $lhs $rhs] return $ruleno } set lastlhs "" foreach {lhs rhs code} $args { if {$lhs == "|"} { set lhs $lastlhs } lappend productions($lhs) [incr ruleno] set rules($ruleno) [list $lhs $rhs] if {$code != "" && $code != "-"} { set codes($ruleno) $code } set lastlhs $lhs } return $ruleno } public method code {type thecode} { switch -- $type { public - private - protected { lappend usercode $type $thecode } constructor { set userconstrcode $thecode } destructor { set userdestrcode $thecode } error { set usererrorcode $thecode } reset { set userresetcode $thecode } returndefault { set userreturndefaultcode $thecode } } } # # ============================================================ # Build a Non-deterministic Finite State Machine from rules # ============================================================ # private method BuildNFSM {} { if {$nfsm != ""} { catch {$nfsm destroy} catch {unset equivalentstates} catch {unset canntbeempty} catch {unset nfsmstatetont} } set nfsm [::struct::graph nfsm$this] # # initially, assign state# to each non-terminal # set nfsmstateno 0 foreach nt [array names productions] { set ntstate($nt) [incr nfsmstateno] set nfsmstatetont($nfsmstateno) $nt $nfsm node insert $nfsmstateno $nfsm node set $nfsmstateno info [list] $nfsm node set $nfsmstateno reductions [list] } set nfsminitstate $ntstate(__init__) # # traverse all rules # foreach ruleno [array names rules] { set lhs [lindex $rules($ruleno) 0] set rhs [lindex $rules($ruleno) 1] # # create new state, make epsilon transition from # the non-terminal's state# to new state # $nfsm node insert [incr nfsmstateno] $nfsm node set $nfsmstateno info [list [list $ruleno 0]] $nfsm node set $nfsmstateno reductions [list] set arc [$nfsm arc insert $ntstate($lhs) $nfsmstateno] $nfsm arc set $arc token "" set laststate $nfsmstateno # # create new state for each step of RHS # for {set i 0} {$i < [llength $rhs]} {incr i} { set token [lindex $rhs $i] $nfsm node insert [incr nfsmstateno] $nfsm node set $nfsmstateno info \ [list [list $ruleno [expr {$i+1}]]] $nfsm node set $nfsmstateno reductions [list] set arc [$nfsm arc insert $laststate $nfsmstateno] $nfsm arc set $arc token $token # # If token is a non-terminal, make epsilon transition # to non-terminal's state# so that this non-terminal # can be read. # # However, don't do that if we're at index 0 and the # non-terminal is our LHS. In that case, we're in the # right state for reading this non-terminal already # (via other rules). Adding a transition would cause # a circle (via epsilon transitions). # if {($i != 0 || $token != $lhs) && \ [info exists productions($token)]} { set arc [$nfsm arc insert $laststate $ntstate($token)] $nfsm arc set $arc token "" } set laststate $nfsmstateno } # # mark the last state as a reduction state # $nfsm node set $laststate reductions [list $ruleno] # # if lhs is __init__, then this is our final state # if {$lhs == "__init__"} { set nfsmendstate $laststate } } } # # ============================================================ # State Machine Helpers # ============================================================ # # # Determine if the non-terminal can produce the empty string # private method CanNTBeEmpty {nt} { if {[info exists canntbeempty($nt)]} { return $canntbeempty($nt) } if {![info exists productions($nt)]} { return 0 } set canbeempty 0 set canntbeempty($nt) 1 ;# protection against infinite recursion foreach rule $productions($nt) { set lhs [lindex $rules(rule) 0] set rhs [lindex $rules(rule) 1] for {set i 0} {$i < [llength $rhs]} {incr i} { set token [lindex $rhs $i] # # if token is a terminal, this rule cannot be empty # if {![info exists productions($token)]} { break } # # if token is the lhs, then ignore # if {$token == $lhs} { continue } if {![CanNTBeEmpty $token]} { break } } # # if we could traverse the rule without finding a terminal, # then this rule can indeed produce the empty string # if {$i >= [llength $rhs]} { set canbeempty 1 break } } set canntbeempty($nt) $canbeempty return $canbeempty } # # Determine which states are reachable by epsilon transitions, # taking into account non-terminals that can produce an empty # string. # private method NFSMEpsilonTransitions {state {beenthere {}}} { if {[info exists equivalentstates($state)]} { return $equivalentstates($state) } if {[lsearch -exact $beenthere $state] != -1} { error "error: infinite loop detected for NFSM state $state" } lappend beenthere $state set resstates [list $state] foreach arc [$nfsm arcs -out $state] { set token [$nfsm arc get $arc token] if {$token != ""} { if {[info exists productions($token)]} { continue } if {![CanNTBeEmpty $token]} { continue } } set newstate [$nfsm arc target $arc] lappend resstates $newstate foreach addstate [NFSMEpsilonTransitions $newstate $beenthere] { lappend resstates $addstate } } set equivalentstates($state) [lsort -unique $resstates] return $equivalentstates($state) } # # Collect terminals that can follow a non-terminal. This info # can be used to resolve reduce/reduce conflicts. # private method CollectPostTerminalsRek {nt {beenthere {}}} { upvar ntpostfollows ntpostfollows upvar temppostterminals temppostterminals if {[info exists ntpostterminals($nt)]} { return $ntpostterminals($nt) } if {[lsearch -exact $beenthere $nt] != -1} { return [list] } lappend beenthere $nt if {[info exists temppostterminals($nt)]} { set res $temppostterminals($nt) } else { set res [list] } if {[info exists ntpostfollows($nt)]} { foreach follows $ntpostfollows($nt) { foreach token [CollectPostTerminalsRek $follows $beenthere] { lappend res $token } } } set ntpostterminals($nt) [lsort -unique $res] return $res } private method CollectPostTerminals {} { catch {unset ntpostterminals} # # Look at each arc in the graph, and see if it is marked with # a non-terminal. If so, then see which terminals can be read # in the target state. # foreach arc [$nfsm arcs] { set nt [$nfsm arc get $arc token] if {$nt == ""} { continue } if {![info exists productions($nt)]} { # terminal continue } set ntstate [$nfsm arc target $arc] set ntstates [NFSMEpsilonTransitions $ntstate] foreach state $ntstates { foreach arc [$nfsm arcs -out $state] { set token [$nfsm arc get $arc token] if {$token == ""} { continue } if {[info exists productions($token)]} { # non-terminal continue } lappend temppostterminals($nt) $token } # # If there is a reduction possible, then everything # that can follow the LHS of the rule that is being # reduced can also follow this nt. # foreach redux [$nfsm node get $state reductions] { set lhs [lindex $rules($redux) 0] lappend ntpostfollows($nt) $lhs } } } lappend temppostterminals(__init__) "" ;# EOF foreach nt [array names productions] { CollectPostTerminalsRek $nt } if {$verbose >= 4} { puts $verbout "CollectPostTerminals: lookahead terminals for reductions" foreach nt [array names productions] { puts -nonewline $verbout " lookaheads for reducing $nt:" if {[llength $ntpostterminals($nt)] == 0} { puts $verbout " (none)" } else { foreach lookahead $ntpostterminals($nt) { if {$lookahead == ""} { puts -nonewline $verbout " (EOF)" } else { puts -nonewline $verbout " $lookahead" } } puts $verbout "" } } puts $verbout "" } } # # ============================================================ # Build a Deterministic Finite State Machine from NFSM # ============================================================ # # # Build up deterministic state dfsmstate from all of nfsmstates # by simulating all transitions from these states in parallel # private method BuildDFSMTransition {dfsmstate nfsmstates} { # # fill in state information for this dfsmstate from all of # the nfsmstates # set dfsminfo [list] set dfsmredux [list] foreach nfsmstate $nfsmstates { foreach info [$nfsm node get $nfsmstate info] { lappend dfsminfo $info } foreach redux [$nfsm node get $nfsmstate reductions] { lappend dfsmredux $redux } } # # initialize dfsmstate info # $dfsm node set $dfsmstate info $dfsminfo $dfsm node set $dfsmstate reductions $dfsmredux # # map nfsmstates to dfsmstate # set nfsmstates [lsort -unique -integer $nfsmstates] set dfsmstatetonfsmstate($dfsmstate) $nfsmstates set nfsmstatetodfsmstate([join $nfsmstates ,]) $dfsmstate foreach nfsmstate $nfsmstates { set nfsmstatetodfsmstate($nfsmstate) $dfsmstate } # # collect all tokens that can be read in any of these states, # and the states that we can enter by reading them # # allnfsmtokens: map(token) : list of nextstate # foreach arc [eval $nfsm arcs -out $nfsmstates] { set token [$nfsm arc get $arc token] if {$token == ""} { continue } set newstate [$nfsm arc target $arc] set newstates [NFSMEpsilonTransitions $newstate] foreach nstate $newstates { lappend allnfsmtokens($token) $nstate } } # # create transitions for each of these tokens # foreach token [array names allnfsmtokens] { # # See if we already have a DFSM state that is equivalent to # all the NFSM states reachable by reading $token. If yes, # link to it, otherwise create a new one. # set allnfsmtokens($token) \ [lsort -unique -integer $allnfsmtokens($token)] set allnfsmindex [join $allnfsmtokens($token) ,] if {[info exists nfsmstatetodfsmstate($allnfsmindex)]} { set equivalentstate $nfsmstatetodfsmstate($allnfsmindex) } else { set equivalentstate -1 } if {$equivalentstate >= 0} { # # create transition to that state # set arc [$dfsm arc insert $dfsmstate $equivalentstate] $dfsm arc set $arc token $token } else { # # else, create a new state, and simulate that state # $dfsm node insert [incr dfsmstateno] set arc [$dfsm arc insert $dfsmstate $dfsmstateno] $dfsm arc set $arc token $token BuildDFSMTransition $dfsmstateno $allnfsmtokens($token) } } } private method BuildDFSM {} { # # initialize DFSM building process # if {$dfsm != ""} { catch {$dfsm destroy} catch {unset nfsmstatetodfsmstate} catch {unset dfsmstatetonfsmstate} } set dfsm [::struct::graph dfsm$this] set dfsmstateno 1 set dfsminitstate 1 $dfsm node insert 1 # # Start DFSM building process by creating an initial state #1 # that represents all nodes that are reachable by epsilon # transitions from the NFSM's initial state. # BuildDFSMTransition 1 [NFSMEpsilonTransitions $nfsminitstate] # # Check which DFSM state the NFSM's end state has been mapped to # if {![info exists nfsmstatetodfsmstate($nfsmendstate)]} { error "assertion failure: no dfsm state for end state $nfsmendstate" } set dfsmendstate $nfsmstatetodfsmstate($nfsmendstate) } # # ============================================================ # Check for conflicts # ============================================================ # # # check for reduce/reduce conflicts # private method CheckReduceReduce {} { foreach state [$dfsm nodes] { set reductions [$dfsm node get $state reductions] if {[llength $reductions] <= 1} { continue } # # multiple reductions possible here, check if their set # of lookaheads is disjunct # catch {unset testlookaheads} foreach redux $reductions { set lhs [lindex $rules($redux) 0] if {![info exists ntpostterminals($lhs)]} { continue } foreach lookahead $ntpostterminals($lhs) { if {[info exists testlookaheads($lookahead)]} { if {$verbose} { puts $verbout "yeti: warning: reduce/reduce conflict in state $state ($lookahead)" DumpState $dfsm $state } } else { set testlookaheads($lookahead) 1 } } } catch {unset testlookaheads} } } private method CheckShiftReduce {} { foreach state [$dfsm nodes] { # # We have computed the reduction lookaheads (which # terminals could follow a non-terminal) in the # ntpostterminals array. # # Make sure that this state, where the reduction # may occur, does not offer an out arc that also # reads this token # catch {unset testlookaheads} set reductions [$dfsm node get $state reductions] foreach redux $reductions { set lhs [lindex $rules($redux) 0] if {![info exists ntpostterminals($lhs)]} { continue } foreach terminal $ntpostterminals($lhs) { set testlookaheads($terminal) $lhs } } foreach arc [$dfsm arcs -out $state] { set token [$dfsm arc get $arc token] if {[info exists testlookaheads($token)]} { if {$verbose} { puts $verbout "yeti: warning: shift/reduce conflict in state $state (shift $token, reduce $testlookaheads($token))" DumpState $dfsm $state } } } } } private method CheckDFSM {} { CheckReduceReduce CheckShiftReduce } # # ============================================================ # Debug: dump the state machines # ============================================================ # private method DumpState {sm state} { foreach info [$sm node get $state info] { set index [lindex $info 1] set ruleno [lindex $info 0] set rule $rules($ruleno) set lhs [lindex $rule 0] set rhs [lindex $rule 1] puts -nonewline $verbout " $lhs -->" for {set i 0} {$i < $index} {incr i} { puts -nonewline $verbout " " puts -nonewline $verbout [lindex $rhs $i] } puts -nonewline $verbout " ." for {} {$i < [llength $rhs]} {incr i} { puts -nonewline $verbout " " puts -nonewline $verbout [lindex $rhs $i] } puts $verbout "" } if {[llength [$sm node get $state info]] > 0} { puts $verbout "" } foreach arc [$sm arcs -out $state] { set token [$sm arc get $arc token] if {$token == ""} { puts -nonewline $verbout " (epsilon): go to state" } else { puts -nonewline $verbout " $token: go to state" } set ts [$sm arc target $arc] puts -nonewline $verbout " " puts -nonewline $verbout $ts puts $verbout "." } if {[llength [$sm arcs -out $state]] > 0} { puts $verbout "" } set reductions [$sm node get $state reductions] if {[llength $reductions] > 1} { foreach redux $reductions { set lhs [lindex $rules($redux) 0] if {[info exists ntpostterminals($lhs)]} { set lookaheads $ntpostterminals($lhs) } else { set lookaheads [list "(default)"] } foreach lookahead $lookaheads { if {$lookahead == ""} { set lookahead "(EOF)" } puts $verbout " $lookahead: reduce using rule# $redux ($lhs)" } } } elseif {[llength $reductions] == 1} { set redux [lindex $reductions 0] set lhs [lindex $rules($redux) 0] puts $verbout " (default): reduce using rule# $redux ($lhs)" } if {[llength $reductions] > 0} { puts $verbout "" } } public method DumpNFSM {} { puts -nonewline $verbout "NFSM: initial state $nfsminitstate, " puts $verbout "final state $nfsmendstate" foreach state [lsort -integer [$nfsm nodes]] { puts $verbout "NFSM State # $state:" if {[info exists nfsmstatetont($state)]} { puts $verbout " represents token $nfsmstatetont($state)" } DumpState $nfsm $state } } public method DumpDFSM {} { puts -nonewline $verbout "DFSM: initial state $dfsminitstate, " puts $verbout "final state $dfsmendstate" foreach state [lsort -integer [$dfsm nodes]] { puts $verbout "DFSM State # $state:" puts $verbout " represents NFSM states $dfsmstatetonfsmstate($state)" puts $verbout "" DumpState $dfsm $state } } # # ============================================================ # Build State Machines # ============================================================ # public method BuildSMS {} { # # Add initial rule __init__ -> $start # if {![info exists rules(0)]} { lappend productions(__init__) 0 set rules(0) [list __init__ [list $start]] } # # Build Non-deterministic Finite State Machine from rules # if {$nfsm == ""} { BuildNFSM } # # Build Deterministic Finite State Machine from NFSM # CollectPostTerminals if {$dfsm == ""} { BuildDFSM } # # Check DFSM for conflicts # CheckDFSM # # Debug output # if {$verbose >= 3} { puts $verbout "-----" puts $verbout "NFSM:" puts $verbout "-----" DumpNFSM } if {$verbose >= 2} { puts $verbout "-----" puts $verbout "DFSM:" puts $verbout "-----" DumpDFSM } } # # ============================================================ # Dump generated Parser # ============================================================ # public method dump {} { # # First, build DFSM if necessary # if {$dfsm == ""} { BuildSMS } # # Create parser code # append data "itcl::class $name {\n" append data " public variable scanner \"\"\n" append data " public variable verbose 0\n" append data " public variable verbout stderr\n" append data " private variable yystate $dfsminitstate\n" append data " private variable yysstack {$dfsminitstate}\n" append data " private variable yydstack {}\n" append data " private variable yyreadnext 1\n" append data " private variable yylookahead\n" append data " private variable yyfinished 0\n" append data " private common yytrans\n" append data " private common yyredux\n" append data " private common yyrules\n" append data "\n" # # yytrans: array($state,$token) -> newstate # append data " array set yytrans {\n" foreach state [lsort -integer [$dfsm nodes]] { foreach arc [$dfsm arcs -out $state] { set token [$dfsm arc get $arc token] set ts [$dfsm arc target $arc] append data " [list $state,$token] $ts\n" } } append data " }\n" append data "\n" # # yyredux: array($state,$token) -> ruleno # array($state) -> ruleno # append data " array set yyredux {\n" foreach state [lsort -integer [$dfsm nodes]] { set reductions [$dfsm node get $state reductions] foreach redux $reductions { set lhs [lindex $rules($redux) 0] set count [expr {[llength $rules($redux)] - 1}] if {[info exists ntpostterminals($lhs)] && \ [llength $reductions] > 1} { foreach lookahead $ntpostterminals($lhs) { append data " $state,$lookahead $redux\n" } } else { append data " $state $redux\n" } } } append data " }\n" append data "\n" # # yyrules: array($ruleno) -> [list lhs rhs] # append data " array set yyrules {\n" foreach ruleno [lsort -integer [array names rules]] { set rule $rules($ruleno) set lhs [lindex $rule 0] set rhs [lindex $rule 1] append data " $ruleno {$lhs {$rhs}}\n" } append data " }\n" append data "\n" # # user code # if {[info exists usercode]} { foreach {type thecode} $usercode { append data " $type {\n" append data $thecode "\n" append data " }\n" append data "\n" } } # # constructor # if {[info exists userconstrcode]} { append data " constructor " $userconstrcode "\n" } else { append data " constructor {args} {\n" append data " eval \$this configure \$args\n" append data " }\n" append data " \n" } # # destructor # if {[info exists userdestrcode]} { append data " destructor " $userdestrcode "\n" } # # reset # append data " public method reset {} {\n" append data " if {\$verbose} {\n" append data " puts \$verbout \"$name: reset, entering state $dfsminitstate\"\n" append data " }\n" append data " set yystate $dfsminitstate\n" append data " set yysstack \[list \$yystate\]\n" append data " set yydstack \[list\]\n" append data " set yyreadnext 1\n" append data " set yyfinished 0\n" if {[info exists userresetcode]} { append data "\n" $userresetcode "\n" } append data " }\n" append data " \n" # # yyerror: overloadable error handling # append data " protected method yyerror {yyerrmsg} {\n" if {[info exists usererrorcode]} { append data $usererrorcode "\n" } else { append data " puts \$verbout \"$name: \$yyerrmsg\"\n" } append data " }\n" append data " \n" # # yyshift: shift token and data # append data " private method yyshift {yytoken yydata} {\n" append data " set yynewstate \$yytrans(\$yystate,\$yytoken)\n" append data " lappend yysstack \$yynewstate\n" append data " lappend yydstack \$yydata\n" append data " \n" append data " if {\$verbose} {\n" append data " puts \$verbout \"$name: shifting token \$yytoken, entering state \$yynewstate\"\n" append data " }\n" append data " \n" append data " set yystate \$yynewstate\n" append data " }\n" append data "\n" # # yyreduce: execute a rule # append data " private method yyreduce {yyruleno} {\n" append data " set yylhs \[lindex \$yyrules(\$yyruleno) 0\]\n" append data " set yyrhs \[lindex \$yyrules(\$yyruleno) 1\]\n" append data " set yycount \[llength \$yyrhs\]\n" append data " set yyssdepth \[llength \$yysstack\]\n" append data " set yydsdepth \[llength \$yydstack\]\n" append data " \n" append data " if {\$verbose} {\n" append data " puts -nonewline \$verbout \"$name: reducing rule # \$yyruleno: \$yylhs -->\"\n" append data " foreach yytoken \$yyrhs {\n" append data " puts -nonewline \$verbout \" \"\n" append data " puts -nonewline \$verbout \$yytoken\n" append data " }\n" append data " puts \$verbout \"\"\n" append data " }\n" append data "\n" append data " for {set yyi 0} {\$yyi < \$yycount} {incr yyi} {\n" append data " set yyvarname \[expr {\$yyi + 1}\]\n" append data " set yyvarval \[lindex \$yydstack \[expr {\$yydsdepth-\$yycount+\$yyi}\]\]\n" append data " set \$yyvarname \$yyvarval\n" append data " }\n" append data " \n" append data " set yyretcode \[catch {\n" append data " switch -- \$yyruleno {\n" append data " 0 {\n" append data " return \$1\n" append data " }\n" foreach ruleno [lsort -integer [array names codes]] { append data " $ruleno {\n" append data $codes($ruleno) "\n" append data " }\n" } append data " }\n" append data " } yyretdata\]\n" append data " \n" if {$userreturndefaultcode ne {}} { append data " if {\$yyretcode == 0} {\n" append data " set yyretcode \[catch {\n" append data $userreturndefaultcode\n append data " } yyretdata\]\n" append data " }\n" } append data " if {\$yyretcode == 1} {\n" append data " global errorInfo\n" append data " set yyerrmsg \"script for rule # \$yyruleno (\$yylhs --> \$yyrhs) failed: \$yyretdata\"\n" append data " yyerror \$yyerrmsg\n" append data " append errorInfo \"\\n while executing script for rule # \$yyruleno (\$yylhs --> \$yyrhs)\"\n" append data " error \$yyerrmsg \$errorInfo\n" append data " }\n" append data " \n" append data " set yysstack \[lrange \$yysstack 0 \[expr {\$yyssdepth-\$yycount-1}\]\]\n" append data " set yydstack \[lrange \$yydstack 0 \[expr {\$yydsdepth-\$yycount-1}\]\]\n" append data " set yystate \[lindex \$yysstack end\]\n" append data " \n" append data " if {\$verbose} {\n" append data " puts \$verbout \"$name: entering state \$yystate\"\n" append data " }\n" append data " \n" append data " return \$yyretdata\n" append data " }\n" append data " \n" # # Single step # append data " public method step {} {\n" append data " if {\$yyfinished} {\n" append data " error \"step beyond end of parse\"\n" append data " }\n" append data " \n" append data " if {\$yyreadnext} {\n" append data " set yylookahead \[\$scanner next\]\n" append data " set yyreadnext 0\n" append data " }\n" append data " \n" append data " set yyterm \[lindex \$yylookahead 0\]\n" append data " set yydata \[lindex \$yylookahead 1\]\n" append data " \n" append data " if {\[info exists yytrans(\$yystate,\$yyterm)\]} {\n" append data " yyshift \$yyterm \$yydata\n" append data " set yyreadnext 1\n" append data " return \[list \$yyterm \$yydata\]\n" append data " } elseif {\[info exists yyredux(\$yystate)\] || \\\n" append data " \[info exists yyredux(\$yystate,\$yyterm)\]} {\n" append data " if {\[info exists yyredux(\$yystate)\]} {\n" append data " set yyruleno \$yyredux(\$yystate)\n" append data " } else {\n" append data " set yyruleno \$yyredux(\$yystate,\$yyterm)\n" append data " }\n" append data " \n" append data " set yyreduxlhs \[lindex \$yyrules(\$yyruleno) 0\]\n" append data " set yyreduxdata \[yyreduce \$yyruleno\]\n" append data " \n" append data " if {\$yyreduxlhs == \"__init__\"} {\n" append data " set yyfinished 1\n" append data " } else {\n" append data " yyshift \$yyreduxlhs \$yyreduxdata\n" append data " }\n" append data " return \[list \$yyreduxlhs \$yyreduxdata\]\n" append data " } else {\n" append data " global errorInfo\n" append data " set yyerrmsg \"parse error reading \\\"\$yyterm\\\" in state \$yystate\"\n" append data " set errorInfo \$yyerrmsg\n" append data " append errorInfo \"\\n expecting one of:\"\n" append data " foreach yylaname \[array names yytrans \$yystate,*\] {\n" append data " append errorInfo \"\\n \" \[lindex \[split \$yylaname ,\] 1\]\n" append data " }\n" append data " yyerror \$yyerrmsg\n" append data " error \$yyerrmsg \$errorInfo\n" append data " }\n" append data " \n" append data " error \"oops: unreachable code\"\n" append data " }\n" append data " \n" # # Main parse method # append data " public method parse {} {\n" append data " if {\$scanner == \"\"} {\n" append data " global errorInfo\n" append data " set yyerrmsg \"cannot start parsing without a scanner\"\n" append data " set errorInfo \$yyerrmsg\n" append data " yyerror \$yyerrmsg\n" append data " error \$yyerrmsg \$errorInfo\n" append data " }\n" append data " \n" append data " while {!\$yyfinished} {\n" append data " set yydata \[step\]\n" append data " }\n" append data " \n" append data " if {\[lindex \$yylookahead 0\] != \"\"} {\n" append data " global errorInfo\n" append data " set yyerrmsg \"parser finished, but not at EOF (lookahead: \[lindex \$yylookahead 0\])\"\n" append data " set errorInfo \$yyerrmsg\n" append data " yyerror \$yyerrmsg\n" append data " error \$yyerrmsg\n" append data " }\n" append data " \n" append data " return \[lindex \$yydata 1\]\n" append data " }\n" # # End of newly generated class # append data "}\n" append data "\n" } } } |
Added assets/yeti0.4.2/ylex.n.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | '\"" '\" Copyright (c) 2001 Frank Pilhofer" '\"" '\".so man.macros" .TH YLEX n 0.4 ylex "Yeti Scanner" .BS .SH NAME Ylex \- Yeti's Scanner .SH SYNOPSIS .PD 0 .PP \fBpackage require ylex ?0.4?\fR .PD .PP .PD 0 \fByeti::ylex\fR \fIname\fR .PP \fIname\fR \fBmacro\fR \fIname\fR \fIregex\fR ?\fI...\fI? .PP \fIname\fR \fBmacro\fR \fIargs\fR .PP \fIname\fR \fBadd\fR ?\fIoptions\fR? \fIregex\fR \fIscript\fR ?\fI...\fI? .PP \fIname\fR \fBadd\fR ?\fIoptions\fR? \fIargs\fR .PP \fIname\fR \fBcode\fR \fItoken\fR \fIscript\fR .PP \fIname\fR \fBdump\fR .PP \fIname\fR \fBconfigure\fR \fB-name\fR \fR?\fIvalue\fR?\fR .PP \fIname\fR \fBconfigure\fR \fB-start\fR \fR?\fIvalue\fR?\fR .PP \fIname\fR \fBconfigure\fR \fB-case\fR \fR?\fIvalue\fR?\fR .PD .BE .SH SCANNER SYNOPSIS .PP .PD 0 \fIscannerName\fR \fIobjName\fR \fR?\fIoptions\fR?\fR .PP \fIname\fR \fBstart\fR \fIstring\fR .PP \fIname\fR \fBreset\fR .PP \fIname\fR \fBnext\fR .PP \fIname\fR \fBstep\fR .PP \fIname\fR \fBrun\fR .PP \fIname\fR \fBconfigure\fR \fB-case\fR \fR?\fIvalue\fR?\fR .PP \fIname\fR \fBconfigure\fR \fB-yydata\fR \fR?\fIvalue\fR?\fR .PP \fIname\fR \fBconfigure\fR \fB-verbose\fR \fR?\fIvalue\fR?\fR .PP \fIname\fR \fBconfigure\fR \fB-verbout\fR \fR?\fIvalue\fR?\fR .PD .BE .SH DESCRIPTION .PP This manual page describes \fBylex\fR, a scanner generator that comes with the \fByeti\fR package. \fBylex\fR is modeled after the standard \fBlex\fR utility, which is used to create scanners in the C programming language. .PP A scanner consists of a number of rules. Each rule associates a regular expression with a Tcl script; this script is executed whenever the regular expression is matched in the input. This code can either act on its own, or it can generate a stream of tokens that can then be evaluated outside of the scanner - e.g. in a parser. .PP \fBylex\fR does not do the scanning by itself, rather it is used, quite like \fBlex\fR, to generate scanners (by way of the \fBdump\fR method). Generated scanners are \fB[incr Tcl]\fR classes that act independently of \fBylex\fR, see the \fBScanner\fR section below. These scanners can be customized; you can use the \fBcode\fR method to add user variables and methods to the class. .SH COMMANDS .TP \fByeti::ylex\fR \fIname\fR Creates a new scanner generator by the name of \fIname\fR. The new scanner generator has an empty set of rules. \fIoptions\fR can be used to configure the scanner generator's public variables, see the \fIvariables\fR section below. .SH METHODS .PD 0 .TP \fIname\fR \fBmacro\fR \fIname\fR \fIregex\fR ?\fI...\fI? .TP \fIname\fR \fBmacro\fR \fIargs\fR .PD Defines \fImacros\fR, which are regular expressions that are stored for later use. Macros can be used in other macros or rules using their name in angular brackets, e.g. \fI<digit>\fR would reference the \fIdigit\fR macro. Macro names must be alphanumerical. In the first form, the \fBmacro\fR method takes an even number of parameters. The first parameter of each pair is the name, the second parameter is a regular expression that may itself contain other macros. In the second form, the method takes a list as single parameter, where this list is composed as above of alternating names and regular expressions. The second form may be more convenient to define multiple macros. .TP \fIname\fR \fBadd\fR ?\fIoptions\fR? \fIregex\fR \fIscript\fR ?\fI...\fI? .PD 0 .TP \fIname\fR \fBadd\fR ?\fIoptions\fR? \fIargs\fR .PD Adds new rules to the scanner. In the first form, the \fBadd\fR method takes an even number of parameters (not counting options). The first parameter of each pair is a regular expression, the second parameter is a script. In the second form, the method takes a list as single parameter (not counting options), where this list is composed as above of alternating regular expressions and scripts. .RS .PP Whenever the regular expression \fIregex\fR is matched in the input string, its corresponding \fIscript\fR will be executed by the scanner. .PP The following options are supported: .IP "-nocase" This option has the same meaning as on \fBregexp\fR. In matching this rule, upper-case characters in the input will be treated as lower-case. .IP "-state \fIname\fR" This rule will only be considered if the scanner is in the state \fIname\fR. See below for more information. .IP "-states \fInames\fR" Same as above, but \fInames\fR is a list of state names in which the rule is active. .RE .TP \fIname\fR \fBcode\fR \fItoken\fR \fIscript\fR Adds custom user code to the generated scanner. \fItoken\fR must be one of .RS .IP \fBconstructor\fR Defines the class's constructor. The \fIscript\fR may have any of the formats allowed by \fB[incr Tcl]\fR, without the \fBconstructor\fR keyword. .IP \fBdestructor\fR Defines the body of the class's destructor. .IP \fBerror\fR Defines the body of the error handler that is called whenever an error occurs, e.g. errors executing a rule's script. \fIscript\fR has access to the \fByyerrmsg\fR parameter, which contains a string with a description of the error and its cause. \fIscript\fR is supposed to inform the user of the problem. The default implementations prints the message to the channel set in the \fBverbout\fR variable. \fIscript\fR is expected to return normally; the parser then returns from the current invocation with the original error. .IP \fBreset\fR The \fIscript\fR is added to the body of the scanner's \fIreset\fR method. .IP \fBpublic\fR .PD 0 .IP \fBprotected\fR .IP \fBprivate\fR Defines public, protected or private class members. The \fIscript\fR may contain many different member definitions, like the respective keywords in an \fB[incr Tcl]\fR class definition. .PD .RE .TP \fIname\fR \fBdump\fR Returns a script containing the generated scanner. This method is called after all configuration options have been set and all rules have been added; the scanner generator object is usually deleted after this method has been called. The returned script can be passed to \fBeval\fR for instant usage, or saved to a file, from where it can later be sourced without involvement of the scanner generator. .SH VARIABLES .TP \fIname\fR \fBconfigure\fR \fB-name\fR \fR?\fIvalue\fR?\fR Defines the class name of the generated scanner class. The default value is \fBunnamed\fR. .TP \fIname\fR \fBconfigure\fR \fB-start\fR \fR?\fIvalue\fR?\fR Defines the initial state for the scanner. Setting this variable is only required if your scanner needs multiple states, and you are not satisfied with the default of \fBINITIAL\fR. .TP \fIname\fR \fBconfigure\fR \fB-case\fR \fR?\fIvalue\fR?\fR If set to 0 (zero), the generated scanner will be case-insensitive (i.e. \fB-nocase\fR will be used on all calls to \fBregexp\fR). The default value is 1 for a case-sensitive scanner. This setting can be overridden on a rule-by-rule basis using the \fB-nocase\fR option upon adding a rule. .SH SCANNER USAGE .PP Scanners are independent of \fBylex\fR, their only dependency is on \fB[incr Tcl]\fR. .PP A scanner reads its input from a string that must be set before scanning can begin. The regular expressions (rules) are then repeatedly matched against the current input position within this string. If more than one regular expression matches text at a certain position, the rule matching the most text is selected. If more than one regular expression matches the same amount of text, the rule that was added to the scanner generator first is selected. If a rule matches, its associated \fIscript\fR is executed, and the read pointer is moved beyond the match. Unmatched text in the input is ignored and skipped. .PP Scanners are \fB[incr Tcl]\fR objects, so its usual rules of object handling (e.g. deletion of scanners) apply. .SH SCANNER COMMANDS .TP \fIscannerName\fR \fIobjName\fR \fR?\fIoptions\fR?\fR Creates a new scanner instance by the name of \fIobjName\fR. \fIoptions\fR can be used to configure the scanner's public variables, see the \fIscanner variables\fR section below. .SH SCANNER METHODS .TP \fIobjName\fR \fBstart\fR \fIstring\fR Initializes the scanner to scan \fIstring\fR. Calling \fBstart\fR implies \fBreset\fR. .TP \fIobjName\fR \fBreset\fR Resets the scanner to the beginning of the input string, and resets the state to the initial state. If you want to scan a different string, you need not call \fBreset\fR but only \fBstart\fR. .TP \fIobjName\fR \fBnext\fR Starts the scanning process. Regular expressions are matched, and their scripts are executed. This repeats until a script executes \fBreturn\fR or until end of input is reached. If a script executes \fBreturn\fR, the script's return value is returned. At the end of input, an empty string is returned. .TP \fIobjName\fR \fBstep\fR Single-steps the parser. This method returns after a single match has been done, regardless of whether its script executed \fBreturn\fR or not. \fBstep\fR returns a list of length three. The first element of this list is the number of the rule that was matched or -1 if at the end of input. The second element is 1 if the script executed \fBreturn\fR, 0 otherwise. The third element is the script's return value if the script executed \fBreturn\fR, empty otherwise. .TP \fIobjName\fR \fBrun\fR Runs the scanning process to completion. \fBnext\fR is repeatedly called until the end of input has been reached. A list of all the results returned from \fBnext\fR is returned. .SH SCANNER VARIABLES .TP \fIobjName\fR \fBconfigure\fR \fB-case\fR \fR?\fIvalue\fR?\fR If this variable is zero, then the \fB-nocase\fR option is implicitly set for all rules. The default value is determined by the setting of the \fBcase\fR variable in the scanner generator. Setting this variable after scanning has started may yield unexpected results because of cached data. .TP \fIobjName\fR \fBconfigure\fR \fB-yydata\fR \fR?\fIvalue\fR?\fR This variable holds the input string. This variable is not meant to be modified, but it can be conveniently configured upon object creation. If that is done, there is no need to call the \fBstart\fR method. .TP \fIobjName\fR \fBconfigure\fR \fB-yyindex\fR \fR?\fIvalue\fR?\fR This variable holds the current index into the input string. This variable is not meant to be set, but reading it may be useful e.g. to determine the position that causes an error in a parser. After an invocation \fBstep\fR or \fBnext\fR, \fByyindex\fR points to the character after the current match. .TP \fIobjName\fR \fBconfigure\fR \fB-verbose\fR \fR?\fIvalue\fR?\fR If this value is non-zero, the scanner prints debug information about its processing. The larger \fBverbose\fR is, the more information is printed (sensible values are 0 to 3). By default, no debug information is printed. .TP \fIobjName\fR \fBconfigure\fR \fB-verbout\fR \fR?\fIvalue\fR?\fR This variable contains the channel that is used for debug output. By default, this is \fBstderr\fR. .SH SCRIPTS .PP Scripts are executed in the context of the scanner. Scripts have access to the following variables. .TP \fByytext\fR This variable contains the text that was matched by the regular expression. .TP \fByystate\fR This variable contains the current state of the scanner. The script may modify this variable to switch the scanner to a different state. .TP \fByystart\fR The absolute index of the first character of the current match. .TP \fByyend\fR The absolute index of the last character of the current match. .TP \fI<i>\fR These variables (\fB$1\fR, \fB$2\fR ...) contain any subexpressions matched by the regular expression. .PP \fBylex\fR reserves names with the \fByy\fR prefix, so scripts should not use or access variables with this prefix other than those documented here. Also, a scanner's public methods and variables as seen above must be respected. .SH STATES .PP Initially, a scanner is in the initial state as determined by the \fBstart\fR variable of the scanner generator. Scripts can switch to a different state by modifying the \fByystate\fR variable during execution of a script. In each state, only a subset of all rules will be considered, as determined by the \fB-state\fR or \fB-states\fR options upon adding rules to the scanner generator; if no such option was used when adding a rule, it will be active in all states. .SH NOTES .PP Unmatched text in the input is ignored. In order to throw an error upon unmatched text, add a "catch-all" rule at the end of the rule set (usually with the regular expression "." that matches any character). .PP Subexpressions are available in the \fI$<i>\fR variables only when using Tcl 8.3 or above. .PP It is illegal to have rules that match the empty string. .SH TODO .PP There should be a means of reading data from elsewhere than a string. It might be inconvenient to have all the input in memory. However, that is not possible with the current regexp engine. .PP Maybe there should be the possibility to read a lex-like input file. .SH KEYWORDS scanner, parser, token |
Added assets/yeti0.4.2/ylex.tcl.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | # # ====================================================================== # # YLEX -- Scanner for the Yeti Package # # A (f)lex-like parser for Tcl # # Copyright (c) Frank Pilhofer, yeti@fpx.de # # ====================================================================== # # CVS Version Tag: $Id: ylex.tcl,v 1.8 2004/07/05 23:46:49 fp Exp $ # package require Tcl 8.0 package require Itcl package provide ylex 0.4.2 # # ---------------------------------------------------------------------- # The Yeti Lexer # ---------------------------------------------------------------------- # namespace eval yeti { namespace import -force ::itcl::* class ylex { # # nocase: whether we want -nocase on all regexps # public variable case public variable verbose public variable verbout public variable start # # ruleno: integer for rule numbering # rules: list of all active rules # regexs: map rule# -> regular expression # igncase: map rule# -> 1 if case should be ignored # codes: map rule# -> user code to execute upon match # states: map name -> list of active rules # macros: map name -> regular expression # private variable ruleno private variable rules private variable regexs private variable igncase private variable codes private variable states private variable macros # # user code to be dumped # public variable name private variable usercode private variable userconstrcode private variable userdestrcode private variable usererrorcode private variable userresetcode # # ============================================================ # Constructor # ============================================================ # constructor {args} { set ruleno 0 set case 1 set verbose 0 set verbout stderr set name "unnamed" set start "INITIAL" eval $this configure $args } # # ============================================================ # Macro handling # ============================================================ # private method substitute_macros {regex} { # # try to find {name} in regex # set hasmacro [regexp -indices {<[[:alnum:]_]+>} $regex found] while {$hasmacro} { set begin [lindex $found 0] set end [lindex $found 1] set macroname [string range $regex \ [expr {$begin + 1}] [expr {$end - 1}]] if {![info exists macros($macroname)]} { error "undefined macro: $macroname" } # # substitute this macro # set regex [string replace $regex \ $begin $end \ "(?:$macros($macroname))"] # # repeat # set hasmacro [regexp -indices {<[[:alnum:]_]+>} $regex found] } return $regex } public method macro {args} { # # There may be a single list of names and regexs # if {[llength $args] == 1} { set args [lindex $args 0] } # # Number of args must be positive and even # if {[llength $args] == 0 || ([llength $args] % 2) != 0} { error "usage: $this macro name regex ?name regex ...?" } foreach {macroname regex} $args { set macros($macroname) [substitute_macros $regex] if {$verbose > 1} { puts $verbout "ylex: macro `$macroname' expands to `$macros($macroname)'" } } } # # ============================================================ # Rule handling # ============================================================ # public method add {args} { set doigncase 0 for {set i 0} {$i < [llength $args]} {incr i} { set arg [lindex $args $i] switch -glob -- $arg { -nocase { set doigncase 1 } -state { lappend thestates [lindex $args [incr i]] } -states { foreach thestate [lindex $args [incr i]] { lappend thestates $thestate } } -- { incr i break } -* { error "illegal option: $arg" } default { break } } } # # One remaining argument can be a list of regexs and codes # if {$i+1 == [llength $args]} { set args [lindex $args $i] set i 0 } # # There must be an even, positive number of remaining args # if {$i >= [llength $args] || (([llength $args] - $i) % 2) != 0} { error "usage: $this add ?options? regex script ?regex script ...?" } for {} {$i < [llength $args]} {incr i} { set orig_regex [lindex $args $i] set code [lindex $args [incr i]] set regex [substitute_macros $orig_regex] # # check if regular expression is valid # if {[catch {regexp -- $regex "Hello World"}]} { error "syntax error in regular expression `$regex'" } # # sanity check: the regular expression must not match the # empty string. # if {[regexp -- $regex ""]} { error "regular expression `$regex' matches empty string" } # # check that there are no unbalanced unquoted braces # set quoted 0 set brlevel 0 for {set j 0} {$j < [string length $regex]} {incr j} { set c [string index $regex $j] if {$quoted} { set quoted 0 continue } elseif {[string equal $c "\\"]} { set quoted 1 continue } elseif {[string equal $c \{]} { incr brlevel } elseif {[string equal $c \}]} { if {[incr brlevel -1] < 0} { error "extra characters after close-brace in regular expression `$regex'" } } } if {$brlevel != 0} { error "missing close brace in regular expression `$regex'" } # # add to rule set # lappend rules [incr ruleno] set regexs($ruleno) $regex set igncase($ruleno) $doigncase if {[info exists thestates]} { foreach thestate $thestates { lappend states($thestate) $ruleno } } else { lappend states(__all__) $ruleno } if {$code != "" && $code != "-"} { set codes($ruleno) $code } if {$verbose > 1} { puts $verbout "ylex: rule $ruleno `$orig_regex' expands to `$regex'" } } return $ruleno } # # ============================================================ # Code handling # ============================================================ # public method code {type thecode} { switch -- $type { public - private - protected { lappend usercode $type $thecode } constructor { set userconstrcode $thecode } destructor { set userdestrcode $thecode } error { set usererrorcode $thecode } reset { set userresetcode $thecode } } } # # ============================================================ # Dump generated Scanner # ============================================================ # public method dump {} { # # Create scanner code # append data "itcl::class $name {\n" append data " public variable case $case\n" append data " public variable verbose 0\n" append data " public variable verbout stderr\n" append data " private variable yyistcl83 -1\n" append data "\n" append data " public variable yydata \"\"\n" append data " public variable yyindex 0\n" append data " private variable yymatches\n" append data "\n" append data " public variable yystate \"$start\"\n" append data " public variable yytext\n" append data " public variable yystart\n" append data " public variable yyend\n" append data "\n" append data " private common yyregexs\n" append data " private common yyigncase\n" append data " private common yystates\n" append data "\n" # # yyregexs: map rule# -> regular expression # append data " array set yyregexs {\n" foreach ruleno [lsort -integer [array names regexs]] { append data " $ruleno {$regexs($ruleno)}\n" } append data " }\n" append data "\n" # # yyigncase: map rule# -> 1 if case should be ignored # append data " array set yyigncase {\n" foreach ruleno [lsort -integer [array names regexs]] { if {[info exists igncase($ruleno)]} { append data " $ruleno $igncase($ruleno)\n" } else { append data " $ruleno 0\n" } } append data " }\n" append data "\n" # # yystates: map name -> list of active rules # append data " array set yystates {\n" set hadall 0 foreach statename [lsort [array names states]] { append data " $statename {\n" foreach ruleno $states($statename) { append data " $ruleno\n" } append data " }\n" if {$statename == "__all__"} { set hadall 1 } } if {!$hadall} { append data " __all__ {\n" append data " }\n" } append data " }\n" append data "\n" # # user code # if {[info exists usercode]} { foreach {type thecode} $usercode { append data " $type {\n" append data $thecode "\n" append data " }\n" append data "\n" } } # # constructor # if {[info exists userconstrcode]} { append data " constructor " $userconstrcode "\n" } else { append data " constructor {args} {\n" append data " eval \$this configure \$args\n" append data " }\n" append data " \n" } # # destructor # if {[info exists userdestrcode]} { append data " destructor " $userdestrcode "\n" } # # reset # append data " public method reset {} {\n" append data " if {\$verbose} {\n" append data " puts \$verbout \"$name: reset, entering state INITIAL\"\n" append data " }\n" append data " \n" append data " set yyindex 0\n" append data " set yystate \"$start\"\n" append data " catch {unset yymatches}\n" if {[info exists userresetcode]} { append data "\n" $userresetcode "\n" } append data " }\n" append data " \n" # # start # append data " public method start {yynewstr} {\n" append data " reset\n" append data " set yydata \$yynewstr\n" append data " }\n" append data " \n" # # yyerror: overloadable error handling # append data " protected method yyerror {yyerrmsg} {\n" if {[info exists usererrorcode]} { append data $usererrorcode "\n" } else { append data " puts \$verbout \"$name: \$yyerrmsg\"\n" } append data " }\n" append data " \n" # # yyupdate: check all regexps for the next match # returns rule for next best match # append data " private method yyupdate {} {\n" append data " if {\$yyistcl83 == -1} {\n" append data " if {\[package vcompare \[info tclversion\] 8.3\] >= 0} {\n" append data " set yyistcl83 1\n" append data " } else {\n" append data " set yyistcl83 0\n" append data " }\n" append data " }\n" append data " \n" append data " set yybestmatch -1\n" append data " set yybestbegin \[string length \$yydata\]\n" append data " set yybestend \$yybestbegin\n" append data " \n" append data " if {\$verbose >= 2} {\n" append data " set yycurdata \[string range \$yydata \$yyindex \[expr {\$yyindex + 16}\]\]\n" append data " set yycurout \[string map {\\r \\\\r \\n \\\\n} \$yycurdata\]\n" append data " if {\$yyindex + 16 < \[string length \$yydata\]} {\n" append data " append yycurout \"...\"\n" append data " }\n" append data " puts \$verbout \"$name: looking for match at position \$yyindex: `\$yycurout'\"\n" append data " }\n" append data " \n" append data " if {\[info exists yystates(\$yystate)\]} {\n" append data " set yyruleset \[concat \$yystates(\$yystate) \$yystates(__all__)\]\n" append data " } else {\n" append data " set yyruleset \$yystates(__all__)\n" append data " }\n" append data " \n" append data " if {\[llength \$yyruleset\] == 0} {\n" append data " if {\$verbose} {\n" append data " puts \$verbout \"$name: no active rules in state \$yystate\"\n" append data " }\n" append data " }\n" set updatecode { foreach yyruleno $yyruleset { # # if the last match is in the past, rerun regexp # if {![info exists yymatches($yyruleno)] || \ [lindex [lindex $yymatches($yyruleno) 0] 0] < $yyindex} { if {$yyistcl83} { if {!$case || $yyigncase($yyruleno)} { set yyfound [regexp -nocase -start $yyindex \ -inline -indices -- \ $yyregexs($yyruleno) $yydata] } else { set yyfound [regexp -start $yyindex \ -inline -indices -- \ $yyregexs($yyruleno) $yydata] } } else { if {!$case || $yyigncase($yyruleno)} { set yyres [regexp -nocase -start $yyindex \ -indices -- $yyregexs($yyruleno) \ $yydata yyfound] } else { set yyres [regexp -start $yyindex \ -indices -- $yyregexs($yyruleno) \ $yydata yyfound] } if {!$yyres} { set yyfound [list] } else { set yyfound [list $yyfound] } } if {[llength $yyfound] > 0} { set yymatches($yyruleno) $yyfound } else { set yymatches($yyruleno) [list \ [string length $yydata] \ [string length $yydata]] } if {$verbose >= 3 && [llength $yymatches($yyruleno)] > 0} { set yymatchbegin [lindex [lindex $yymatches($yyruleno) 0] 0] set yymatchend [lindex [lindex $yymatches($yyruleno) 0] 1] if {$yymatchbegin != [string length $yydata]} { set yymatchdata [string range $yydata $yymatchbegin $yymatchend] set yymatchout [string map {\r "\\r" \n "\\n"} $yymatchdata] if {[string length $yymatchout] > 20} { set yymatchout [string range $yymatchout 0 16] append yymatchout "..." } puts $verbout "%name%: next match for rule $yyruleno is $yymatchbegin-$yymatchend: `$yymatchout'" } else { puts $verbout "%name%: rule $yyruleno does not match anwhere in remaining text" } } } # # see if the match is better than the one we already have # set yymatchbegin [lindex [lindex $yymatches($yyruleno) 0] 0] set yymatchend [lindex [lindex $yymatches($yyruleno) 0] 1] if {($yymatchbegin < $yybestbegin) || \ ($yymatchbegin == $yybestbegin && $yymatchend > $yybestend)} { set yybestmatch $yyruleno set yybestbegin $yymatchbegin set yybestend $yymatchend if {$verbose >= 2} { set yybestdata [string range $yydata $yybestbegin $yybestend] set yymatchout [string map {\r "\\r" \n "\\n"} $yybestdata] if {[string length $yymatchout] > 20} { set yymatchout [string range $yymatchout 0 16] append yymatchout "..." } puts $verbout "%name%: new best match rule $yyruleno, $yybestbegin-$yybestend: `$yymatchout'" } } } } append data [string map [list %name% $name] $updatecode] append data " \n" append data " return \$yybestmatch\n" append data " }\n" append data " \n" # # step # append data " public method step {} {\n" append data " if {\$yyindex >= \[string length \$yydata\]} {\n" append data " if {\$verbose} {\n" append data " puts \$verbout \"$name: scanner at EOF\"\n" append data " }\n" append data " return \[list -1 1 \"\"\]\n" append data " }\n" append data " \n" append data " set yyruleno \[yyupdate\]\n" append data " \n" append data " if {\$yyruleno == -1} {\n" append data " if {\$verbose} {\n" append data " puts \$verbout \"$name: no further match until EOF\"\n" append data " }\n" append data " set yyindex \[string length \$yydata\]\n" append data " return \[list -1 1 \"\"\]\n" append data " }\n" append data " \n" append data " set yymatch \$yymatches(\$yyruleno)\n" append data " set yyindices \[lindex \$yymatch 0\]\n" append data " set yystart \[lindex \$yyindices 0\]\n" append data " set yyend \[lindex \$yyindices 1\]\n" append data " set yytext \[string range \$yydata \$yystart \$yyend\]\n" append data " \n" append data " if {\$verbose} {\n" append data " set yymatchout \[string map {\\r \\\\r \\n \\\\n} \$yytext\]\n" append data " if {\[string length \$yymatchout] > 20} {\n" append data " set yymatchout \[string range \$yymatchout 0 16\]\n" append data " append yymatchout \"...\"\n" append data " }\n" append data " puts \$verbout \"$name: best match at \$yyindex using rule \$yyruleno, \$yystart-\$yyend: `\$yymatchout'\"\n" append data " }\n" append data " \n" append data " set yyindex \[expr {\$yyend + 1}\]\n" append data " \n" # # Set $1, $2 ... to the submatches. We start at index # zero, because the first list element is the complete # match. # append data " for {set yyi 0} {\$yyi < \[llength \$yymatch\]} {incr yyi} {\n" append data " set yysubidxs \[lindex \$yymatch \$yyi\]\n" append data " set yysubbegin \[lindex \$yysubidxs 0\]\n" append data " set yysubend \[lindex \$yysubidxs 1\]\n" append data " set \$yyi \[string range \$yydata \$yysubbegin \$yysubend\]\n" append data " }\n" append data " \n" # # exec user code # append data " set yyoldstate \$yystate\n" append data " set yyretcode \[catch {\n" append data " switch -- \$yyruleno {\n" foreach ruleno [lsort -integer [array names codes]] { append data " $ruleno {\n" append data $codes($ruleno) "\n" append data " }\n" } append data " }\n" append data " } yyretdata\]\n" append data " \n" # # enter new state # append data " if {\$verbose} {\n" append data " if {!\[string match \$yyoldstate \$yystate\]} {\n" append data " puts \$verbout \"$name: leaving state \$yyoldstate, entering \$yystate\"\n" append data " }\n" append data " }\n" append data " \n" # # did the user code throw an error? # append data " if {\$yyretcode == 1} {\n" append data " global errorInfo\n" append data " set yyerrmsg \"script for rule # \$yyruleno failed: \$yyretdata\"\n" append data " yyerror \$yyerrmsg\n" append data " append errorInfo \"\\n while executing script for rule # \$yyruleno\"\n" append data " error \$yyerrmsg \$errorInfo\n" append data " }\n" append data " \n" # # did the user code execute a return? # append data " if {\$yyretcode == 2} {\n" append data " return \[list \$yyruleno 1 \$yyretdata\]\n" append data " }\n" append data " \n" append data " return \[list \$yyruleno 0 \"\"\]\n" append data " }\n" append data " \n" # # next # append data " public method next {} {\n" append data " while {42} {\n" append data " set yysd \[step\]\n" append data " if {\[lindex \$yysd 1\] == 1} {\n" append data " return \[lindex \$yysd 2\]\n" append data " }\n" append data " }\n" append data " }\n" append data " \n" # # run # append data " public method run {} {\n" append data " set yyresult \[list\]\n" append data " set yysd \[next\]\n" append data " while {\$yysd != \"\"} {\n" append data " lappend yyresult \$yysd\n" append data " set yysd \[next\]\n" append data " }\n" append data " return \$yyresult\n" append data " }\n" # # end of class # append data "}\n" append data "\n" } } } |