Tcl Scanners

Development and usage of C, Java and Tcl based scanners in Tcl-applications

Dr. Detlef Groth and Alexander Straub, MPIMG Berlin, Germany, dgroth(at)

Abstract: Although since many years many scanner generation frameworks like flex (C), jflex (java) and fickle or ylex (Tcl) are existing most of the scanners and parsers are still handwritten. This is this case although it is well proven that automatically generated scanners are easier to write and to maintain and are at least as fast as handwritten ones. Tcl lexers like fickle and ylex are Tcl-only implementations and are well suited for relative small files. In bioinformatics however we are working often with files several gigabytes in size. Here tcl-based scanners are offering only a poor performance if compared with C- or java based scanners. In order to overcome those limitations we were trying to utilise C-based scanners inside tcl-programs using the critcl package and java based scanners using the jacl/tcljava package with promising results.

The current scanner generator packages for tcl, ylex, fickle and tcLex have some serious drawbacks thereof limiting its usability. tcLex is a old C-extension and not actively maintained. ylex requires that the whole string which will be scanned must be in memory which is not suitable for large files. Both ylex and tcLex use more a tclish than a lex-like syntax to dynamically construct scanners. The scanners can be saved as tcl-code as well to generate scanners which can be used independently from the ylex-package. Fickle in contrast use lex-like input files and generates only static scanners which can be used independently from the fickle command line application. In order to overcome the global type of variables and procedures I recently developed ifickle which generates itcl-classes. Those scanners can be integrated into a larger framework/package like the biotcl-package. However due to its heavy use of the regexp command both ylex and fickle/ifickle are generating very slow scanners if the file size increases. Ifickle based scanners have a slightly larger startup overhead and are slightly less slow on larger files.

Table 1 compares the performance of various wc-scanners either hand crafted or generated with scanner generation frameworks like perl-parseLex, flex or re2c. As it visible performance of ylex and fickle / ifickle based scanners is not acceptable if larger files as often required if analysing biological data like genomes, proteomes.

In order to overcome the slow performance with scanners generated by tcl-based scanners generators, we were investigating the usability of C and Java based scanners for integration into tcl-programs. From the two C-based scanners, flex and re2c we choose re2c for an wc-implementation. The reason was, that in contrast to flex, re2c does not depend from external libraries and the code generated from re2c can thereof be easily integrated into tcl-programs using the critcl-package. Furthermore it seems that from our data and from the data of others re2c based scanners are 2 - 5 times faster than flex based scanners. Starting to use re2c was somehow difficult, but after fixing some initial issues with buffer filling and wrapping the re2c application into the critcl-application it was possible to use the re2c scanner either standalone or via the tcl application. The performance was comparable with the pure re2c application however the startup cost was with 1 second for small files quite high.

Because coding in C is for many people much more difficult than coding in java we were further investigating the possibility to utilise java based scanners inside tcl. There are two possible choices for communicating between the java machine and the tcl application. Recently there has been some effort to compile the tclblend library with stubs to utilise it inside starkits, however there is currently no starkit enabled tclblend library available. Tclblend would enable tcl programmers to directly communicate with java classes like with tcljava. In our study we were utilising a second approach, communication via sockets. The tcl application is looking for a free socket port and starts the java application. The java application sends it's parsing results via the socket connections to the tcl application. This approach has bigger startup penalty, because two interpreters must be started, but for large files which should be scanned, the startup is less import than the actual scanner performance. As can be seen from table 1 the scanning speed is comparable with a re2c based wc scanner. Both are about 3 orders of magnitude faster in comparison to tcl based scanners.

We were furthermore writing scanners with ifickle, re2c and jflex for biological data (Blast). Table 2 summaries our observations. The speed for tcl based scanners on larger files is very slow, however Coding and Integration with pure tcl based scanners is easier to accomplish in comparison with re2c or java based scanners.


re2c and jflex based scanners can be utilised for tcl-application thereof limiting the bad performance of current tcl based scanner generators. The big speed difference might be due to the fact that both jflex and re2c did not use any regexp library, but rather are building its own effective mechanism to translate regular expressions into a fast scanner. It might be therefore of interest to study the code generated by re2c and especially for jflex and trying to build a similar tcl based scanner generator. Using the critcl/re2c or socket/java approach gives some startup penalties but this is not of importance if large amount of data, as common in biology, needs to be scanned.



Table 1:


Table 2:


Source Code


Implementation of wc with ifickle.

 public variable nline 0
 public variable nword 0
 public variable nchar 0
 %buffersize 1024
 \n { incr nline; incr nchar 2 ; }
 [^ \t\n]+ { incr nword; incr nchar $yyleng ;}
 . { incr nchar;}
 if {[llength $argv] == 0} {
    puts stderr "usage wc-fickle inputfile"
    exit 0
 if {[catch {open [lindex $argv 0] r} yyin]} {
    puts stderr "Could not open [lindex $argv 0]"
    exit 0
 set sc [iwcfickle \#auto -yyin $yyin]
 $sc yylex
 puts [format "%7d %7d %7d %s" \
     [$sc cget -nline] \
     [$sc cget -nword] \
     [$sc cget -nchar] [lindex $argv 0]]
 close $yyin


Implementation of wc with java-jflex
 /* WC.flex */
 %class Wc
  int nchars = 0;
  int nwords = 0;
  int nlines = 0; 
  System.out.println("   "+nlines+"\t"+nwords+"\t"+nchars);
 [\n] { nlines += 1; nchars += 1;  }
 [ \t]+ { nchars += yylength() ; }
 [^ \t\n]+ { nwords += 1; nchars += yylength(); }


WC with re2c, tcl and the critcl package

 /* File: wc-critcl.tcl */
 source ./critcl.kit
 package require critcl
 set cfile [file rootname [info script]].c
 if [catch {open $cfile r} infh] {
    puts stderr "Cannot open $cfile : $infh"
 } else {
    set ccode [read $infh]
    close $infh
 critcl::ccode $ccode
 critcl::cproc lines {} int { return numline; }
 critcl::cproc words {} int { return numword; }
 critcl::cproc chars {} int { return numchar; }
 critcl::cproc myscan {char* filename} void { readFile(filename); }
 if {[llength $argv] != 1 || ![file exists [lindex $argv 0]]} {
    puts "Usage: [info script] <filename>"
    exit 0 
 myscan [lindex $argv 0]
 puts [format "%7d %7d %7d %s" [lines] [words] [chars] [lindex $argv 0]]

re2c wc application ready for usage inside tcl applications with the critcl package.

/* File: */
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 #include <fcntl.h>

 #define    EOI    319
 #define    BSIZE    8192
 #define    YYCTYPE        uchar
 #define    YYCURSOR    cursor
 #define    YYLIMIT        s->lim
 #define    YYMARKER    s->ptr
 #define    YYFILL(n)    {cursor = fill(s, cursor);}
 #define    RET(i)    {s->cur = cursor; return i;}
 typedef unsigned int uint;
 typedef unsigned char uchar;
 int numline, numchar,numword= 0;
 typedef struct Scanner {
    int            fd;
    uchar        *bot, *tok, *ptr, *cur, *pos, *lim, *top, *eof;
    uint        line;
 } Scanner;
 uchar *fill(Scanner *s, uchar *cursor){
    if(!s->eof) {
        uint cnt = s->tok - s->bot;
            memcpy(s->bot, s->tok, s->lim - s->tok);
            s->tok = s->bot;
            s->ptr -= cnt;
            cursor -= cnt;
            s->pos -= cnt;
            s->lim -= cnt;
        if((s->top - s->lim) < BSIZE){
            uchar *buf = (uchar*) malloc(((s->lim - s->bot) + BSIZE)*sizeof(uchar));
            memcpy(buf, s->tok, s->lim - s->tok);
            s->tok = buf;
            s->ptr = &buf[s->ptr - s->bot];
            cursor = &buf[cursor - s->bot];
            s->pos = &buf[s->pos - s->bot];
            s->lim = &buf[s->lim - s->bot];
            s->top = &s->lim[BSIZE];
            s->bot = buf;
        if((cnt = read(s->fd, (char*) s->lim, BSIZE)) != BSIZE){
            s->eof = &s->lim[cnt]; *(s->eof)++ = '\n';
        s->lim += cnt;
    return cursor;
 int scan(Scanner *s){
    uchar *cursor = s->cur;
    s->tok = cursor;
    [ \t]+ {
        numchar += YYCURSOR-s->tok;
        goto std;
    "\n" {
        if(cursor == s->eof) RET(EOI);
        s->pos = cursor; s->line++;
        goto std;
    [!-~]+ { ++numword; numchar += YYCURSOR-s->tok; goto std; }
    [\000] { RET(EOI) ;}

 int readFile (char* filename) {
    Scanner in;
    int t;
    memset((char*) &in, 0, sizeof(in));
    in.fd = open(filename, O_RDONLY);
    if (in.fd == -1) {
         printf("error reading file: %s ", filename);
         return 1 ;
    while((t = scan(&in)) != EOI){    }
    return 0 ;
 int main(int argc, char *argv[]){
    int ret ;
    if(argc != 2) {
        printf("usage: %s <filename>", argv[0]);
        return 0 ;
    ret = readFile(argv[1]);
    printf("\t%i\t%i\t%i\t%s\n", numline,numword,numchar,argv[1]);
    return ret ;

This re2c code needs to be compiled in the following way:

 $ re2c > wc-critcl.c
 # (optional) compile command line application
 $ gcc -o wc-critcl wc-critcl.c
 # (optional) run command line application
 $ ./wc-critcl wc-critcl.tcl
       21      106    694 wc-critcl.tcl
 # tcl application
 $ tclkit wc-critcl.tcl wc-critcl.tcl
       21     106     694 wc-critcl.tcl
 $ wc wc-critcl.tcl
       21     106     694 wc-critcl.tcl