]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/m2/tools-src/buildpg
3 # Copyright (C) 2000-2022 Free Software Foundation, Inc.
4 # This file is part of GNU Modula-2.
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)
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.
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
22 # builds the pg.bnf from ppg.mod
23 # usage buildpg ppg.mod destination [-e]
24 # -e build without error recovery
29 includeNonErrorChecking
() {
30 sed -e "1,/StartNonErrorChecking/d" < $PPGSRC |\
31 sed -e "1,/EndNonErrorChecking/!d"
34 includeErrorChecking
() {
35 sed -e "1,/StartErrorChecking/d" < $PPGSRC |\
36 sed -e "1,/EndErrorChecking/!d"
40 echo "% module" $PPGDST "begin"
41 sed -e "1,/% declaration/!d" < $PPGSRC |
sed -e "s/ppg/${PPGDST}/g"
43 echo "% declaration" $PPGDST "begin"
45 sed -e "1,/% declaration/d" < $PPGSRC |
sed -e "1,/% rules/!d" |
sed -e "s/ppg/${PPGDST}/g"
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"
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}) ;/"
60 cat << EOFEOF | sed -e "s/ppg/${PPGDST}/g"
61 error 'WarnError' 'WarnString'
62 tokenfunc 'GetCurrentTokenType()'
64 token 'identifier' identtok -- internal token
65 token 'literal' literaltok
67 token ':=' lbecomestok
68 token '=:' rbecomestok
72 token '{' lcparatok -- left curly para
73 token '}' rcparatok -- right curly para
76 token "error" errortok
77 token "tokenfunc" tfunctok
78 token "symfunc" symfunctok
81 token "module" moduletok
82 token "begin" begintok
83 token "rules" rulestok
87 token "token" tokentok
88 token "special" specialtok
89 token "first" firsttok
90 token "follow" followtok
93 token "declaration" declarationtok
94 token "epsilon" epsilontok
95 token '' eoftok -- internal token
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 { }
108 Rules := "%" "rules" { Defs } ExtBNF =:
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
128 [ Literal % p^.description := LastLiteral % ]
131 Factor := "%" Modula2Code "%" |
132 Ident % WITH CurrentFactor^ DO
134 ident := CurrentIdent
136 Literal % WITH CurrentFactor^ DO
138 string := LastLiteral ;
139 IF GetSymKey(Aliases, LastLiteral)=NulName
141 WarnError1('no token defined for literal %s', LastLiteral)
144 "{" % WITH CurrentFactor^ DO
146 expr := NewExpression() ;
147 CurrentExpression := expr ;
150 "[" % WITH CurrentFactor^ DO
152 expr := NewExpression() ;
153 CurrentExpression := expr ;
156 "(" % WITH CurrentFactor^ DO
158 expr := NewExpression() ;
159 CurrentExpression := expr ;
163 Statement := % VAR i: IdentDesc ; %
165 % VAR p: ProductionDesc ; %
166 % p := FindDefinition(CurrentIdent^.name) ;
171 IF NOT ((p^.statement=NIL) OR (p^.statement^.expr=NIL))
173 WarnError1('already declared rule %s', CurrentIdent^.name)
176 i := CurrentIdent ; %
178 % VAR e: ExpressionDesc ; %
179 % e := NewExpression() ;
180 CurrentExpression := e ; %
181 % VAR s: StatementDesc ; %
182 % s := NewStatement() ;
188 % p^.statement := s ; %
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 =:
199 First := "first" "{" { LitOrTokenOrIdent
200 % WITH CurrentSetDesc^ DO
201 next := TailProduction^.first ;
203 TailProduction^.first := CurrentSetDesc
206 Follow := "follow" "{" { LitOrTokenOrIdent
207 % WITH CurrentSetDesc^ DO
208 next := TailProduction^.followinfo^.follow ;
210 TailProduction^.followinfo^.follow := CurrentSetDesc
213 LitOrTokenOrIdent := Literal % CurrentSetDesc := NewSetDesc() ;
214 WITH CurrentSetDesc^ DO
216 string := LastLiteral ;
220 Ident % CurrentSetDesc := NewSetDesc() ;
221 WITH CurrentSetDesc^ DO
223 ident := CurrentIdent ;
227 Literal := '"' CollectLiteral '"' |
228 "'" CollectLiteral "'" =:
230 CollectTok := % CurrentSetDesc := NewSetDesc() ;
231 WITH CurrentSetDesc^ DO
233 string := GetCurrentToken() ;
235 IF NOT ContainsSymKey(Values, GetCurrentToken())
237 AddEntry(Values, GetCurrentToken(), LargestValue) ;
238 AddEntry(ReverseValues, Name(LargestValue), GetCurrentToken()) ;
239 AddEntry(Aliases, GetCurrentToken(), GetCurrentToken()) ;
240 AddEntry(ReverseAliases, GetCurrentToken(), GetCurrentToken()) ;
243 AdvanceToken() ; % =:
245 CollectLiteral := % LastLiteral := GetCurrentToken() ;
248 DefineToken := % AddEntry(Aliases, LastLiteral, GetCurrentToken()) ;
249 AddEntry(ReverseAliases, GetCurrentToken(), LastLiteral) ;
250 AddEntry(Values, GetCurrentToken(), LargestValue) ;
251 AddEntry(ReverseValues, Name(LargestValue), GetCurrentToken()) ;
255 Token := Literal DefineToken =:
257 ErrorProcedures := Literal % ErrorProcArray := LastLiteral %
258 Literal % ErrorProcString := LastLiteral % =:
259 TokenProcedure := Literal % TokenTypeProc := LastLiteral % =:
260 SymProcedure := Literal % SymIsProc := LastLiteral % =:
262 Production := Statement =:
263 Expression := % VAR t1, t2: TermDesc ;
264 e : ExpressionDesc ; %
265 % e := CurrentExpression ;
267 CurrentTerm := t1 ; %
268 Term % e^.term := t1 ; %
269 { "|" % t2 := NewTerm() ;
271 Term % t1^.next := t2 ;
274 Term := % VAR t1: TermDesc ; f1, f2: FactorDesc ; %
275 % CurrentFactor := NewFactor() ;
276 f1 := CurrentFactor ;
277 t1 := CurrentTerm ; %
278 Factor % t1^.factor := f1 ;
280 CurrentFactor := f2 %
281 { Factor % f1^.next := f2 ;
284 CurrentFactor := f2 ; % }