include problem
[unix-history] / .ref-BSD-3 / usr / doc / ratfor / m3
CommitLineData
8340f87c
BJ
1.NH
2IMPLEMENTATION
3.PP
4Ratfor
5was originally written in
6C[4]
7on the
8.UC UNIX
9operating system[5].
10The language is specified by a context free grammar
11and the compiler constructed using
12the
13.UC YACC
14compiler-compiler[6].
15.PP
16The
17Ratfor
18grammar is simple and straightforward, being essentially
19.P1
20prog : stat
21 | prog stat
22stat : \f3if\fP (...) stat
23 | \f3if\fP (...) stat \f3else\fP stat
24 | \f3while\fP (...) stat
25 | \f3for\fP (...; ...; ...) stat
26 | \f3do\fP ... stat
27 | \f3repeat\fP stat
28 | \f3repeat\fP stat \f3until\fP (...)
29 | \f3switch\fP (...) { \f3case\fP ...: prog ...
30 \f3default\fP: prog }
31 | \f3return\fP
32 | \f3break\fP
33 | \f3next\fP
34 | digits stat
35 | { prog }
36 | anything unrecognizable
37.P2
38The observation
39that
40Ratfor
41knows no Fortran
42follows directly from the rule that says a statement is
43``anything unrecognizable''.
44In fact most of Fortran falls into this category,
45since any statement that does not begin with one of the keywords
46is by definition ``unrecognizable.''
47.PP
48Code generation is also simple.
49If the first thing on a source line is
50not a keyword
51(like
52.UL if ,
53.UL else ,
54etc.)
55the entire statement is simply copied to the output
56with appropriate character translation and formatting.
57(Leading digits are treated as a label.)
58Keywords cause only slightly more complicated actions.
59For example, when
60.UL if
61is recognized, two consecutive labels L and L+1
62are generated and the value of L is stacked.
63The condition is then isolated, and the code
64.P1
65if (.not. (condition)) goto L
66.P2
67is output.
68The
69.ul
70statement
71part of the
72.UL if
73is then translated.
74When the end of the
75statement is encountered
76(which may be some distance away and include nested \f3if\fP's, of course),
77the code
78.P1
79L continue
80.P2
81is generated, unless there is an
82.UL else
83clause, in which case
84the code is
85.P1
86 goto L+1
87L continue
88.P2
89In this latter case,
90the code
91.P1
92L+1 continue
93.P2
94is produced after the
95.ul
96statement
97part of the
98.UL else.
99Code generation for the various loops is equally simple.
100.PP
101One might argue that more care should be taken
102in code generation.
103For example,
104if there is no trailing
105.UL else ,
106.P1
107 if (i > 0) x = a
108.P2
109should be left alone, not converted into
110.P1
111 if (.not. (i .gt. 0)) goto 100
112 x = a
113100 continue
114.P2
115But what are optimizing compilers for, if not to improve code?
116It is a rare program indeed where this kind of ``inefficiency''
117will make even a measurable difference.
118In the few cases where it is important,
119the offending lines can be protected by `%'.
120.PP
121The use of a compiler-compiler is definitely the preferred method
122of software development.
123The language is well-defined,
124with few syntactic irregularities.
125Implementation is quite simple;
126the original construction took under a week.
127The language
128is sufficiently simple, however, that an
129.ul
130ad hoc
131recognizer can be readily constructed to do the same job
132if no compiler-compiler is available.
133.PP
134The C version of
135Ratfor
136is used on
137.UC UNIX
138and on the Honeywell
139.UC GCOS
140systems.
141C compilers are not as widely available as Fortran, however,
142so there is also a
143Ratfor
144written in itself
145and originally bootstrapped with the C version.
146The
147Ratfor
148version
149was written so as to translate into the portable subset
150of Fortran described in [1],
151so it is portable,
152having been run essentially without change
153on at least twelve distinct machines.
154(The main restrictions of the portable subset are:
155only one character per machine word;
156subscripts in the
157form
158.ul
159c*v\(+-c;
160avoiding expressions in places like
161.UC DO
162loops;
163consistency in subroutine argument usage,
164and in
165.UC COMMON
166declarations.
167Ratfor
168itself will not gratuitously generate non-standard Fortran.)
169.PP
170The
171Ratfor
172version is about 1500 lines of
173Ratfor
174(compared to about 1000 lines of C);
175this compiles into 2500 lines of Fortran.
176This expansion ratio is somewhat higher than average,
177since the compiled code contains unnecessary occurrences
178of
179.UC COMMON
180declarations.
181The execution time of the
182Ratfor
183version is dominated by
184two routines that read and write cards.
185Clearly these routines could be replaced
186by machine coded local versions;
187unless this is done, the efficiency of other parts of the translation process
188is largely irrelevant.