]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/m2/tools-src/buildpg
Merge modula-2 front end onto gcc.
[thirdparty/gcc.git] / gcc / m2 / tools-src / buildpg
1 #!/bin/sh
2
3 # Copyright (C) 2000-2022 Free Software Foundation, Inc.
4 # This file is part of GNU Modula-2.
5 #
6 # GNU Modula-2 is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3, or (at your option)
9 # any later version.
10 #
11 # GNU Modula-2 is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with GNU Modula-2; see the file COPYING. If not, write to the
18 # Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA.
20 #
21
22 # builds the pg.bnf from ppg.mod
23 # usage buildpg ppg.mod destination [-e]
24 # -e build without error recovery
25 #
26 PPGSRC=$1
27 PPGDST=$2
28
29 includeNonErrorChecking () {
30 sed -e "1,/StartNonErrorChecking/d" < $PPGSRC |\
31 sed -e "1,/EndNonErrorChecking/!d"
32 }
33
34 includeErrorChecking () {
35 sed -e "1,/StartErrorChecking/d" < $PPGSRC |\
36 sed -e "1,/EndErrorChecking/!d"
37 }
38
39
40 echo "% module" $PPGDST "begin"
41 sed -e "1,/% declaration/!d" < $PPGSRC | sed -e "s/ppg/${PPGDST}/g"
42
43 echo "% declaration" $PPGDST "begin"
44
45 sed -e "1,/% declaration/d" < $PPGSRC | sed -e "1,/% rules/!d" | sed -e "s/ppg/${PPGDST}/g"
46
47 if [ "$3" = "-e" ] ; then
48 includeNonErrorChecking
49 echo "% module" $PPGDST "end"
50 sed -e "1,/% module pg end/d" < $PPGSRC | sed -e "s/ppg/${PPGDST}/g"
51 else
52 includeErrorChecking
53 echo "% module" $PPGDST "end"
54 sed -e "1,/% module pg end/d" < $PPGSRC | sed -e "s/ppg/${PPGDST}/g" |\
55 sed -e "s/WasNoError := Main() ;/Main({eoftok}) ;/"
56 fi
57
58 echo "% rules"
59
60 cat << EOFEOF | sed -e "s/ppg/${PPGDST}/g"
61 error 'WarnError' 'WarnString'
62 tokenfunc 'GetCurrentTokenType()'
63
64 token 'identifier' identtok -- internal token
65 token 'literal' literaltok
66 token '%' codetok
67 token ':=' lbecomestok
68 token '=:' rbecomestok
69 token '|' bartok
70 token '[' lsparatok
71 token ']' rsparatok
72 token '{' lcparatok -- left curly para
73 token '}' rcparatok -- right curly para
74 token '(' lparatok
75 token ')' rparatok
76 token "error" errortok
77 token "tokenfunc" tfunctok
78 token "symfunc" symfunctok
79 token '"' dquotetok
80 token "'" squotetok
81 token "module" moduletok
82 token "begin" begintok
83 token "rules" rulestok
84 token "end" endtok
85 token '<' lesstok
86 token '>' gretok
87 token "token" tokentok
88 token "special" specialtok
89 token "first" firsttok
90 token "follow" followtok
91 token "BNF" BNFtok
92 token "FNB" FNBtok
93 token "declaration" declarationtok
94 token "epsilon" epsilontok
95 token '' eoftok -- internal token
96
97 special Ident first { < identtok > } follow { }
98 special Modula2Code first { } follow { '%' }
99 special StartModName first { < identtok > } follow { }
100 special EndModName first { < identtok > } follow { }
101 special DoDeclaration first { < identtok > } follow { }
102 special CollectLiteral first { < literaltok > } follow { }
103 special CollectTok first { < identtok > } follow { }
104 special DefineToken first { < identtok > } follow { }
105
106 BNF
107
108 Rules := "%" "rules" { Defs } ExtBNF =:
109
110 Special := Ident
111 % VAR p: ProductionDesc ; %
112 % p := NewProduction() ;
113 p^.statement := NewStatement() ;
114 p^.statement^.followinfo^.calcfollow := TRUE ;
115 p^.statement^.followinfo^.epsilon := false ;
116 p^.statement^.followinfo^.reachend := false ;
117 p^.statement^.ident := CurrentIdent ;
118 p^.statement^.expr := NIL ;
119 p^.firstsolved := TRUE ;
120 p^.followinfo^.calcfollow := TRUE ;
121 p^.followinfo^.epsilon := false ;
122 p^.followinfo^.reachend := false %
123 First Follow [ "epsilon" % p^.statement^.followinfo^.epsilon := true ; (* these are not used - but they are displayed when debugging *)
124 p^.statement^.followinfo^.reachend := true ;
125 p^.followinfo^.epsilon := true ;
126 p^.followinfo^.reachend := true
127 % ]
128 [ Literal % p^.description := LastLiteral % ]
129 =:
130
131 Factor := "%" Modula2Code "%" |
132 Ident % WITH CurrentFactor^ DO
133 type := id ;
134 ident := CurrentIdent
135 END ; % |
136 Literal % WITH CurrentFactor^ DO
137 type := lit ;
138 string := LastLiteral ;
139 IF GetSymKey(Aliases, LastLiteral)=NulName
140 THEN
141 WarnError1('no token defined for literal %s', LastLiteral)
142 END
143 END ; % |
144 "{" % WITH CurrentFactor^ DO
145 type := mult ;
146 expr := NewExpression() ;
147 CurrentExpression := expr ;
148 END ; %
149 Expression "}" |
150 "[" % WITH CurrentFactor^ DO
151 type := opt ;
152 expr := NewExpression() ;
153 CurrentExpression := expr ;
154 END ; %
155 Expression "]" |
156 "(" % WITH CurrentFactor^ DO
157 type := sub ;
158 expr := NewExpression() ;
159 CurrentExpression := expr ;
160 END ; %
161 Expression ")" =:
162
163 Statement := % VAR i: IdentDesc ; %
164 Ident
165 % VAR p: ProductionDesc ; %
166 % p := FindDefinition(CurrentIdent^.name) ;
167 IF p=NIL
168 THEN
169 p := NewProduction()
170 ELSE
171 IF NOT ((p^.statement=NIL) OR (p^.statement^.expr=NIL))
172 THEN
173 WarnError1('already declared rule %s', CurrentIdent^.name)
174 END
175 END ;
176 i := CurrentIdent ; %
177 ":="
178 % VAR e: ExpressionDesc ; %
179 % e := NewExpression() ;
180 CurrentExpression := e ; %
181 % VAR s: StatementDesc ; %
182 % s := NewStatement() ;
183 WITH s^ DO
184 ident := i ;
185 expr := e
186 END ; %
187 Expression
188 % p^.statement := s ; %
189 "=:" =:
190
191 Defs := "special" Special | "token" Token | "error" ErrorProcedures |
192 "tokenfunc" TokenProcedure | "symfunc" SymProcedure =:
193 ExtBNF := "BNF" { Production } "FNB" =:
194 Main := Header Decls Footer Rules =:
195 Header := "%" "module" StartModName =:
196 Decls := "%" "declaration" DoDeclaration =:
197 Footer := "%" "module" EndModName =:
198
199 First := "first" "{" { LitOrTokenOrIdent
200 % WITH CurrentSetDesc^ DO
201 next := TailProduction^.first ;
202 END ;
203 TailProduction^.first := CurrentSetDesc
204 %
205 } "}" =:
206 Follow := "follow" "{" { LitOrTokenOrIdent
207 % WITH CurrentSetDesc^ DO
208 next := TailProduction^.followinfo^.follow ;
209 END ;
210 TailProduction^.followinfo^.follow := CurrentSetDesc
211 %
212 } "}" =:
213 LitOrTokenOrIdent := Literal % CurrentSetDesc := NewSetDesc() ;
214 WITH CurrentSetDesc^ DO
215 type := litel ;
216 string := LastLiteral ;
217 END ;
218 % |
219 '<' CollectTok '>' |
220 Ident % CurrentSetDesc := NewSetDesc() ;
221 WITH CurrentSetDesc^ DO
222 type := idel ;
223 ident := CurrentIdent ;
224 END ;
225 % =:
226
227 Literal := '"' CollectLiteral '"' |
228 "'" CollectLiteral "'" =:
229
230 CollectTok := % CurrentSetDesc := NewSetDesc() ;
231 WITH CurrentSetDesc^ DO
232 type := tokel ;
233 string := GetCurrentToken() ;
234 END ;
235 IF NOT ContainsSymKey(Values, GetCurrentToken())
236 THEN
237 AddEntry(Values, GetCurrentToken(), LargestValue) ;
238 AddEntry(ReverseValues, Name(LargestValue), GetCurrentToken()) ;
239 AddEntry(Aliases, GetCurrentToken(), GetCurrentToken()) ;
240 AddEntry(ReverseAliases, GetCurrentToken(), GetCurrentToken()) ;
241 INC(LargestValue)
242 END ;
243 AdvanceToken() ; % =:
244
245 CollectLiteral := % LastLiteral := GetCurrentToken() ;
246 AdvanceToken ; % =:
247
248 DefineToken := % AddEntry(Aliases, LastLiteral, GetCurrentToken()) ;
249 AddEntry(ReverseAliases, GetCurrentToken(), LastLiteral) ;
250 AddEntry(Values, GetCurrentToken(), LargestValue) ;
251 AddEntry(ReverseValues, Name(LargestValue), GetCurrentToken()) ;
252 INC(LargestValue) ;
253 AdvanceToken ; % =:
254
255 Token := Literal DefineToken =:
256
257 ErrorProcedures := Literal % ErrorProcArray := LastLiteral %
258 Literal % ErrorProcString := LastLiteral % =:
259 TokenProcedure := Literal % TokenTypeProc := LastLiteral % =:
260 SymProcedure := Literal % SymIsProc := LastLiteral % =:
261
262 Production := Statement =:
263 Expression := % VAR t1, t2: TermDesc ;
264 e : ExpressionDesc ; %
265 % e := CurrentExpression ;
266 t1 := NewTerm() ;
267 CurrentTerm := t1 ; %
268 Term % e^.term := t1 ; %
269 { "|" % t2 := NewTerm() ;
270 CurrentTerm := t2 %
271 Term % t1^.next := t2 ;
272 t1 := t2 % } =:
273
274 Term := % VAR t1: TermDesc ; f1, f2: FactorDesc ; %
275 % CurrentFactor := NewFactor() ;
276 f1 := CurrentFactor ;
277 t1 := CurrentTerm ; %
278 Factor % t1^.factor := f1 ;
279 f2 := NewFactor() ;
280 CurrentFactor := f2 %
281 { Factor % f1^.next := f2 ;
282 f1 := f2 ;
283 f2 := NewFactor() ;
284 CurrentFactor := f2 ; % }
285 =:
286
287 FNB
288
289 EOFEOF