File: pascal.tcl

package info (click to toggle)
tclex 1.2a1-9
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 552 kB
  • ctags: 436
  • sloc: ansic: 3,772; tcl: 452; sh: 85; makefile: 83; lex: 48
file content (40 lines) | stat: -rw-r--r-- 1,005 bytes parent folder | download | duplicates (8)
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/local/bin/tclsh8.0
#
# Project  : tcLex examples
# File     : pascal.tcl
# Language : Tcl, with tcLex
# Version  : 0.1
# Dates    : 10/11/1998
# Author   : Neil Walker
#
# "scanner for a toy Pascal-like langauge"
#
# To run this program:
#    ./pascal.tcl textfile
#

package require tcLex 1.1

lexer pascal -longest -nocase {
    {} {[0-9]+}             {integer}   {puts "An integer: $integer"}

    {} {[0-9]+\.[0-9]*}     {float}     {puts "A float: $float"}

    {} {if|then|begin|end|procedure|function} {keyword} {puts "A keyword: $keyword"}

    {} {[a-z][a-z0-9]*}   {id}    {puts "An identifier: $id"}

    {} {\+|-|\*|/}          {op}        {puts "An operator: $op"}

    {} "\{[^\}\n]*\}"       {}          {# eat up one-line comments}

    {} "[ \t\n]+"           {}          {# eat up whitespace}

    {} {.}                  {c}         {puts "Unrecognized character: $c"}
}

if {$argc > 0} {
    pascal eval [read [open [lindex $argv 0] r]]
} else {
    pascal eval [read stdin]
}