Debian Codegraph @

ipl/packs/tcll1/tcll1.icn

raw
1 # TCLL1 -- an LL(1) parser generator
2 # Main program.
3 # (written by Dr. Thomas W. Christopher)
4 #
5
6 link readll1,parsell1,scangram,semgram,semstk,gramanal,ll1
7
8 procedure main(L)
9 local filename,baseFilename,flags,filenameParts,gf
10
11 flags := ""
12 if L[1][1]=="-" then {
13 flags := L[1]
14 filename := L[2]
15 } else {
16 filename:=L[1]
17 }
18 if /filename then
19 stop("usage: [iconx] tcll1 [flags] filename.grm")
20
21 filenameParts:=fileSuffix(filename)
22 baseFilename:=filenameParts[1]
23 if filename==(baseFilename||".ll1") then
24 stop("would write output over input")
25 initScanner( filename |
26 (/filenameParts[2] & baseFilename||".grm")) |
27 stop("unable to open input: ",filename)
28
29 initGrammar()
30 initSemanticsStack()
31
32 gf:=findFileOnPATH("tcll1.ll1") |
33 stop("unable to find parser's grammar file: tcll1.ll1")
34 parseLL1(readLL1(gf)) |
35 stop("unable to read parser's grammar file: tcll1.ll1")
36
37 finishDeclarations()
38 ll1(baseFilename||".ll1")
39 if find("p",flags) then printGrammar()
40 write(errorCount," error",(errorCount~=1&"s")|"",
41 " and ",warningCount," warning",(warningCount~=1&"s")|"")
42
43 end
44
45 # From: filename.icn in Icon Program Library
46 # Author: Robert J. Alexander, 5 Dec. 89
47 # Modified: Thomas Christopher, 12 Oct. 94
48
49 procedure fileSuffix(s,separator)
50 local i
51 /separator := "."
52 i := *s + 1
53 every i := find(separator,s)
54 return [s[1:i],s[(*s >= i) + 1:0] | &null]
55 end
56
57 procedure findFileOnPATH(s) #adapted from DOPEN.ICN
58 local file, paths, path, filename
59
60 if file := open(s) then { # look in current directory
61 close(file)
62 return s
63 }
64
65 paths := getenv("PATH") | fail
66
67 paths := map(paths,"\\;","/ ") #convert DOS to UNIX-style
68 s := "/" || s # platform-specific
69
70 paths ? {
71 while path := tab(upto(' ') | 0) do {
72 if file := open(filename:=path || s) then {
73 close(file)
74 return filename
75 }
76 tab(many(' ')) | break
77 }
78 }
79
80 fail
81 end
82
83 #
84 # Error reporting required by parseLL1():
85 #
86 procedure reportParseError(t)
87 error("unexpected input ",t.body,
88 " at line ",t.line," column ",t.column)
89 return
90 end
91
92