Debian Codegraph @

gcc/m2/tools-src/buildpg

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