summaryrefslogtreecommitdiffstats
path: root/fickle/examples/csa.fcl
blob: 3cd6dc35e646ba2d4ef9d3661ee54c7ae7891fe8 (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
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
# $Id: csa.fcl,v 1.1.1.1 2004/07/23 19:22:41 tang Exp $

# Counts the lines of comments, code, and whitespace within a C
# program.

# This is based upon example 'ch2-09.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/.

%{
#!/usr/bin/tclsh

set comments 0
set code 0
set whitespace 0

proc update_count { a b c } {
    incr ::comments $a
    incr ::code $b
    incr ::whitespace $c
    puts -nonewline "code: $::code, comments: $::comments, whitespace: $::whitespace\r"
    flush stdout
}

%}

%option noheaders stack nodefault
%x COMMENT

%%

^[ \t]*"/*"                 { BEGIN COMMENT }
^[ \t]*"/*".*"*/"[ \t]*\n   { update_count 1 0 0 }
<COMMENT>"*/"[ \t]*\n       { BEGIN INITIAL; update_count 1 0 0 }
<COMMENT>"*/"               { BEGIN INITIAL }
<COMMENT>\n                 { update_count 1 0 0 }
<COMMENT>.\n                { update_count 1 0 0 }

^[ \t]*\n                   { update_count 0 0 1 }

.+"/*".*"*/".*\n            { update_count 0 1 0 }
.*"/*".*"*/".+\n            { update_count 0 1 0 }
.+"/*".*\n                  { BEGIN COMMENT; update_count 0 1 0 }
.\n                         { update_count 0 1 0 }

<*>.                        # do nothing

%%

if {[llength $argv] == 0} {
    puts stderr "C source analyzer needs a filename."
    exit 0
}

if {[catch {open [lindex $argv 0] r} yyin]} {
    puts stderr "Could not open [lindex $argv 0]"
    exit 0
}

yylex

close $yyin

puts ""