blob: 871b3ad97f746c771c9e4ae92cf9b95790169a2f (
plain)
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
|
%{
#!/usr/bin/tclsh
# Counts characters, words, and lines within its input.
# This is based upon example 'ch2-02.l' from "lex & yacc" by John
# R. Levine, Tony Mason, and Doug Brown (by O'Reilly & Associates, ISBN
# 1-56592-000-7). For more information on using lex and yacc, see
# http://www.oreilly.com/catalog/lex/.
set charCount 0
set wordCount 0
set lineCount 0
%}
word [^ \t\n]+
eol \n
%%
{word} { incr ::wordCount; incr ::charCount $yyleng }
{eol} { incr ::charCount; incr ::lineCount }
. { incr ::charCount }
%%
if {[llength $argv] > 0} {
if {[catch {open [lindex $argv 0]} f]} {
puts stderr "could not open file [lindex $argv 0]"
exit 1
}
set yyin $f
}
yylex
puts "$charCount $wordCount $lineCount"
return 0
|