Debian Codegraph @

gcc/m2/pge-boot/Gpge.cc

raw
1 /* do not edit automatically generated by mc from pge. */
2 /* pge.mod master source file of the ebnf parser generator.
3
4 Copyright (C) 2003-2025 Free Software Foundation, Inc.
5 Contributed by Gaius Mulley <[email protected]>.
6
7 This file is part of GNU Modula-2.
8
9 GNU Modula-2 is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GNU Modula-2 is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU Modula-2; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 #include <stdbool.h>
24 # if !defined (PROC_D)
25 # define PROC_D
26 typedef void (*PROC_t) (void);
27 typedef struct { PROC_t proc; } PROC;
28 # endif
29
30 # if !defined (TRUE)
31 # define TRUE (1==1)
32 # endif
33
34 # if !defined (FALSE)
35 # define FALSE (1==0)
36 # endif
37
38 #include <stddef.h>
39 #include <string.h>
40 #include <limits.h>
41 # include "GStorage.h"
42 # include "Gmcrts.h"
43 #if defined(__cplusplus)
44 # undef NULL
45 # define NULL 0
46 #endif
47 # include "GPushBackInput.h"
48 # include "Gbnflex.h"
49 # include "GStrLib.h"
50 # include "GStorage.h"
51 # include "GNameKey.h"
52 # include "GNumberIO.h"
53 # include "GSymbolKey.h"
54 # include "GLists.h"
55 # include "GDynamicStrings.h"
56 # include "GASCII.h"
57 # include "GStrIO.h"
58 # include "GStdIO.h"
59 # include "GDebug.h"
60 # include "GArgs.h"
61 # include "GSYSTEM.h"
62 # include "Glibc.h"
63 # include "GOutput.h"
64 # include "GM2RTS.h"
65
66 # define MaxCodeHunkLength 8192
67 # define MaxFileName 8192
68 # define MaxString 8192
69 # define DefaultRecovery true
70 # define MaxElementsInSet 32
71 # define BaseRightLimit 75
72 # define BaseRightMargin 50
73 # define BaseNewLine 3
74 typedef struct pge_termdesc_r pge_termdesc;
75
76 typedef pge_termdesc *pge_TermDesc;
77
78 typedef struct pge_DoProcedure_p pge_DoProcedure;
79
80 typedef unsigned int pge_SetOfStop;
81
82 typedef struct pge__T1_r pge__T1;
83
84 typedef pge__T1 *pge_IdentDesc;
85
86 typedef struct pge__T2_r pge__T2;
87
88 typedef pge__T2 *pge_ProductionDesc;
89
90 typedef struct pge__T3_r pge__T3;
91
92 typedef pge__T3 *pge_StatementDesc;
93
94 typedef struct pge__T4_r pge__T4;
95
96 typedef pge__T4 *pge_ExpressionDesc;
97
98 typedef struct pge__T5_r pge__T5;
99
100 typedef struct pge__T6_r pge__T6;
101
102 typedef pge__T6 *pge_FollowDesc;
103
104 typedef struct pge__T7_r pge__T7;
105
106 typedef pge__T7 *pge_SetDesc;
107
108 typedef struct pge__T8_r pge__T8;
109
110 typedef pge__T8 *pge_CodeDesc;
111
112 typedef struct pge__T9_r pge__T9;
113
114 typedef pge__T9 *pge_CodeHunk;
115
116 typedef struct pge__T10_a pge__T10;
117
118 typedef struct pge__T11_a pge__T11;
119
120 typedef enum {pge_idel, pge_tokel, pge_litel} pge_ElementType;
121
122 typedef enum {pge_m2none, pge_m2if, pge_m2elsif, pge_m2while} pge_m2condition;
123
124 typedef enum {pge_unknown, pge_true, pge_false} pge_TraverseResult;
125
126 typedef enum {pge_id, pge_lit, pge_sub, pge_opt, pge_mult, pge_m2} pge_FactorType;
127
128 typedef pge__T5 *pge_FactorDesc;
129
130 struct pge_termdesc_r {
131 pge_FactorDesc factor;
132 pge_TermDesc next;
133 pge_FollowDesc followinfo;
134 unsigned int line;
135 };
136
137 typedef void (*pge_DoProcedure_t) (pge_ProductionDesc);
138 struct pge_DoProcedure_p { pge_DoProcedure_t proc; };
139
140 struct pge__T1_r {
141 pge_ProductionDesc definition;
142 NameKey_Name name;
143 unsigned int line;
144 };
145
146 struct pge__T2_r {
147 pge_ProductionDesc next;
148 pge_StatementDesc statement;
149 pge_SetDesc first;
150 bool firstsolved;
151 pge_FollowDesc followinfo;
152 unsigned int line;
153 NameKey_Name description;
154 };
155
156 struct pge__T3_r {
157 pge_IdentDesc ident;
158 pge_ExpressionDesc expr;
159 pge_FollowDesc followinfo;
160 unsigned int line;
161 };
162
163 struct pge__T4_r {
164 pge_TermDesc term;
165 pge_FollowDesc followinfo;
166 unsigned int line;
167 };
168
169 struct pge__T5_r {
170 pge_FollowDesc followinfo;
171 pge_FactorDesc next;
172 unsigned int line;
173 pge_FactorDesc pushed;
174 pge_FactorType type; /* case tag */
175 union {
176 pge_IdentDesc ident;
177 NameKey_Name string;
178 pge_ExpressionDesc expr;
179 pge_CodeDesc code;
180 };
181 };
182
183 struct pge__T6_r {
184 bool calcfollow;
185 pge_SetDesc follow;
186 pge_TraverseResult reachend;
187 pge_TraverseResult epsilon;
188 unsigned int line;
189 };
190
191 struct pge__T7_r {
192 pge_SetDesc next;
193 pge_ElementType type; /* case tag */
194 union {
195 pge_IdentDesc ident;
196 NameKey_Name string;
197 };
198 };
199
200 struct pge__T8_r {
201 pge_CodeHunk code;
202 unsigned int indent;
203 unsigned int line;
204 };
205
206 struct pge__T10_a { char array[MaxCodeHunkLength+1]; };
207 struct pge__T11_a { char array[MaxFileName+1]; };
208 struct pge__T9_r {
209 pge__T10 codetext;
210 pge_CodeHunk next;
211 };
212
213 static unsigned int LastLineNo;
214 static bool Finished;
215 static bool SuppressFileLineTag;
216 static bool KeywordFormatting;
217 static bool PrettyPrint;
218 static bool EmitCode;
219 static bool Texinfo;
220 static bool Sphinx;
221 static bool FreeDocLicense;
222 static bool Debugging;
223 static bool WasNoError;
224 static unsigned int LinePrologue;
225 static unsigned int LineEpilogue;
226 static unsigned int LineDeclaration;
227 static pge_CodeHunk CodePrologue;
228 static pge_CodeHunk CodeEpilogue;
229 static pge_CodeHunk CodeDeclaration;
230 static pge_ProductionDesc CurrentProduction;
231 static pge_ProductionDesc TailProduction;
232 static pge_ProductionDesc HeadProduction;
233 static pge_ExpressionDesc CurrentExpression;
234 static pge_TermDesc CurrentTerm;
235 static pge_FactorDesc CurrentFactor;
236 static pge_IdentDesc CurrentIdent;
237 static pge_StatementDesc CurrentStatement;
238 static pge_SetDesc CurrentSetDesc;
239 static SymbolKey_SymbolTree ReverseValues;
240 static SymbolKey_SymbolTree Values;
241 static SymbolKey_SymbolTree ReverseAliases;
242 static SymbolKey_SymbolTree Aliases;
243 static NameKey_Name ModuleName;
244 static NameKey_Name LastLiteral;
245 static NameKey_Name LastIdent;
246 static NameKey_Name SymIsProc;
247 static NameKey_Name TokenTypeProc;
248 static NameKey_Name ErrorProcArray;
249 static NameKey_Name ErrorProcString;
250 static pge__T11 ArgName;
251 static pge__T11 FileName;
252 static bool OnLineStart;
253 static bool BeginningOfLine;
254 static unsigned int Indent;
255 static bool EmittedVar;
256 static bool ErrorRecovery;
257 static unsigned int LargestValue;
258 static bool InitialElement;
259 static unsigned int ParametersUsed;
260
261 /*
262 DescribeStop - issues a message explaining what tokens were expected
263 */
264
265 static DynamicStrings_String DescribeStop (pge_SetOfStop stopset);
266
267 /*
268 DescribeError - issues a message explaining what tokens were expected
269 */
270
271 static void DescribeError (void);
272
273 /*
274 AddEntry - adds an entry into, t, containing [def:value].
275 */
276
277 static void AddEntry (SymbolKey_SymbolTree *t, NameKey_Name def, NameKey_Name value);
278
279 /*
280 Format1 - converts string, src, into, dest, together with encapsulated
281 entity, n. It only formats the first %s or %d with n.
282 */
283
284 static void Format1 (const char *src_, unsigned int _src_high, unsigned int n, char *dest, unsigned int _dest_high);
285
286 /*
287 WarnError1 -
288 */
289
290 static void WarnError1 (const char *a_, unsigned int _a_high, unsigned int n);
291
292 /*
293 PrettyFollow -
294 */
295
296 static void PrettyFollow (const char *start_, unsigned int _start_high, const char *end_, unsigned int _end_high, pge_FollowDesc f);
297
298 /*
299 NewFollow - creates a new follow descriptor and returns the data structure.
300 */
301
302 static pge_FollowDesc NewFollow (void);
303
304 /*
305 AssignEpsilon - assigns the epsilon value and sets the epsilon to value,
306 providing condition is TRUE.
307 */
308
309 static void AssignEpsilon (bool condition, pge_FollowDesc f, pge_TraverseResult value);
310
311 /*
312 GetEpsilon - returns the value of epsilon
313 */
314
315 static pge_TraverseResult GetEpsilon (pge_FollowDesc f);
316
317 /*
318 AssignReachEnd - assigns the reachend value providing that, condition, is TRUE.
319 */
320
321 static void AssignReachEnd (bool condition, pge_FollowDesc f, pge_TraverseResult value);
322
323 /*
324 GetReachEnd - returns the value of reachend
325 */
326
327 static pge_TraverseResult GetReachEnd (pge_FollowDesc f);
328
329 /*
330 AssignFollow - assigns the follow set and sets the calcfollow to TRUE.
331 */
332
333 static void AssignFollow (pge_FollowDesc f, pge_SetDesc s);
334
335 /*
336 GetFollow - returns the follow set.
337 */
338
339 static pge_SetDesc GetFollow (pge_FollowDesc f);
340
341 /*
342 NewProduction - creates a new production and returns the data structure.
343 */
344
345 static pge_ProductionDesc NewProduction (void);
346
347 /*
348 NewFactor -
349 */
350
351 static pge_FactorDesc NewFactor (void);
352
353 /*
354 NewTerm - returns a new term.
355 */
356
357 static pge_TermDesc NewTerm (void);
358
359 /*
360 NewExpression - returns a new expression.
361 */
362
363 static pge_ExpressionDesc NewExpression (void);
364
365 /*
366 NewStatement - returns a new statement.
367 */
368
369 static pge_StatementDesc NewStatement (void);
370
371 /*
372 NewSetDesc - creates a new set description and returns the data structure.
373 */
374
375 static pge_SetDesc NewSetDesc (void);
376
377 /*
378 NewCodeDesc - creates a new code descriptor and initializes all fields to zero.
379 */
380
381 static pge_CodeDesc NewCodeDesc (void);
382
383 /*
384 CodeFragmentPrologue - consumes code text up to a "%" after a newline.
385 */
386
387 static void CodeFragmentPrologue (void);
388
389 /*
390 CodeFragmentEpilogue - consumes code text up to a "%" after a newline.
391 */
392
393 static void CodeFragmentEpilogue (void);
394
395 /*
396 CodeFragmentDeclaration - consumes code text up to a "%" after a newline.
397 */
398
399 static void CodeFragmentDeclaration (void);
400
401 /*
402 GetCodeFragment - collects the code fragment up until ^ %
403 */
404
405 static void GetCodeFragment (pge_CodeHunk *h);
406
407 /*
408 WriteCodeHunkList - writes the CodeHunk list in the correct order.
409 */
410
411 static void WriteCodeHunkList (pge_CodeHunk l);
412
413 /*
414 WriteIndent - writes, n, spaces.
415 */
416
417 static void WriteIndent (unsigned int n);
418
419 /*
420 CheckWrite -
421 */
422
423 static void CheckWrite (char ch, unsigned int *curpos, unsigned int left, bool *seentext);
424
425 /*
426 WriteStringIndent - writes a string but it will try and remove upto indent spaces
427 if they exist.
428 */
429
430 static void WriteStringIndent (const char *a_, unsigned int _a_high, unsigned int indent, unsigned int *curpos, unsigned int left, bool *seentext);
431
432 /*
433 WriteCodeHunkListIndent - writes the CodeHunk list in the correct order
434 but it removes up to indent spaces if they exist.
435 */
436
437 static void WriteCodeHunkListIndent (pge_CodeHunk l, unsigned int indent, unsigned int *curpos, unsigned int left, bool *seentext);
438
439 /*
440 Add - adds a character to a code hunk and creates another code hunk if necessary.
441 */
442
443 static pge_CodeHunk Add (pge_CodeHunk *p, char ch, unsigned int *i);
444
445 /*
446 ConsHunk - combine two possible code hunks.
447 */
448
449 static void ConsHunk (pge_CodeHunk *p, pge_CodeHunk q);
450
451 /*
452 GetName - returns the next symbol which is checked for a legal name.
453 */
454
455 static NameKey_Name GetName (void);
456
457 /*
458 SyntaxError - after a syntax error we skip all tokens up until we reach
459 a stop symbol.
460 */
461
462 static void SyntaxError (pge_SetOfStop stop);
463
464 /*
465 SyntaxCheck -
466 */
467
468 static void SyntaxCheck (pge_SetOfStop stop);
469
470 /*
471 Expect -
472 */
473
474 static void Expect (bnflex_TokenType t, pge_SetOfStop stop);
475
476 /*
477 Ident - error checking varient of Ident
478 */
479
480 static void Ident (pge_SetOfStop stop);
481
482 /*
483 Modula2Code - error checking varient of Modula2Code
484 */
485
486 static void Modula2Code (pge_SetOfStop stop);
487
488 /*
489 StartModName := % ModuleName := GetName() ; ignore begintok CodeFragmentPrologue % =:
490 */
491
492 static void StartModName (pge_SetOfStop stop);
493
494 /*
495 EndModName :=
496 */
497
498 static void EndModName (pge_SetOfStop stop);
499
500 /*
501 DoDeclaration := % CodeFragmentDeclaration % =:
502 */
503
504 static void DoDeclaration (pge_SetOfStop stop);
505
506 /*
507 CollectLiteral :=
508 % LastLiteral := GetCurrentToken() ;
509 AdvanceToken ; %
510
511
512 first symbols:literaltok
513
514 cannot reachend
515 */
516
517 static void CollectLiteral (pge_SetOfStop stopset);
518
519 /*
520 CollectTok :=
521 % CurrentSetDesc := NewSetDesc() ;
522 WITH CurrentSetDesc^ DO
523 type := tokel ;
524 string := GetCurrentToken() ;
525 END ;
526 IF NOT ContainsSymKey(Values, GetCurrentToken())
527 THEN
528 AddEntry(Values, GetCurrentToken(), LargestValue) ;
529 AddEntry(ReverseValues, Name(LargestValue), GetCurrentToken()) ;
530 AddEntry(Aliases, GetCurrentToken(), GetCurrentToken()) ;
531 AddEntry(ReverseAliases, GetCurrentToken(), GetCurrentToken()) ;
532 INC(LargestValue)
533 END ;
534 AdvanceToken() ; %
535
536
537 first symbols:identtok
538
539 cannot reachend
540 */
541
542 static void CollectTok (pge_SetOfStop stopset);
543
544 /*
545 DefineToken :=
546 % AddEntry(Aliases, LastLiteral, GetCurrentToken()) ;
547 AddEntry(ReverseAliases, GetCurrentToken(), LastLiteral) ;
548 AddEntry(Values, GetCurrentToken(), LargestValue) ;
549 AddEntry(ReverseValues, Name(LargestValue), GetCurrentToken()) ;
550 INC(LargestValue) ;
551 AdvanceToken ; %
552
553
554 first symbols:identtok
555
556 cannot reachend
557 */
558
559 static void DefineToken (pge_SetOfStop stopset);
560
561 /*
562 Rules := '%' 'rules' { Defs } ExtBNF
563
564 first symbols:codetok
565
566 cannot reachend
567 */
568
569 static void Rules (pge_SetOfStop stopset);
570
571 /*
572 Special := Ident
573 % VAR p: ProductionDesc ; %
574
575 % p := NewProduction() ;
576 p^.statement := NewStatement() ;
577 p^.statement^.followinfo^.calcfollow := TRUE ;
578 p^.statement^.followinfo^.epsilon := false ;
579 p^.statement^.followinfo^.reachend := false ;
580 p^.statement^.ident := CurrentIdent ;
581 p^.statement^.expr := NIL ;
582 p^.firstsolved := TRUE ;
583 p^.followinfo^.calcfollow := TRUE ;
584 p^.followinfo^.epsilon := false ;
585 p^.followinfo^.reachend := false %
586 First Follow [ 'epsilon'
587 % p^.statement^.followinfo^.epsilon := true ; these are not used - but they are displayed when debugging
588 p^.statement^.followinfo^.reachend := true ;
589 p^.followinfo^.epsilon := true ;
590 p^.followinfo^.reachend := true
591 %
592 ] [ Literal
593 % p^.description := LastLiteral %
594 ]
595
596 first symbols:identtok
597
598 cannot reachend
599 */
600
601 static void Special (pge_SetOfStop stopset);
602
603 /*
604 Factor := '%' Modula2Code '%' |
605 Ident
606 % WITH CurrentFactor^ DO
607 type := id ;
608 ident := CurrentIdent
609 END ; %
610 | Literal
611 % WITH CurrentFactor^ DO
612 type := lit ;
613 string := LastLiteral ;
614 IF GetSymKey(Aliases, LastLiteral)=NulName
615 THEN
616 WarnError1('no token defined for literal %s', LastLiteral)
617 END
618 END ; %
619 | '{'
620 % WITH CurrentFactor^ DO
621 type := mult ;
622 expr := NewExpression() ;
623 CurrentExpression := expr ;
624 END ; %
625 Expression '}' | '['
626 % WITH CurrentFactor^ DO
627 type := opt ;
628 expr := NewExpression() ;
629 CurrentExpression := expr ;
630 END ; %
631 Expression ']' | '('
632 % WITH CurrentFactor^ DO
633 type := sub ;
634 expr := NewExpression() ;
635 CurrentExpression := expr ;
636 END ; %
637 Expression ')'
638
639 first symbols:dquotetok, squotetok, lparatok, lsparatok, lcparatok, identtok, codetok
640
641 cannot reachend
642 */
643
644 static void Factor (pge_SetOfStop stopset);
645
646 /*
647 Statement :=
648 % VAR i: IdentDesc ; %
649 Ident
650 % VAR p: ProductionDesc ; %
651
652 % p := FindDefinition(CurrentIdent^.name) ;
653 IF p=NIL
654 THEN
655 p := NewProduction()
656 ELSE
657 IF NOT ((p^.statement=NIL) OR (p^.statement^.expr=NIL))
658 THEN
659 WarnError1('already declared rule %s', CurrentIdent^.name)
660 END
661 END ;
662 i := CurrentIdent ; %
663 ':='
664 % VAR e: ExpressionDesc ; %
665
666 % e := NewExpression() ;
667 CurrentExpression := e ; %
668
669 % VAR s: StatementDesc ; %
670
671 % s := NewStatement() ;
672 WITH s^ DO
673 ident := i ;
674 expr := e
675 END ; %
676 Expression
677 % p^.statement := s ; %
678 '=:'
679
680 first symbols:identtok
681
682 cannot reachend
683 */
684
685 static void Statement (pge_SetOfStop stopset);
686
687 /*
688 Defs := 'special' Special | 'token' Token |
689 'error' ErrorProcedures |
690 'tokenfunc' TokenProcedure |
691 'symfunc' SymProcedure
692
693 first symbols:symfunctok, tfunctok, errortok, tokentok, specialtok
694
695 cannot reachend
696 */
697
698 static void Defs (pge_SetOfStop stopset);
699
700 /*
701 ExtBNF := 'BNF' { Production } 'FNB'
702
703 first symbols:BNFtok
704
705 cannot reachend
706 */
707
708 static void ExtBNF (pge_SetOfStop stopset);
709
710 /*
711 Main := Header Decls Footer Rules
712
713 first symbols:codetok
714
715 cannot reachend
716 */
717
718 static void Main (pge_SetOfStop stopset);
719
720 /*
721 Header := '%' 'module' StartModName
722
723 first symbols:codetok
724
725 cannot reachend
726 */
727
728 static void Header (pge_SetOfStop stopset);
729
730 /*
731 Decls := '%' 'declaration' DoDeclaration
732
733 first symbols:codetok
734
735 cannot reachend
736 */
737
738 static void Decls (pge_SetOfStop stopset);
739
740 /*
741 Footer := '%' 'module' EndModName
742
743 first symbols:codetok
744
745 cannot reachend
746 */
747
748 static void Footer (pge_SetOfStop stopset);
749
750 /*
751 First := 'first' '{' { LitOrTokenOrIdent
752 % WITH CurrentSetDesc^ DO
753 next := TailProduction^.first ;
754 END ;
755 TailProduction^.first := CurrentSetDesc
756 %
757 } '}'
758
759 first symbols:firsttok
760
761 cannot reachend
762 */
763
764 static void First (pge_SetOfStop stopset);
765
766 /*
767 Follow := 'follow' '{' { LitOrTokenOrIdent
768 % WITH CurrentSetDesc^ DO
769 next := TailProduction^.followinfo^.follow ;
770 END ;
771 TailProduction^.followinfo^.follow := CurrentSetDesc
772 %
773 } '}'
774
775 first symbols:followtok
776
777 cannot reachend
778 */
779
780 static void Follow (pge_SetOfStop stopset);
781
782 /*
783 LitOrTokenOrIdent := Literal
784 % CurrentSetDesc := NewSetDesc() ;
785 WITH CurrentSetDesc^ DO
786 type := litel ;
787 string := LastLiteral ;
788 END ;
789 %
790 | '<' CollectTok '>' |
791 Ident
792 % CurrentSetDesc := NewSetDesc() ;
793 WITH CurrentSetDesc^ DO
794 type := idel ;
795 ident := CurrentIdent ;
796 END ;
797 %
798
799
800 first symbols:dquotetok, squotetok, identtok, lesstok
801
802 cannot reachend
803 */
804
805 static void LitOrTokenOrIdent (pge_SetOfStop stopset);
806
807 /*
808 Literal := '"' CollectLiteral '"' |
809 "'" CollectLiteral "'"
810
811 first symbols:squotetok, dquotetok
812
813 cannot reachend
814 */
815
816 static void Literal (pge_SetOfStop stopset);
817
818 /*
819 Token := Literal DefineToken
820
821 first symbols:dquotetok, squotetok
822
823 cannot reachend
824 */
825
826 static void Token (pge_SetOfStop stopset);
827
828 /*
829 ErrorProcedures := Literal
830 % ErrorProcArray := LastLiteral %
831 Literal
832 % ErrorProcString := LastLiteral %
833
834
835 first symbols:dquotetok, squotetok
836
837 cannot reachend
838 */
839
840 static void ErrorProcedures (pge_SetOfStop stopset);
841
842 /*
843 TokenProcedure := Literal
844 % TokenTypeProc := LastLiteral %
845
846
847 first symbols:dquotetok, squotetok
848
849 cannot reachend
850 */
851
852 static void TokenProcedure (pge_SetOfStop stopset);
853
854 /*
855 SymProcedure := Literal
856 % SymIsProc := LastLiteral %
857
858
859 first symbols:dquotetok, squotetok
860
861 cannot reachend
862 */
863
864 static void SymProcedure (pge_SetOfStop stopset);
865
866 /*
867 Production := Statement
868
869 first symbols:identtok
870
871 cannot reachend
872 */
873
874 static void Production (pge_SetOfStop stopset);
875
876 /*
877 Expression :=
878 % VAR t1, t2: TermDesc ;
879 e : ExpressionDesc ; %
880
881 % e := CurrentExpression ;
882 t1 := NewTerm() ;
883 CurrentTerm := t1 ; %
884 Term
885 % e^.term := t1 ; %
886 { '|'
887 % t2 := NewTerm() ;
888 CurrentTerm := t2 %
889 Term
890 % t1^.next := t2 ;
891 t1 := t2 %
892 }
893
894 first symbols:dquotetok, squotetok, lparatok, lsparatok, lcparatok, identtok, codetok
895
896 cannot reachend
897 */
898
899 static void Expression (pge_SetOfStop stopset);
900
901 /*
902 Term :=
903 % VAR t1: TermDesc ; f1, f2: FactorDesc ; %
904
905 % CurrentFactor := NewFactor() ;
906 f1 := CurrentFactor ;
907 t1 := CurrentTerm ; %
908 Factor
909 % t1^.factor := f1 ;
910 f2 := NewFactor() ;
911 CurrentFactor := f2 %
912 { Factor
913 % f1^.next := f2 ;
914 f1 := f2 ;
915 f2 := NewFactor() ;
916 CurrentFactor := f2 ; %
917 }
918
919 first symbols:squotetok, dquotetok, codetok, identtok, lcparatok, lsparatok, lparatok
920
921 cannot reachend
922 */
923
924 static void Term (pge_SetOfStop stopset);
925
926 /*
927 GetDefinitionName - returns the name of the rule inside, p.
928 */
929
930 static NameKey_Name GetDefinitionName (pge_ProductionDesc p);
931
932 /*
933 FindDefinition - searches and returns the rule which defines, n.
934 */
935
936 static pge_ProductionDesc FindDefinition (NameKey_Name n);
937
938 /*
939 BackPatchIdent - found an ident, i, we must look for the corresponding rule and
940 set the definition accordingly.
941 */
942
943 static void BackPatchIdent (pge_IdentDesc i);
944
945 /*
946 BackPatchFactor - runs through the factor looking for an ident
947 */
948
949 static void BackPatchFactor (pge_FactorDesc f);
950
951 /*
952 BackPatchTerm - runs through all terms to find idents.
953 */
954
955 static void BackPatchTerm (pge_TermDesc t);
956
957 /*
958 BackPatchExpression - runs through the term to find any idents.
959 */
960
961 static void BackPatchExpression (pge_ExpressionDesc e);
962
963 /*
964 BackPatchSet -
965 */
966
967 static void BackPatchSet (pge_SetDesc s);
968
969 /*
970 BackPatchIdentToDefinitions - search through all the rules and add a link from any ident
971 to the definition.
972 */
973
974 static void BackPatchIdentToDefinitions (pge_ProductionDesc d);
975
976 /*
977 CalculateFirstAndFollow -
978 */
979
980 static void CalculateFirstAndFollow (pge_ProductionDesc p);
981
982 /*
983 ForeachRuleDo -
984 */
985
986 static void ForeachRuleDo (pge_DoProcedure p);
987
988 /*
989 WhileNotCompleteDo -
990 */
991
992 static void WhileNotCompleteDo (pge_DoProcedure p);
993
994 /*
995 NewLine - generate a newline and indent.
996 */
997
998 static void NewLine (unsigned int Left);
999
1000 /*
1001 CheckNewLine -
1002 */
1003
1004 static void CheckNewLine (unsigned int Left);
1005
1006 /*
1007 IndentString - writes out a string with a preceeding indent.
1008 */
1009
1010 static void IndentString (const char *a_, unsigned int _a_high);
1011
1012 /*
1013 KeyWord - writes out a keywork with optional formatting directives.
1014 */
1015
1016 static void KeyWord (NameKey_Name n);
1017
1018 /*
1019 PrettyPara -
1020 */
1021
1022 static void PrettyPara (const char *c1_, unsigned int _c1_high, const char *c2_, unsigned int _c2_high, pge_ExpressionDesc e, unsigned int Left);
1023
1024 /*
1025 WriteKeyTexinfo -
1026 */
1027
1028 static void WriteKeyTexinfo (NameKey_Name s);
1029
1030 /*
1031 PrettyCommentFactor -
1032 */
1033
1034 static void PrettyCommentFactor (pge_FactorDesc f, unsigned int Left);
1035
1036 /*
1037 PeepTerm - returns the length of characters in term.
1038 */
1039
1040 static unsigned int PeepTerm (pge_TermDesc t);
1041
1042 /*
1043 PeepExpression - returns the length of the expression.
1044 */
1045
1046 static unsigned int PeepExpression (pge_ExpressionDesc e);
1047
1048 /*
1049 PeepFactor - returns the length of character in the factor
1050 */
1051
1052 static unsigned int PeepFactor (pge_FactorDesc f);
1053
1054 /*
1055 PrettyCommentTerm -
1056 */
1057
1058 static void PrettyCommentTerm (pge_TermDesc t, unsigned int Left);
1059
1060 /*
1061 PrettyCommentExpression -
1062 */
1063
1064 static void PrettyCommentExpression (pge_ExpressionDesc e, unsigned int Left);
1065
1066 /*
1067 PrettyCommentStatement -
1068 */
1069
1070 static void PrettyCommentStatement (pge_StatementDesc s, unsigned int Left);
1071
1072 /*
1073 PrettyCommentProduction - generates the comment for rule, p.
1074 */
1075
1076 static void PrettyCommentProduction (pge_ProductionDesc p);
1077
1078 /*
1079 PrettyPrintProduction - pretty prints the ebnf rule, p.
1080 */
1081
1082 static void PrettyPrintProduction (pge_ProductionDesc p);
1083
1084 /*
1085 EmitFileLineTag - emits a line and file tag using the C preprocessor syntax.
1086 */
1087
1088 static void EmitFileLineTag (unsigned int line);
1089
1090 /*
1091 EmitRule - generates a comment and code for rule, p.
1092 */
1093
1094 static void EmitRule (pge_ProductionDesc p);
1095
1096 /*
1097 CodeCondition -
1098 */
1099
1100 static void CodeCondition (pge_m2condition m);
1101
1102 /*
1103 CodeThenDo - codes a "THEN" or "DO" depending upon, m.
1104 */
1105
1106 static void CodeThenDo (pge_m2condition m);
1107
1108 /*
1109 CodeElseEnd - builds an ELSE END statement using string, end.
1110 */
1111
1112 static void CodeElseEnd (const char *end_, unsigned int _end_high, bool consumed, pge_FactorDesc f, bool inopt);
1113
1114 /*
1115 CodeEnd - codes a "END" depending upon, m.
1116 */
1117
1118 static void CodeEnd (pge_m2condition m, pge_TermDesc t, bool consumed, pge_FactorDesc f, bool inopt);
1119
1120 /*
1121 EmitNonVarCode - writes out, code, providing it is not a variable declaration.
1122 */
1123
1124 static void EmitNonVarCode (pge_CodeDesc code, unsigned int curpos, unsigned int left);
1125
1126 /*
1127 ChainOn -
1128 */
1129
1130 static pge_FactorDesc ChainOn (pge_FactorDesc codeStack, pge_FactorDesc f);
1131
1132 /*
1133 FlushCode -
1134 */
1135
1136 static void FlushCode (pge_FactorDesc *codeStack);
1137
1138 /*
1139 CodeFactor -
1140 */
1141
1142 static void CodeFactor (pge_FactorDesc f, pge_TermDesc t, pge_m2condition l, pge_m2condition n, bool inopt, bool inwhile, bool consumed, pge_FactorDesc codeStack);
1143
1144 /*
1145 CodeTerm -
1146 */
1147
1148 static void CodeTerm (pge_TermDesc t, pge_m2condition m, bool inopt, bool inwhile, bool consumed, pge_FactorDesc codeStack);
1149
1150 /*
1151 CodeExpression -
1152 */
1153
1154 static void CodeExpression (pge_ExpressionDesc e, pge_m2condition m, bool inopt, bool inwhile, bool consumed, pge_FactorDesc codeStack);
1155
1156 /*
1157 CodeStatement -
1158 */
1159
1160 static void CodeStatement (pge_StatementDesc s, pge_m2condition m);
1161
1162 /*
1163 CodeProduction - only encode grammer rules which are not special.
1164 */
1165
1166 static void CodeProduction (pge_ProductionDesc p);
1167
1168 /*
1169 RecoverCondition -
1170 */
1171
1172 static void RecoverCondition (pge_m2condition m);
1173
1174 /*
1175 ConditionIndent - returns the number of spaces indentation created via, m.
1176 */
1177
1178 static unsigned int ConditionIndent (pge_m2condition m);
1179
1180 /*
1181 WriteGetTokenType - writes out the method of determining the token type.
1182 */
1183
1184 static void WriteGetTokenType (void);
1185
1186 /*
1187 NumberOfElements - returns the number of elements in set, to, which lie between low..high
1188 */
1189
1190 static unsigned int NumberOfElements (pge_SetDesc to, unsigned int low, unsigned int high);
1191
1192 /*
1193 WriteElement - writes the literal name for element, e.
1194 */
1195
1196 static void WriteElement (unsigned int e);
1197
1198 /*
1199 EmitIsInSet - writes out the equivalent of GetTokenType() IN { toset }
1200 */
1201
1202 static void EmitIsInSet (pge_SetDesc to, NameKey_Name low, NameKey_Name high);
1203
1204 /*
1205 EmitIsInSubSet - writes out a test to see whether GetTokenype() is in { subset }
1206 */
1207
1208 static void EmitIsInSubSet (pge_SetDesc to, unsigned int low, unsigned int high);
1209
1210 /*
1211 EmitIsInFirst -
1212 */
1213
1214 static void EmitIsInFirst (pge_SetDesc to, pge_m2condition m);
1215 static void FlushRecoverCode (pge_FactorDesc *codeStack);
1216
1217 /*
1218 RecoverFactor -
1219 */
1220
1221 static void RecoverFactor (pge_FactorDesc f, pge_m2condition m, pge_FactorDesc codeStack);
1222
1223 /*
1224 OptExpSeen - returns TRUE if we can see an optional expression in the factor.
1225 This is not the same as epsilon. Example { '+' } matches epsilon as
1226 well as { '+' | '-' } but OptExpSeen returns TRUE in the second case
1227 and FALSE in the first.
1228 */
1229
1230 static bool OptExpSeen (pge_FactorDesc f);
1231
1232 /*
1233 RecoverTerm -
1234 */
1235
1236 static void RecoverTerm (pge_TermDesc t, pge_m2condition new_, pge_m2condition old);
1237
1238 /*
1239 RecoverExpression -
1240 */
1241
1242 static void RecoverExpression (pge_ExpressionDesc e, pge_m2condition new_, pge_m2condition old);
1243
1244 /*
1245 RecoverStatement -
1246 */
1247
1248 static void RecoverStatement (pge_StatementDesc s, pge_m2condition m);
1249
1250 /*
1251 EmitFirstFactor - generate a list of all first tokens between the range: low..high.
1252 */
1253
1254 static void EmitFirstFactor (pge_FactorDesc f, unsigned int low, unsigned int high);
1255
1256 /*
1257 EmitUsed -
1258 */
1259
1260 static void EmitUsed (unsigned int wordno);
1261
1262 /*
1263 EmitStopParameters - generate the stop set.
1264 */
1265
1266 static void EmitStopParameters (bool FormalParameters);
1267
1268 /*
1269 IsBetween - returns TRUE if the value of the token, string, is
1270 in the range: low..high
1271 */
1272
1273 static bool IsBetween (NameKey_Name string, unsigned int low, unsigned int high);
1274
1275 /*
1276 IsEmptySet - returns TRUE if no elements exist in set, to, with values, low..high.
1277 */
1278
1279 static bool IsEmptySet (pge_SetDesc to, unsigned int low, unsigned int high);
1280
1281 /*
1282 EmitSet - emits the tokens in the set, to, which have values low..high
1283 */
1284
1285 static void EmitSet (pge_SetDesc to, unsigned int low, unsigned int high);
1286
1287 /*
1288 EmitSetName - emits the tokens in the set, to, which have values low..high, using
1289 their names.
1290 */
1291
1292 static void EmitSetName (pge_SetDesc to, unsigned int low, unsigned int high);
1293
1294 /*
1295 EmitStopParametersAndSet - generates the stop parameters together with a set
1296 inclusion of all the symbols in set, to.
1297 */
1298
1299 static void EmitStopParametersAndSet (pge_SetDesc to);
1300
1301 /*
1302 EmitSetAsParameters - generates the first symbols as parameters to a set function.
1303 */
1304
1305 static void EmitSetAsParameters (pge_SetDesc to);
1306
1307 /*
1308 EmitStopParametersAndFollow - generates the stop parameters together with a set
1309 inclusion of all the follow symbols for subsequent
1310 sentances.
1311 */
1312
1313 static void EmitStopParametersAndFollow (pge_FactorDesc f, pge_m2condition m);
1314
1315 /*
1316 EmitFirstAsParameters -
1317 */
1318
1319 static void EmitFirstAsParameters (pge_FactorDesc f);
1320
1321 /*
1322 RecoverProduction - only encode grammer rules which are not special.
1323 Generate error recovery code.
1324 */
1325
1326 static void RecoverProduction (pge_ProductionDesc p);
1327
1328 /*
1329 IsWhite - returns TRUE if, ch, is a space or a tab.
1330 */
1331
1332 static bool IsWhite (char ch);
1333
1334 /*
1335 FindStr - returns TRUE if, str, was seen inside the code hunk
1336 */
1337
1338 static bool FindStr (pge_CodeHunk *code, unsigned int *i, const char *str_, unsigned int _str_high);
1339
1340 /*
1341 WriteUpto -
1342 */
1343
1344 static void WriteUpto (pge_CodeHunk code, pge_CodeHunk upto, unsigned int limit);
1345
1346 /*
1347 CheckForVar - checks for any local variables which need to be emitted during
1348 this production.
1349 */
1350
1351 static void CheckForVar (pge_CodeHunk code);
1352
1353 /*
1354 VarFactor -
1355 */
1356
1357 static void VarFactor (pge_FactorDesc f);
1358
1359 /*
1360 VarTerm -
1361 */
1362
1363 static void VarTerm (pge_TermDesc t);
1364
1365 /*
1366 VarExpression -
1367 */
1368
1369 static void VarExpression (pge_ExpressionDesc e);
1370
1371 /*
1372 VarStatement -
1373 */
1374
1375 static void VarStatement (pge_StatementDesc s);
1376
1377 /*
1378 VarProduction - writes out all variable declarations.
1379 */
1380
1381 static void VarProduction (pge_ProductionDesc p);
1382
1383 /*
1384 In - returns TRUE if token, s, is already in the set, to.
1385 */
1386
1387 static bool In (pge_SetDesc to, NameKey_Name s);
1388
1389 /*
1390 IntersectionIsNil - given two set lists, s1, s2, return TRUE if the
1391 s1 * s2 = {}
1392 */
1393
1394 static bool IntersectionIsNil (pge_SetDesc s1, pge_SetDesc s2);
1395
1396 /*
1397 AddSet - adds a first symbol to a production.
1398 */
1399
1400 static void AddSet (pge_SetDesc *to, NameKey_Name s);
1401
1402 /*
1403 OrSet -
1404 */
1405
1406 static void OrSet (pge_SetDesc *to, pge_SetDesc from);
1407
1408 /*
1409 CalcFirstFactor -
1410 */
1411
1412 static void CalcFirstFactor (pge_FactorDesc f, pge_ProductionDesc from, pge_SetDesc *to);
1413
1414 /*
1415 CalcFirstTerm -
1416 */
1417
1418 static void CalcFirstTerm (pge_TermDesc t, pge_ProductionDesc from, pge_SetDesc *to);
1419
1420 /*
1421 CalcFirstExpression -
1422 */
1423
1424 static void CalcFirstExpression (pge_ExpressionDesc e, pge_ProductionDesc from, pge_SetDesc *to);
1425
1426 /*
1427 CalcFirstStatement -
1428 */
1429
1430 static void CalcFirstStatement (pge_StatementDesc s, pge_ProductionDesc from, pge_SetDesc *to);
1431
1432 /*
1433 CalcFirstProduction - calculates all of the first symbols for the grammer
1434 */
1435
1436 static void CalcFirstProduction (pge_ProductionDesc p, pge_ProductionDesc from, pge_SetDesc *to);
1437 static void WorkOutFollowFactor (pge_FactorDesc f, pge_SetDesc *followset, pge_SetDesc after);
1438
1439 /*
1440 WorkOutFollowTerm -
1441 */
1442
1443 static void WorkOutFollowTerm (pge_TermDesc t, pge_SetDesc *followset, pge_SetDesc after);
1444
1445 /*
1446 WorkOutFollowExpression -
1447 */
1448
1449 static void WorkOutFollowExpression (pge_ExpressionDesc e, pge_SetDesc *followset, pge_SetDesc after);
1450
1451 /*
1452 CollectFollow - collects the follow set from, f, into, to.
1453 */
1454
1455 static void CollectFollow (pge_SetDesc *to, pge_FollowDesc f);
1456
1457 /*
1458 CalcFollowFactor -
1459 */
1460
1461 static void CalcFollowFactor (pge_FactorDesc f, pge_SetDesc after);
1462
1463 /*
1464 CalcFollowTerm -
1465 */
1466
1467 static void CalcFollowTerm (pge_TermDesc t, pge_SetDesc after);
1468
1469 /*
1470 CalcFollowExpression -
1471 */
1472
1473 static void CalcFollowExpression (pge_ExpressionDesc e, pge_SetDesc after);
1474
1475 /*
1476 CalcFollowStatement - given a bnf statement generate the follow set.
1477 */
1478
1479 static void CalcFollowStatement (pge_StatementDesc s);
1480
1481 /*
1482 CalcFollowProduction -
1483 */
1484
1485 static void CalcFollowProduction (pge_ProductionDesc p);
1486
1487 /*
1488 CalcEpsilonFactor -
1489 */
1490
1491 static void CalcEpsilonFactor (pge_FactorDesc f);
1492
1493 /*
1494 CalcEpsilonTerm -
1495 */
1496
1497 static void CalcEpsilonTerm (pge_TermDesc t);
1498
1499 /*
1500 CalcEpsilonExpression -
1501 */
1502
1503 static void CalcEpsilonExpression (pge_ExpressionDesc e);
1504
1505 /*
1506 CalcEpsilonStatement - given a bnf statement generate the follow set.
1507 */
1508
1509 static void CalcEpsilonStatement (pge_StatementDesc s);
1510
1511 /*
1512 CalcEpsilonProduction -
1513 */
1514
1515 static void CalcEpsilonProduction (pge_ProductionDesc p);
1516
1517 /*
1518 CalcReachEndFactor -
1519 */
1520
1521 static pge_TraverseResult CalcReachEndFactor (pge_FactorDesc f);
1522
1523 /*
1524 CalcReachEndTerm -
1525 */
1526
1527 static pge_TraverseResult CalcReachEndTerm (pge_TermDesc t);
1528
1529 /*
1530 CalcReachEndExpression -
1531 */
1532
1533 static void CalcReachEndExpression (pge_ExpressionDesc e);
1534
1535 /*
1536 CalcReachEndStatement -
1537 */
1538
1539 static void CalcReachEndStatement (pge_StatementDesc s);
1540
1541 /*
1542 CalcReachEndStatement -
1543 */
1544
1545 static void stop (void);
1546
1547 /*
1548 CalcReachEndProduction -
1549 */
1550
1551 static void CalcReachEndProduction (pge_ProductionDesc p);
1552
1553 /*
1554 EmptyFactor -
1555 */
1556
1557 static bool EmptyFactor (pge_FactorDesc f);
1558
1559 /*
1560 EmptyTerm - returns TRUE if the term maybe empty.
1561 */
1562
1563 static bool EmptyTerm (pge_TermDesc t);
1564
1565 /*
1566 EmptyExpression -
1567 */
1568
1569 static bool EmptyExpression (pge_ExpressionDesc e);
1570
1571 /*
1572 EmptyStatement - returns TRUE if statement, s, is empty.
1573 */
1574
1575 static bool EmptyStatement (pge_StatementDesc s);
1576
1577 /*
1578 EmptyProduction - returns if production, p, maybe empty.
1579 */
1580
1581 static bool EmptyProduction (pge_ProductionDesc p);
1582
1583 /*
1584 EmitFDLNotice -
1585 */
1586
1587 static void EmitFDLNotice (void);
1588
1589 /*
1590 EmitRules - generates the BNF rules.
1591 */
1592
1593 static void EmitRules (void);
1594
1595 /*
1596 DescribeElement -
1597 */
1598
1599 static void DescribeElement (unsigned int name);
1600
1601 /*
1602 EmitInTestStop - construct a test for stop element, name.
1603 */
1604
1605 static void EmitInTestStop (NameKey_Name name);
1606
1607 /*
1608 DescribeStopElement -
1609 */
1610
1611 static void DescribeStopElement (unsigned int name);
1612
1613 /*
1614 EmitDescribeStop -
1615 */
1616
1617 static void EmitDescribeStop (void);
1618
1619 /*
1620 EmitDescribeError -
1621 */
1622
1623 static void EmitDescribeError (void);
1624
1625 /*
1626 EmitSetTypes - write out the set types used during error recovery
1627 */
1628
1629 static void EmitSetTypes (void);
1630
1631 /*
1632 EmitSupport - generates the support routines.
1633 */
1634
1635 static void EmitSupport (void);
1636
1637 /*
1638 DisposeSetDesc - dispose of the set list, s.
1639 */
1640
1641 static void DisposeSetDesc (pge_SetDesc *s);
1642
1643 /*
1644 OptionalFactor -
1645 */
1646
1647 static bool OptionalFactor (pge_FactorDesc f);
1648
1649 /*
1650 OptionalTerm - returns TRUE if the term maybe empty.
1651 */
1652
1653 static bool OptionalTerm (pge_TermDesc t);
1654
1655 /*
1656 OptionalExpression -
1657 */
1658
1659 static bool OptionalExpression (pge_ExpressionDesc e);
1660
1661 /*
1662 OptionalStatement - returns FALSE if statement, s, does not have a optional ambiguity.
1663 */
1664
1665 static bool OptionalStatement (pge_StatementDesc s);
1666
1667 /*
1668 OptionalProduction -
1669 */
1670
1671 static bool OptionalProduction (pge_ProductionDesc p);
1672
1673 /*
1674 CheckFirstFollow -
1675 */
1676
1677 static bool CheckFirstFollow (pge_FactorDesc f, pge_FactorDesc after);
1678
1679 /*
1680 ConstrainedEmptyFactor -
1681 */
1682
1683 static bool ConstrainedEmptyFactor (pge_FactorDesc f);
1684
1685 /*
1686 ConstrainedEmptyTerm - returns TRUE if the term maybe empty.
1687 */
1688
1689 static bool ConstrainedEmptyTerm (pge_TermDesc t);
1690
1691 /*
1692 ConstrainedEmptyExpression -
1693 */
1694
1695 static bool ConstrainedEmptyExpression (pge_ExpressionDesc e);
1696
1697 /*
1698 ConstrainedEmptyStatement - returns FALSE if statement, s, does not have a optional ambiguity.
1699 */
1700
1701 static bool ConstrainedEmptyStatement (pge_StatementDesc s);
1702
1703 /*
1704 ConstrainedEmptyProduction - returns TRUE if a problem exists with, p.
1705 */
1706
1707 static bool ConstrainedEmptyProduction (pge_ProductionDesc p);
1708
1709 /*
1710 TestForLALR1 -
1711 */
1712
1713 static void TestForLALR1 (pge_ProductionDesc p);
1714
1715 /*
1716 DoEpsilon - runs the epsilon interrelated rules
1717 */
1718
1719 static void DoEpsilon (pge_ProductionDesc p);
1720
1721 /*
1722 CheckComplete - checks that production, p, is complete.
1723 */
1724
1725 static void CheckComplete (pge_ProductionDesc p);
1726
1727 /*
1728 PostProcessRules - backpatch the ident to rule definitions and emit comments and code.
1729 */
1730
1731 static void PostProcessRules (void);
1732
1733 /*
1734 DisplayHelp - display a summary help and then exit (0).
1735 */
1736
1737 static void DisplayHelp (void);
1738
1739 /*
1740 ParseArgs -
1741 */
1742
1743 static void ParseArgs (void);
1744
1745 /*
1746 Init - initialize the modules data structures
1747 */
1748
1749 static void Init (void);
1750
1751 /*
1752 DescribeStop - issues a message explaining what tokens were expected
1753 */
1754
1755 static DynamicStrings_String DescribeStop (pge_SetOfStop stopset);
1756
1757 /*
1758 DescribeError - issues a message explaining what tokens were expected
1759 */
1760
1761 static void DescribeError (void);
1762
1763 /*
1764 AddEntry - adds an entry into, t, containing [def:value].
1765 */
1766
1767 static void AddEntry (SymbolKey_SymbolTree *t, NameKey_Name def, NameKey_Name value);
1768
1769 /*
1770 Format1 - converts string, src, into, dest, together with encapsulated
1771 entity, n. It only formats the first %s or %d with n.
1772 */
1773
1774 static void Format1 (const char *src_, unsigned int _src_high, unsigned int n, char *dest, unsigned int _dest_high);
1775
1776 /*
1777 WarnError1 -
1778 */
1779
1780 static void WarnError1 (const char *a_, unsigned int _a_high, unsigned int n);
1781
1782 /*
1783 PrettyFollow -
1784 */
1785
1786 static void PrettyFollow (const char *start_, unsigned int _start_high, const char *end_, unsigned int _end_high, pge_FollowDesc f);
1787
1788 /*
1789 NewFollow - creates a new follow descriptor and returns the data structure.
1790 */
1791
1792 static pge_FollowDesc NewFollow (void);
1793
1794 /*
1795 AssignEpsilon - assigns the epsilon value and sets the epsilon to value,
1796 providing condition is TRUE.
1797 */
1798
1799 static void AssignEpsilon (bool condition, pge_FollowDesc f, pge_TraverseResult value);
1800
1801 /*
1802 GetEpsilon - returns the value of epsilon
1803 */
1804
1805 static pge_TraverseResult GetEpsilon (pge_FollowDesc f);
1806
1807 /*
1808 AssignReachEnd - assigns the reachend value providing that, condition, is TRUE.
1809 */
1810
1811 static void AssignReachEnd (bool condition, pge_FollowDesc f, pge_TraverseResult value);
1812
1813 /*
1814 GetReachEnd - returns the value of reachend
1815 */
1816
1817 static pge_TraverseResult GetReachEnd (pge_FollowDesc f);
1818
1819 /*
1820 AssignFollow - assigns the follow set and sets the calcfollow to TRUE.
1821 */
1822
1823 static void AssignFollow (pge_FollowDesc f, pge_SetDesc s);
1824
1825 /*
1826 GetFollow - returns the follow set.
1827 */
1828
1829 static pge_SetDesc GetFollow (pge_FollowDesc f);
1830
1831 /*
1832 NewProduction - creates a new production and returns the data structure.
1833 */
1834
1835 static pge_ProductionDesc NewProduction (void);
1836
1837 /*
1838 NewFactor -
1839 */
1840
1841 static pge_FactorDesc NewFactor (void);
1842
1843 /*
1844 NewTerm - returns a new term.
1845 */
1846
1847 static pge_TermDesc NewTerm (void);
1848
1849 /*
1850 NewExpression - returns a new expression.
1851 */
1852
1853 static pge_ExpressionDesc NewExpression (void);
1854
1855 /*
1856 NewStatement - returns a new statement.
1857 */
1858
1859 static pge_StatementDesc NewStatement (void);
1860
1861 /*
1862 NewSetDesc - creates a new set description and returns the data structure.
1863 */
1864
1865 static pge_SetDesc NewSetDesc (void);
1866
1867 /*
1868 NewCodeDesc - creates a new code descriptor and initializes all fields to zero.
1869 */
1870
1871 static pge_CodeDesc NewCodeDesc (void);
1872
1873 /*
1874 CodeFragmentPrologue - consumes code text up to a "%" after a newline.
1875 */
1876
1877 static void CodeFragmentPrologue (void);
1878
1879 /*
1880 CodeFragmentEpilogue - consumes code text up to a "%" after a newline.
1881 */
1882
1883 static void CodeFragmentEpilogue (void);
1884
1885 /*
1886 CodeFragmentDeclaration - consumes code text up to a "%" after a newline.
1887 */
1888
1889 static void CodeFragmentDeclaration (void);
1890
1891 /*
1892 GetCodeFragment - collects the code fragment up until ^ %
1893 */
1894
1895 static void GetCodeFragment (pge_CodeHunk *h);
1896
1897 /*
1898 WriteCodeHunkList - writes the CodeHunk list in the correct order.
1899 */
1900
1901 static void WriteCodeHunkList (pge_CodeHunk l);
1902
1903 /*
1904 WriteIndent - writes, n, spaces.
1905 */
1906
1907 static void WriteIndent (unsigned int n);
1908
1909 /*
1910 CheckWrite -
1911 */
1912
1913 static void CheckWrite (char ch, unsigned int *curpos, unsigned int left, bool *seentext);
1914
1915 /*
1916 WriteStringIndent - writes a string but it will try and remove upto indent spaces
1917 if they exist.
1918 */
1919
1920 static void WriteStringIndent (const char *a_, unsigned int _a_high, unsigned int indent, unsigned int *curpos, unsigned int left, bool *seentext);
1921
1922 /*
1923 WriteCodeHunkListIndent - writes the CodeHunk list in the correct order
1924 but it removes up to indent spaces if they exist.
1925 */
1926
1927 static void WriteCodeHunkListIndent (pge_CodeHunk l, unsigned int indent, unsigned int *curpos, unsigned int left, bool *seentext);
1928
1929 /*
1930 Add - adds a character to a code hunk and creates another code hunk if necessary.
1931 */
1932
1933 static pge_CodeHunk Add (pge_CodeHunk *p, char ch, unsigned int *i);
1934
1935 /*
1936 ConsHunk - combine two possible code hunks.
1937 */
1938
1939 static void ConsHunk (pge_CodeHunk *p, pge_CodeHunk q);
1940
1941 /*
1942 GetName - returns the next symbol which is checked for a legal name.
1943 */
1944
1945 static NameKey_Name GetName (void);
1946
1947 /*
1948 SyntaxError - after a syntax error we skip all tokens up until we reach
1949 a stop symbol.
1950 */
1951
1952 static void SyntaxError (pge_SetOfStop stop);
1953
1954 /*
1955 SyntaxCheck -
1956 */
1957
1958 static void SyntaxCheck (pge_SetOfStop stop);
1959
1960 /*
1961 Expect -
1962 */
1963
1964 static void Expect (bnflex_TokenType t, pge_SetOfStop stop);
1965
1966 /*
1967 Ident - error checking varient of Ident
1968 */
1969
1970 static void Ident (pge_SetOfStop stop);
1971
1972 /*
1973 Modula2Code - error checking varient of Modula2Code
1974 */
1975
1976 static void Modula2Code (pge_SetOfStop stop);
1977
1978 /*
1979 StartModName := % ModuleName := GetName() ; ignore begintok CodeFragmentPrologue % =:
1980 */
1981
1982 static void StartModName (pge_SetOfStop stop);
1983
1984 /*
1985 EndModName :=
1986 */
1987
1988 static void EndModName (pge_SetOfStop stop);
1989
1990 /*
1991 DoDeclaration := % CodeFragmentDeclaration % =:
1992 */
1993
1994 static void DoDeclaration (pge_SetOfStop stop);
1995
1996 /*
1997 CollectLiteral :=
1998 % LastLiteral := GetCurrentToken() ;
1999 AdvanceToken ; %
2000
2001
2002 first symbols:literaltok
2003
2004 cannot reachend
2005 */
2006
2007 static void CollectLiteral (pge_SetOfStop stopset);
2008
2009 /*
2010 CollectTok :=
2011 % CurrentSetDesc := NewSetDesc() ;
2012 WITH CurrentSetDesc^ DO
2013 type := tokel ;
2014 string := GetCurrentToken() ;
2015 END ;
2016 IF NOT ContainsSymKey(Values, GetCurrentToken())
2017 THEN
2018 AddEntry(Values, GetCurrentToken(), LargestValue) ;
2019 AddEntry(ReverseValues, Name(LargestValue), GetCurrentToken()) ;
2020 AddEntry(Aliases, GetCurrentToken(), GetCurrentToken()) ;
2021 AddEntry(ReverseAliases, GetCurrentToken(), GetCurrentToken()) ;
2022 INC(LargestValue)
2023 END ;
2024 AdvanceToken() ; %
2025
2026
2027 first symbols:identtok
2028
2029 cannot reachend
2030 */
2031
2032 static void CollectTok (pge_SetOfStop stopset);
2033
2034 /*
2035 DefineToken :=
2036 % AddEntry(Aliases, LastLiteral, GetCurrentToken()) ;
2037 AddEntry(ReverseAliases, GetCurrentToken(), LastLiteral) ;
2038 AddEntry(Values, GetCurrentToken(), LargestValue) ;
2039 AddEntry(ReverseValues, Name(LargestValue), GetCurrentToken()) ;
2040 INC(LargestValue) ;
2041 AdvanceToken ; %
2042
2043
2044 first symbols:identtok
2045
2046 cannot reachend
2047 */
2048
2049 static void DefineToken (pge_SetOfStop stopset);
2050
2051 /*
2052 Rules := '%' 'rules' { Defs } ExtBNF
2053
2054 first symbols:codetok
2055
2056 cannot reachend
2057 */
2058
2059 static void Rules (pge_SetOfStop stopset);
2060
2061 /*
2062 Special := Ident
2063 % VAR p: ProductionDesc ; %
2064
2065 % p := NewProduction() ;
2066 p^.statement := NewStatement() ;
2067 p^.statement^.followinfo^.calcfollow := TRUE ;
2068 p^.statement^.followinfo^.epsilon := false ;
2069 p^.statement^.followinfo^.reachend := false ;
2070 p^.statement^.ident := CurrentIdent ;
2071 p^.statement^.expr := NIL ;
2072 p^.firstsolved := TRUE ;
2073 p^.followinfo^.calcfollow := TRUE ;
2074 p^.followinfo^.epsilon := false ;
2075 p^.followinfo^.reachend := false %
2076 First Follow [ 'epsilon'
2077 % p^.statement^.followinfo^.epsilon := true ; these are not used - but they are displayed when debugging
2078 p^.statement^.followinfo^.reachend := true ;
2079 p^.followinfo^.epsilon := true ;
2080 p^.followinfo^.reachend := true
2081 %
2082 ] [ Literal
2083 % p^.description := LastLiteral %
2084 ]
2085
2086 first symbols:identtok
2087
2088 cannot reachend
2089 */
2090
2091 static void Special (pge_SetOfStop stopset);
2092
2093 /*
2094 Factor := '%' Modula2Code '%' |
2095 Ident
2096 % WITH CurrentFactor^ DO
2097 type := id ;
2098 ident := CurrentIdent
2099 END ; %
2100 | Literal
2101 % WITH CurrentFactor^ DO
2102 type := lit ;
2103 string := LastLiteral ;
2104 IF GetSymKey(Aliases, LastLiteral)=NulName
2105 THEN
2106 WarnError1('no token defined for literal %s', LastLiteral)
2107 END
2108 END ; %
2109 | '{'
2110 % WITH CurrentFactor^ DO
2111 type := mult ;
2112 expr := NewExpression() ;
2113 CurrentExpression := expr ;
2114 END ; %
2115 Expression '}' | '['
2116 % WITH CurrentFactor^ DO
2117 type := opt ;
2118 expr := NewExpression() ;
2119 CurrentExpression := expr ;
2120 END ; %
2121 Expression ']' | '('
2122 % WITH CurrentFactor^ DO
2123 type := sub ;
2124 expr := NewExpression() ;
2125 CurrentExpression := expr ;
2126 END ; %
2127 Expression ')'
2128
2129 first symbols:dquotetok, squotetok, lparatok, lsparatok, lcparatok, identtok, codetok
2130
2131 cannot reachend
2132 */
2133
2134 static void Factor (pge_SetOfStop stopset);
2135
2136 /*
2137 Statement :=
2138 % VAR i: IdentDesc ; %
2139 Ident
2140 % VAR p: ProductionDesc ; %
2141
2142 % p := FindDefinition(CurrentIdent^.name) ;
2143 IF p=NIL
2144 THEN
2145 p := NewProduction()
2146 ELSE
2147 IF NOT ((p^.statement=NIL) OR (p^.statement^.expr=NIL))
2148 THEN
2149 WarnError1('already declared rule %s', CurrentIdent^.name)
2150 END
2151 END ;
2152 i := CurrentIdent ; %
2153 ':='
2154 % VAR e: ExpressionDesc ; %
2155
2156 % e := NewExpression() ;
2157 CurrentExpression := e ; %
2158
2159 % VAR s: StatementDesc ; %
2160
2161 % s := NewStatement() ;
2162 WITH s^ DO
2163 ident := i ;
2164 expr := e
2165 END ; %
2166 Expression
2167 % p^.statement := s ; %
2168 '=:'
2169
2170 first symbols:identtok
2171
2172 cannot reachend
2173 */
2174
2175 static void Statement (pge_SetOfStop stopset);
2176
2177 /*
2178 Defs := 'special' Special | 'token' Token |
2179 'error' ErrorProcedures |
2180 'tokenfunc' TokenProcedure |
2181 'symfunc' SymProcedure
2182
2183 first symbols:symfunctok, tfunctok, errortok, tokentok, specialtok
2184
2185 cannot reachend
2186 */
2187
2188 static void Defs (pge_SetOfStop stopset);
2189
2190 /*
2191 ExtBNF := 'BNF' { Production } 'FNB'
2192
2193 first symbols:BNFtok
2194
2195 cannot reachend
2196 */
2197
2198 static void ExtBNF (pge_SetOfStop stopset);
2199
2200 /*
2201 Main := Header Decls Footer Rules
2202
2203 first symbols:codetok
2204
2205 cannot reachend
2206 */
2207
2208 static void Main (pge_SetOfStop stopset);
2209
2210 /*
2211 Header := '%' 'module' StartModName
2212
2213 first symbols:codetok
2214
2215 cannot reachend
2216 */
2217
2218 static void Header (pge_SetOfStop stopset);
2219
2220 /*
2221 Decls := '%' 'declaration' DoDeclaration
2222
2223 first symbols:codetok
2224
2225 cannot reachend
2226 */
2227
2228 static void Decls (pge_SetOfStop stopset);
2229
2230 /*
2231 Footer := '%' 'module' EndModName
2232
2233 first symbols:codetok
2234
2235 cannot reachend
2236 */
2237
2238 static void Footer (pge_SetOfStop stopset);
2239
2240 /*
2241 First := 'first' '{' { LitOrTokenOrIdent
2242 % WITH CurrentSetDesc^ DO
2243 next := TailProduction^.first ;
2244 END ;
2245 TailProduction^.first := CurrentSetDesc
2246 %
2247 } '}'
2248
2249 first symbols:firsttok
2250
2251 cannot reachend
2252 */
2253
2254 static void First (pge_SetOfStop stopset);
2255
2256 /*
2257 Follow := 'follow' '{' { LitOrTokenOrIdent
2258 % WITH CurrentSetDesc^ DO
2259 next := TailProduction^.followinfo^.follow ;
2260 END ;
2261 TailProduction^.followinfo^.follow := CurrentSetDesc
2262 %
2263 } '}'
2264
2265 first symbols:followtok
2266
2267 cannot reachend
2268 */
2269
2270 static void Follow (pge_SetOfStop stopset);
2271
2272 /*
2273 LitOrTokenOrIdent := Literal
2274 % CurrentSetDesc := NewSetDesc() ;
2275 WITH CurrentSetDesc^ DO
2276 type := litel ;
2277 string := LastLiteral ;
2278 END ;
2279 %
2280 | '<' CollectTok '>' |
2281 Ident
2282 % CurrentSetDesc := NewSetDesc() ;
2283 WITH CurrentSetDesc^ DO
2284 type := idel ;
2285 ident := CurrentIdent ;
2286 END ;
2287 %
2288
2289
2290 first symbols:dquotetok, squotetok, identtok, lesstok
2291
2292 cannot reachend
2293 */
2294
2295 static void LitOrTokenOrIdent (pge_SetOfStop stopset);
2296
2297 /*
2298 Literal := '"' CollectLiteral '"' |
2299 "'" CollectLiteral "'"
2300
2301 first symbols:squotetok, dquotetok
2302
2303 cannot reachend
2304 */
2305
2306 static void Literal (pge_SetOfStop stopset);
2307
2308 /*
2309 Token := Literal DefineToken
2310
2311 first symbols:dquotetok, squotetok
2312
2313 cannot reachend
2314 */
2315
2316 static void Token (pge_SetOfStop stopset);
2317
2318 /*
2319 ErrorProcedures := Literal
2320 % ErrorProcArray := LastLiteral %
2321 Literal
2322 % ErrorProcString := LastLiteral %
2323
2324
2325 first symbols:dquotetok, squotetok
2326
2327 cannot reachend
2328 */
2329
2330 static void ErrorProcedures (pge_SetOfStop stopset);
2331
2332 /*
2333 TokenProcedure := Literal
2334 % TokenTypeProc := LastLiteral %
2335
2336
2337 first symbols:dquotetok, squotetok
2338
2339 cannot reachend
2340 */
2341
2342 static void TokenProcedure (pge_SetOfStop stopset);
2343
2344 /*
2345 SymProcedure := Literal
2346 % SymIsProc := LastLiteral %
2347
2348
2349 first symbols:dquotetok, squotetok
2350
2351 cannot reachend
2352 */
2353
2354 static void SymProcedure (pge_SetOfStop stopset);
2355
2356 /*
2357 Production := Statement
2358
2359 first symbols:identtok
2360
2361 cannot reachend
2362 */
2363
2364 static void Production (pge_SetOfStop stopset);
2365
2366 /*
2367 Expression :=
2368 % VAR t1, t2: TermDesc ;
2369 e : ExpressionDesc ; %
2370
2371 % e := CurrentExpression ;
2372 t1 := NewTerm() ;
2373 CurrentTerm := t1 ; %
2374 Term
2375 % e^.term := t1 ; %
2376 { '|'
2377 % t2 := NewTerm() ;
2378 CurrentTerm := t2 %
2379 Term
2380 % t1^.next := t2 ;
2381 t1 := t2 %
2382 }
2383
2384 first symbols:dquotetok, squotetok, lparatok, lsparatok, lcparatok, identtok, codetok
2385
2386 cannot reachend
2387 */
2388
2389 static void Expression (pge_SetOfStop stopset);
2390
2391 /*
2392 Term :=
2393 % VAR t1: TermDesc ; f1, f2: FactorDesc ; %
2394
2395 % CurrentFactor := NewFactor() ;
2396 f1 := CurrentFactor ;
2397 t1 := CurrentTerm ; %
2398 Factor
2399 % t1^.factor := f1 ;
2400 f2 := NewFactor() ;
2401 CurrentFactor := f2 %
2402 { Factor
2403 % f1^.next := f2 ;
2404 f1 := f2 ;
2405 f2 := NewFactor() ;
2406 CurrentFactor := f2 ; %
2407 }
2408
2409 first symbols:squotetok, dquotetok, codetok, identtok, lcparatok, lsparatok, lparatok
2410
2411 cannot reachend
2412 */
2413
2414 static void Term (pge_SetOfStop stopset);
2415
2416 /*
2417 GetDefinitionName - returns the name of the rule inside, p.
2418 */
2419
2420 static NameKey_Name GetDefinitionName (pge_ProductionDesc p);
2421
2422 /*
2423 FindDefinition - searches and returns the rule which defines, n.
2424 */
2425
2426 static pge_ProductionDesc FindDefinition (NameKey_Name n);
2427
2428 /*
2429 BackPatchIdent - found an ident, i, we must look for the corresponding rule and
2430 set the definition accordingly.
2431 */
2432
2433 static void BackPatchIdent (pge_IdentDesc i);
2434
2435 /*
2436 BackPatchFactor - runs through the factor looking for an ident
2437 */
2438
2439 static void BackPatchFactor (pge_FactorDesc f);
2440
2441 /*
2442 BackPatchTerm - runs through all terms to find idents.
2443 */
2444
2445 static void BackPatchTerm (pge_TermDesc t);
2446
2447 /*
2448 BackPatchExpression - runs through the term to find any idents.
2449 */
2450
2451 static void BackPatchExpression (pge_ExpressionDesc e);
2452
2453 /*
2454 BackPatchSet -
2455 */
2456
2457 static void BackPatchSet (pge_SetDesc s);
2458
2459 /*
2460 BackPatchIdentToDefinitions - search through all the rules and add a link from any ident
2461 to the definition.
2462 */
2463
2464 static void BackPatchIdentToDefinitions (pge_ProductionDesc d);
2465
2466 /*
2467 CalculateFirstAndFollow -
2468 */
2469
2470 static void CalculateFirstAndFollow (pge_ProductionDesc p);
2471
2472 /*
2473 ForeachRuleDo -
2474 */
2475
2476 static void ForeachRuleDo (pge_DoProcedure p);
2477
2478 /*
2479 WhileNotCompleteDo -
2480 */
2481
2482 static void WhileNotCompleteDo (pge_DoProcedure p);
2483
2484 /*
2485 NewLine - generate a newline and indent.
2486 */
2487
2488 static void NewLine (unsigned int Left);
2489
2490 /*
2491 CheckNewLine -
2492 */
2493
2494 static void CheckNewLine (unsigned int Left);
2495
2496 /*
2497 IndentString - writes out a string with a preceeding indent.
2498 */
2499
2500 static void IndentString (const char *a_, unsigned int _a_high);
2501
2502 /*
2503 KeyWord - writes out a keywork with optional formatting directives.
2504 */
2505
2506 static void KeyWord (NameKey_Name n);
2507
2508 /*
2509 PrettyPara -
2510 */
2511
2512 static void PrettyPara (const char *c1_, unsigned int _c1_high, const char *c2_, unsigned int _c2_high, pge_ExpressionDesc e, unsigned int Left);
2513
2514 /*
2515 WriteKeyTexinfo -
2516 */
2517
2518 static void WriteKeyTexinfo (NameKey_Name s);
2519
2520 /*
2521 PrettyCommentFactor -
2522 */
2523
2524 static void PrettyCommentFactor (pge_FactorDesc f, unsigned int Left);
2525
2526 /*
2527 PeepTerm - returns the length of characters in term.
2528 */
2529
2530 static unsigned int PeepTerm (pge_TermDesc t);
2531
2532 /*
2533 PeepExpression - returns the length of the expression.
2534 */
2535
2536 static unsigned int PeepExpression (pge_ExpressionDesc e);
2537
2538 /*
2539 PeepFactor - returns the length of character in the factor
2540 */
2541
2542 static unsigned int PeepFactor (pge_FactorDesc f);
2543
2544 /*
2545 PrettyCommentTerm -
2546 */
2547
2548 static void PrettyCommentTerm (pge_TermDesc t, unsigned int Left);
2549
2550 /*
2551 PrettyCommentExpression -
2552 */
2553
2554 static void PrettyCommentExpression (pge_ExpressionDesc e, unsigned int Left);
2555
2556 /*
2557 PrettyCommentStatement -
2558 */
2559
2560 static void PrettyCommentStatement (pge_StatementDesc s, unsigned int Left);
2561
2562 /*
2563 PrettyCommentProduction - generates the comment for rule, p.
2564 */
2565
2566 static void PrettyCommentProduction (pge_ProductionDesc p);
2567
2568 /*
2569 PrettyPrintProduction - pretty prints the ebnf rule, p.
2570 */
2571
2572 static void PrettyPrintProduction (pge_ProductionDesc p);
2573
2574 /*
2575 EmitFileLineTag - emits a line and file tag using the C preprocessor syntax.
2576 */
2577
2578 static void EmitFileLineTag (unsigned int line);
2579
2580 /*
2581 EmitRule - generates a comment and code for rule, p.
2582 */
2583
2584 static void EmitRule (pge_ProductionDesc p);
2585
2586 /*
2587 CodeCondition -
2588 */
2589
2590 static void CodeCondition (pge_m2condition m);
2591
2592 /*
2593 CodeThenDo - codes a "THEN" or "DO" depending upon, m.
2594 */
2595
2596 static void CodeThenDo (pge_m2condition m);
2597
2598 /*
2599 CodeElseEnd - builds an ELSE END statement using string, end.
2600 */
2601
2602 static void CodeElseEnd (const char *end_, unsigned int _end_high, bool consumed, pge_FactorDesc f, bool inopt);
2603
2604 /*
2605 CodeEnd - codes a "END" depending upon, m.
2606 */
2607
2608 static void CodeEnd (pge_m2condition m, pge_TermDesc t, bool consumed, pge_FactorDesc f, bool inopt);
2609
2610 /*
2611 EmitNonVarCode - writes out, code, providing it is not a variable declaration.
2612 */
2613
2614 static void EmitNonVarCode (pge_CodeDesc code, unsigned int curpos, unsigned int left);
2615
2616 /*
2617 ChainOn -
2618 */
2619
2620 static pge_FactorDesc ChainOn (pge_FactorDesc codeStack, pge_FactorDesc f);
2621
2622 /*
2623 FlushCode -
2624 */
2625
2626 static void FlushCode (pge_FactorDesc *codeStack);
2627
2628 /*
2629 CodeFactor -
2630 */
2631
2632 static void CodeFactor (pge_FactorDesc f, pge_TermDesc t, pge_m2condition l, pge_m2condition n, bool inopt, bool inwhile, bool consumed, pge_FactorDesc codeStack);
2633
2634 /*
2635 CodeTerm -
2636 */
2637
2638 static void CodeTerm (pge_TermDesc t, pge_m2condition m, bool inopt, bool inwhile, bool consumed, pge_FactorDesc codeStack);
2639
2640 /*
2641 CodeExpression -
2642 */
2643
2644 static void CodeExpression (pge_ExpressionDesc e, pge_m2condition m, bool inopt, bool inwhile, bool consumed, pge_FactorDesc codeStack);
2645
2646 /*
2647 CodeStatement -
2648 */
2649
2650 static void CodeStatement (pge_StatementDesc s, pge_m2condition m);
2651
2652 /*
2653 CodeProduction - only encode grammer rules which are not special.
2654 */
2655
2656 static void CodeProduction (pge_ProductionDesc p);
2657
2658 /*
2659 RecoverCondition -
2660 */
2661
2662 static void RecoverCondition (pge_m2condition m);
2663
2664 /*
2665 ConditionIndent - returns the number of spaces indentation created via, m.
2666 */
2667
2668 static unsigned int ConditionIndent (pge_m2condition m);
2669
2670 /*
2671 WriteGetTokenType - writes out the method of determining the token type.
2672 */
2673
2674 static void WriteGetTokenType (void);
2675
2676 /*
2677 NumberOfElements - returns the number of elements in set, to, which lie between low..high
2678 */
2679
2680 static unsigned int NumberOfElements (pge_SetDesc to, unsigned int low, unsigned int high);
2681
2682 /*
2683 WriteElement - writes the literal name for element, e.
2684 */
2685
2686 static void WriteElement (unsigned int e);
2687
2688 /*
2689 EmitIsInSet - writes out the equivalent of GetTokenType() IN { toset }
2690 */
2691
2692 static void EmitIsInSet (pge_SetDesc to, NameKey_Name low, NameKey_Name high);
2693
2694 /*
2695 EmitIsInSubSet - writes out a test to see whether GetTokenype() is in { subset }
2696 */
2697
2698 static void EmitIsInSubSet (pge_SetDesc to, unsigned int low, unsigned int high);
2699
2700 /*
2701 EmitIsInFirst -
2702 */
2703
2704 static void EmitIsInFirst (pge_SetDesc to, pge_m2condition m);
2705 static void FlushRecoverCode (pge_FactorDesc *codeStack);
2706
2707 /*
2708 RecoverFactor -
2709 */
2710
2711 static void RecoverFactor (pge_FactorDesc f, pge_m2condition m, pge_FactorDesc codeStack);
2712
2713 /*
2714 OptExpSeen - returns TRUE if we can see an optional expression in the factor.
2715 This is not the same as epsilon. Example { '+' } matches epsilon as
2716 well as { '+' | '-' } but OptExpSeen returns TRUE in the second case
2717 and FALSE in the first.
2718 */
2719
2720 static bool OptExpSeen (pge_FactorDesc f);
2721
2722 /*
2723 RecoverTerm -
2724 */
2725
2726 static void RecoverTerm (pge_TermDesc t, pge_m2condition new_, pge_m2condition old);
2727
2728 /*
2729 RecoverExpression -
2730 */
2731
2732 static void RecoverExpression (pge_ExpressionDesc e, pge_m2condition new_, pge_m2condition old);
2733
2734 /*
2735 RecoverStatement -
2736 */
2737
2738 static void RecoverStatement (pge_StatementDesc s, pge_m2condition m);
2739
2740 /*
2741 EmitFirstFactor - generate a list of all first tokens between the range: low..high.
2742 */
2743
2744 static void EmitFirstFactor (pge_FactorDesc f, unsigned int low, unsigned int high);
2745
2746 /*
2747 EmitUsed -
2748 */
2749
2750 static void EmitUsed (unsigned int wordno);
2751
2752 /*
2753 EmitStopParameters - generate the stop set.
2754 */
2755
2756 static void EmitStopParameters (bool FormalParameters);
2757
2758 /*
2759 IsBetween - returns TRUE if the value of the token, string, is
2760 in the range: low..high
2761 */
2762
2763 static bool IsBetween (NameKey_Name string, unsigned int low, unsigned int high);
2764
2765 /*
2766 IsEmptySet - returns TRUE if no elements exist in set, to, with values, low..high.
2767 */
2768
2769 static bool IsEmptySet (pge_SetDesc to, unsigned int low, unsigned int high);
2770
2771 /*
2772 EmitSet - emits the tokens in the set, to, which have values low..high
2773 */
2774
2775 static void EmitSet (pge_SetDesc to, unsigned int low, unsigned int high);
2776
2777 /*
2778 EmitSetName - emits the tokens in the set, to, which have values low..high, using
2779 their names.
2780 */
2781
2782 static void EmitSetName (pge_SetDesc to, unsigned int low, unsigned int high);
2783
2784 /*
2785 EmitStopParametersAndSet - generates the stop parameters together with a set
2786 inclusion of all the symbols in set, to.
2787 */
2788
2789 static void EmitStopParametersAndSet (pge_SetDesc to);
2790
2791 /*
2792 EmitSetAsParameters - generates the first symbols as parameters to a set function.
2793 */
2794
2795 static void EmitSetAsParameters (pge_SetDesc to);
2796
2797 /*
2798 EmitStopParametersAndFollow - generates the stop parameters together with a set
2799 inclusion of all the follow symbols for subsequent
2800 sentances.
2801 */
2802
2803 static void EmitStopParametersAndFollow (pge_FactorDesc f, pge_m2condition m);
2804
2805 /*
2806 EmitFirstAsParameters -
2807 */
2808
2809 static void EmitFirstAsParameters (pge_FactorDesc f);
2810
2811 /*
2812 RecoverProduction - only encode grammer rules which are not special.
2813 Generate error recovery code.
2814 */
2815
2816 static void RecoverProduction (pge_ProductionDesc p);
2817
2818 /*
2819 IsWhite - returns TRUE if, ch, is a space or a tab.
2820 */
2821
2822 static bool IsWhite (char ch);
2823
2824 /*
2825 FindStr - returns TRUE if, str, was seen inside the code hunk
2826 */
2827
2828 static bool FindStr (pge_CodeHunk *code, unsigned int *i, const char *str_, unsigned int _str_high);
2829
2830 /*
2831 WriteUpto -
2832 */
2833
2834 static void WriteUpto (pge_CodeHunk code, pge_CodeHunk upto, unsigned int limit);
2835
2836 /*
2837 CheckForVar - checks for any local variables which need to be emitted during
2838 this production.
2839 */
2840
2841 static void CheckForVar (pge_CodeHunk code);
2842
2843 /*
2844 VarFactor -
2845 */
2846
2847 static void VarFactor (pge_FactorDesc f);
2848
2849 /*
2850 VarTerm -
2851 */
2852
2853 static void VarTerm (pge_TermDesc t);
2854
2855 /*
2856 VarExpression -
2857 */
2858
2859 static void VarExpression (pge_ExpressionDesc e);
2860
2861 /*
2862 VarStatement -
2863 */
2864
2865 static void VarStatement (pge_StatementDesc s);
2866
2867 /*
2868 VarProduction - writes out all variable declarations.
2869 */
2870
2871 static void VarProduction (pge_ProductionDesc p);
2872
2873 /*
2874 In - returns TRUE if token, s, is already in the set, to.
2875 */
2876
2877 static bool In (pge_SetDesc to, NameKey_Name s);
2878
2879 /*
2880 IntersectionIsNil - given two set lists, s1, s2, return TRUE if the
2881 s1 * s2 = {}
2882 */
2883
2884 static bool IntersectionIsNil (pge_SetDesc s1, pge_SetDesc s2);
2885
2886 /*
2887 AddSet - adds a first symbol to a production.
2888 */
2889
2890 static void AddSet (pge_SetDesc *to, NameKey_Name s);
2891
2892 /*
2893 OrSet -
2894 */
2895
2896 static void OrSet (pge_SetDesc *to, pge_SetDesc from);
2897
2898 /*
2899 CalcFirstFactor -
2900 */
2901
2902 static void CalcFirstFactor (pge_FactorDesc f, pge_ProductionDesc from, pge_SetDesc *to);
2903
2904 /*
2905 CalcFirstTerm -
2906 */
2907
2908 static void CalcFirstTerm (pge_TermDesc t, pge_ProductionDesc from, pge_SetDesc *to);
2909
2910 /*
2911 CalcFirstExpression -
2912 */
2913
2914 static void CalcFirstExpression (pge_ExpressionDesc e, pge_ProductionDesc from, pge_SetDesc *to);
2915
2916 /*
2917 CalcFirstStatement -
2918 */
2919
2920 static void CalcFirstStatement (pge_StatementDesc s, pge_ProductionDesc from, pge_SetDesc *to);
2921
2922 /*
2923 CalcFirstProduction - calculates all of the first symbols for the grammer
2924 */
2925
2926 static void CalcFirstProduction (pge_ProductionDesc p, pge_ProductionDesc from, pge_SetDesc *to);
2927 static void WorkOutFollowFactor (pge_FactorDesc f, pge_SetDesc *followset, pge_SetDesc after);
2928
2929 /*
2930 WorkOutFollowTerm -
2931 */
2932
2933 static void WorkOutFollowTerm (pge_TermDesc t, pge_SetDesc *followset, pge_SetDesc after);
2934
2935 /*
2936 WorkOutFollowExpression -
2937 */
2938
2939 static void WorkOutFollowExpression (pge_ExpressionDesc e, pge_SetDesc *followset, pge_SetDesc after);
2940
2941 /*
2942 CollectFollow - collects the follow set from, f, into, to.
2943 */
2944
2945 static void CollectFollow (pge_SetDesc *to, pge_FollowDesc f);
2946
2947 /*
2948 CalcFollowFactor -
2949 */
2950
2951 static void CalcFollowFactor (pge_FactorDesc f, pge_SetDesc after);
2952
2953 /*
2954 CalcFollowTerm -
2955 */
2956
2957 static void CalcFollowTerm (pge_TermDesc t, pge_SetDesc after);
2958
2959 /*
2960 CalcFollowExpression -
2961 */
2962
2963 static void CalcFollowExpression (pge_ExpressionDesc e, pge_SetDesc after);
2964
2965 /*
2966 CalcFollowStatement - given a bnf statement generate the follow set.
2967 */
2968
2969 static void CalcFollowStatement (pge_StatementDesc s);
2970
2971 /*
2972 CalcFollowProduction -
2973 */
2974
2975 static void CalcFollowProduction (pge_ProductionDesc p);
2976
2977 /*
2978 CalcEpsilonFactor -
2979 */
2980
2981 static void CalcEpsilonFactor (pge_FactorDesc f);
2982
2983 /*
2984 CalcEpsilonTerm -
2985 */
2986
2987 static void CalcEpsilonTerm (pge_TermDesc t);
2988
2989 /*
2990 CalcEpsilonExpression -
2991 */
2992
2993 static void CalcEpsilonExpression (pge_ExpressionDesc e);
2994
2995 /*
2996 CalcEpsilonStatement - given a bnf statement generate the follow set.
2997 */
2998
2999 static void CalcEpsilonStatement (pge_StatementDesc s);
3000
3001 /*
3002 CalcEpsilonProduction -
3003 */
3004
3005 static void CalcEpsilonProduction (pge_ProductionDesc p);
3006
3007 /*
3008 CalcReachEndFactor -
3009 */
3010
3011 static pge_TraverseResult CalcReachEndFactor (pge_FactorDesc f);
3012
3013 /*
3014 CalcReachEndTerm -
3015 */
3016
3017 static pge_TraverseResult CalcReachEndTerm (pge_TermDesc t);
3018
3019 /*
3020 CalcReachEndExpression -
3021 */
3022
3023 static void CalcReachEndExpression (pge_ExpressionDesc e);
3024
3025 /*
3026 CalcReachEndStatement -
3027 */
3028
3029 static void CalcReachEndStatement (pge_StatementDesc s);
3030
3031 /*
3032 CalcReachEndStatement -
3033 */
3034
3035 static void stop (void);
3036
3037 /*
3038 CalcReachEndProduction -
3039 */
3040
3041 static void CalcReachEndProduction (pge_ProductionDesc p);
3042
3043 /*
3044 EmptyFactor -
3045 */
3046
3047 static bool EmptyFactor (pge_FactorDesc f);
3048
3049 /*
3050 EmptyTerm - returns TRUE if the term maybe empty.
3051 */
3052
3053 static bool EmptyTerm (pge_TermDesc t);
3054
3055 /*
3056 EmptyExpression -
3057 */
3058
3059 static bool EmptyExpression (pge_ExpressionDesc e);
3060
3061 /*
3062 EmptyStatement - returns TRUE if statement, s, is empty.
3063 */
3064
3065 static bool EmptyStatement (pge_StatementDesc s);
3066
3067 /*
3068 EmptyProduction - returns if production, p, maybe empty.
3069 */
3070
3071 static bool EmptyProduction (pge_ProductionDesc p);
3072
3073 /*
3074 EmitFDLNotice -
3075 */
3076
3077 static void EmitFDLNotice (void);
3078
3079 /*
3080 EmitRules - generates the BNF rules.
3081 */
3082
3083 static void EmitRules (void);
3084
3085 /*
3086 DescribeElement -
3087 */
3088
3089 static void DescribeElement (unsigned int name);
3090
3091 /*
3092 EmitInTestStop - construct a test for stop element, name.
3093 */
3094
3095 static void EmitInTestStop (NameKey_Name name);
3096
3097 /*
3098 DescribeStopElement -
3099 */
3100
3101 static void DescribeStopElement (unsigned int name);
3102
3103 /*
3104 EmitDescribeStop -
3105 */
3106
3107 static void EmitDescribeStop (void);
3108
3109 /*
3110 EmitDescribeError -
3111 */
3112
3113 static void EmitDescribeError (void);
3114
3115 /*
3116 EmitSetTypes - write out the set types used during error recovery
3117 */
3118
3119 static void EmitSetTypes (void);
3120
3121 /*
3122 EmitSupport - generates the support routines.
3123 */
3124
3125 static void EmitSupport (void);
3126
3127 /*
3128 DisposeSetDesc - dispose of the set list, s.
3129 */
3130
3131 static void DisposeSetDesc (pge_SetDesc *s);
3132
3133 /*
3134 OptionalFactor -
3135 */
3136
3137 static bool OptionalFactor (pge_FactorDesc f);
3138
3139 /*
3140 OptionalTerm - returns TRUE if the term maybe empty.
3141 */
3142
3143 static bool OptionalTerm (pge_TermDesc t);
3144
3145 /*
3146 OptionalExpression -
3147 */
3148
3149 static bool OptionalExpression (pge_ExpressionDesc e);
3150
3151 /*
3152 OptionalStatement - returns FALSE if statement, s, does not have a optional ambiguity.
3153 */
3154
3155 static bool OptionalStatement (pge_StatementDesc s);
3156
3157 /*
3158 OptionalProduction -
3159 */
3160
3161 static bool OptionalProduction (pge_ProductionDesc p);
3162
3163 /*
3164 CheckFirstFollow -
3165 */
3166
3167 static bool CheckFirstFollow (pge_FactorDesc f, pge_FactorDesc after);
3168
3169 /*
3170 ConstrainedEmptyFactor -
3171 */
3172
3173 static bool ConstrainedEmptyFactor (pge_FactorDesc f);
3174
3175 /*
3176 ConstrainedEmptyTerm - returns TRUE if the term maybe empty.
3177 */
3178
3179 static bool ConstrainedEmptyTerm (pge_TermDesc t);
3180
3181 /*
3182 ConstrainedEmptyExpression -
3183 */
3184
3185 static bool ConstrainedEmptyExpression (pge_ExpressionDesc e);
3186
3187 /*
3188 ConstrainedEmptyStatement - returns FALSE if statement, s, does not have a optional ambiguity.
3189 */
3190
3191 static bool ConstrainedEmptyStatement (pge_StatementDesc s);
3192
3193 /*
3194 ConstrainedEmptyProduction - returns TRUE if a problem exists with, p.
3195 */
3196
3197 static bool ConstrainedEmptyProduction (pge_ProductionDesc p);
3198
3199 /*
3200 TestForLALR1 -
3201 */
3202
3203 static void TestForLALR1 (pge_ProductionDesc p);
3204
3205 /*
3206 DoEpsilon - runs the epsilon interrelated rules
3207 */
3208
3209 static void DoEpsilon (pge_ProductionDesc p);
3210
3211 /*
3212 CheckComplete - checks that production, p, is complete.
3213 */
3214
3215 static void CheckComplete (pge_ProductionDesc p);
3216
3217 /*
3218 PostProcessRules - backpatch the ident to rule definitions and emit comments and code.
3219 */
3220
3221 static void PostProcessRules (void);
3222
3223 /*
3224 DisplayHelp - display a summary help and then exit (0).
3225 */
3226
3227 static void DisplayHelp (void);
3228
3229 /*
3230 ParseArgs -
3231 */
3232
3233 static void ParseArgs (void);
3234
3235 /*
3236 Init - initialize the modules data structures
3237 */
3238
3239 static void Init (void);
3240
3241
3242 /*
3243 DescribeStop - issues a message explaining what tokens were expected
3244 */
3245
3246 static DynamicStrings_String DescribeStop (pge_SetOfStop stopset)
3247 {
3248 unsigned int n;
3249 DynamicStrings_String str;
3250 DynamicStrings_String message;
3251
3252 n = 0;
3253 message = DynamicStrings_InitString ((const char *) "", 0);
3254 if ((((1 << (bnflex_literaltok-bnflex_identtok)) & (stopset)) != 0))
3255 {
3256 message = DynamicStrings_ConCat (DynamicStrings_ConCatChar (message, ' '), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) "literal", 7)));
3257 n += 1;
3258 }
3259 if ((((1 << (bnflex_identtok-bnflex_identtok)) & (stopset)) != 0))
3260 {
3261 message = DynamicStrings_ConCat (DynamicStrings_ConCatChar (message, ' '), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) "identifier", 10)));
3262 n += 1;
3263 }
3264 if ((((1 << (bnflex_FNBtok-bnflex_identtok)) & (stopset)) != 0))
3265 {
3266 message = DynamicStrings_ConCat (DynamicStrings_ConCatChar (message, ' '), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) "FNB", 3)));
3267 n += 1;
3268 }
3269 if ((((1 << (bnflex_BNFtok-bnflex_identtok)) & (stopset)) != 0))
3270 {
3271 message = DynamicStrings_ConCat (DynamicStrings_ConCatChar (message, ' '), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) "BNF", 3)));
3272 n += 1;
3273 }
3274 if ((((1 << (bnflex_epsilontok-bnflex_identtok)) & (stopset)) != 0))
3275 {
3276 message = DynamicStrings_ConCat (DynamicStrings_ConCatChar (message, ' '), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) "epsilon", 7)));
3277 n += 1;
3278 }
3279 if ((((1 << (bnflex_followtok-bnflex_identtok)) & (stopset)) != 0))
3280 {
3281 message = DynamicStrings_ConCat (DynamicStrings_ConCatChar (message, ' '), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) "follow", 6)));
3282 n += 1;
3283 }
3284 if ((((1 << (bnflex_firsttok-bnflex_identtok)) & (stopset)) != 0))
3285 {
3286 message = DynamicStrings_ConCat (DynamicStrings_ConCatChar (message, ' '), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) "first", 5)));
3287 n += 1;
3288 }
3289 if ((((1 << (bnflex_specialtok-bnflex_identtok)) & (stopset)) != 0))
3290 {
3291 message = DynamicStrings_ConCat (DynamicStrings_ConCatChar (message, ' '), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) "special", 7)));
3292 n += 1;
3293 }
3294 if ((((1 << (bnflex_tokentok-bnflex_identtok)) & (stopset)) != 0))
3295 {
3296 message = DynamicStrings_ConCat (DynamicStrings_ConCatChar (message, ' '), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) "token", 5)));
3297 n += 1;
3298 }
3299 if ((((1 << (bnflex_declarationtok-bnflex_identtok)) & (stopset)) != 0))
3300 {
3301 message = DynamicStrings_ConCat (DynamicStrings_ConCatChar (message, ' '), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) "declaration", 11)));
3302 n += 1;
3303 }
3304 if ((((1 << (bnflex_endtok-bnflex_identtok)) & (stopset)) != 0))
3305 {
3306 message = DynamicStrings_ConCat (DynamicStrings_ConCatChar (message, ' '), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) "end", 3)));
3307 n += 1;
3308 }
3309 if ((((1 << (bnflex_rulestok-bnflex_identtok)) & (stopset)) != 0))
3310 {
3311 message = DynamicStrings_ConCat (DynamicStrings_ConCatChar (message, ' '), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) "rules", 5)));
3312 n += 1;
3313 }
3314 if ((((1 << (bnflex_begintok-bnflex_identtok)) & (stopset)) != 0))
3315 {
3316 message = DynamicStrings_ConCat (DynamicStrings_ConCatChar (message, ' '), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) "begin", 5)));
3317 n += 1;
3318 }
3319 if ((((1 << (bnflex_moduletok-bnflex_identtok)) & (stopset)) != 0))
3320 {
3321 message = DynamicStrings_ConCat (DynamicStrings_ConCatChar (message, ' '), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) "module", 6)));
3322 n += 1;
3323 }
3324 if ((((1 << (bnflex_dquotetok-bnflex_identtok)) & (stopset)) != 0))
3325 {
3326 message = DynamicStrings_ConCatChar (DynamicStrings_ConCatChar (DynamicStrings_ConCatChar (DynamicStrings_ConCatChar (DynamicStrings_ConCatChar (message, ' '), '`'), '"'), '\''), ',');
3327 n += 1;
3328 }
3329 if ((((1 << (bnflex_squotetok-bnflex_identtok)) & (stopset)) != 0))
3330 {
3331 message = DynamicStrings_ConCatChar (DynamicStrings_ConCatChar (DynamicStrings_ConCatChar (DynamicStrings_ConCatChar (DynamicStrings_ConCatChar (message, ' '), '"'), '\''), '"'), ',');
3332 n += 1;
3333 }
3334 if ((((1 << (bnflex_symfunctok-bnflex_identtok)) & (stopset)) != 0))
3335 {
3336 message = DynamicStrings_ConCat (DynamicStrings_ConCatChar (message, ' '), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) "symfunc", 7)));
3337 n += 1;
3338 }
3339 if ((((1 << (bnflex_tfunctok-bnflex_identtok)) & (stopset)) != 0))
3340 {
3341 message = DynamicStrings_ConCat (DynamicStrings_ConCatChar (message, ' '), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) "tokenfunc", 9)));
3342 n += 1;
3343 }
3344 if ((((1 << (bnflex_errortok-bnflex_identtok)) & (stopset)) != 0))
3345 {
3346 message = DynamicStrings_ConCat (DynamicStrings_ConCatChar (message, ' '), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) "error", 5)));
3347 n += 1;
3348 }
3349 if ((((1 << (bnflex_gretok-bnflex_identtok)) & (stopset)) != 0))
3350 {
3351 message = DynamicStrings_ConCat (DynamicStrings_ConCatChar (message, ' '), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) ">", 1)));
3352 n += 1;
3353 }
3354 if ((((1 << (bnflex_lesstok-bnflex_identtok)) & (stopset)) != 0))
3355 {
3356 message = DynamicStrings_ConCat (DynamicStrings_ConCatChar (message, ' '), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) "<", 1)));
3357 n += 1;
3358 }
3359 if ((((1 << (bnflex_rparatok-bnflex_identtok)) & (stopset)) != 0))
3360 {
3361 message = DynamicStrings_ConCat (DynamicStrings_ConCatChar (message, ' '), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) ")", 1)));
3362 n += 1;
3363 }
3364 if ((((1 << (bnflex_lparatok-bnflex_identtok)) & (stopset)) != 0))
3365 {
3366 message = DynamicStrings_ConCat (DynamicStrings_ConCatChar (message, ' '), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) "(", 1)));
3367 n += 1;
3368 }
3369 if ((((1 << (bnflex_rcparatok-bnflex_identtok)) & (stopset)) != 0))
3370 {
3371 message = DynamicStrings_ConCat (DynamicStrings_ConCatChar (message, ' '), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) "}", 1)));
3372 n += 1;
3373 }
3374 if ((((1 << (bnflex_lcparatok-bnflex_identtok)) & (stopset)) != 0))
3375 {
3376 message = DynamicStrings_ConCat (DynamicStrings_ConCatChar (message, ' '), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) "{", 1)));
3377 n += 1;
3378 }
3379 if ((((1 << (bnflex_rsparatok-bnflex_identtok)) & (stopset)) != 0))
3380 {
3381 message = DynamicStrings_ConCat (DynamicStrings_ConCatChar (message, ' '), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) "]", 1)));
3382 n += 1;
3383 }
3384 if ((((1 << (bnflex_lsparatok-bnflex_identtok)) & (stopset)) != 0))
3385 {
3386 message = DynamicStrings_ConCat (DynamicStrings_ConCatChar (message, ' '), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) "[", 1)));
3387 n += 1;
3388 }
3389 if ((((1 << (bnflex_bartok-bnflex_identtok)) & (stopset)) != 0))
3390 {
3391 message = DynamicStrings_ConCat (DynamicStrings_ConCatChar (message, ' '), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) "|", 1)));
3392 n += 1;
3393 }
3394 if ((((1 << (bnflex_rbecomestok-bnflex_identtok)) & (stopset)) != 0))
3395 {
3396 message = DynamicStrings_ConCat (DynamicStrings_ConCatChar (message, ' '), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) "=:", 2)));
3397 n += 1;
3398 }
3399 if ((((1 << (bnflex_lbecomestok-bnflex_identtok)) & (stopset)) != 0))
3400 {
3401 message = DynamicStrings_ConCat (DynamicStrings_ConCatChar (message, ' '), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) ":=", 2)));
3402 n += 1;
3403 }
3404 if ((((1 << (bnflex_codetok-bnflex_identtok)) & (stopset)) != 0))
3405 {
3406 message = DynamicStrings_ConCat (DynamicStrings_ConCatChar (message, ' '), DynamicStrings_Mark (DynamicStrings_InitString ((const char *) "%", 1)));
3407 n += 1;
3408 }
3409 if ((((1 << (bnflex_eoftok-bnflex_identtok)) & (stopset)) != 0))
3410 {} /* empty. */
3411 /* eoftok has no token name (needed to generate error messages) */
3412 if (n == 0)
3413 {
3414 str = DynamicStrings_InitString ((const char *) " syntax error", 13);
3415 message = DynamicStrings_KillString (message);
3416 }
3417 else if (n == 1)
3418 {
3419 /* avoid dangling else. */
3420 str = DynamicStrings_ConCat (message, DynamicStrings_Mark (DynamicStrings_InitString ((const char *) " missing ", 9)));
3421 }
3422 else
3423 {
3424 /* avoid dangling else. */
3425 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) " expecting one of", 17), message);
3426 message = DynamicStrings_KillString (message);
3427 }
3428 return str;
3429 /* static analysis guarentees a RETURN statement will be used before here. */
3430 __builtin_unreachable ();
3431 }
3432
3433
3434 /*
3435 DescribeError - issues a message explaining what tokens were expected
3436 */
3437
3438 static void DescribeError (void)
3439 {
3440 DynamicStrings_String str;
3441
3442 str = DynamicStrings_InitString ((const char *) "", 0);
3443 switch (bnflex_GetCurrentTokenType ())
3444 {
3445 case bnflex_literaltok:
3446 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "syntax error, found literal", 27), DynamicStrings_Mark (str));
3447 break;
3448
3449 case bnflex_identtok:
3450 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "syntax error, found identifier", 30), DynamicStrings_Mark (str));
3451 break;
3452
3453 case bnflex_FNBtok:
3454 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "syntax error, found FNB", 23), DynamicStrings_Mark (str));
3455 break;
3456
3457 case bnflex_BNFtok:
3458 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "syntax error, found BNF", 23), DynamicStrings_Mark (str));
3459 break;
3460
3461 case bnflex_epsilontok:
3462 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "syntax error, found epsilon", 27), DynamicStrings_Mark (str));
3463 break;
3464
3465 case bnflex_followtok:
3466 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "syntax error, found follow", 26), DynamicStrings_Mark (str));
3467 break;
3468
3469 case bnflex_firsttok:
3470 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "syntax error, found first", 25), DynamicStrings_Mark (str));
3471 break;
3472
3473 case bnflex_specialtok:
3474 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "syntax error, found special", 27), DynamicStrings_Mark (str));
3475 break;
3476
3477 case bnflex_tokentok:
3478 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "syntax error, found token", 25), DynamicStrings_Mark (str));
3479 break;
3480
3481 case bnflex_declarationtok:
3482 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "syntax error, found declaration", 31), DynamicStrings_Mark (str));
3483 break;
3484
3485 case bnflex_endtok:
3486 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "syntax error, found end", 23), DynamicStrings_Mark (str));
3487 break;
3488
3489 case bnflex_rulestok:
3490 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "syntax error, found rules", 25), DynamicStrings_Mark (str));
3491 break;
3492
3493 case bnflex_begintok:
3494 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "syntax error, found begin", 25), DynamicStrings_Mark (str));
3495 break;
3496
3497 case bnflex_moduletok:
3498 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "syntax error, found module", 26), DynamicStrings_Mark (str));
3499 break;
3500
3501 case bnflex_dquotetok:
3502 str = DynamicStrings_ConCat (DynamicStrings_ConCatChar (DynamicStrings_ConCatChar (DynamicStrings_InitString ((const char *) "syntax error, found '", 21), '"'), '\''), DynamicStrings_Mark (str));
3503 break;
3504
3505 case bnflex_squotetok:
3506 str = DynamicStrings_ConCat (DynamicStrings_ConCatChar (DynamicStrings_ConCatChar (DynamicStrings_InitString ((const char *) "syntax error, found \"", 21), '\''), '"'), DynamicStrings_Mark (str));
3507 break;
3508
3509 case bnflex_symfunctok:
3510 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "syntax error, found symfunc", 27), DynamicStrings_Mark (str));
3511 break;
3512
3513 case bnflex_tfunctok:
3514 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "syntax error, found tokenfunc", 29), DynamicStrings_Mark (str));
3515 break;
3516
3517 case bnflex_errortok:
3518 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "syntax error, found error", 25), DynamicStrings_Mark (str));
3519 break;
3520
3521 case bnflex_gretok:
3522 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "syntax error, found >", 21), DynamicStrings_Mark (str));
3523 break;
3524
3525 case bnflex_lesstok:
3526 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "syntax error, found <", 21), DynamicStrings_Mark (str));
3527 break;
3528
3529 case bnflex_rparatok:
3530 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "syntax error, found )", 21), DynamicStrings_Mark (str));
3531 break;
3532
3533 case bnflex_lparatok:
3534 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "syntax error, found (", 21), DynamicStrings_Mark (str));
3535 break;
3536
3537 case bnflex_rcparatok:
3538 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "syntax error, found }", 21), DynamicStrings_Mark (str));
3539 break;
3540
3541 case bnflex_lcparatok:
3542 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "syntax error, found {", 21), DynamicStrings_Mark (str));
3543 break;
3544
3545 case bnflex_rsparatok:
3546 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "syntax error, found ]", 21), DynamicStrings_Mark (str));
3547 break;
3548
3549 case bnflex_lsparatok:
3550 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "syntax error, found [", 21), DynamicStrings_Mark (str));
3551 break;
3552
3553 case bnflex_bartok:
3554 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "syntax error, found |", 21), DynamicStrings_Mark (str));
3555 break;
3556
3557 case bnflex_rbecomestok:
3558 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "syntax error, found =:", 22), DynamicStrings_Mark (str));
3559 break;
3560
3561 case bnflex_lbecomestok:
3562 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "syntax error, found :=", 22), DynamicStrings_Mark (str));
3563 break;
3564
3565 case bnflex_codetok:
3566 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "syntax error, found %", 21), DynamicStrings_Mark (str));
3567 break;
3568
3569 case bnflex_eoftok:
3570 str = DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "syntax error, found ", 20), DynamicStrings_Mark (str));
3571 break;
3572
3573
3574 default:
3575 break;
3576 }
3577 PushBackInput_WarnString (str);
3578 }
3579
3580
3581 /*
3582 AddEntry - adds an entry into, t, containing [def:value].
3583 */
3584
3585 static void AddEntry (SymbolKey_SymbolTree *t, NameKey_Name def, NameKey_Name value)
3586 {
3587 if (SymbolKey_ContainsSymKey ((*t), def))
3588 {
3589 WarnError1 ((const char *) "already seen a definition for token '%s'", 40, def);
3590 }
3591 else
3592 {
3593 SymbolKey_PutSymKey ((*t), def, value);
3594 }
3595 }
3596
3597
3598 /*
3599 Format1 - converts string, src, into, dest, together with encapsulated
3600 entity, n. It only formats the first %s or %d with n.
3601 */
3602
3603 static void Format1 (const char *src_, unsigned int _src_high, unsigned int n, char *dest, unsigned int _dest_high)
3604 {
3605 typedef struct Format1__T12_a Format1__T12;
3606
3607 struct Format1__T12_a { char array[MaxString+1]; };
3608 unsigned int HighSrc;
3609 unsigned int HighDest;
3610 unsigned int i;
3611 unsigned int j;
3612 Format1__T12 str;
3613 char src[_src_high+1];
3614
3615 /* make a local copy of each unbounded array. */
3616 memcpy (src, src_, _src_high+1);
3617
3618 HighSrc = StrLib_StrLen ((const char *) src, _src_high);
3619 HighDest = _dest_high;
3620 i = 0;
3621 j = 0;
3622 while ((((i < HighSrc) && (src[i] != ASCII_nul)) && (j < HighDest)) && (src[i] != '%'))
3623 {
3624 const_cast<char *>(dest)[j] = src[i];
3625 i += 1;
3626 j += 1;
3627 }
3628 if ((((i+1) < HighSrc) && (src[i] == '%')) && (j < HighDest))
3629 {
3630 /* avoid gcc warning by using compound statement even if not strictly necessary. */
3631 if (src[i+1] == 's')
3632 {
3633 const_cast<char *>(dest)[j] = ASCII_nul;
3634 NameKey_GetKey (n, (char *) &str.array[0], MaxString);
3635 StrLib_StrConCat ((const char *) dest, _dest_high, (const char *) &str.array[0], MaxString, (char *) dest, _dest_high);
3636 j = StrLib_StrLen ((const char *) dest, _dest_high);
3637 i += 2;
3638 }
3639 else if (src[i+1] == 'd')
3640 {
3641 /* avoid dangling else. */
3642 const_cast<char *>(dest)[j] = ASCII_nul;
3643 NumberIO_CardToStr (n, 0, (char *) &str.array[0], MaxString);
3644 StrLib_StrConCat ((const char *) dest, _dest_high, (const char *) &str.array[0], MaxString, (char *) dest, _dest_high);
3645 j = StrLib_StrLen ((const char *) dest, _dest_high);
3646 i += 2;
3647 }
3648 else
3649 {
3650 /* avoid dangling else. */
3651 const_cast<char *>(dest)[j] = src[i];
3652 i += 1;
3653 j += 1;
3654 }
3655 }
3656 /* and finish off copying src into dest */
3657 while (((i < HighSrc) && (src[i] != ASCII_nul)) && (j < HighDest))
3658 {
3659 const_cast<char *>(dest)[j] = src[i];
3660 i += 1;
3661 j += 1;
3662 }
3663 if (j < HighDest)
3664 {
3665 const_cast<char *>(dest)[j] = ASCII_nul;
3666 }
3667 }
3668
3669
3670 /*
3671 WarnError1 -
3672 */
3673
3674 static void WarnError1 (const char *a_, unsigned int _a_high, unsigned int n)
3675 {
3676 typedef struct WarnError1__T13_a WarnError1__T13;
3677
3678 struct WarnError1__T13_a { char array[MaxString+1]; };
3679 WarnError1__T13 line;
3680 char a[_a_high+1];
3681
3682 /* make a local copy of each unbounded array. */
3683 memcpy (a, a_, _a_high+1);
3684
3685 Format1 ((const char *) a, _a_high, n, (char *) &line.array[0], MaxString);
3686 PushBackInput_WarnError ((const char *) &line.array[0], MaxString);
3687 }
3688
3689
3690 /*
3691 PrettyFollow -
3692 */
3693
3694 static void PrettyFollow (const char *start_, unsigned int _start_high, const char *end_, unsigned int _end_high, pge_FollowDesc f)
3695 {
3696 char start[_start_high+1];
3697 char end[_end_high+1];
3698
3699 /* make a local copy of each unbounded array. */
3700 memcpy (start, start_, _start_high+1);
3701 memcpy (end, end_, _end_high+1);
3702
3703 if (Debugging)
3704 {
3705 Output_WriteString ((const char *) start, _start_high);
3706 if (f != NULL)
3707 {
3708 if (f->calcfollow)
3709 {
3710 Output_WriteString ((const char *) "followset defined as:", 21);
3711 EmitSet (f->follow, static_cast<unsigned int> (0), static_cast<unsigned int> (0));
3712 }
3713 switch (f->reachend)
3714 {
3715 case pge_true:
3716 Output_WriteString ((const char *) " [E]", 4);
3717 break;
3718
3719 case pge_false:
3720 Output_WriteString ((const char *) " [C]", 4);
3721 break;
3722
3723 case pge_unknown:
3724 Output_WriteString ((const char *) " [U]", 4);
3725 break;
3726
3727
3728 default:
3729 break;
3730 }
3731 switch (f->epsilon)
3732 {
3733 case pge_true:
3734 Output_WriteString ((const char *) " [e]", 4);
3735 break;
3736
3737 case pge_false:
3738 break;
3739
3740 case pge_unknown:
3741 Output_WriteString ((const char *) " [u]", 4);
3742 break;
3743
3744
3745 default:
3746 break;
3747 }
3748 }
3749 Output_WriteString ((const char *) end, _end_high);
3750 }
3751 }
3752
3753
3754 /*
3755 NewFollow - creates a new follow descriptor and returns the data structure.
3756 */
3757
3758 static pge_FollowDesc NewFollow (void)
3759 {
3760 pge_FollowDesc f;
3761
3762 Storage_ALLOCATE ((void **) &f, sizeof (pge__T6));
3763 f->follow = NULL;
3764 f->reachend = pge_unknown;
3765 f->epsilon = pge_unknown;
3766 return f;
3767 /* static analysis guarentees a RETURN statement will be used before here. */
3768 __builtin_unreachable ();
3769 }
3770
3771
3772 /*
3773 AssignEpsilon - assigns the epsilon value and sets the epsilon to value,
3774 providing condition is TRUE.
3775 */
3776
3777 static void AssignEpsilon (bool condition, pge_FollowDesc f, pge_TraverseResult value)
3778 {
3779 if ((condition && (value != pge_unknown)) && (f->epsilon == pge_unknown))
3780 {
3781 f->epsilon = value;
3782 Finished = false;
3783 }
3784 }
3785
3786
3787 /*
3788 GetEpsilon - returns the value of epsilon
3789 */
3790
3791 static pge_TraverseResult GetEpsilon (pge_FollowDesc f)
3792 {
3793 if (f == NULL)
3794 {
3795 Debug_Halt ((const char *) "why is the follow info NIL?", 27, (const char *) "m2/gm2-auto/pge.mod", 19, (const char *) "GetEpsilon", 10, 597);
3796 }
3797 else
3798 {
3799 return f->epsilon;
3800 }
3801 ReturnException ("m2/gm2-auto/pge.mod", 1, 7);
3802 __builtin_unreachable ();
3803 }
3804
3805
3806 /*
3807 AssignReachEnd - assigns the reachend value providing that, condition, is TRUE.
3808 */
3809
3810 static void AssignReachEnd (bool condition, pge_FollowDesc f, pge_TraverseResult value)
3811 {
3812 if (condition)
3813 {
3814 if ((f->reachend == pge_unknown) && (value != pge_unknown))
3815 {
3816 f->reachend = value;
3817 Finished = false;
3818 }
3819 }
3820 }
3821
3822
3823 /*
3824 GetReachEnd - returns the value of reachend
3825 */
3826
3827 static pge_TraverseResult GetReachEnd (pge_FollowDesc f)
3828 {
3829 if (f == NULL)
3830 {
3831 Debug_Halt ((const char *) "why is the follow info NIL?", 27, (const char *) "m2/gm2-auto/pge.mod", 19, (const char *) "GetReachEnd", 11, 632);
3832 }
3833 else
3834 {
3835 return f->reachend;
3836 }
3837 ReturnException ("m2/gm2-auto/pge.mod", 1, 7);
3838 __builtin_unreachable ();
3839 }
3840
3841
3842 /*
3843 AssignFollow - assigns the follow set and sets the calcfollow to TRUE.
3844 */
3845
3846 static void AssignFollow (pge_FollowDesc f, pge_SetDesc s)
3847 {
3848 if (f->calcfollow)
3849 {
3850 Debug_Halt ((const char *) "why are we reassigning this follow set?", 39, (const char *) "m2/gm2-auto/pge.mod", 19, (const char *) "AssignFollow", 12, 649);
3851 }
3852 f->follow = s;
3853 f->calcfollow = true;
3854 }
3855
3856
3857 /*
3858 GetFollow - returns the follow set.
3859 */
3860
3861 static pge_SetDesc GetFollow (pge_FollowDesc f)
3862 {
3863 if (f == NULL)
3864 {
3865 Debug_Halt ((const char *) "why is the follow info NIL?", 27, (const char *) "m2/gm2-auto/pge.mod", 19, (const char *) "GetFollow", 9, 666);
3866 }
3867 else
3868 {
3869 if (f->calcfollow)
3870 {
3871 return f->follow;
3872 }
3873 else
3874 {
3875 Debug_Halt ((const char *) "not calculated the follow set yet..", 35, (const char *) "m2/gm2-auto/pge.mod", 19, (const char *) "GetFollow", 9, 674);
3876 }
3877 }
3878 ReturnException ("m2/gm2-auto/pge.mod", 1, 7);
3879 __builtin_unreachable ();
3880 }
3881
3882
3883 /*
3884 NewProduction - creates a new production and returns the data structure.
3885 */
3886
3887 static pge_ProductionDesc NewProduction (void)
3888 {
3889 pge_ProductionDesc p;
3890
3891 Storage_ALLOCATE ((void **) &p, sizeof (pge__T2));
3892 if (TailProduction != NULL)
3893 {
3894 TailProduction->next = p;
3895 }
3896 TailProduction = p;
3897 if (HeadProduction == NULL)
3898 {
3899 HeadProduction = p;
3900 }
3901 p->next = NULL;
3902 p->statement = NULL;
3903 p->first = NULL;
3904 p->firstsolved = false;
3905 p->followinfo = NewFollow ();
3906 p->line = PushBackInput_GetCurrentLine ();
3907 p->description = NameKey_NulName;
3908 return p;
3909 /* static analysis guarentees a RETURN statement will be used before here. */
3910 __builtin_unreachable ();
3911 }
3912
3913
3914 /*
3915 NewFactor -
3916 */
3917
3918 static pge_FactorDesc NewFactor (void)
3919 {
3920 pge_FactorDesc f;
3921
3922 Storage_ALLOCATE ((void **) &f, sizeof (pge__T5));
3923 f->next = NULL;
3924 f->followinfo = NewFollow ();
3925 f->line = PushBackInput_GetCurrentLine ();
3926 return f;
3927 /* static analysis guarentees a RETURN statement will be used before here. */
3928 __builtin_unreachable ();
3929 }
3930
3931
3932 /*
3933 NewTerm - returns a new term.
3934 */
3935
3936 static pge_TermDesc NewTerm (void)
3937 {
3938 pge_TermDesc t;
3939
3940 Storage_ALLOCATE ((void **) &t, sizeof (pge_termdesc));
3941 t->factor = NULL;
3942 t->followinfo = NewFollow ();
3943 t->next = NULL;
3944 t->line = PushBackInput_GetCurrentLine ();
3945 return t;
3946 /* static analysis guarentees a RETURN statement will be used before here. */
3947 __builtin_unreachable ();
3948 }
3949
3950
3951 /*
3952 NewExpression - returns a new expression.
3953 */
3954
3955 static pge_ExpressionDesc NewExpression (void)
3956 {
3957 pge_ExpressionDesc e;
3958
3959 Storage_ALLOCATE ((void **) &e, sizeof (pge__T4));
3960 e->term = NULL;
3961 e->followinfo = NewFollow ();
3962 e->line = PushBackInput_GetCurrentLine ();
3963 return e;
3964 /* static analysis guarentees a RETURN statement will be used before here. */
3965 __builtin_unreachable ();
3966 }
3967
3968
3969 /*
3970 NewStatement - returns a new statement.
3971 */
3972
3973 static pge_StatementDesc NewStatement (void)
3974 {
3975 pge_StatementDesc s;
3976
3977 Storage_ALLOCATE ((void **) &s, sizeof (pge__T3));
3978 s->ident = NULL;
3979 s->expr = NULL;
3980 s->followinfo = NewFollow ();
3981 s->line = PushBackInput_GetCurrentLine ();
3982 return s;
3983 /* static analysis guarentees a RETURN statement will be used before here. */
3984 __builtin_unreachable ();
3985 }
3986
3987
3988 /*
3989 NewSetDesc - creates a new set description and returns the data structure.
3990 */
3991
3992 static pge_SetDesc NewSetDesc (void)
3993 {
3994 pge_SetDesc s;
3995
3996 Storage_ALLOCATE ((void **) &s, sizeof (pge__T7));
3997 s->next = NULL;
3998 return s;
3999 /* static analysis guarentees a RETURN statement will be used before here. */
4000 __builtin_unreachable ();
4001 }
4002
4003
4004 /*
4005 NewCodeDesc - creates a new code descriptor and initializes all fields to zero.
4006 */
4007
4008 static pge_CodeDesc NewCodeDesc (void)
4009 {
4010 pge_CodeDesc c;
4011
4012 Storage_ALLOCATE ((void **) &c, sizeof (pge__T8));
4013 c->code = NULL;
4014 c->indent = 0;
4015 c->line = PushBackInput_GetCurrentLine ();
4016 return c;
4017 /* static analysis guarentees a RETURN statement will be used before here. */
4018 __builtin_unreachable ();
4019 }
4020
4021
4022 /*
4023 CodeFragmentPrologue - consumes code text up to a "%" after a newline.
4024 */
4025
4026 static void CodeFragmentPrologue (void)
4027 {
4028 LinePrologue = PushBackInput_GetCurrentLine ();
4029 GetCodeFragment (&CodePrologue);
4030 }
4031
4032
4033 /*
4034 CodeFragmentEpilogue - consumes code text up to a "%" after a newline.
4035 */
4036
4037 static void CodeFragmentEpilogue (void)
4038 {
4039 LineEpilogue = PushBackInput_GetCurrentLine ();
4040 GetCodeFragment (&CodeEpilogue);
4041 }
4042
4043
4044 /*
4045 CodeFragmentDeclaration - consumes code text up to a "%" after a newline.
4046 */
4047
4048 static void CodeFragmentDeclaration (void)
4049 {
4050 LineDeclaration = PushBackInput_GetCurrentLine ();
4051 GetCodeFragment (&CodeDeclaration);
4052 }
4053
4054
4055 /*
4056 GetCodeFragment - collects the code fragment up until ^ %
4057 */
4058
4059 static void GetCodeFragment (pge_CodeHunk *h)
4060 {
4061 unsigned int i;
4062 char ch;
4063
4064 (*h) = NULL;
4065 i = 0;
4066 while (((bnflex_PutChar (bnflex_GetChar ())) != '%') && ((bnflex_PutChar (bnflex_GetChar ())) != ASCII_nul))
4067 {
4068 do {
4069 while (((bnflex_PutChar (bnflex_GetChar ())) != ASCII_nul) && ((bnflex_PutChar (bnflex_GetChar ())) != ASCII_lf))
4070 {
4071 (*h) = Add (h, bnflex_GetChar (), &i);
4072 }
4073 if ((bnflex_PutChar (bnflex_GetChar ())) == ASCII_lf)
4074 {
4075 /* consume line feed */
4076 (*h) = Add (h, bnflex_GetChar (), &i);
4077 ch = bnflex_PutChar (ASCII_lf);
4078 }
4079 else if ((bnflex_PutChar (bnflex_GetChar ())) == ASCII_nul)
4080 {
4081 /* avoid dangling else. */
4082 ch = bnflex_PutChar (ASCII_nul);
4083 ch = bnflex_PutChar (ASCII_lf);
4084 }
4085 else
4086 {
4087 /* avoid dangling else. */
4088 ch = bnflex_PutChar (bnflex_PutChar (bnflex_GetChar ()));
4089 }
4090 } while (! ((bnflex_GetChar ()) == ASCII_lf));
4091 }
4092 if ((bnflex_PutChar (bnflex_GetChar ())) == '%')
4093 {
4094 (*h) = Add (h, ASCII_nul, &i);
4095 ch = bnflex_PutChar (' '); /* to give the following token % a delimiter infront of it */
4096 bnflex_AdvanceToken (); /* to give the following token % a delimiter infront of it */
4097 }
4098 else
4099 {
4100 PushBackInput_WarnError ((const char *) "expecting % to terminate code fragment, found end of file", 57);
4101 }
4102 }
4103
4104
4105 /*
4106 WriteCodeHunkList - writes the CodeHunk list in the correct order.
4107 */
4108
4109 static void WriteCodeHunkList (pge_CodeHunk l)
4110 {
4111 if (l != NULL)
4112 {
4113 OnLineStart = false;
4114 /* recursion */
4115 WriteCodeHunkList (l->next);
4116 Output_WriteString ((const char *) &l->codetext.array[0], MaxCodeHunkLength);
4117 }
4118 }
4119
4120
4121 /*
4122 WriteIndent - writes, n, spaces.
4123 */
4124
4125 static void WriteIndent (unsigned int n)
4126 {
4127 while (n > 0)
4128 {
4129 Output_Write (' ');
4130 n -= 1;
4131 }
4132 OnLineStart = false;
4133 }
4134
4135
4136 /*
4137 CheckWrite -
4138 */
4139
4140 static void CheckWrite (char ch, unsigned int *curpos, unsigned int left, bool *seentext)
4141 {
4142 if (ch == ASCII_lf)
4143 {
4144 NewLine (left);
4145 (*curpos) = 0;
4146 (*seentext) = false;
4147 }
4148 else
4149 {
4150 Output_Write (ch);
4151 (*curpos) += 1;
4152 }
4153 }
4154
4155
4156 /*
4157 WriteStringIndent - writes a string but it will try and remove upto indent spaces
4158 if they exist.
4159 */
4160
4161 static void WriteStringIndent (const char *a_, unsigned int _a_high, unsigned int indent, unsigned int *curpos, unsigned int left, bool *seentext)
4162 {
4163 unsigned int l;
4164 unsigned int i;
4165 char a[_a_high+1];
4166
4167 /* make a local copy of each unbounded array. */
4168 memcpy (a, a_, _a_high+1);
4169
4170 i = 0;
4171 l = StrLib_StrLen ((const char *) a, _a_high);
4172 while (i < l)
4173 {
4174 if ((*seentext))
4175 {
4176 CheckWrite (a[i], curpos, left, seentext);
4177 }
4178 else
4179 {
4180 if (a[i] == ' ')
4181 {
4182 /* ignore space for now */
4183 (*curpos) += 1;
4184 }
4185 else
4186 {
4187 if ((*curpos) >= indent)
4188 {
4189 WriteIndent ((*curpos)-indent);
4190 }
4191 (*seentext) = true;
4192 CheckWrite (a[i], curpos, left, seentext);
4193 }
4194 }
4195 i += 1;
4196 }
4197 }
4198
4199
4200 /*
4201 WriteCodeHunkListIndent - writes the CodeHunk list in the correct order
4202 but it removes up to indent spaces if they exist.
4203 */
4204
4205 static void WriteCodeHunkListIndent (pge_CodeHunk l, unsigned int indent, unsigned int *curpos, unsigned int left, bool *seentext)
4206 {
4207 if (l != NULL)
4208 {
4209 /* recursion */
4210 WriteCodeHunkListIndent (l->next, indent, curpos, left, seentext);
4211 WriteStringIndent ((const char *) &l->codetext.array[0], MaxCodeHunkLength, indent, curpos, left, seentext);
4212 }
4213 }
4214
4215
4216 /*
4217 Add - adds a character to a code hunk and creates another code hunk if necessary.
4218 */
4219
4220 static pge_CodeHunk Add (pge_CodeHunk *p, char ch, unsigned int *i)
4221 {
4222 pge_CodeHunk q;
4223
4224 if (((*p) == NULL) || ((*i) > MaxCodeHunkLength))
4225 {
4226 Storage_ALLOCATE ((void **) &q, sizeof (pge__T9));
4227 q->next = (*p);
4228 q->codetext.array[0] = ch;
4229 (*i) = 1;
4230 return q;
4231 }
4232 else
4233 {
4234 (*p)->codetext.array[(*i)] = ch;
4235 (*i) += 1;
4236 return (*p);
4237 }
4238 /* static analysis guarentees a RETURN statement will be used before here. */
4239 __builtin_unreachable ();
4240 }
4241
4242
4243 /*
4244 ConsHunk - combine two possible code hunks.
4245 */
4246
4247 static void ConsHunk (pge_CodeHunk *p, pge_CodeHunk q)
4248 {
4249 pge_CodeHunk r;
4250
4251 if ((*p) != NULL)
4252 {
4253 r = q;
4254 while (r->next != NULL)
4255 {
4256 r = r->next;
4257 }
4258 r->next = (*p);
4259 }
4260 (*p) = q;
4261 }
4262
4263
4264 /*
4265 GetName - returns the next symbol which is checked for a legal name.
4266 */
4267
4268 static NameKey_Name GetName (void)
4269 {
4270 NameKey_Name name;
4271
4272 if (bnflex_IsReserved (bnflex_GetCurrentToken ()))
4273 {
4274 PushBackInput_WarnError ((const char *) "expecting a name and found a reserved word", 42);
4275 bnflex_AdvanceToken (); /* move on to another token */
4276 return NameKey_NulName; /* move on to another token */
4277 }
4278 else
4279 {
4280 name = bnflex_GetCurrentToken ();
4281 bnflex_AdvanceToken ();
4282 return name;
4283 }
4284 /* static analysis guarentees a RETURN statement will be used before here. */
4285 __builtin_unreachable ();
4286 }
4287
4288
4289 /*
4290 SyntaxError - after a syntax error we skip all tokens up until we reach
4291 a stop symbol.
4292 */
4293
4294 static void SyntaxError (pge_SetOfStop stop)
4295 {
4296 DescribeError ();
4297 if (Debugging)
4298 {
4299 StrIO_WriteLn ();
4300 StrIO_WriteString ((const char *) "skipping token *** ", 19);
4301 }
4302 while (! ((((1 << (bnflex_GetCurrentTokenType ()-bnflex_identtok)) & (stop)) != 0)))
4303 {
4304 bnflex_AdvanceToken ();
4305 }
4306 if (Debugging)
4307 {
4308 StrIO_WriteString ((const char *) " ***", 4);
4309 StrIO_WriteLn ();
4310 }
4311 WasNoError = false;
4312 }
4313
4314
4315 /*
4316 SyntaxCheck -
4317 */
4318
4319 static void SyntaxCheck (pge_SetOfStop stop)
4320 {
4321 if (! ((((1 << (bnflex_GetCurrentTokenType ()-bnflex_identtok)) & (stop)) != 0)))
4322 {
4323 SyntaxError (stop);
4324 }
4325 }
4326
4327
4328 /*
4329 Expect -
4330 */
4331
4332 static void Expect (bnflex_TokenType t, pge_SetOfStop stop)
4333 {
4334 if ((bnflex_GetCurrentTokenType ()) == t)
4335 {
4336 bnflex_AdvanceToken ();
4337 }
4338 else
4339 {
4340 SyntaxError (stop);
4341 }
4342 SyntaxCheck (stop);
4343 }
4344
4345
4346 /*
4347 Ident - error checking varient of Ident
4348 */
4349
4350 static void Ident (pge_SetOfStop stop)
4351 {
4352 if ((bnflex_GetCurrentTokenType ()) == bnflex_identtok)
4353 {
4354 Storage_ALLOCATE ((void **) &CurrentIdent, sizeof (pge__T1));
4355 CurrentIdent->definition = NULL;
4356 CurrentIdent->name = GetName ();
4357 CurrentIdent->line = PushBackInput_GetCurrentLine ();
4358 }
4359 }
4360
4361
4362 /*
4363 Modula2Code - error checking varient of Modula2Code
4364 */
4365
4366 static void Modula2Code (pge_SetOfStop stop)
4367 {
4368 pge_CodeHunk p;
4369 unsigned int i;
4370 bool quote;
4371 unsigned int line;
4372 unsigned int position;
4373
4374 line = PushBackInput_GetCurrentLine ();
4375 bnflex_PushBackToken (bnflex_GetCurrentToken ());
4376 position = PushBackInput_GetColumnPosition ();
4377 p = NULL;
4378 bnflex_SkipWhite ();
4379 while (((bnflex_PutChar (bnflex_GetChar ())) != '%') && ((bnflex_PutChar (bnflex_GetChar ())) != ASCII_nul))
4380 {
4381 if ((bnflex_PutChar (bnflex_GetChar ())) == '"')
4382 {
4383 /* avoid dangling else. */
4384 do {
4385 p = Add (&p, bnflex_GetChar (), &i);
4386 } while (! (((bnflex_PutChar (bnflex_GetChar ())) == '"') || ((bnflex_PutChar (bnflex_GetChar ())) == ASCII_nul)));
4387 p = Add (&p, '"', &i);
4388 if (((bnflex_PutChar (bnflex_GetChar ())) == '"') && ((bnflex_GetChar ()) == '"'))
4389 {} /* empty. */
4390 }
4391 else if ((bnflex_PutChar (bnflex_GetChar ())) == '\'')
4392 {
4393 /* avoid dangling else. */
4394 do {
4395 p = Add (&p, bnflex_GetChar (), &i);
4396 } while (! (((bnflex_PutChar (bnflex_GetChar ())) == '\'') || ((bnflex_PutChar (bnflex_GetChar ())) == ASCII_nul)));
4397 p = Add (&p, '\'', &i);
4398 if (((bnflex_PutChar (bnflex_GetChar ())) == '\'') && ((bnflex_GetChar ()) == '\''))
4399 {} /* empty. */
4400 }
4401 else if (((bnflex_PutChar (bnflex_GetChar ())) == '\\') && ((bnflex_GetChar ()) == '\\'))
4402 {
4403 /* avoid dangling else. */
4404 p = Add (&p, bnflex_GetChar (), &i);
4405 }
4406 else if ((bnflex_PutChar (bnflex_GetChar ())) != '%')
4407 {
4408 /* avoid dangling else. */
4409 p = Add (&p, bnflex_GetChar (), &i);
4410 }
4411 }
4412 p = Add (&p, ASCII_nul, &i);
4413 CurrentFactor->type = pge_m2;
4414 CurrentFactor->code = NewCodeDesc ();
4415 CurrentFactor->code->code = p;
4416 CurrentFactor->code->indent = position;
4417 if ((bnflex_PutChar (' ')) == ' ')
4418 {} /* empty. */
4419 bnflex_AdvanceToken (); /* read the next token ready for the parser */
4420 if (! WasNoError) /* read the next token ready for the parser */
4421 {
4422 WarnError1 ((const char *) "error probably occurred before the start of inline code on line %d", 66, line);
4423 }
4424 }
4425
4426
4427 /*
4428 StartModName := % ModuleName := GetName() ; ignore begintok CodeFragmentPrologue % =:
4429 */
4430
4431 static void StartModName (pge_SetOfStop stop)
4432 {
4433 ModuleName = GetName ();
4434 CodeFragmentPrologue ();
4435 }
4436
4437
4438 /*
4439 EndModName :=
4440 */
4441
4442 static void EndModName (pge_SetOfStop stop)
4443 {
4444 if (ModuleName != (GetName ()))
4445 {
4446 PushBackInput_WarnError ((const char *) "expecting same module name at end as beginning", 46);
4447 }
4448 /* ignore endtok as it consumes the token afterwards */
4449 CodeFragmentEpilogue ();
4450 }
4451
4452
4453 /*
4454 DoDeclaration := % CodeFragmentDeclaration % =:
4455 */
4456
4457 static void DoDeclaration (pge_SetOfStop stop)
4458 {
4459 if (ModuleName != (GetName ()))
4460 {
4461 PushBackInput_WarnError ((const char *) "expecting same module name in declaration as in the beginning", 61);
4462 }
4463 /* ignore begintok as it consumes the token afterwards */
4464 CodeFragmentDeclaration ();
4465 }
4466
4467
4468 /*
4469 CollectLiteral :=
4470 % LastLiteral := GetCurrentToken() ;
4471 AdvanceToken ; %
4472
4473
4474 first symbols:literaltok
4475
4476 cannot reachend
4477 */
4478
4479 static void CollectLiteral (pge_SetOfStop stopset)
4480 {
4481 LastLiteral = bnflex_GetCurrentToken (); /* */
4482 bnflex_AdvanceToken ();
4483 }
4484
4485
4486 /*
4487 CollectTok :=
4488 % CurrentSetDesc := NewSetDesc() ;
4489 WITH CurrentSetDesc^ DO
4490 type := tokel ;
4491 string := GetCurrentToken() ;
4492 END ;
4493 IF NOT ContainsSymKey(Values, GetCurrentToken())
4494 THEN
4495 AddEntry(Values, GetCurrentToken(), LargestValue) ;
4496 AddEntry(ReverseValues, Name(LargestValue), GetCurrentToken()) ;
4497 AddEntry(Aliases, GetCurrentToken(), GetCurrentToken()) ;
4498 AddEntry(ReverseAliases, GetCurrentToken(), GetCurrentToken()) ;
4499 INC(LargestValue)
4500 END ;
4501 AdvanceToken() ; %
4502
4503
4504 first symbols:identtok
4505
4506 cannot reachend
4507 */
4508
4509 static void CollectTok (pge_SetOfStop stopset)
4510 {
4511 CurrentSetDesc = NewSetDesc (); /* */
4512 CurrentSetDesc->type = pge_tokel;
4513 CurrentSetDesc->string = bnflex_GetCurrentToken ();
4514 if (! (SymbolKey_ContainsSymKey (Values, bnflex_GetCurrentToken ())))
4515 {
4516 AddEntry (&Values, bnflex_GetCurrentToken (), LargestValue);
4517 AddEntry (&ReverseValues, (NameKey_Name) (LargestValue), bnflex_GetCurrentToken ());
4518 AddEntry (&Aliases, bnflex_GetCurrentToken (), bnflex_GetCurrentToken ());
4519 AddEntry (&ReverseAliases, bnflex_GetCurrentToken (), bnflex_GetCurrentToken ());
4520 LargestValue += 1;
4521 }
4522 bnflex_AdvanceToken ();
4523 }
4524
4525
4526 /*
4527 DefineToken :=
4528 % AddEntry(Aliases, LastLiteral, GetCurrentToken()) ;
4529 AddEntry(ReverseAliases, GetCurrentToken(), LastLiteral) ;
4530 AddEntry(Values, GetCurrentToken(), LargestValue) ;
4531 AddEntry(ReverseValues, Name(LargestValue), GetCurrentToken()) ;
4532 INC(LargestValue) ;
4533 AdvanceToken ; %
4534
4535
4536 first symbols:identtok
4537
4538 cannot reachend
4539 */
4540
4541 static void DefineToken (pge_SetOfStop stopset)
4542 {
4543 AddEntry (&Aliases, LastLiteral, bnflex_GetCurrentToken ()); /* */
4544 AddEntry (&ReverseAliases, bnflex_GetCurrentToken (), LastLiteral);
4545 AddEntry (&Values, bnflex_GetCurrentToken (), LargestValue);
4546 AddEntry (&ReverseValues, (NameKey_Name) (LargestValue), bnflex_GetCurrentToken ());
4547 LargestValue += 1;
4548 bnflex_AdvanceToken ();
4549 }
4550
4551
4552 /*
4553 Rules := '%' 'rules' { Defs } ExtBNF
4554
4555 first symbols:codetok
4556
4557 cannot reachend
4558 */
4559
4560 static void Rules (pge_SetOfStop stopset)
4561 {
4562 Expect (bnflex_codetok, stopset|(pge_SetOfStop) ((1 << (bnflex_rulestok-bnflex_identtok))));
4563 Expect (bnflex_rulestok, stopset|(pge_SetOfStop) ((1 << (bnflex_symfunctok-bnflex_identtok)) | (1 << (bnflex_tfunctok-bnflex_identtok)) | (1 << (bnflex_errortok-bnflex_identtok)) | (1 << (bnflex_tokentok-bnflex_identtok)) | (1 << (bnflex_specialtok-bnflex_identtok)) | (1 << (bnflex_BNFtok-bnflex_identtok))));
4564 while ((((1 << (bnflex_GetCurrentTokenType ())) & ((unsigned int) ((1 << (bnflex_specialtok)) | (1 << (bnflex_tokentok)) | (1 << (bnflex_errortok)) | (1 << (bnflex_tfunctok)) | (1 << (bnflex_symfunctok))))) != 0))
4565 {
4566 Defs (stopset|(pge_SetOfStop) ((1 << (bnflex_BNFtok-bnflex_identtok)) | (1 << (bnflex_specialtok-bnflex_identtok)) | (1 << (bnflex_tokentok-bnflex_identtok)) | (1 << (bnflex_errortok-bnflex_identtok)) | (1 << (bnflex_tfunctok-bnflex_identtok)) | (1 << (bnflex_symfunctok-bnflex_identtok))));
4567 }
4568 /* while */
4569 ExtBNF (stopset);
4570 }
4571
4572
4573 /*
4574 Special := Ident
4575 % VAR p: ProductionDesc ; %
4576
4577 % p := NewProduction() ;
4578 p^.statement := NewStatement() ;
4579 p^.statement^.followinfo^.calcfollow := TRUE ;
4580 p^.statement^.followinfo^.epsilon := false ;
4581 p^.statement^.followinfo^.reachend := false ;
4582 p^.statement^.ident := CurrentIdent ;
4583 p^.statement^.expr := NIL ;
4584 p^.firstsolved := TRUE ;
4585 p^.followinfo^.calcfollow := TRUE ;
4586 p^.followinfo^.epsilon := false ;
4587 p^.followinfo^.reachend := false %
4588 First Follow [ 'epsilon'
4589 % p^.statement^.followinfo^.epsilon := true ; these are not used - but they are displayed when debugging
4590 p^.statement^.followinfo^.reachend := true ;
4591 p^.followinfo^.epsilon := true ;
4592 p^.followinfo^.reachend := true
4593 %
4594 ] [ Literal
4595 % p^.description := LastLiteral %
4596 ]
4597
4598 first symbols:identtok
4599
4600 cannot reachend
4601 */
4602
4603 static void Special (pge_SetOfStop stopset)
4604 {
4605 pge_ProductionDesc p;
4606
4607 Ident (stopset|(pge_SetOfStop) ((1 << (bnflex_firsttok-bnflex_identtok))));
4608 p = NewProduction ();
4609 p->statement = NewStatement ();
4610 p->statement->followinfo->calcfollow = true;
4611 p->statement->followinfo->epsilon = pge_false;
4612 p->statement->followinfo->reachend = pge_false;
4613 p->statement->ident = CurrentIdent;
4614 p->statement->expr = NULL;
4615 p->firstsolved = true;
4616 p->followinfo->calcfollow = true;
4617 p->followinfo->epsilon = pge_false;
4618 p->followinfo->reachend = pge_false;
4619 First (stopset|(pge_SetOfStop) ((1 << (bnflex_followtok-bnflex_identtok))));
4620 Follow (stopset|(pge_SetOfStop) ((1 << (bnflex_epsilontok-bnflex_identtok)) | (1 << (bnflex_squotetok-bnflex_identtok)) | (1 << (bnflex_dquotetok-bnflex_identtok))));
4621 if ((bnflex_GetCurrentTokenType ()) == bnflex_epsilontok)
4622 {
4623 Expect (bnflex_epsilontok, stopset|(pge_SetOfStop) ((1 << (bnflex_dquotetok-bnflex_identtok)) | (1 << (bnflex_squotetok-bnflex_identtok))));
4624 p->statement->followinfo->epsilon = pge_true; /* these are not used - but they are displayed when debugging */
4625 p->statement->followinfo->reachend = pge_true; /* these are not used - but they are displayed when debugging */
4626 p->followinfo->epsilon = pge_true;
4627 p->followinfo->reachend = pge_true;
4628 }
4629 if ((((1 << (bnflex_GetCurrentTokenType ())) & ((unsigned int) ((1 << (bnflex_dquotetok)) | (1 << (bnflex_squotetok))))) != 0))
4630 {
4631 Literal (stopset);
4632 p->description = LastLiteral;
4633 }
4634 }
4635
4636
4637 /*
4638 Factor := '%' Modula2Code '%' |
4639 Ident
4640 % WITH CurrentFactor^ DO
4641 type := id ;
4642 ident := CurrentIdent
4643 END ; %
4644 | Literal
4645 % WITH CurrentFactor^ DO
4646 type := lit ;
4647 string := LastLiteral ;
4648 IF GetSymKey(Aliases, LastLiteral)=NulName
4649 THEN
4650 WarnError1('no token defined for literal %s', LastLiteral)
4651 END
4652 END ; %
4653 | '{'
4654 % WITH CurrentFactor^ DO
4655 type := mult ;
4656 expr := NewExpression() ;
4657 CurrentExpression := expr ;
4658 END ; %
4659 Expression '}' | '['
4660 % WITH CurrentFactor^ DO
4661 type := opt ;
4662 expr := NewExpression() ;
4663 CurrentExpression := expr ;
4664 END ; %
4665 Expression ']' | '('
4666 % WITH CurrentFactor^ DO
4667 type := sub ;
4668 expr := NewExpression() ;
4669 CurrentExpression := expr ;
4670 END ; %
4671 Expression ')'
4672
4673 first symbols:dquotetok, squotetok, lparatok, lsparatok, lcparatok, identtok, codetok
4674
4675 cannot reachend
4676 */
4677
4678 static void Factor (pge_SetOfStop stopset)
4679 {
4680 if ((bnflex_GetCurrentTokenType ()) == bnflex_codetok)
4681 {
4682 Expect (bnflex_codetok, stopset);
4683 Modula2Code (stopset|(pge_SetOfStop) ((1 << (bnflex_codetok-bnflex_identtok))));
4684 Expect (bnflex_codetok, stopset);
4685 }
4686 else if ((bnflex_GetCurrentTokenType ()) == bnflex_identtok)
4687 {
4688 /* avoid dangling else. */
4689 Ident (stopset);
4690 CurrentFactor->type = pge_id;
4691 CurrentFactor->ident = CurrentIdent;
4692 }
4693 else if ((((1 << (bnflex_GetCurrentTokenType ())) & ((unsigned int) ((1 << (bnflex_dquotetok)) | (1 << (bnflex_squotetok))))) != 0))
4694 {
4695 /* avoid dangling else. */
4696 Literal (stopset);
4697 CurrentFactor->type = pge_lit;
4698 CurrentFactor->string = LastLiteral;
4699 if ((SymbolKey_GetSymKey (Aliases, LastLiteral)) == NameKey_NulName)
4700 {
4701 WarnError1 ((const char *) "no token defined for literal %s", 31, LastLiteral);
4702 }
4703 }
4704 else if ((bnflex_GetCurrentTokenType ()) == bnflex_lcparatok)
4705 {
4706 /* avoid dangling else. */
4707 Expect (bnflex_lcparatok, stopset|(pge_SetOfStop) ((1 << (bnflex_lparatok-bnflex_identtok)) | (1 << (bnflex_lsparatok-bnflex_identtok)) | (1 << (bnflex_lcparatok-bnflex_identtok)) | (1 << (bnflex_identtok-bnflex_identtok)) | (1 << (bnflex_codetok-bnflex_identtok)) | (1 << (bnflex_dquotetok-bnflex_identtok)) | (1 << (bnflex_squotetok-bnflex_identtok))));
4708 CurrentFactor->type = pge_mult;
4709 CurrentFactor->expr = NewExpression ();
4710 CurrentExpression = CurrentFactor->expr;
4711 Expression (stopset|(pge_SetOfStop) ((1 << (bnflex_rcparatok-bnflex_identtok))));
4712 Expect (bnflex_rcparatok, stopset);
4713 }
4714 else if ((bnflex_GetCurrentTokenType ()) == bnflex_lsparatok)
4715 {
4716 /* avoid dangling else. */
4717 Expect (bnflex_lsparatok, stopset|(pge_SetOfStop) ((1 << (bnflex_lparatok-bnflex_identtok)) | (1 << (bnflex_lsparatok-bnflex_identtok)) | (1 << (bnflex_lcparatok-bnflex_identtok)) | (1 << (bnflex_identtok-bnflex_identtok)) | (1 << (bnflex_codetok-bnflex_identtok)) | (1 << (bnflex_dquotetok-bnflex_identtok)) | (1 << (bnflex_squotetok-bnflex_identtok))));
4718 CurrentFactor->type = pge_opt;
4719 CurrentFactor->expr = NewExpression ();
4720 CurrentExpression = CurrentFactor->expr;
4721 Expression (stopset|(pge_SetOfStop) ((1 << (bnflex_rsparatok-bnflex_identtok))));
4722 Expect (bnflex_rsparatok, stopset);
4723 }
4724 else if ((bnflex_GetCurrentTokenType ()) == bnflex_lparatok)
4725 {
4726 /* avoid dangling else. */
4727 Expect (bnflex_lparatok, stopset|(pge_SetOfStop) ((1 << (bnflex_lparatok-bnflex_identtok)) | (1 << (bnflex_lsparatok-bnflex_identtok)) | (1 << (bnflex_lcparatok-bnflex_identtok)) | (1 << (bnflex_identtok-bnflex_identtok)) | (1 << (bnflex_codetok-bnflex_identtok)) | (1 << (bnflex_dquotetok-bnflex_identtok)) | (1 << (bnflex_squotetok-bnflex_identtok))));
4728 CurrentFactor->type = pge_sub;
4729 CurrentFactor->expr = NewExpression ();
4730 CurrentExpression = CurrentFactor->expr;
4731 Expression (stopset|(pge_SetOfStop) ((1 << (bnflex_rparatok-bnflex_identtok))));
4732 Expect (bnflex_rparatok, stopset);
4733 }
4734 else
4735 {
4736 /* avoid dangling else. */
4737 PushBackInput_WarnError ((const char *) "expecting one of: ( [ { \" single quote identifier %", 51);
4738 }
4739 }
4740
4741
4742 /*
4743 Statement :=
4744 % VAR i: IdentDesc ; %
4745 Ident
4746 % VAR p: ProductionDesc ; %
4747
4748 % p := FindDefinition(CurrentIdent^.name) ;
4749 IF p=NIL
4750 THEN
4751 p := NewProduction()
4752 ELSE
4753 IF NOT ((p^.statement=NIL) OR (p^.statement^.expr=NIL))
4754 THEN
4755 WarnError1('already declared rule %s', CurrentIdent^.name)
4756 END
4757 END ;
4758 i := CurrentIdent ; %
4759 ':='
4760 % VAR e: ExpressionDesc ; %
4761
4762 % e := NewExpression() ;
4763 CurrentExpression := e ; %
4764
4765 % VAR s: StatementDesc ; %
4766
4767 % s := NewStatement() ;
4768 WITH s^ DO
4769 ident := i ;
4770 expr := e
4771 END ; %
4772 Expression
4773 % p^.statement := s ; %
4774 '=:'
4775
4776 first symbols:identtok
4777
4778 cannot reachend
4779 */
4780
4781 static void Statement (pge_SetOfStop stopset)
4782 {
4783 pge_IdentDesc i;
4784 pge_ProductionDesc p;
4785 pge_ExpressionDesc e;
4786 pge_StatementDesc s;
4787
4788 Ident (stopset|(pge_SetOfStop) ((1 << (bnflex_lbecomestok-bnflex_identtok))));
4789 p = FindDefinition (CurrentIdent->name);
4790 if (p == NULL)
4791 {
4792 p = NewProduction ();
4793 }
4794 else
4795 {
4796 if (! ((p->statement == NULL) || (p->statement->expr == NULL)))
4797 {
4798 WarnError1 ((const char *) "already declared rule %s", 24, CurrentIdent->name);
4799 }
4800 }
4801 i = CurrentIdent;
4802 Expect (bnflex_lbecomestok, stopset|(pge_SetOfStop) ((1 << (bnflex_lparatok-bnflex_identtok)) | (1 << (bnflex_lsparatok-bnflex_identtok)) | (1 << (bnflex_lcparatok-bnflex_identtok)) | (1 << (bnflex_identtok-bnflex_identtok)) | (1 << (bnflex_codetok-bnflex_identtok)) | (1 << (bnflex_dquotetok-bnflex_identtok)) | (1 << (bnflex_squotetok-bnflex_identtok))));
4803 e = NewExpression ();
4804 CurrentExpression = e;
4805 s = NewStatement ();
4806 s->ident = i;
4807 s->expr = e;
4808 Expression (stopset|(pge_SetOfStop) ((1 << (bnflex_rbecomestok-bnflex_identtok))));
4809 p->statement = s;
4810 Expect (bnflex_rbecomestok, stopset);
4811 }
4812
4813
4814 /*
4815 Defs := 'special' Special | 'token' Token |
4816 'error' ErrorProcedures |
4817 'tokenfunc' TokenProcedure |
4818 'symfunc' SymProcedure
4819
4820 first symbols:symfunctok, tfunctok, errortok, tokentok, specialtok
4821
4822 cannot reachend
4823 */
4824
4825 static void Defs (pge_SetOfStop stopset)
4826 {
4827 if ((bnflex_GetCurrentTokenType ()) == bnflex_specialtok)
4828 {
4829 Expect (bnflex_specialtok, stopset|(pge_SetOfStop) ((1 << (bnflex_identtok-bnflex_identtok))));
4830 Special (stopset);
4831 }
4832 else if ((bnflex_GetCurrentTokenType ()) == bnflex_tokentok)
4833 {
4834 /* avoid dangling else. */
4835 Expect (bnflex_tokentok, stopset|(pge_SetOfStop) ((1 << (bnflex_dquotetok-bnflex_identtok)) | (1 << (bnflex_squotetok-bnflex_identtok))));
4836 Token (stopset);
4837 }
4838 else if ((bnflex_GetCurrentTokenType ()) == bnflex_errortok)
4839 {
4840 /* avoid dangling else. */
4841 Expect (bnflex_errortok, stopset|(pge_SetOfStop) ((1 << (bnflex_dquotetok-bnflex_identtok)) | (1 << (bnflex_squotetok-bnflex_identtok))));
4842 ErrorProcedures (stopset);
4843 }
4844 else if ((bnflex_GetCurrentTokenType ()) == bnflex_tfunctok)
4845 {
4846 /* avoid dangling else. */
4847 Expect (bnflex_tfunctok, stopset|(pge_SetOfStop) ((1 << (bnflex_dquotetok-bnflex_identtok)) | (1 << (bnflex_squotetok-bnflex_identtok))));
4848 TokenProcedure (stopset);
4849 }
4850 else if ((bnflex_GetCurrentTokenType ()) == bnflex_symfunctok)
4851 {
4852 /* avoid dangling else. */
4853 Expect (bnflex_symfunctok, stopset|(pge_SetOfStop) ((1 << (bnflex_dquotetok-bnflex_identtok)) | (1 << (bnflex_squotetok-bnflex_identtok))));
4854 SymProcedure (stopset);
4855 }
4856 else
4857 {
4858 /* avoid dangling else. */
4859 PushBackInput_WarnError ((const char *) "expecting one of: symfunc tokenfunc error token special", 55);
4860 }
4861 }
4862
4863
4864 /*
4865 ExtBNF := 'BNF' { Production } 'FNB'
4866
4867 first symbols:BNFtok
4868
4869 cannot reachend
4870 */
4871
4872 static void ExtBNF (pge_SetOfStop stopset)
4873 {
4874 Expect (bnflex_BNFtok, stopset|(pge_SetOfStop) ((1 << (bnflex_FNBtok-bnflex_identtok)) | (1 << (bnflex_identtok-bnflex_identtok))));
4875 while ((bnflex_GetCurrentTokenType ()) == bnflex_identtok)
4876 {
4877 Production (stopset|(pge_SetOfStop) ((1 << (bnflex_FNBtok-bnflex_identtok)) | (1 << (bnflex_identtok-bnflex_identtok))));
4878 }
4879 /* while */
4880 Expect (bnflex_FNBtok, stopset);
4881 }
4882
4883
4884 /*
4885 Main := Header Decls Footer Rules
4886
4887 first symbols:codetok
4888
4889 cannot reachend
4890 */
4891
4892 static void Main (pge_SetOfStop stopset)
4893 {
4894 Header (stopset|(pge_SetOfStop) ((1 << (bnflex_codetok-bnflex_identtok))));
4895 Decls (stopset|(pge_SetOfStop) ((1 << (bnflex_codetok-bnflex_identtok))));
4896 Footer (stopset|(pge_SetOfStop) ((1 << (bnflex_codetok-bnflex_identtok))));
4897 Rules (stopset);
4898 }
4899
4900
4901 /*
4902 Header := '%' 'module' StartModName
4903
4904 first symbols:codetok
4905
4906 cannot reachend
4907 */
4908
4909 static void Header (pge_SetOfStop stopset)
4910 {
4911 Expect (bnflex_codetok, stopset|(pge_SetOfStop) ((1 << (bnflex_moduletok-bnflex_identtok))));
4912 Expect (bnflex_moduletok, stopset|(pge_SetOfStop) ((1 << (bnflex_identtok-bnflex_identtok))));
4913 StartModName (stopset);
4914 }
4915
4916
4917 /*
4918 Decls := '%' 'declaration' DoDeclaration
4919
4920 first symbols:codetok
4921
4922 cannot reachend
4923 */
4924
4925 static void Decls (pge_SetOfStop stopset)
4926 {
4927 Expect (bnflex_codetok, stopset|(pge_SetOfStop) ((1 << (bnflex_declarationtok-bnflex_identtok))));
4928 Expect (bnflex_declarationtok, stopset|(pge_SetOfStop) ((1 << (bnflex_identtok-bnflex_identtok))));
4929 DoDeclaration (stopset);
4930 }
4931
4932
4933 /*
4934 Footer := '%' 'module' EndModName
4935
4936 first symbols:codetok
4937
4938 cannot reachend
4939 */
4940
4941 static void Footer (pge_SetOfStop stopset)
4942 {
4943 Expect (bnflex_codetok, stopset|(pge_SetOfStop) ((1 << (bnflex_moduletok-bnflex_identtok))));
4944 Expect (bnflex_moduletok, stopset|(pge_SetOfStop) ((1 << (bnflex_identtok-bnflex_identtok))));
4945 EndModName (stopset);
4946 }
4947
4948
4949 /*
4950 First := 'first' '{' { LitOrTokenOrIdent
4951 % WITH CurrentSetDesc^ DO
4952 next := TailProduction^.first ;
4953 END ;
4954 TailProduction^.first := CurrentSetDesc
4955 %
4956 } '}'
4957
4958 first symbols:firsttok
4959
4960 cannot reachend
4961 */
4962
4963 static void First (pge_SetOfStop stopset)
4964 {
4965 Expect (bnflex_firsttok, stopset|(pge_SetOfStop) ((1 << (bnflex_lcparatok-bnflex_identtok))));
4966 Expect (bnflex_lcparatok, stopset|(pge_SetOfStop) ((1 << (bnflex_rcparatok-bnflex_identtok)) | (1 << (bnflex_identtok-bnflex_identtok)) | (1 << (bnflex_lesstok-bnflex_identtok)) | (1 << (bnflex_dquotetok-bnflex_identtok)) | (1 << (bnflex_squotetok-bnflex_identtok))));
4967 while ((((1 << (bnflex_GetCurrentTokenType ())) & ((unsigned int) ((1 << (bnflex_lesstok)) | (1 << (bnflex_identtok)) | (1 << (bnflex_squotetok)) | (1 << (bnflex_dquotetok))))) != 0))
4968 {
4969 LitOrTokenOrIdent (stopset|(pge_SetOfStop) ((1 << (bnflex_rcparatok-bnflex_identtok)) | (1 << (bnflex_lesstok-bnflex_identtok)) | (1 << (bnflex_identtok-bnflex_identtok)) | (1 << (bnflex_squotetok-bnflex_identtok)) | (1 << (bnflex_dquotetok-bnflex_identtok))));
4970 CurrentSetDesc->next = TailProduction->first;
4971 TailProduction->first = CurrentSetDesc;
4972 }
4973 /* while */
4974 Expect (bnflex_rcparatok, stopset);
4975 }
4976
4977
4978 /*
4979 Follow := 'follow' '{' { LitOrTokenOrIdent
4980 % WITH CurrentSetDesc^ DO
4981 next := TailProduction^.followinfo^.follow ;
4982 END ;
4983 TailProduction^.followinfo^.follow := CurrentSetDesc
4984 %
4985 } '}'
4986
4987 first symbols:followtok
4988
4989 cannot reachend
4990 */
4991
4992 static void Follow (pge_SetOfStop stopset)
4993 {
4994 Expect (bnflex_followtok, stopset|(pge_SetOfStop) ((1 << (bnflex_lcparatok-bnflex_identtok))));
4995 Expect (bnflex_lcparatok, stopset|(pge_SetOfStop) ((1 << (bnflex_rcparatok-bnflex_identtok)) | (1 << (bnflex_identtok-bnflex_identtok)) | (1 << (bnflex_lesstok-bnflex_identtok)) | (1 << (bnflex_dquotetok-bnflex_identtok)) | (1 << (bnflex_squotetok-bnflex_identtok))));
4996 while ((((1 << (bnflex_GetCurrentTokenType ())) & ((unsigned int) ((1 << (bnflex_lesstok)) | (1 << (bnflex_identtok)) | (1 << (bnflex_squotetok)) | (1 << (bnflex_dquotetok))))) != 0))
4997 {
4998 LitOrTokenOrIdent (stopset|(pge_SetOfStop) ((1 << (bnflex_rcparatok-bnflex_identtok)) | (1 << (bnflex_lesstok-bnflex_identtok)) | (1 << (bnflex_identtok-bnflex_identtok)) | (1 << (bnflex_squotetok-bnflex_identtok)) | (1 << (bnflex_dquotetok-bnflex_identtok))));
4999 CurrentSetDesc->next = TailProduction->followinfo->follow;
5000 TailProduction->followinfo->follow = CurrentSetDesc;
5001 }
5002 /* while */
5003 Expect (bnflex_rcparatok, stopset);
5004 }
5005
5006
5007 /*
5008 LitOrTokenOrIdent := Literal
5009 % CurrentSetDesc := NewSetDesc() ;
5010 WITH CurrentSetDesc^ DO
5011 type := litel ;
5012 string := LastLiteral ;
5013 END ;
5014 %
5015 | '<' CollectTok '>' |
5016 Ident
5017 % CurrentSetDesc := NewSetDesc() ;
5018 WITH CurrentSetDesc^ DO
5019 type := idel ;
5020 ident := CurrentIdent ;
5021 END ;
5022 %
5023
5024
5025 first symbols:dquotetok, squotetok, identtok, lesstok
5026
5027 cannot reachend
5028 */
5029
5030 static void LitOrTokenOrIdent (pge_SetOfStop stopset)
5031 {
5032 if ((((1 << (bnflex_GetCurrentTokenType ())) & ((unsigned int) ((1 << (bnflex_dquotetok)) | (1 << (bnflex_squotetok))))) != 0))
5033 {
5034 Literal (stopset);
5035 CurrentSetDesc = NewSetDesc ();
5036 CurrentSetDesc->type = pge_litel;
5037 CurrentSetDesc->string = LastLiteral;
5038 }
5039 else if ((bnflex_GetCurrentTokenType ()) == bnflex_lesstok)
5040 {
5041 /* avoid dangling else. */
5042 Expect (bnflex_lesstok, stopset|(pge_SetOfStop) ((1 << (bnflex_identtok-bnflex_identtok))));
5043 CollectTok (stopset|(pge_SetOfStop) ((1 << (bnflex_gretok-bnflex_identtok))));
5044 Expect (bnflex_gretok, stopset);
5045 }
5046 else if ((bnflex_GetCurrentTokenType ()) == bnflex_identtok)
5047 {
5048 /* avoid dangling else. */
5049 Ident (stopset);
5050 CurrentSetDesc = NewSetDesc ();
5051 CurrentSetDesc->type = pge_idel;
5052 CurrentSetDesc->ident = CurrentIdent;
5053 }
5054 else
5055 {
5056 /* avoid dangling else. */
5057 PushBackInput_WarnError ((const char *) "expecting one of: identifier < \" single quote", 45);
5058 }
5059 }
5060
5061
5062 /*
5063 Literal := '"' CollectLiteral '"' |
5064 "'" CollectLiteral "'"
5065
5066 first symbols:squotetok, dquotetok
5067
5068 cannot reachend
5069 */
5070
5071 static void Literal (pge_SetOfStop stopset)
5072 {
5073 if ((bnflex_GetCurrentTokenType ()) == bnflex_dquotetok)
5074 {
5075 Expect (bnflex_dquotetok, stopset|(pge_SetOfStop) ((1 << (bnflex_literaltok-bnflex_identtok))));
5076 CollectLiteral (stopset|(pge_SetOfStop) ((1 << (bnflex_dquotetok-bnflex_identtok))));
5077 Expect (bnflex_dquotetok, stopset);
5078 }
5079 else if ((bnflex_GetCurrentTokenType ()) == bnflex_squotetok)
5080 {
5081 /* avoid dangling else. */
5082 Expect (bnflex_squotetok, stopset|(pge_SetOfStop) ((1 << (bnflex_literaltok-bnflex_identtok))));
5083 CollectLiteral (stopset|(pge_SetOfStop) ((1 << (bnflex_squotetok-bnflex_identtok))));
5084 Expect (bnflex_squotetok, stopset);
5085 }
5086 else
5087 {
5088 /* avoid dangling else. */
5089 PushBackInput_WarnError ((const char *) "expecting one of: single quote \"", 32);
5090 }
5091 }
5092
5093
5094 /*
5095 Token := Literal DefineToken
5096
5097 first symbols:dquotetok, squotetok
5098
5099 cannot reachend
5100 */
5101
5102 static void Token (pge_SetOfStop stopset)
5103 {
5104 Literal (stopset|(pge_SetOfStop) ((1 << (bnflex_identtok-bnflex_identtok))));
5105 DefineToken (stopset);
5106 }
5107
5108
5109 /*
5110 ErrorProcedures := Literal
5111 % ErrorProcArray := LastLiteral %
5112 Literal
5113 % ErrorProcString := LastLiteral %
5114
5115
5116 first symbols:dquotetok, squotetok
5117
5118 cannot reachend
5119 */
5120
5121 static void ErrorProcedures (pge_SetOfStop stopset)
5122 {
5123 Literal (stopset|(pge_SetOfStop) ((1 << (bnflex_squotetok-bnflex_identtok)) | (1 << (bnflex_dquotetok-bnflex_identtok))));
5124 ErrorProcArray = LastLiteral;
5125 Literal (stopset);
5126 ErrorProcString = LastLiteral;
5127 }
5128
5129
5130 /*
5131 TokenProcedure := Literal
5132 % TokenTypeProc := LastLiteral %
5133
5134
5135 first symbols:dquotetok, squotetok
5136
5137 cannot reachend
5138 */
5139
5140 static void TokenProcedure (pge_SetOfStop stopset)
5141 {
5142 Literal (stopset);
5143 TokenTypeProc = LastLiteral;
5144 }
5145
5146
5147 /*
5148 SymProcedure := Literal
5149 % SymIsProc := LastLiteral %
5150
5151
5152 first symbols:dquotetok, squotetok
5153
5154 cannot reachend
5155 */
5156
5157 static void SymProcedure (pge_SetOfStop stopset)
5158 {
5159 Literal (stopset);
5160 SymIsProc = LastLiteral;
5161 }
5162
5163
5164 /*
5165 Production := Statement
5166
5167 first symbols:identtok
5168
5169 cannot reachend
5170 */
5171
5172 static void Production (pge_SetOfStop stopset)
5173 {
5174 Statement (stopset);
5175 }
5176
5177
5178 /*
5179 Expression :=
5180 % VAR t1, t2: TermDesc ;
5181 e : ExpressionDesc ; %
5182
5183 % e := CurrentExpression ;
5184 t1 := NewTerm() ;
5185 CurrentTerm := t1 ; %
5186 Term
5187 % e^.term := t1 ; %
5188 { '|'
5189 % t2 := NewTerm() ;
5190 CurrentTerm := t2 %
5191 Term
5192 % t1^.next := t2 ;
5193 t1 := t2 %
5194 }
5195
5196 first symbols:dquotetok, squotetok, lparatok, lsparatok, lcparatok, identtok, codetok
5197
5198 cannot reachend
5199 */
5200
5201 static void Expression (pge_SetOfStop stopset)
5202 {
5203 pge_TermDesc t1;
5204 pge_TermDesc t2;
5205 pge_ExpressionDesc e;
5206
5207 e = CurrentExpression;
5208 t1 = NewTerm ();
5209 CurrentTerm = t1;
5210 Term (stopset|(pge_SetOfStop) ((1 << (bnflex_bartok-bnflex_identtok))));
5211 e->term = t1;
5212 while ((bnflex_GetCurrentTokenType ()) == bnflex_bartok)
5213 {
5214 Expect (bnflex_bartok, stopset|(pge_SetOfStop) ((1 << (bnflex_codetok-bnflex_identtok)) | (1 << (bnflex_identtok-bnflex_identtok)) | (1 << (bnflex_lcparatok-bnflex_identtok)) | (1 << (bnflex_lsparatok-bnflex_identtok)) | (1 << (bnflex_lparatok-bnflex_identtok)) | (1 << (bnflex_squotetok-bnflex_identtok)) | (1 << (bnflex_dquotetok-bnflex_identtok))));
5215 t2 = NewTerm ();
5216 CurrentTerm = t2;
5217 Term (stopset|(pge_SetOfStop) ((1 << (bnflex_bartok-bnflex_identtok))));
5218 t1->next = t2;
5219 t1 = t2;
5220 }
5221 /* while */
5222 }
5223
5224
5225 /*
5226 Term :=
5227 % VAR t1: TermDesc ; f1, f2: FactorDesc ; %
5228
5229 % CurrentFactor := NewFactor() ;
5230 f1 := CurrentFactor ;
5231 t1 := CurrentTerm ; %
5232 Factor
5233 % t1^.factor := f1 ;
5234 f2 := NewFactor() ;
5235 CurrentFactor := f2 %
5236 { Factor
5237 % f1^.next := f2 ;
5238 f1 := f2 ;
5239 f2 := NewFactor() ;
5240 CurrentFactor := f2 ; %
5241 }
5242
5243 first symbols:squotetok, dquotetok, codetok, identtok, lcparatok, lsparatok, lparatok
5244
5245 cannot reachend
5246 */
5247
5248 static void Term (pge_SetOfStop stopset)
5249 {
5250 pge_TermDesc t1;
5251 pge_FactorDesc f1;
5252 pge_FactorDesc f2;
5253
5254 CurrentFactor = NewFactor ();
5255 f1 = CurrentFactor;
5256 t1 = CurrentTerm;
5257 Factor (stopset|(pge_SetOfStop) ((1 << (bnflex_lparatok-bnflex_identtok)) | (1 << (bnflex_lsparatok-bnflex_identtok)) | (1 << (bnflex_lcparatok-bnflex_identtok)) | (1 << (bnflex_identtok-bnflex_identtok)) | (1 << (bnflex_codetok-bnflex_identtok)) | (1 << (bnflex_dquotetok-bnflex_identtok)) | (1 << (bnflex_squotetok-bnflex_identtok))));
5258 t1->factor = f1;
5259 f2 = NewFactor ();
5260 CurrentFactor = f2;
5261 while ((((1 << (bnflex_GetCurrentTokenType ())) & ((unsigned int) ((1 << (bnflex_codetok)) | (1 << (bnflex_identtok)) | (1 << (bnflex_lcparatok)) | (1 << (bnflex_lsparatok)) | (1 << (bnflex_lparatok)) | (1 << (bnflex_squotetok)) | (1 << (bnflex_dquotetok))))) != 0))
5262 {
5263 Factor (stopset|(pge_SetOfStop) ((1 << (bnflex_codetok-bnflex_identtok)) | (1 << (bnflex_identtok-bnflex_identtok)) | (1 << (bnflex_lcparatok-bnflex_identtok)) | (1 << (bnflex_lsparatok-bnflex_identtok)) | (1 << (bnflex_lparatok-bnflex_identtok)) | (1 << (bnflex_squotetok-bnflex_identtok)) | (1 << (bnflex_dquotetok-bnflex_identtok))));
5264 f1->next = f2;
5265 f1 = f2;
5266 f2 = NewFactor ();
5267 CurrentFactor = f2;
5268 }
5269 /* while */
5270 }
5271
5272
5273 /*
5274 GetDefinitionName - returns the name of the rule inside, p.
5275 */
5276
5277 static NameKey_Name GetDefinitionName (pge_ProductionDesc p)
5278 {
5279 if (p != NULL)
5280 {
5281 if ((p->statement != NULL) && (p->statement->ident != NULL))
5282 {
5283 return p->statement->ident->name;
5284 }
5285 }
5286 return NameKey_NulName;
5287 /* static analysis guarentees a RETURN statement will be used before here. */
5288 __builtin_unreachable ();
5289 }
5290
5291
5292 /*
5293 FindDefinition - searches and returns the rule which defines, n.
5294 */
5295
5296 static pge_ProductionDesc FindDefinition (NameKey_Name n)
5297 {
5298 pge_ProductionDesc p;
5299 pge_ProductionDesc f;
5300
5301 p = HeadProduction;
5302 f = NULL;
5303 while (p != NULL)
5304 {
5305 if ((GetDefinitionName (p)) == n)
5306 {
5307 /* avoid gcc warning by using compound statement even if not strictly necessary. */
5308 if (f == NULL)
5309 {
5310 f = p;
5311 }
5312 else
5313 {
5314 StrIO_WriteString ((const char *) "multiple definition for rule: ", 30);
5315 NameKey_WriteKey (n);
5316 StrIO_WriteLn ();
5317 }
5318 }
5319 p = p->next;
5320 }
5321 return f;
5322 /* static analysis guarentees a RETURN statement will be used before here. */
5323 __builtin_unreachable ();
5324 }
5325
5326
5327 /*
5328 BackPatchIdent - found an ident, i, we must look for the corresponding rule and
5329 set the definition accordingly.
5330 */
5331
5332 static void BackPatchIdent (pge_IdentDesc i)
5333 {
5334 if (i != NULL)
5335 {
5336 i->definition = FindDefinition (i->name);
5337 if (i->definition == NULL)
5338 {
5339 WarnError1 ((const char *) "unable to find production %s", 28, i->name);
5340 WasNoError = false;
5341 }
5342 }
5343 }
5344
5345
5346 /*
5347 BackPatchFactor - runs through the factor looking for an ident
5348 */
5349
5350 static void BackPatchFactor (pge_FactorDesc f)
5351 {
5352 while (f != NULL)
5353 {
5354 switch (f->type)
5355 {
5356 case pge_id:
5357 BackPatchIdent (f->ident);
5358 break;
5359
5360 case pge_sub:
5361 case pge_opt:
5362 case pge_mult:
5363 BackPatchExpression (f->expr);
5364 break;
5365
5366
5367 default:
5368 break;
5369 }
5370 f = f->next;
5371 }
5372 }
5373
5374
5375 /*
5376 BackPatchTerm - runs through all terms to find idents.
5377 */
5378
5379 static void BackPatchTerm (pge_TermDesc t)
5380 {
5381 while (t != NULL)
5382 {
5383 BackPatchFactor (t->factor);
5384 t = t->next;
5385 }
5386 }
5387
5388
5389 /*
5390 BackPatchExpression - runs through the term to find any idents.
5391 */
5392
5393 static void BackPatchExpression (pge_ExpressionDesc e)
5394 {
5395 if (e != NULL)
5396 {
5397 BackPatchTerm (e->term);
5398 }
5399 }
5400
5401
5402 /*
5403 BackPatchSet -
5404 */
5405
5406 static void BackPatchSet (pge_SetDesc s)
5407 {
5408 while (s != NULL)
5409 {
5410 switch (s->type)
5411 {
5412 case pge_idel:
5413 BackPatchIdent (s->ident);
5414 break;
5415
5416
5417 default:
5418 break;
5419 }
5420 s = s->next;
5421 }
5422 }
5423
5424
5425 /*
5426 BackPatchIdentToDefinitions - search through all the rules and add a link from any ident
5427 to the definition.
5428 */
5429
5430 static void BackPatchIdentToDefinitions (pge_ProductionDesc d)
5431 {
5432 if ((d != NULL) && (d->statement != NULL))
5433 {
5434 BackPatchExpression (d->statement->expr);
5435 }
5436 }
5437
5438
5439 /*
5440 CalculateFirstAndFollow -
5441 */
5442
5443 static void CalculateFirstAndFollow (pge_ProductionDesc p)
5444 {
5445 if (Debugging)
5446 {
5447 StrIO_WriteLn ();
5448 NameKey_WriteKey (p->statement->ident->name);
5449 StrIO_WriteLn ();
5450 StrIO_WriteString ((const char *) " calculating first", 19);
5451 }
5452 CalcFirstProduction (p, p, &p->first);
5453 BackPatchSet (p->first);
5454 if (Debugging)
5455 {
5456 StrIO_WriteString ((const char *) " calculating follow set", 24);
5457 }
5458 if (p->followinfo->follow == NULL)
5459 {
5460 CalcFollowProduction (p);
5461 }
5462 BackPatchSet (p->followinfo->follow);
5463 }
5464
5465
5466 /*
5467 ForeachRuleDo -
5468 */
5469
5470 static void ForeachRuleDo (pge_DoProcedure p)
5471 {
5472 CurrentProduction = HeadProduction;
5473 while (CurrentProduction != NULL)
5474 {
5475 (*p.proc) (CurrentProduction);
5476 CurrentProduction = CurrentProduction->next;
5477 }
5478 }
5479
5480
5481 /*
5482 WhileNotCompleteDo -
5483 */
5484
5485 static void WhileNotCompleteDo (pge_DoProcedure p)
5486 {
5487 do {
5488 Finished = true;
5489 ForeachRuleDo (p);
5490 } while (! (Finished));
5491 }
5492
5493
5494 /*
5495 NewLine - generate a newline and indent.
5496 */
5497
5498 static void NewLine (unsigned int Left)
5499 {
5500 Output_WriteLn ();
5501 BeginningOfLine = true;
5502 Indent = 0;
5503 while (Indent < Left)
5504 {
5505 Output_Write (' ');
5506 Indent += 1;
5507 }
5508 }
5509
5510
5511 /*
5512 CheckNewLine -
5513 */
5514
5515 static void CheckNewLine (unsigned int Left)
5516 {
5517 if (Indent == Left)
5518 {
5519 Left = BaseNewLine;
5520 }
5521 if (Indent > BaseRightMargin)
5522 {
5523 NewLine (Left);
5524 }
5525 }
5526
5527
5528 /*
5529 IndentString - writes out a string with a preceeding indent.
5530 */
5531
5532 static void IndentString (const char *a_, unsigned int _a_high)
5533 {
5534 unsigned int i;
5535 char a[_a_high+1];
5536
5537 /* make a local copy of each unbounded array. */
5538 memcpy (a, a_, _a_high+1);
5539
5540 i = 0;
5541 while (i < Indent)
5542 {
5543 Output_Write (' ');
5544 i += 1;
5545 }
5546 Output_WriteString ((const char *) a, _a_high);
5547 LastLineNo = 0;
5548 }
5549
5550
5551 /*
5552 KeyWord - writes out a keywork with optional formatting directives.
5553 */
5554
5555 static void KeyWord (NameKey_Name n)
5556 {
5557 if (KeywordFormatting)
5558 {
5559 Output_WriteString ((const char *) "{%K", 3);
5560 if (((n == (NameKey_MakeKey ((const char *) "}", 1))) || (n == (NameKey_MakeKey ((const char *) "{", 1)))) || (n == (NameKey_MakeKey ((const char *) "%", 1))))
5561 {
5562 Output_Write ('%'); /* escape }, { or % */
5563 }
5564 Output_WriteKey (n);
5565 Output_Write ('}');
5566 }
5567 else
5568 {
5569 Output_WriteKey (n);
5570 }
5571 }
5572
5573
5574 /*
5575 PrettyPara -
5576 */
5577
5578 static void PrettyPara (const char *c1_, unsigned int _c1_high, const char *c2_, unsigned int _c2_high, pge_ExpressionDesc e, unsigned int Left)
5579 {
5580 char c1[_c1_high+1];
5581 char c2[_c2_high+1];
5582
5583 /* make a local copy of each unbounded array. */
5584 memcpy (c1, c1_, _c1_high+1);
5585 memcpy (c2, c2_, _c2_high+1);
5586
5587 Output_WriteString ((const char *) c1, _c1_high);
5588 Indent += StrLib_StrLen ((const char *) c1, _c1_high);
5589 Left = Indent;
5590 PrettyCommentExpression (e, Left);
5591 Output_WriteString ((const char *) c2, _c2_high);
5592 Indent += StrLib_StrLen ((const char *) c2, _c2_high);
5593 }
5594
5595
5596 /*
5597 WriteKeyTexinfo -
5598 */
5599
5600 static void WriteKeyTexinfo (NameKey_Name s)
5601 {
5602 DynamicStrings_String ds;
5603 char ch;
5604 unsigned int i;
5605 unsigned int l;
5606
5607 if (Texinfo)
5608 {
5609 ds = DynamicStrings_InitStringCharStar (NameKey_KeyToCharStar (s));
5610 l = DynamicStrings_Length (ds);
5611 i = 0;
5612 while (i < l)
5613 {
5614 ch = DynamicStrings_char (ds, static_cast<int> (i));
5615 if ((ch == '{') || (ch == '}'))
5616 {
5617 Output_Write ('@');
5618 }
5619 Output_Write (ch);
5620 i += 1;
5621 }
5622 }
5623 else
5624 {
5625 Output_WriteKey (s);
5626 }
5627 }
5628
5629
5630 /*
5631 PrettyCommentFactor -
5632 */
5633
5634 static void PrettyCommentFactor (pge_FactorDesc f, unsigned int Left)
5635 {
5636 unsigned int curpos;
5637 bool seentext;
5638
5639 while (f != NULL)
5640 {
5641 CheckNewLine (Left);
5642 switch (f->type)
5643 {
5644 case pge_id:
5645 Output_WriteKey (f->ident->name);
5646 Output_WriteString ((const char *) " ", 1);
5647 Indent += (NameKey_LengthKey (f->ident->name))+1;
5648 break;
5649
5650 case pge_lit:
5651 if ((NameKey_MakeKey ((const char *) "'", 1)) == f->string)
5652 {
5653 Output_Write ('"');
5654 WriteKeyTexinfo (f->string);
5655 Output_WriteString ((const char *) "\" ", 2);
5656 }
5657 else
5658 {
5659 Output_Write ('\'');
5660 WriteKeyTexinfo (f->string);
5661 Output_WriteString ((const char *) "' ", 2);
5662 }
5663 Indent += (NameKey_LengthKey (f->string))+3;
5664 break;
5665
5666 case pge_sub:
5667 PrettyPara ((const char *) "( ", 2, (const char *) " ) ", 3, f->expr, Left);
5668 break;
5669
5670 case pge_opt:
5671 PrettyPara ((const char *) "[ ", 2, (const char *) " ] ", 3, f->expr, Left);
5672 break;
5673
5674 case pge_mult:
5675 if (Texinfo)
5676 {
5677 PrettyPara ((const char *) "@{ ", 3, (const char *) " @} ", 4, f->expr, Left);
5678 }
5679 else
5680 {
5681 PrettyPara ((const char *) "{ ", 2, (const char *) " } ", 3, f->expr, Left);
5682 }
5683 break;
5684
5685 case pge_m2:
5686 if (EmitCode)
5687 {
5688 NewLine (Left);
5689 Output_WriteString ((const char *) "% ", 2);
5690 seentext = false;
5691 curpos = 0;
5692 WriteCodeHunkListIndent (f->code->code, f->code->indent, &curpos, Left+2, &seentext);
5693 Output_WriteString ((const char *) " %", 2);
5694 NewLine (Left);
5695 }
5696 break;
5697
5698
5699 default:
5700 break;
5701 }
5702 PrettyFollow ((const char *) "<f:", 3, (const char *) ":f>", 3, f->followinfo);
5703 f = f->next;
5704 }
5705 }
5706
5707
5708 /*
5709 PeepTerm - returns the length of characters in term.
5710 */
5711
5712 static unsigned int PeepTerm (pge_TermDesc t)
5713 {
5714 unsigned int l;
5715
5716 l = 0;
5717 while (t != NULL)
5718 {
5719 l += PeepFactor (t->factor);
5720 if (t->next != NULL)
5721 {
5722 l += 3;
5723 }
5724 t = t->next;
5725 }
5726 return l;
5727 /* static analysis guarentees a RETURN statement will be used before here. */
5728 __builtin_unreachable ();
5729 }
5730
5731
5732 /*
5733 PeepExpression - returns the length of the expression.
5734 */
5735
5736 static unsigned int PeepExpression (pge_ExpressionDesc e)
5737 {
5738 if (e == NULL)
5739 {
5740 return 0;
5741 }
5742 else
5743 {
5744 return PeepTerm (e->term);
5745 }
5746 /* static analysis guarentees a RETURN statement will be used before here. */
5747 __builtin_unreachable ();
5748 }
5749
5750
5751 /*
5752 PeepFactor - returns the length of character in the factor
5753 */
5754
5755 static unsigned int PeepFactor (pge_FactorDesc f)
5756 {
5757 unsigned int l;
5758
5759 l = 0;
5760 while (f != NULL)
5761 {
5762 switch (f->type)
5763 {
5764 case pge_id:
5765 l += (NameKey_LengthKey (f->ident->name))+1;
5766 break;
5767
5768 case pge_lit:
5769 l += (NameKey_LengthKey (f->string))+3;
5770 break;
5771
5772 case pge_opt:
5773 case pge_mult:
5774 case pge_sub:
5775 l += PeepExpression (f->expr);
5776 break;
5777
5778 case pge_m2:
5779 break;
5780
5781
5782 default:
5783 break;
5784 }
5785 f = f->next; /* empty */
5786 }
5787 return l;
5788 /* static analysis guarentees a RETURN statement will be used before here. */
5789 __builtin_unreachable ();
5790 }
5791
5792
5793 /*
5794 PrettyCommentTerm -
5795 */
5796
5797 static void PrettyCommentTerm (pge_TermDesc t, unsigned int Left)
5798 {
5799 while (t != NULL)
5800 {
5801 CheckNewLine (Left);
5802 PrettyCommentFactor (t->factor, Left);
5803 if (t->next != NULL)
5804 {
5805 Output_WriteString ((const char *) " | ", 3);
5806 Indent += 3;
5807 if (((PeepFactor (t->factor))+Indent) > BaseRightMargin)
5808 {
5809 NewLine (Left);
5810 }
5811 }
5812 PrettyFollow ((const char *) "<t:", 3, (const char *) ":t>", 3, t->followinfo);
5813 t = t->next;
5814 }
5815 }
5816
5817
5818 /*
5819 PrettyCommentExpression -
5820 */
5821
5822 static void PrettyCommentExpression (pge_ExpressionDesc e, unsigned int Left)
5823 {
5824 if (e != NULL)
5825 {
5826 PrettyCommentTerm (e->term, Left);
5827 PrettyFollow ((const char *) "<e:", 3, (const char *) ":e>", 3, e->followinfo);
5828 }
5829 }
5830
5831
5832 /*
5833 PrettyCommentStatement -
5834 */
5835
5836 static void PrettyCommentStatement (pge_StatementDesc s, unsigned int Left)
5837 {
5838 if (s != NULL)
5839 {
5840 PrettyCommentExpression (s->expr, Left);
5841 PrettyFollow ((const char *) "<s:", 3, (const char *) ":s>", 3, s->followinfo);
5842 }
5843 }
5844
5845
5846 /*
5847 PrettyCommentProduction - generates the comment for rule, p.
5848 */
5849
5850 static void PrettyCommentProduction (pge_ProductionDesc p)
5851 {
5852 pge_SetDesc to;
5853
5854 if (p != NULL)
5855 {
5856 BeginningOfLine = true;
5857 Indent = 0;
5858 Output_WriteString ((const char *) "(*", 2);
5859 NewLine (3);
5860 Output_WriteKey (GetDefinitionName (p));
5861 Output_WriteString ((const char *) " := ", 4);
5862 Indent += (NameKey_LengthKey (GetDefinitionName (p)))+4;
5863 PrettyCommentStatement (p->statement, Indent);
5864 NewLine (0);
5865 if (ErrorRecovery)
5866 {
5867 NewLine (3);
5868 Output_WriteString ((const char *) "first symbols:", 15);
5869 EmitSet (p->first, static_cast<unsigned int> (0), static_cast<unsigned int> (0));
5870 NewLine (3);
5871 PrettyFollow ((const char *) "<p:", 3, (const char *) ":p>", 3, p->followinfo);
5872 NewLine (3);
5873 switch (GetReachEnd (p->followinfo))
5874 {
5875 case pge_true:
5876 Output_WriteString ((const char *) "reachend", 8);
5877 break;
5878
5879 case pge_false:
5880 Output_WriteString ((const char *) "cannot reachend", 15);
5881 break;
5882
5883 case pge_unknown:
5884 Output_WriteString ((const char *) "unknown...", 10);
5885 break;
5886
5887
5888 default:
5889 break;
5890 }
5891 NewLine (0);
5892 }
5893 Output_WriteString ((const char *) "*)", 2);
5894 NewLine (0);
5895 }
5896 }
5897
5898
5899 /*
5900 PrettyPrintProduction - pretty prints the ebnf rule, p.
5901 */
5902
5903 static void PrettyPrintProduction (pge_ProductionDesc p)
5904 {
5905 pge_SetDesc to;
5906
5907 if (p != NULL)
5908 {
5909 BeginningOfLine = true;
5910 Indent = 0;
5911 if (Texinfo)
5912 {
5913 Output_WriteString ((const char *) "@example", 8);
5914 NewLine (0);
5915 }
5916 else if (Sphinx)
5917 {
5918 /* avoid dangling else. */
5919 Output_WriteString ((const char *) ".. code-block:: ebnf", 20);
5920 NewLine (0);
5921 }
5922 Output_WriteKey (GetDefinitionName (p));
5923 Output_WriteString ((const char *) " := ", 4);
5924 Indent += (NameKey_LengthKey (GetDefinitionName (p)))+4;
5925 PrettyCommentStatement (p->statement, Indent);
5926 if (p->description != NameKey_NulName)
5927 {
5928 Output_WriteKey (p->description);
5929 }
5930 NewLine (0);
5931 WriteIndent ((NameKey_LengthKey (GetDefinitionName (p)))+1);
5932 Output_WriteString ((const char *) " =: ", 4);
5933 NewLine (0);
5934 if (Texinfo)
5935 {
5936 Output_WriteString ((const char *) "@findex ", 8);
5937 Output_WriteKey (GetDefinitionName (p));
5938 Output_WriteString ((const char *) " (ebnf)", 7);
5939 NewLine (0);
5940 Output_WriteString ((const char *) "@end example", 12);
5941 NewLine (0);
5942 }
5943 else if (Sphinx)
5944 {
5945 /* avoid dangling else. */
5946 Output_WriteString ((const char *) ".. index::", 10);
5947 NewLine (0);
5948 Output_WriteString ((const char *) " pair: ", 8);
5949 Output_WriteKey (GetDefinitionName (p));
5950 Output_WriteString ((const char *) "; (ebnf)", 8);
5951 NewLine (0);
5952 }
5953 NewLine (0);
5954 }
5955 }
5956
5957
5958 /*
5959 EmitFileLineTag - emits a line and file tag using the C preprocessor syntax.
5960 */
5961
5962 static void EmitFileLineTag (unsigned int line)
5963 {
5964 if (! SuppressFileLineTag && (line != LastLineNo))
5965 {
5966 LastLineNo = line;
5967 if (! OnLineStart)
5968 {
5969 Output_WriteLn ();
5970 }
5971 Output_WriteString ((const char *) "# ", 2);
5972 Output_WriteCard (line, 0);
5973 Output_WriteString ((const char *) " \"", 2);
5974 Output_WriteString ((const char *) &FileName.array[0], MaxFileName);
5975 Output_Write ('"');
5976 Output_WriteLn ();
5977 OnLineStart = true;
5978 }
5979 }
5980
5981
5982 /*
5983 EmitRule - generates a comment and code for rule, p.
5984 */
5985
5986 static void EmitRule (pge_ProductionDesc p)
5987 {
5988 if (PrettyPrint)
5989 {
5990 PrettyPrintProduction (p);
5991 }
5992 else
5993 {
5994 PrettyCommentProduction (p);
5995 if (ErrorRecovery)
5996 {
5997 RecoverProduction (p);
5998 }
5999 else
6000 {
6001 CodeProduction (p);
6002 }
6003 }
6004 }
6005
6006
6007 /*
6008 CodeCondition -
6009 */
6010
6011 static void CodeCondition (pge_m2condition m)
6012 {
6013 switch (m)
6014 {
6015 case pge_m2if:
6016 case pge_m2none:
6017 IndentString ((const char *) "IF ", 3);
6018 break;
6019
6020 case pge_m2elsif:
6021 IndentString ((const char *) "ELSIF ", 6);
6022 break;
6023
6024 case pge_m2while:
6025 IndentString ((const char *) "WHILE ", 6);
6026 break;
6027
6028
6029 default:
6030 Debug_Halt ((const char *) "unrecognised m2condition", 24, (const char *) "m2/gm2-auto/pge.mod", 19, (const char *) "CodeCondition", 13, 2686);
6031 break;
6032 }
6033 }
6034
6035
6036 /*
6037 CodeThenDo - codes a "THEN" or "DO" depending upon, m.
6038 */
6039
6040 static void CodeThenDo (pge_m2condition m)
6041 {
6042 switch (m)
6043 {
6044 case pge_m2if:
6045 case pge_m2none:
6046 case pge_m2elsif:
6047 if (LastLineNo == 0)
6048 {
6049 Output_WriteLn ();
6050 }
6051 IndentString ((const char *) "THEN", 4);
6052 Output_WriteLn ();
6053 break;
6054
6055 case pge_m2while:
6056 Output_WriteString ((const char *) " DO", 3);
6057 Output_WriteLn ();
6058 break;
6059
6060
6061 default:
6062 Debug_Halt ((const char *) "unrecognised m2condition", 24, (const char *) "m2/gm2-auto/pge.mod", 19, (const char *) "CodeThenDo", 10, 2712);
6063 break;
6064 }
6065 OnLineStart = true;
6066 }
6067
6068
6069 /*
6070 CodeElseEnd - builds an ELSE END statement using string, end.
6071 */
6072
6073 static void CodeElseEnd (const char *end_, unsigned int _end_high, bool consumed, pge_FactorDesc f, bool inopt)
6074 {
6075 char end[_end_high+1];
6076
6077 /* make a local copy of each unbounded array. */
6078 memcpy (end, end_, _end_high+1);
6079
6080 Output_WriteLn ();
6081 OnLineStart = true;
6082 EmitFileLineTag (f->line);
6083 if (! inopt)
6084 {
6085 IndentString ((const char *) "ELSE", 4);
6086 StrIO_WriteLn ();
6087 Indent += 3;
6088 if (consumed)
6089 {
6090 IndentString ((const char *) "", 0);
6091 Output_WriteKey (ErrorProcArray);
6092 Output_Write ('(');
6093 switch (f->type)
6094 {
6095 case pge_id:
6096 Output_Write ('\'');
6097 Output_WriteKey (f->ident->name);
6098 Output_WriteString ((const char *) " - expected", 11);
6099 Output_WriteString ((const char *) "') ;", 4);
6100 break;
6101
6102 case pge_lit:
6103 if ((NameKey_MakeKey ((const char *) "'", 1)) == f->string)
6104 {
6105 Output_Write ('"');
6106 KeyWord (f->string);
6107 Output_WriteString ((const char *) " - expected", 11);
6108 Output_WriteString ((const char *) "\") ;", 4);
6109 }
6110 else if ((NameKey_MakeKey ((const char *) "\"", 1)) == f->string)
6111 {
6112 /* avoid dangling else. */
6113 Output_Write ('\'');
6114 KeyWord (f->string);
6115 Output_WriteString ((const char *) " - expected", 11);
6116 Output_WriteString ((const char *) "') ;", 4);
6117 }
6118 else
6119 {
6120 /* avoid dangling else. */
6121 Output_Write ('"');
6122 Output_Write ('\'');
6123 KeyWord (f->string);
6124 Output_WriteString ((const char *) "' - expected", 12);
6125 Output_WriteString ((const char *) "\") ;", 4);
6126 }
6127 break;
6128
6129
6130 default:
6131 break;
6132 }
6133 Output_WriteLn ();
6134 }
6135 IndentString ((const char *) "RETURN( FALSE )", 15);
6136 Indent -= 3;
6137 Output_WriteLn ();
6138 }
6139 IndentString ((const char *) end, _end_high);
6140 Output_WriteLn ();
6141 OnLineStart = true;
6142 }
6143
6144
6145 /*
6146 CodeEnd - codes a "END" depending upon, m.
6147 */
6148
6149 static void CodeEnd (pge_m2condition m, pge_TermDesc t, bool consumed, pge_FactorDesc f, bool inopt)
6150 {
6151 Indent -= 3;
6152 Output_WriteLn ();
6153 OnLineStart = true;
6154 switch (m)
6155 {
6156 case pge_m2none:
6157 if (t == NULL)
6158 {
6159 CodeElseEnd ((const char *) "END ;", 5, consumed, f, inopt);
6160 }
6161 break;
6162
6163 case pge_m2if:
6164 if (t == NULL)
6165 {
6166 CodeElseEnd ((const char *) "END ; (* if *)", 15, consumed, f, inopt);
6167 }
6168 break;
6169
6170 case pge_m2elsif:
6171 if (t == NULL)
6172 {
6173 CodeElseEnd ((const char *) "END ; (* elsif *)", 18, consumed, f, inopt);
6174 }
6175 break;
6176
6177 case pge_m2while:
6178 IndentString ((const char *) "END ; (* while *)", 18);
6179 break;
6180
6181
6182 default:
6183 Debug_Halt ((const char *) "unrecognised m2condition", 24, (const char *) "m2/gm2-auto/pge.mod", 19, (const char *) "CodeEnd", 7, 2796);
6184 break;
6185 }
6186 OnLineStart = false;
6187 }
6188
6189
6190 /*
6191 EmitNonVarCode - writes out, code, providing it is not a variable declaration.
6192 */
6193
6194 static void EmitNonVarCode (pge_CodeDesc code, unsigned int curpos, unsigned int left)
6195 {
6196 unsigned int i;
6197 pge_CodeHunk t;
6198 bool seentext;
6199
6200 t = code->code;
6201 if ((! (FindStr (&t, &i, (const char *) "VAR", 3))) && EmitCode)
6202 {
6203 seentext = false;
6204 curpos = 0;
6205 EmitFileLineTag (code->line);
6206 IndentString ((const char *) "", 0);
6207 WriteCodeHunkListIndent (code->code, code->indent, &curpos, left, &seentext);
6208 Output_WriteString ((const char *) " ;", 2);
6209 Output_WriteLn ();
6210 OnLineStart = true;
6211 }
6212 }
6213
6214
6215 /*
6216 ChainOn -
6217 */
6218
6219 static pge_FactorDesc ChainOn (pge_FactorDesc codeStack, pge_FactorDesc f)
6220 {
6221 pge_FactorDesc s;
6222
6223 f->pushed = NULL;
6224 if (codeStack == NULL)
6225 {
6226 return f;
6227 }
6228 else
6229 {
6230 s = codeStack;
6231 while (s->pushed != NULL)
6232 {
6233 s = s->pushed;
6234 }
6235 s->pushed = f;
6236 return codeStack;
6237 }
6238 /* static analysis guarentees a RETURN statement will be used before here. */
6239 __builtin_unreachable ();
6240 }
6241
6242
6243 /*
6244 FlushCode -
6245 */
6246
6247 static void FlushCode (pge_FactorDesc *codeStack)
6248 {
6249 if ((*codeStack) != NULL)
6250 {
6251 NewLine (Indent);
6252 Output_WriteString ((const char *) "(* begin flushing code *)", 25);
6253 OnLineStart = false;
6254 while ((*codeStack) != NULL)
6255 {
6256 NewLine (Indent);
6257 EmitNonVarCode ((*codeStack)->code, 0, Indent);
6258 NewLine (Indent);
6259 (*codeStack) = (*codeStack)->pushed;
6260 if ((*codeStack) != NULL)
6261 {
6262 Output_WriteString ((const char *) " (* again flushing code *)", 26);
6263 Output_WriteLn ();
6264 OnLineStart = true;
6265 }
6266 }
6267 NewLine (Indent);
6268 Output_WriteString ((const char *) "(* end flushing code *)", 23);
6269 OnLineStart = false;
6270 }
6271 }
6272
6273
6274 /*
6275 CodeFactor -
6276 */
6277
6278 static void CodeFactor (pge_FactorDesc f, pge_TermDesc t, pge_m2condition l, pge_m2condition n, bool inopt, bool inwhile, bool consumed, pge_FactorDesc codeStack)
6279 {
6280 if (f == NULL)
6281 {
6282 /* avoid dangling else. */
6283 if (! inwhile && ! inopt) /* ((l=m2elsif) OR (l=m2if) OR (l=m2none)) AND */
6284 {
6285 Output_WriteLn ();
6286 IndentString ((const char *) "RETURN( TRUE )", 14);
6287 OnLineStart = false;
6288 }
6289 }
6290 else
6291 {
6292 EmitFileLineTag (f->line);
6293 switch (f->type)
6294 {
6295 case pge_id:
6296 FlushCode (&codeStack);
6297 CodeCondition (n);
6298 Output_WriteKey (f->ident->name);
6299 Output_WriteString ((const char *) "()", 2);
6300 CodeThenDo (n);
6301 Indent += 3;
6302 CodeFactor (f->next, NULL, n, pge_m2none, inopt, inwhile, true, NULL);
6303 CodeEnd (n, t, consumed, f, inopt);
6304 break;
6305
6306 case pge_lit:
6307 FlushCode (&codeStack);
6308 CodeCondition (n);
6309 Output_WriteKey (SymIsProc);
6310 Output_Write ('(');
6311 Output_WriteKey (SymbolKey_GetSymKey (Aliases, f->string));
6312 Output_Write (')');
6313 CodeThenDo (n);
6314 Indent += 3;
6315 CodeFactor (f->next, NULL, n, pge_m2none, inopt, inwhile, true, NULL);
6316 CodeEnd (n, t, consumed, f, inopt);
6317 break;
6318
6319 case pge_sub:
6320 FlushCode (&codeStack);
6321 CodeExpression (f->expr, pge_m2none, inopt, inwhile, consumed, NULL);
6322 if (f->next != NULL)
6323 {
6324 /*
6325 * the test above makes sure that we don't emit a RETURN( TRUE )
6326 * after a subexpression. Remember sub expressions are not conditional
6327 */
6328 CodeFactor (f->next, t, n, pge_m2none, inopt, inwhile, true, NULL);
6329 }
6330 break;
6331
6332 case pge_opt:
6333 FlushCode (&codeStack);
6334 CodeExpression (f->expr, pge_m2if, true, inwhile, false, NULL);
6335 CodeFactor (f->next, t, n, pge_m2none, inopt, inwhile, consumed, NULL);
6336 break;
6337
6338 case pge_mult:
6339 FlushCode (&codeStack);
6340 CodeExpression (f->expr, pge_m2while, false, true, consumed, NULL);
6341 CodeFactor (f->next, t, n, pge_m2none, inopt, inwhile, consumed, NULL);
6342 break;
6343
6344 case pge_m2:
6345 codeStack = ChainOn (codeStack, f);
6346 if (consumed || (f->next == NULL))
6347 {
6348 FlushCode (&codeStack);
6349 }
6350 CodeFactor (f->next, t, n, pge_m2none, inopt, inwhile, consumed, codeStack);
6351 break;
6352
6353
6354 default:
6355 break;
6356 }
6357 }
6358 }
6359
6360
6361 /*
6362 CodeTerm -
6363 */
6364
6365 static void CodeTerm (pge_TermDesc t, pge_m2condition m, bool inopt, bool inwhile, bool consumed, pge_FactorDesc codeStack)
6366 {
6367 pge_m2condition l;
6368
6369 l = m;
6370 while (t != NULL)
6371 {
6372 EmitFileLineTag (t->line);
6373 if ((t->factor->type == pge_m2) && (m == pge_m2elsif))
6374 {
6375 m = pge_m2if;
6376 IndentString ((const char *) "ELSE", 4);
6377 Output_WriteLn ();
6378 OnLineStart = true;
6379 Indent += 3;
6380 CodeFactor (t->factor, t->next, pge_m2none, pge_m2none, inopt, inwhile, consumed, codeStack);
6381 Indent -= 3;
6382 IndentString ((const char *) "END ;", 5);
6383 Output_WriteLn ();
6384 OnLineStart = true;
6385 }
6386 else
6387 {
6388 CodeFactor (t->factor, t->next, pge_m2none, m, inopt, inwhile, consumed, codeStack);
6389 }
6390 l = m;
6391 if (t->next != NULL)
6392 {
6393 m = pge_m2elsif;
6394 }
6395 t = t->next;
6396 }
6397 }
6398
6399
6400 /*
6401 CodeExpression -
6402 */
6403
6404 static void CodeExpression (pge_ExpressionDesc e, pge_m2condition m, bool inopt, bool inwhile, bool consumed, pge_FactorDesc codeStack)
6405 {
6406 if (e != NULL)
6407 {
6408 EmitFileLineTag (e->line);
6409 CodeTerm (e->term, m, inopt, inwhile, consumed, codeStack);
6410 }
6411 }
6412
6413
6414 /*
6415 CodeStatement -
6416 */
6417
6418 static void CodeStatement (pge_StatementDesc s, pge_m2condition m)
6419 {
6420 if (s != NULL)
6421 {
6422 EmitFileLineTag (s->line);
6423 CodeExpression (s->expr, m, false, false, false, NULL);
6424 }
6425 }
6426
6427
6428 /*
6429 CodeProduction - only encode grammer rules which are not special.
6430 */
6431
6432 static void CodeProduction (pge_ProductionDesc p)
6433 {
6434 if ((p != NULL) && (! p->firstsolved || ((p->statement != NULL) && (p->statement->expr != NULL))))
6435 {
6436 BeginningOfLine = true;
6437 Indent = 0;
6438 Output_WriteLn ();
6439 EmitFileLineTag (p->line);
6440 IndentString ((const char *) "PROCEDURE ", 10);
6441 Output_WriteKey (GetDefinitionName (p));
6442 Output_WriteString ((const char *) " () : BOOLEAN ;", 15);
6443 VarProduction (p);
6444 Output_WriteLn ();
6445 OnLineStart = true;
6446 EmitFileLineTag (p->line);
6447 IndentString ((const char *) "BEGIN", 5);
6448 StrIO_WriteLn ();
6449 OnLineStart = false;
6450 EmitFileLineTag (p->line);
6451 Indent = 3;
6452 CodeStatement (p->statement, pge_m2none);
6453 Output_WriteLn ();
6454 Indent = 0;
6455 IndentString ((const char *) "END ", 4);
6456 NameKey_WriteKey (GetDefinitionName (p));
6457 Output_WriteString ((const char *) " ;", 2);
6458 Output_WriteLn ();
6459 Output_WriteLn ();
6460 Output_WriteLn ();
6461 }
6462 }
6463
6464
6465 /*
6466 RecoverCondition -
6467 */
6468
6469 static void RecoverCondition (pge_m2condition m)
6470 {
6471 switch (m)
6472 {
6473 case pge_m2if:
6474 IndentString ((const char *) "IF ", 3);
6475 break;
6476
6477 case pge_m2none:
6478 IndentString ((const char *) "IF ", 3);
6479 break;
6480
6481 case pge_m2elsif:
6482 IndentString ((const char *) "ELSIF ", 6);
6483 break;
6484
6485 case pge_m2while:
6486 IndentString ((const char *) "WHILE ", 6);
6487 break;
6488
6489
6490 default:
6491 Debug_Halt ((const char *) "unrecognised m2condition", 24, (const char *) "m2/gm2-auto/pge.mod", 19, (const char *) "RecoverCondition", 16, 3054);
6492 break;
6493 }
6494 }
6495
6496
6497 /*
6498 ConditionIndent - returns the number of spaces indentation created via, m.
6499 */
6500
6501 static unsigned int ConditionIndent (pge_m2condition m)
6502 {
6503 switch (m)
6504 {
6505 case pge_m2if:
6506 return 3;
6507 break;
6508
6509 case pge_m2none:
6510 return 3;
6511 break;
6512
6513 case pge_m2elsif:
6514 return 6;
6515 break;
6516
6517 case pge_m2while:
6518 return 6;
6519 break;
6520
6521
6522 default:
6523 Debug_Halt ((const char *) "unrecognised m2condition", 24, (const char *) "m2/gm2-auto/pge.mod", 19, (const char *) "ConditionIndent", 15, 3074);
6524 break;
6525 }
6526 ReturnException ("m2/gm2-auto/pge.mod", 1, 7);
6527 __builtin_unreachable ();
6528 }
6529
6530
6531 /*
6532 WriteGetTokenType - writes out the method of determining the token type.
6533 */
6534
6535 static void WriteGetTokenType (void)
6536 {
6537 Output_WriteKey (TokenTypeProc);
6538 }
6539
6540
6541 /*
6542 NumberOfElements - returns the number of elements in set, to, which lie between low..high
6543 */
6544
6545 static unsigned int NumberOfElements (pge_SetDesc to, unsigned int low, unsigned int high)
6546 {
6547 unsigned int n;
6548
6549 n = 0;
6550 while (to != NULL)
6551 {
6552 switch (to->type)
6553 {
6554 case pge_tokel:
6555 if ((high == 0) || (IsBetween (to->string, low, high)))
6556 {
6557 n += 1;
6558 }
6559 break;
6560
6561 case pge_litel:
6562 if ((high == 0) || (IsBetween (SymbolKey_GetSymKey (Aliases, to->string), low, high)))
6563 {
6564 n += 1;
6565 }
6566 break;
6567
6568 case pge_idel:
6569 PushBackInput_WarnError ((const char *) "not expecting ident in first symbol list", 40);
6570 WasNoError = false;
6571 break;
6572
6573
6574 default:
6575 PushBackInput_WarnError ((const char *) "unknown enuneration element", 27);
6576 WasNoError = false;
6577 break;
6578 }
6579 to = to->next;
6580 }
6581 return n;
6582 /* static analysis guarentees a RETURN statement will be used before here. */
6583 __builtin_unreachable ();
6584 }
6585
6586
6587 /*
6588 WriteElement - writes the literal name for element, e.
6589 */
6590
6591 static void WriteElement (unsigned int e)
6592 {
6593 Output_WriteKey (SymbolKey_GetSymKey (ReverseValues, e));
6594 }
6595
6596
6597 /*
6598 EmitIsInSet - writes out the equivalent of GetTokenType() IN { toset }
6599 */
6600
6601 static void EmitIsInSet (pge_SetDesc to, NameKey_Name low, NameKey_Name high)
6602 {
6603 if ((NumberOfElements (to, low, high)) == 1)
6604 {
6605 WriteGetTokenType ();
6606 Output_Write ('=');
6607 EmitSet (to, low, high);
6608 }
6609 else
6610 {
6611 WriteGetTokenType ();
6612 Output_WriteString ((const char *) " IN SetOfStop", 13);
6613 if (LargestValue > MaxElementsInSet)
6614 {
6615 Output_WriteCard (((unsigned int ) (low)) / MaxElementsInSet, 0);
6616 }
6617 Output_WriteString ((const char *) " {", 2);
6618 EmitSet (to, low, high);
6619 Output_WriteString ((const char *) "}", 1);
6620 }
6621 }
6622
6623
6624 /*
6625 EmitIsInSubSet - writes out a test to see whether GetTokenype() is in { subset }
6626 */
6627
6628 static void EmitIsInSubSet (pge_SetDesc to, unsigned int low, unsigned int high)
6629 {
6630 if ((NumberOfElements (to, low, high)) == 1)
6631 {
6632 Output_Write ('(');
6633 EmitIsInSet (to, low, high);
6634 Output_Write (')');
6635 }
6636 else if (low == 0)
6637 {
6638 /* avoid dangling else. */
6639 /* no need to check whether GetTokenType > low */
6640 Output_WriteString ((const char *) "((", 2);
6641 WriteGetTokenType ();
6642 Output_Write ('<');
6643 WriteElement (static_cast<unsigned int> (((int ) (high))+1));
6644 Output_WriteString ((const char *) ") AND (", 7);
6645 EmitIsInSet (to, low, high);
6646 Output_WriteString ((const char *) "))", 2);
6647 }
6648 else if (((unsigned int ) (high)) > LargestValue)
6649 {
6650 /* avoid dangling else. */
6651 /* no need to check whether GetTokenType < high */
6652 Output_WriteString ((const char *) "((", 2);
6653 WriteGetTokenType ();
6654 Output_WriteString ((const char *) ">=", 2);
6655 WriteElement (low);
6656 Output_WriteString ((const char *) ") AND (", 7);
6657 EmitIsInSet (to, low, high);
6658 Output_WriteString ((const char *) "))", 2);
6659 }
6660 else
6661 {
6662 /* avoid dangling else. */
6663 Output_WriteString ((const char *) "((", 2);
6664 WriteGetTokenType ();
6665 Output_WriteString ((const char *) ">=", 2);
6666 WriteElement (low);
6667 Output_WriteString ((const char *) ") AND (", 7);
6668 WriteGetTokenType ();
6669 Output_Write ('<');
6670 WriteElement (static_cast<unsigned int> (((int ) (high))+1));
6671 Output_WriteString ((const char *) ") AND (", 7);
6672 EmitIsInSet (to, low, high);
6673 Output_WriteString ((const char *) "))", 2);
6674 }
6675 }
6676
6677
6678 /*
6679 EmitIsInFirst -
6680 */
6681
6682 static void EmitIsInFirst (pge_SetDesc to, pge_m2condition m)
6683 {
6684 unsigned int i;
6685 bool first;
6686
6687 if ((NumberOfElements (to, static_cast<unsigned int> (0), static_cast<unsigned int> (0))) == 1)
6688 {
6689 /* only one element */
6690 WriteGetTokenType ();
6691 Output_Write ('=');
6692 EmitSet (to, static_cast<unsigned int> (0), static_cast<unsigned int> (0));
6693 }
6694 else
6695 {
6696 if (LargestValue <= MaxElementsInSet)
6697 {
6698 Output_Write ('(');
6699 WriteGetTokenType ();
6700 Output_WriteString ((const char *) " IN ", 4);
6701 EmitSetAsParameters (to);
6702 Output_WriteString ((const char *) ")", 1);
6703 }
6704 else
6705 {
6706 i = 0;
6707 first = true;
6708 do {
6709 if (! (IsEmptySet (to, i*MaxElementsInSet, ((i+1)*MaxElementsInSet)-1)))
6710 {
6711 if (! first)
6712 {
6713 Output_WriteString ((const char *) " OR", 3);
6714 NewLine (Indent+(ConditionIndent (m)));
6715 Indent -= ConditionIndent (m);
6716 }
6717 EmitIsInSubSet (to, i*MaxElementsInSet, ((i+1)*MaxElementsInSet)-1);
6718 first = false;
6719 }
6720 i += 1;
6721 } while (! ((i*MaxElementsInSet) > LargestValue));
6722 }
6723 }
6724 }
6725
6726 static void FlushRecoverCode (pge_FactorDesc *codeStack)
6727 {
6728 /*
6729 FlushCode -
6730 */
6731 if ((*codeStack) != NULL)
6732 {
6733 while ((*codeStack) != NULL)
6734 {
6735 EmitNonVarCode ((*codeStack)->code, 0, Indent);
6736 (*codeStack) = (*codeStack)->pushed;
6737 }
6738 }
6739 }
6740
6741
6742 /*
6743 RecoverFactor -
6744 */
6745
6746 static void RecoverFactor (pge_FactorDesc f, pge_m2condition m, pge_FactorDesc codeStack)
6747 {
6748 pge_SetDesc to;
6749
6750 if (f == NULL)
6751 {} /* empty. */
6752 else
6753 {
6754 EmitFileLineTag (f->line);
6755 switch (f->type)
6756 {
6757 case pge_id:
6758 to = NULL;
6759 CalcFirstFactor (f, NULL, &to);
6760 if ((to != NULL) && (m != pge_m2none))
6761 {
6762 RecoverCondition (m);
6763 EmitIsInFirst (to, m);
6764 CodeThenDo (m);
6765 Indent += 3;
6766 }
6767 FlushRecoverCode (&codeStack);
6768 IndentString ((const char *) "", 0);
6769 Output_WriteKey (f->ident->name);
6770 Output_Write ('(');
6771 EmitStopParametersAndFollow (f, m);
6772 Output_WriteString ((const char *) ") ;", 3);
6773 Output_WriteLn ();
6774 RecoverFactor (f->next, pge_m2none, codeStack);
6775 if ((to != NULL) && (m != pge_m2none))
6776 {
6777 Indent -= 3;
6778 }
6779 break;
6780
6781 case pge_lit:
6782 if (m == pge_m2none)
6783 {
6784 FlushRecoverCode (&codeStack);
6785 IndentString ((const char *) "Expect(", 7);
6786 Output_WriteKey (SymbolKey_GetSymKey (Aliases, f->string));
6787 Output_WriteString ((const char *) ", ", 2);
6788 EmitStopParametersAndFollow (f, m);
6789 Output_WriteString ((const char *) ") ;", 3);
6790 Output_WriteLn ();
6791 RecoverFactor (f->next, pge_m2none, codeStack);
6792 }
6793 else
6794 {
6795 RecoverCondition (m);
6796 WriteGetTokenType ();
6797 Output_Write ('=');
6798 Output_WriteKey (SymbolKey_GetSymKey (Aliases, f->string));
6799 CodeThenDo (m);
6800 Indent += 3;
6801 IndentString ((const char *) "Expect(", 7);
6802 Output_WriteKey (SymbolKey_GetSymKey (Aliases, f->string));
6803 Output_WriteString ((const char *) ", ", 2);
6804 EmitStopParametersAndFollow (f, m);
6805 Output_WriteString ((const char *) ") ;", 3);
6806 Output_WriteLn ();
6807 FlushRecoverCode (&codeStack);
6808 RecoverFactor (f->next, pge_m2none, codeStack);
6809 Indent -= 3;
6810 }
6811 break;
6812
6813 case pge_sub:
6814 FlushRecoverCode (&codeStack);
6815 RecoverExpression (f->expr, pge_m2none, m);
6816 RecoverFactor (f->next, pge_m2none, codeStack);
6817 break;
6818
6819 case pge_opt:
6820 FlushRecoverCode (&codeStack);
6821 if (OptExpSeen (f))
6822 {
6823 to = NULL;
6824 CalcFirstExpression (f->expr, NULL, &to);
6825 RecoverCondition (m);
6826 EmitIsInFirst (to, m);
6827 CodeThenDo (m);
6828 Indent += 3;
6829 IndentString ((const char *) "(* seen optional [ | ] expression *)", 36);
6830 Output_WriteLn ();
6831 stop ();
6832 RecoverExpression (f->expr, pge_m2none, pge_m2if);
6833 IndentString ((const char *) "(* end of optional [ | ] expression *)", 38);
6834 Output_WriteLn ();
6835 Indent -= 3;
6836 IndentString ((const char *) "END ;", 5);
6837 Output_WriteLn ();
6838 }
6839 else
6840 {
6841 RecoverExpression (f->expr, pge_m2if, m);
6842 }
6843 RecoverFactor (f->next, pge_m2none, codeStack);
6844 break;
6845
6846 case pge_mult:
6847 FlushRecoverCode (&codeStack);
6848 if (((OptExpSeen (f)) || (m == pge_m2if)) || (m == pge_m2elsif))
6849 {
6850 /* avoid dangling else. */
6851 to = NULL;
6852 CalcFirstExpression (f->expr, NULL, &to);
6853 RecoverCondition (m);
6854 EmitIsInFirst (to, m);
6855 CodeThenDo (m);
6856 Indent += 3;
6857 IndentString ((const char *) "(* seen optional { | } expression *)", 36);
6858 Output_WriteLn ();
6859 RecoverCondition (pge_m2while);
6860 EmitIsInFirst (to, pge_m2while);
6861 CodeThenDo (pge_m2while);
6862 Indent += 3;
6863 RecoverExpression (f->expr, pge_m2none, pge_m2while);
6864 IndentString ((const char *) "(* end of optional { | } expression *)", 38);
6865 Output_WriteLn ();
6866 Indent -= 3;
6867 IndentString ((const char *) "END ;", 5);
6868 Output_WriteLn ();
6869 Indent -= 3;
6870 if (m == pge_m2none)
6871 {
6872 IndentString ((const char *) "END ;", 5);
6873 Output_WriteLn ();
6874 Indent -= 3;
6875 }
6876 }
6877 else
6878 {
6879 RecoverExpression (f->expr, pge_m2while, m);
6880 }
6881 RecoverFactor (f->next, pge_m2none, codeStack);
6882 break;
6883
6884 case pge_m2:
6885 codeStack = ChainOn (codeStack, f);
6886 if (f->next == NULL)
6887 {
6888 FlushRecoverCode (&codeStack);
6889 }
6890 else
6891 {
6892 RecoverFactor (f->next, m, codeStack); /* was m2none */
6893 }
6894 break;
6895
6896
6897 default:
6898 break;
6899 }
6900 }
6901 }
6902
6903
6904 /*
6905 OptExpSeen - returns TRUE if we can see an optional expression in the factor.
6906 This is not the same as epsilon. Example { '+' } matches epsilon as
6907 well as { '+' | '-' } but OptExpSeen returns TRUE in the second case
6908 and FALSE in the first.
6909 */
6910
6911 static bool OptExpSeen (pge_FactorDesc f)
6912 {
6913 if (f == NULL)
6914 {
6915 return false;
6916 }
6917 else
6918 {
6919 switch (f->type)
6920 {
6921 case pge_id:
6922 case pge_lit:
6923 return false;
6924 break;
6925
6926 case pge_sub:
6927 return false; /* is this correct? */
6928 break;
6929
6930 case pge_opt:
6931 case pge_mult:
6932 return ((f->expr != NULL) && (f->expr->term != NULL)) && (f->expr->term->next != NULL); /* is this correct? */
6933 break;
6934
6935 case pge_m2:
6936 return true;
6937 break;
6938
6939
6940 default:
6941 break;
6942 }
6943 }
6944 PushBackInput_WarnError ((const char *) "all cases were not handled", 26);
6945 WasNoError = false;
6946 ReturnException ("m2/gm2-auto/pge.mod", 1, 7);
6947 __builtin_unreachable ();
6948 }
6949
6950
6951 /*
6952 RecoverTerm -
6953 */
6954
6955 static void RecoverTerm (pge_TermDesc t, pge_m2condition new_, pge_m2condition old)
6956 {
6957 bool LastWasM2Only;
6958 bool alternative;
6959 pge_SetDesc to;
6960
6961 LastWasM2Only = (t->factor->type == pge_m2) && (t->factor->next == NULL); /* does the factor only contain inline code? */
6962 to = NULL;
6963 CalcFirstTerm (t, NULL, &to);
6964 alternative = false;
6965 if (t->next != NULL)
6966 {
6967 new_ = pge_m2if;
6968 }
6969 while (t != NULL)
6970 {
6971 EmitFileLineTag (t->line);
6972 LastWasM2Only = (t->factor->type == pge_m2) && (t->factor->next == NULL);
6973 if ((t->factor->type == pge_m2) && (new_ == pge_m2elsif))
6974 {
6975 new_ = pge_m2if;
6976 IndentString ((const char *) "ELSE", 4);
6977 Output_WriteLn ();
6978 Indent += 3;
6979 RecoverFactor (t->factor, pge_m2none, NULL);
6980 alternative = false;
6981 }
6982 else
6983 {
6984 RecoverFactor (t->factor, new_, NULL);
6985 }
6986 if (t->next != NULL)
6987 {
6988 new_ = pge_m2elsif;
6989 alternative = true;
6990 }
6991 t = t->next;
6992 }
6993 if ((new_ == pge_m2if) || (new_ == pge_m2elsif))
6994 {
6995 if (alternative && (old != pge_m2while))
6996 {
6997 IndentString ((const char *) "ELSE", 4);
6998 Output_WriteLn ();
6999 Indent += 3;
7000 IndentString ((const char *) "", 0);
7001 Output_WriteKey (ErrorProcArray);
7002 Output_WriteString ((const char *) "('expecting one of: ", 20);
7003 EmitSetName (to, static_cast<unsigned int> (0), static_cast<unsigned int> (0));
7004 Output_WriteString ((const char *) "')", 2);
7005 Output_WriteLn ();
7006 Indent -= 3;
7007 }
7008 else if (LastWasM2Only)
7009 {
7010 /* avoid dangling else. */
7011 Indent -= 3;
7012 }
7013 IndentString ((const char *) "END ;", 5);
7014 Output_WriteLn ();
7015 }
7016 else if (new_ == pge_m2while)
7017 {
7018 /* avoid dangling else. */
7019 IndentString ((const char *) "END (* while *) ;", 17);
7020 Output_WriteLn ();
7021 }
7022 else if (LastWasM2Only)
7023 {
7024 /* avoid dangling else. */
7025 Indent -= 3;
7026 }
7027 }
7028
7029
7030 /*
7031 RecoverExpression -
7032 */
7033
7034 static void RecoverExpression (pge_ExpressionDesc e, pge_m2condition new_, pge_m2condition old)
7035 {
7036 if (e != NULL)
7037 {
7038 EmitFileLineTag (e->line);
7039 RecoverTerm (e->term, new_, old);
7040 }
7041 }
7042
7043
7044 /*
7045 RecoverStatement -
7046 */
7047
7048 static void RecoverStatement (pge_StatementDesc s, pge_m2condition m)
7049 {
7050 if (s != NULL)
7051 {
7052 EmitFileLineTag (s->line);
7053 RecoverExpression (s->expr, m, pge_m2none);
7054 }
7055 }
7056
7057
7058 /*
7059 EmitFirstFactor - generate a list of all first tokens between the range: low..high.
7060 */
7061
7062 static void EmitFirstFactor (pge_FactorDesc f, unsigned int low, unsigned int high)
7063 {
7064 }
7065
7066
7067 /*
7068 EmitUsed -
7069 */
7070
7071 static void EmitUsed (unsigned int wordno)
7072 {
7073 if (! ((((1 << (wordno)) & (ParametersUsed)) != 0)))
7074 {
7075 Output_WriteString ((const char *) " (* <* unused *> *) ", 20);
7076 }
7077 }
7078
7079
7080 /*
7081 EmitStopParameters - generate the stop set.
7082 */
7083
7084 static void EmitStopParameters (bool FormalParameters)
7085 {
7086 unsigned int i;
7087
7088 if (LargestValue <= MaxElementsInSet)
7089 {
7090 Output_WriteString ((const char *) "stopset", 7);
7091 if (FormalParameters)
7092 {
7093 Output_WriteString ((const char *) ": SetOfStop", 11);
7094 EmitUsed (0);
7095 }
7096 else
7097 {
7098 ParametersUsed |= (1 << (0 ));
7099 }
7100 }
7101 else
7102 {
7103 i = 0;
7104 do {
7105 Output_WriteString ((const char *) "stopset", 7);
7106 Output_WriteCard (i, 0);
7107 if (FormalParameters)
7108 {
7109 Output_WriteString ((const char *) ": SetOfStop", 11);
7110 Output_WriteCard (i, 0);
7111 EmitUsed (i);
7112 }
7113 else
7114 {
7115 ParametersUsed |= (1 << (i ));
7116 }
7117 i += 1;
7118 if ((i*MaxElementsInSet) < LargestValue)
7119 {
7120 /* avoid gcc warning by using compound statement even if not strictly necessary. */
7121 if (FormalParameters)
7122 {
7123 Output_WriteString ((const char *) "; ", 2);
7124 }
7125 else
7126 {
7127 Output_WriteString ((const char *) ", ", 2);
7128 }
7129 }
7130 } while (! ((i*MaxElementsInSet) >= LargestValue));
7131 }
7132 }
7133
7134
7135 /*
7136 IsBetween - returns TRUE if the value of the token, string, is
7137 in the range: low..high
7138 */
7139
7140 static bool IsBetween (NameKey_Name string, unsigned int low, unsigned int high)
7141 {
7142 return ((SymbolKey_GetSymKey (Values, string)) >= low) && ((SymbolKey_GetSymKey (Values, string)) <= high);
7143 /* static analysis guarentees a RETURN statement will be used before here. */
7144 __builtin_unreachable ();
7145 }
7146
7147
7148 /*
7149 IsEmptySet - returns TRUE if no elements exist in set, to, with values, low..high.
7150 */
7151
7152 static bool IsEmptySet (pge_SetDesc to, unsigned int low, unsigned int high)
7153 {
7154 while (to != NULL)
7155 {
7156 switch (to->type)
7157 {
7158 case pge_tokel:
7159 if (IsBetween (to->string, low, high))
7160 {
7161 return false;
7162 }
7163 break;
7164
7165 case pge_litel:
7166 if (IsBetween (SymbolKey_GetSymKey (Aliases, to->string), low, high))
7167 {
7168 return false;
7169 }
7170 break;
7171
7172 case pge_idel:
7173 PushBackInput_WarnError ((const char *) "not expecting ident in first symbol list", 40);
7174 WasNoError = false;
7175 break;
7176
7177
7178 default:
7179 PushBackInput_WarnError ((const char *) "unknown enuneration element", 27);
7180 WasNoError = false;
7181 break;
7182 }
7183 to = to->next;
7184 }
7185 return true;
7186 /* static analysis guarentees a RETURN statement will be used before here. */
7187 __builtin_unreachable ();
7188 }
7189
7190
7191 /*
7192 EmitSet - emits the tokens in the set, to, which have values low..high
7193 */
7194
7195 static void EmitSet (pge_SetDesc to, unsigned int low, unsigned int high)
7196 {
7197 bool first;
7198
7199 first = true;
7200 while (to != NULL)
7201 {
7202 switch (to->type)
7203 {
7204 case pge_tokel:
7205 if ((high == 0) || (IsBetween (to->string, low, high)))
7206 {
7207 if (! first)
7208 {
7209 Output_WriteString ((const char *) ", ", 2);
7210 }
7211 Output_WriteKey (to->string);
7212 first = false;
7213 }
7214 break;
7215
7216 case pge_litel:
7217 if ((high == 0) || (IsBetween (SymbolKey_GetSymKey (Aliases, to->string), low, high)))
7218 {
7219 if (! first)
7220 {
7221 Output_WriteString ((const char *) ", ", 2);
7222 }
7223 Output_WriteKey (SymbolKey_GetSymKey (Aliases, to->string));
7224 first = false;
7225 }
7226 break;
7227
7228 case pge_idel:
7229 PushBackInput_WarnError ((const char *) "not expecting ident in first symbol list", 40);
7230 WasNoError = false;
7231 break;
7232
7233
7234 default:
7235 PushBackInput_WarnError ((const char *) "unknown enuneration element", 27);
7236 WasNoError = false;
7237 break;
7238 }
7239 to = to->next;
7240 }
7241 }
7242
7243
7244 /*
7245 EmitSetName - emits the tokens in the set, to, which have values low..high, using
7246 their names.
7247 */
7248
7249 static void EmitSetName (pge_SetDesc to, unsigned int low, unsigned int high)
7250 {
7251 while (to != NULL)
7252 {
7253 switch (to->type)
7254 {
7255 case pge_tokel:
7256 if ((high == 0) || (IsBetween (to->string, low, high)))
7257 {
7258 /* avoid gcc warning by using compound statement even if not strictly necessary. */
7259 if ((NameKey_MakeKey ((const char *) "'", 1)) == (SymbolKey_GetSymKey (ReverseAliases, to->string)))
7260 {
7261 Output_WriteString ((const char *) "single quote", 12);
7262 }
7263 else
7264 {
7265 KeyWord (SymbolKey_GetSymKey (ReverseAliases, to->string));
7266 }
7267 }
7268 break;
7269
7270 case pge_litel:
7271 if ((high == 0) || (IsBetween (SymbolKey_GetSymKey (Aliases, to->string), low, high)))
7272 {
7273 Output_WriteKey (to->string);
7274 }
7275 break;
7276
7277 case pge_idel:
7278 PushBackInput_WarnError ((const char *) "not expecting ident in first symbol list", 40);
7279 WasNoError = false;
7280 break;
7281
7282
7283 default:
7284 PushBackInput_WarnError ((const char *) "unknown enuneration element", 27);
7285 WasNoError = false;
7286 break;
7287 }
7288 to = to->next;
7289 if (to != NULL)
7290 {
7291 Output_Write (' ');
7292 }
7293 }
7294 }
7295
7296
7297 /*
7298 EmitStopParametersAndSet - generates the stop parameters together with a set
7299 inclusion of all the symbols in set, to.
7300 */
7301
7302 static void EmitStopParametersAndSet (pge_SetDesc to)
7303 {
7304 unsigned int i;
7305
7306 if (LargestValue <= MaxElementsInSet)
7307 {
7308 /* avoid dangling else. */
7309 Output_WriteString ((const char *) "stopset", 7);
7310 ParametersUsed |= (1 << (0 ));
7311 if ((to != NULL) && ((NumberOfElements (to, static_cast<unsigned int> (0), static_cast<unsigned int> (MaxElementsInSet-1))) > 0))
7312 {
7313 Output_WriteString ((const char *) " + SetOfStop", 12);
7314 Output_Write ('{');
7315 EmitSet (to, static_cast<unsigned int> (0), static_cast<unsigned int> (MaxElementsInSet-1));
7316 Output_Write ('}');
7317 }
7318 }
7319 else
7320 {
7321 i = 0;
7322 do {
7323 Output_WriteString ((const char *) "stopset", 7);
7324 Output_WriteCard (i, 0);
7325 ParametersUsed |= (1 << (i ));
7326 if ((to != NULL) && ((NumberOfElements (to, i*MaxElementsInSet, ((i+1)*MaxElementsInSet)-1)) > 0))
7327 {
7328 Output_WriteString ((const char *) " + SetOfStop", 12);
7329 Output_WriteCard (i, 0);
7330 Output_Write ('{');
7331 EmitSet (to, i*MaxElementsInSet, ((i+1)*MaxElementsInSet)-1);
7332 Output_Write ('}');
7333 }
7334 i += 1;
7335 if ((i*MaxElementsInSet) < LargestValue)
7336 {
7337 Output_WriteString ((const char *) ", ", 2);
7338 }
7339 } while (! ((i*MaxElementsInSet) >= LargestValue));
7340 }
7341 }
7342
7343
7344 /*
7345 EmitSetAsParameters - generates the first symbols as parameters to a set function.
7346 */
7347
7348 static void EmitSetAsParameters (pge_SetDesc to)
7349 {
7350 unsigned int i;
7351
7352 if (LargestValue <= MaxElementsInSet)
7353 {
7354 Output_Write ('{');
7355 EmitSet (to, static_cast<unsigned int> (0), static_cast<unsigned int> (MaxElementsInSet-1));
7356 }
7357 else
7358 {
7359 i = 0;
7360 do {
7361 Output_Write ('{');
7362 EmitSet (to, i*MaxElementsInSet, ((i+1)*MaxElementsInSet)-1);
7363 i += 1;
7364 if (((i+1)*MaxElementsInSet) > LargestValue)
7365 {
7366 Output_WriteString ((const char *) "}, ", 3);
7367 }
7368 } while (! (((i+1)*MaxElementsInSet) >= LargestValue));
7369 }
7370 Output_Write ('}');
7371 }
7372
7373
7374 /*
7375 EmitStopParametersAndFollow - generates the stop parameters together with a set
7376 inclusion of all the follow symbols for subsequent
7377 sentances.
7378 */
7379
7380 static void EmitStopParametersAndFollow (pge_FactorDesc f, pge_m2condition m)
7381 {
7382 pge_SetDesc to;
7383
7384 to = NULL;
7385 /*
7386 IF m=m2while
7387 THEN
7388 CalcFirstFactor(f, NIL, to)
7389 END ;
7390 */
7391 CollectFollow (&to, f->followinfo);
7392 EmitStopParametersAndSet (to);
7393 if (Debugging)
7394 {
7395 Output_WriteLn ();
7396 Output_WriteString ((const char *) "factor is: ", 11);
7397 PrettyCommentFactor (f, StrLib_StrLen ((const char *) "factor is: ", 11));
7398 Output_WriteLn ();
7399 Output_WriteString ((const char *) "follow set:", 11);
7400 EmitSet (to, static_cast<unsigned int> (0), static_cast<unsigned int> (0));
7401 Output_WriteLn ();
7402 }
7403 }
7404
7405
7406 /*
7407 EmitFirstAsParameters -
7408 */
7409
7410 static void EmitFirstAsParameters (pge_FactorDesc f)
7411 {
7412 pge_SetDesc to;
7413
7414 to = NULL;
7415 CalcFirstFactor (f, NULL, &to);
7416 EmitSetAsParameters (to);
7417 }
7418
7419
7420 /*
7421 RecoverProduction - only encode grammer rules which are not special.
7422 Generate error recovery code.
7423 */
7424
7425 static void RecoverProduction (pge_ProductionDesc p)
7426 {
7427 DynamicStrings_String s;
7428
7429 if ((p != NULL) && (! p->firstsolved || ((p->statement != NULL) && (p->statement->expr != NULL))))
7430 {
7431 BeginningOfLine = true;
7432 Indent = 0;
7433 Output_WriteLn ();
7434 OnLineStart = false;
7435 EmitFileLineTag (p->line);
7436 IndentString ((const char *) "PROCEDURE ", 10);
7437 Output_WriteKey (GetDefinitionName (p));
7438 Output_WriteString ((const char *) " (", 2);
7439 ParametersUsed = (unsigned int) 0;
7440 Output_StartBuffer ();
7441 Output_WriteString ((const char *) ") ;", 3);
7442 VarProduction (p);
7443 Output_WriteLn ();
7444 OnLineStart = false;
7445 EmitFileLineTag (p->line);
7446 Indent = 0;
7447 IndentString ((const char *) "BEGIN", 5);
7448 Output_WriteLn ();
7449 OnLineStart = false;
7450 EmitFileLineTag (p->line);
7451 Indent = 3;
7452 RecoverStatement (p->statement, pge_m2none);
7453 Indent = 0;
7454 IndentString ((const char *) "END ", 4);
7455 Output_WriteKey (GetDefinitionName (p));
7456 Output_WriteString ((const char *) " ;", 2);
7457 Output_WriteLn ();
7458 Output_WriteLn ();
7459 Output_WriteLn ();
7460 s = Output_EndBuffer ();
7461 EmitStopParameters (true);
7462 Output_KillWriteS (s);
7463 }
7464 }
7465
7466
7467 /*
7468 IsWhite - returns TRUE if, ch, is a space or a tab.
7469 */
7470
7471 static bool IsWhite (char ch)
7472 {
7473 return ((ch == ' ') || (ch == ASCII_tab)) || (ch == ASCII_lf);
7474 /* static analysis guarentees a RETURN statement will be used before here. */
7475 __builtin_unreachable ();
7476 }
7477
7478
7479 /*
7480 FindStr - returns TRUE if, str, was seen inside the code hunk
7481 */
7482
7483 static bool FindStr (pge_CodeHunk *code, unsigned int *i, const char *str_, unsigned int _str_high)
7484 {
7485 unsigned int j;
7486 unsigned int k;
7487 pge_CodeHunk t;
7488 char str[_str_high+1];
7489
7490 /* make a local copy of each unbounded array. */
7491 memcpy (str, str_, _str_high+1);
7492
7493 t = (*code);
7494 k = (StrLib_StrLen ((const char *) &(*code)->codetext.array[0], MaxCodeHunkLength))+1;
7495 j = StrLib_StrLen ((const char *) str, _str_high);
7496 while (t != NULL)
7497 {
7498 do {
7499 while ((k > 0) && (IsWhite (t->codetext.array[k-1])))
7500 {
7501 k -= 1;
7502 }
7503 if (k == 0)
7504 {
7505 t = t->next;
7506 k = MaxCodeHunkLength+1;
7507 }
7508 } while (! ((t == NULL) || (! (IsWhite (t->codetext.array[k-1])))));
7509 /* found another word check it */
7510 if (t != NULL)
7511 {
7512 j = StrLib_StrLen ((const char *) str, _str_high);
7513 (*i) = k;
7514 while (((t != NULL) && (j > 0)) && ((str[j-1] == t->codetext.array[k-1]) || ((IsWhite (str[j-1])) && (IsWhite (t->codetext.array[k-1])))))
7515 {
7516 j -= 1;
7517 k -= 1;
7518 if (j == 0)
7519 {
7520 /* found word remember position */
7521 (*code) = t;
7522 }
7523 if (k == 0)
7524 {
7525 t = t->next;
7526 k = MaxCodeHunkLength+1;
7527 }
7528 }
7529 if (k > 0)
7530 {
7531 k -= 1;
7532 }
7533 else
7534 {
7535 t = t->next;
7536 }
7537 }
7538 }
7539 return (t == NULL) && (j == 0);
7540 /* static analysis guarentees a RETURN statement will be used before here. */
7541 __builtin_unreachable ();
7542 }
7543
7544
7545 /*
7546 WriteUpto -
7547 */
7548
7549 static void WriteUpto (pge_CodeHunk code, pge_CodeHunk upto, unsigned int limit)
7550 {
7551 if (code != upto)
7552 {
7553 WriteUpto (code->next, upto, limit);
7554 Output_WriteString ((const char *) &code->codetext.array[0], MaxCodeHunkLength);
7555 }
7556 else
7557 {
7558 while ((limit <= MaxCodeHunkLength) && (code->codetext.array[limit] != ASCII_nul))
7559 {
7560 Output_Write (code->codetext.array[limit]);
7561 limit += 1;
7562 }
7563 }
7564 }
7565
7566
7567 /*
7568 CheckForVar - checks for any local variables which need to be emitted during
7569 this production.
7570 */
7571
7572 static void CheckForVar (pge_CodeHunk code)
7573 {
7574 unsigned int i;
7575 pge_CodeHunk t;
7576
7577 t = code;
7578 if ((FindStr (&t, &i, (const char *) "VAR", 3)) && EmitCode)
7579 {
7580 if (! EmittedVar)
7581 {
7582 Output_WriteLn ();
7583 Indent = 0;
7584 IndentString ((const char *) "VAR", 3);
7585 Indent += 3;
7586 Output_WriteLn ();
7587 EmittedVar = true;
7588 }
7589 WriteUpto (code, t, i);
7590 }
7591 }
7592
7593
7594 /*
7595 VarFactor -
7596 */
7597
7598 static void VarFactor (pge_FactorDesc f)
7599 {
7600 while (f != NULL)
7601 {
7602 switch (f->type)
7603 {
7604 case pge_id:
7605 break;
7606
7607 case pge_lit:
7608 break;
7609
7610 case pge_sub:
7611 case pge_opt:
7612 case pge_mult:
7613 VarExpression (f->expr);
7614 break;
7615
7616 case pge_m2:
7617 CheckForVar (f->code->code);
7618 break;
7619
7620
7621 default:
7622 break;
7623 }
7624 f = f->next;
7625 }
7626 }
7627
7628
7629 /*
7630 VarTerm -
7631 */
7632
7633 static void VarTerm (pge_TermDesc t)
7634 {
7635 while (t != NULL)
7636 {
7637 VarFactor (t->factor);
7638 t = t->next;
7639 }
7640 }
7641
7642
7643 /*
7644 VarExpression -
7645 */
7646
7647 static void VarExpression (pge_ExpressionDesc e)
7648 {
7649 if (e != NULL)
7650 {
7651 VarTerm (e->term);
7652 }
7653 }
7654
7655
7656 /*
7657 VarStatement -
7658 */
7659
7660 static void VarStatement (pge_StatementDesc s)
7661 {
7662 if (s != NULL)
7663 {
7664 VarExpression (s->expr);
7665 }
7666 }
7667
7668
7669 /*
7670 VarProduction - writes out all variable declarations.
7671 */
7672
7673 static void VarProduction (pge_ProductionDesc p)
7674 {
7675 EmittedVar = false;
7676 if (p != NULL)
7677 {
7678 VarStatement (p->statement);
7679 }
7680 }
7681
7682
7683 /*
7684 In - returns TRUE if token, s, is already in the set, to.
7685 */
7686
7687 static bool In (pge_SetDesc to, NameKey_Name s)
7688 {
7689 while (to != NULL)
7690 {
7691 switch (to->type)
7692 {
7693 case pge_idel:
7694 if (s == to->ident->name)
7695 {
7696 return true;
7697 }
7698 break;
7699
7700 case pge_tokel:
7701 case pge_litel:
7702 if (s == to->string)
7703 {
7704 return true;
7705 }
7706 break;
7707
7708
7709 default:
7710 PushBackInput_WarnError ((const char *) "internal error CASE type not known", 34);
7711 WasNoError = false;
7712 break;
7713 }
7714 to = to->next;
7715 }
7716 return false;
7717 /* static analysis guarentees a RETURN statement will be used before here. */
7718 __builtin_unreachable ();
7719 }
7720
7721
7722 /*
7723 IntersectionIsNil - given two set lists, s1, s2, return TRUE if the
7724 s1 * s2 = {}
7725 */
7726
7727 static bool IntersectionIsNil (pge_SetDesc s1, pge_SetDesc s2)
7728 {
7729 while (s1 != NULL)
7730 {
7731 switch (s1->type)
7732 {
7733 case pge_idel:
7734 if (In (s2, s1->ident->name))
7735 {
7736 return false;
7737 }
7738 break;
7739
7740 case pge_tokel:
7741 case pge_litel:
7742 if (In (s2, s1->string))
7743 {
7744 return false;
7745 }
7746 break;
7747
7748
7749 default:
7750 PushBackInput_WarnError ((const char *) "internal error CASE type not known", 34);
7751 WasNoError = false;
7752 break;
7753 }
7754 s1 = s1->next;
7755 }
7756 return true;
7757 /* static analysis guarentees a RETURN statement will be used before here. */
7758 __builtin_unreachable ();
7759 }
7760
7761
7762 /*
7763 AddSet - adds a first symbol to a production.
7764 */
7765
7766 static void AddSet (pge_SetDesc *to, NameKey_Name s)
7767 {
7768 pge_SetDesc d;
7769
7770 if (! (In ((*to), s)))
7771 {
7772 d = NewSetDesc ();
7773 d->type = pge_tokel;
7774 d->string = s;
7775 d->next = (*to);
7776 (*to) = d;
7777 Finished = false;
7778 }
7779 }
7780
7781
7782 /*
7783 OrSet -
7784 */
7785
7786 static void OrSet (pge_SetDesc *to, pge_SetDesc from)
7787 {
7788 while (from != NULL)
7789 {
7790 switch (from->type)
7791 {
7792 case pge_tokel:
7793 AddSet (to, from->string);
7794 break;
7795
7796 case pge_litel:
7797 AddSet (to, SymbolKey_GetSymKey (Aliases, from->string));
7798 break;
7799
7800 case pge_idel:
7801 PushBackInput_WarnError ((const char *) "not expecting ident in first symbol list", 40);
7802 WasNoError = false;
7803 break;
7804
7805
7806 default:
7807 Debug_Halt ((const char *) "unknown element in enumeration type", 35, (const char *) "m2/gm2-auto/pge.mod", 19, (const char *) "OrSet", 5, 4134);
7808 break;
7809 }
7810 from = from->next;
7811 }
7812 }
7813
7814
7815 /*
7816 CalcFirstFactor -
7817 */
7818
7819 static void CalcFirstFactor (pge_FactorDesc f, pge_ProductionDesc from, pge_SetDesc *to)
7820 {
7821 while (f != NULL)
7822 {
7823 switch (f->type)
7824 {
7825 case pge_id:
7826 if (f->ident->definition == NULL)
7827 {
7828 WarnError1 ((const char *) "no rule found for an 'ident' called '%s'", 40, f->ident->name);
7829 M2RTS_HALT (-1);
7830 __builtin_unreachable ();
7831 }
7832 OrSet (to, f->ident->definition->first);
7833 if ((GetReachEnd (f->ident->definition->followinfo)) == pge_false)
7834 {
7835 return ;
7836 }
7837 break;
7838
7839 case pge_lit:
7840 if ((SymbolKey_GetSymKey (Aliases, f->string)) == SymbolKey_NulKey)
7841 {
7842 WarnError1 ((const char *) "unknown token for '%s'", 22, f->string);
7843 WasNoError = false;
7844 }
7845 else
7846 {
7847 AddSet (to, SymbolKey_GetSymKey (Aliases, f->string));
7848 }
7849 return ;
7850 break;
7851
7852 case pge_sub:
7853 case pge_opt:
7854 case pge_mult:
7855 CalcFirstExpression (f->expr, from, to);
7856 break;
7857
7858 case pge_m2:
7859 break;
7860
7861
7862 default:
7863 break;
7864 }
7865 f = f->next;
7866 }
7867 }
7868
7869
7870 /*
7871 CalcFirstTerm -
7872 */
7873
7874 static void CalcFirstTerm (pge_TermDesc t, pge_ProductionDesc from, pge_SetDesc *to)
7875 {
7876 while (t != NULL)
7877 {
7878 CalcFirstFactor (t->factor, from, to);
7879 t = t->next;
7880 }
7881 }
7882
7883
7884 /*
7885 CalcFirstExpression -
7886 */
7887
7888 static void CalcFirstExpression (pge_ExpressionDesc e, pge_ProductionDesc from, pge_SetDesc *to)
7889 {
7890 if (e != NULL)
7891 {
7892 CalcFirstTerm (e->term, from, to);
7893 }
7894 }
7895
7896
7897 /*
7898 CalcFirstStatement -
7899 */
7900
7901 static void CalcFirstStatement (pge_StatementDesc s, pge_ProductionDesc from, pge_SetDesc *to)
7902 {
7903 if (s != NULL)
7904 {
7905 CalcFirstExpression (s->expr, from, to);
7906 }
7907 }
7908
7909
7910 /*
7911 CalcFirstProduction - calculates all of the first symbols for the grammer
7912 */
7913
7914 static void CalcFirstProduction (pge_ProductionDesc p, pge_ProductionDesc from, pge_SetDesc *to)
7915 {
7916 pge_SetDesc s;
7917
7918 if (p != NULL)
7919 {
7920 /* avoid gcc warning by using compound statement even if not strictly necessary. */
7921 if (p->firstsolved)
7922 {
7923 s = p->first;
7924 while (s != NULL)
7925 {
7926 switch (s->type)
7927 {
7928 case pge_idel:
7929 CalcFirstProduction (s->ident->definition, from, to);
7930 break;
7931
7932 case pge_tokel:
7933 case pge_litel:
7934 AddSet (to, s->string);
7935 break;
7936
7937
7938 default:
7939 break;
7940 }
7941 s = s->next;
7942 }
7943 }
7944 else
7945 {
7946 CalcFirstStatement (p->statement, from, to);
7947 }
7948 }
7949 }
7950
7951 static void WorkOutFollowFactor (pge_FactorDesc f, pge_SetDesc *followset, pge_SetDesc after)
7952 {
7953 pge_TraverseResult foundepsilon;
7954 pge_TraverseResult canreachend;
7955
7956 /*
7957 WorkOutFollow -
7958 */
7959 foundepsilon = pge_true;
7960 canreachend = pge_true;
7961 while ((f != NULL) && (foundepsilon == pge_true))
7962 {
7963 switch (f->type)
7964 {
7965 case pge_id:
7966 if (f->ident->definition == NULL)
7967 {
7968 WarnError1 ((const char *) "no rule found for an 'ident' called '%s'", 40, f->ident->name);
7969 M2RTS_HALT (-1);
7970 __builtin_unreachable ();
7971 }
7972 OrSet (followset, f->ident->definition->first);
7973 break;
7974
7975 case pge_lit:
7976 AddSet (followset, SymbolKey_GetSymKey (Aliases, f->string));
7977 break;
7978
7979 case pge_sub:
7980 WorkOutFollowExpression (f->expr, followset, NULL);
7981 break;
7982
7983 case pge_opt:
7984 WorkOutFollowExpression (f->expr, followset, NULL);
7985 break;
7986
7987 case pge_mult:
7988 WorkOutFollowExpression (f->expr, followset, NULL);
7989 break;
7990
7991 case pge_m2:
7992 break;
7993
7994
7995 default:
7996 break;
7997 }
7998 if ((GetEpsilon (f->followinfo)) == pge_unknown)
7999 {
8000 PushBackInput_WarnError ((const char *) "internal error: epsilon unknown", 31);
8001 PrettyCommentFactor (f, 3);
8002 WasNoError = false;
8003 }
8004 foundepsilon = GetEpsilon (f->followinfo);
8005 canreachend = GetReachEnd (f->followinfo); /* only goes from FALSE -> TRUE */
8006 f = f->next; /* only goes from FALSE -> TRUE */
8007 }
8008 if (canreachend == pge_true)
8009 {
8010 OrSet (followset, after);
8011 }
8012 }
8013
8014
8015 /*
8016 WorkOutFollowTerm -
8017 */
8018
8019 static void WorkOutFollowTerm (pge_TermDesc t, pge_SetDesc *followset, pge_SetDesc after)
8020 {
8021 if (t != NULL)
8022 {
8023 while (t != NULL)
8024 {
8025 WorkOutFollowFactor (t->factor, followset, after); /* { '|' Term } */
8026 t = t->next; /* { '|' Term } */
8027 }
8028 }
8029 }
8030
8031
8032 /*
8033 WorkOutFollowExpression -
8034 */
8035
8036 static void WorkOutFollowExpression (pge_ExpressionDesc e, pge_SetDesc *followset, pge_SetDesc after)
8037 {
8038 if (e != NULL)
8039 {
8040 WorkOutFollowTerm (e->term, followset, after);
8041 }
8042 }
8043
8044
8045 /*
8046 CollectFollow - collects the follow set from, f, into, to.
8047 */
8048
8049 static void CollectFollow (pge_SetDesc *to, pge_FollowDesc f)
8050 {
8051 OrSet (to, f->follow);
8052 }
8053
8054
8055 /*
8056 CalcFollowFactor -
8057 */
8058
8059 static void CalcFollowFactor (pge_FactorDesc f, pge_SetDesc after)
8060 {
8061 while (f != NULL)
8062 {
8063 switch (f->type)
8064 {
8065 case pge_id:
8066 WorkOutFollowFactor (f->next, &f->followinfo->follow, after);
8067 break;
8068
8069 case pge_lit:
8070 WorkOutFollowFactor (f->next, &f->followinfo->follow, after);
8071 break;
8072
8073 case pge_opt:
8074 case pge_sub:
8075 CalcFirstFactor (f->next, NULL, &f->followinfo->follow);
8076 if ((f->next == NULL) || ((GetReachEnd (f->next->followinfo)) == pge_true))
8077 {
8078 OrSet (&f->followinfo->follow, after);
8079 CalcFollowExpression (f->expr, f->followinfo->follow);
8080 }
8081 else
8082 {
8083 CalcFollowExpression (f->expr, f->followinfo->follow);
8084 }
8085 break;
8086
8087 case pge_mult:
8088 CalcFirstFactor (f, NULL, &f->followinfo->follow);
8089 /* include first as we may repeat this sentance */
8090 if (Debugging)
8091 {
8092 StrIO_WriteLn ();
8093 StrIO_WriteString ((const char *) "found mult: and first is: ", 26);
8094 EmitSet (f->followinfo->follow, static_cast<unsigned int> (0), static_cast<unsigned int> (0));
8095 StrIO_WriteLn ();
8096 }
8097 if ((f->next == NULL) || ((GetReachEnd (f->next->followinfo)) == pge_true))
8098 {
8099 OrSet (&f->followinfo->follow, after);
8100 CalcFollowExpression (f->expr, f->followinfo->follow);
8101 }
8102 else
8103 {
8104 CalcFollowExpression (f->expr, f->followinfo->follow);
8105 }
8106 break;
8107
8108
8109 default:
8110 break;
8111 }
8112 f = f->next;
8113 }
8114 }
8115
8116
8117 /*
8118 CalcFollowTerm -
8119 */
8120
8121 static void CalcFollowTerm (pge_TermDesc t, pge_SetDesc after)
8122 {
8123 if (t != NULL)
8124 {
8125 while (t != NULL)
8126 {
8127 CalcFollowFactor (t->factor, after); /* { '|' Term } */
8128 t = t->next; /* { '|' Term } */
8129 }
8130 }
8131 }
8132
8133
8134 /*
8135 CalcFollowExpression -
8136 */
8137
8138 static void CalcFollowExpression (pge_ExpressionDesc e, pge_SetDesc after)
8139 {
8140 if (e != NULL)
8141 {
8142 CalcFollowTerm (e->term, after);
8143 }
8144 }
8145
8146
8147 /*
8148 CalcFollowStatement - given a bnf statement generate the follow set.
8149 */
8150
8151 static void CalcFollowStatement (pge_StatementDesc s)
8152 {
8153 if (s != NULL)
8154 {
8155 CalcFollowExpression (s->expr, NULL);
8156 }
8157 }
8158
8159
8160 /*
8161 CalcFollowProduction -
8162 */
8163
8164 static void CalcFollowProduction (pge_ProductionDesc p)
8165 {
8166 if (p != NULL)
8167 {
8168 CalcFollowStatement (p->statement);
8169 }
8170 }
8171
8172
8173 /*
8174 CalcEpsilonFactor -
8175 */
8176
8177 static void CalcEpsilonFactor (pge_FactorDesc f)
8178 {
8179 while (f != NULL)
8180 {
8181 switch (f->type)
8182 {
8183 case pge_id:
8184 AssignEpsilon ((GetEpsilon (f->ident->definition->followinfo)) != pge_unknown, f->followinfo, GetEpsilon (f->ident->definition->followinfo));
8185 break;
8186
8187 case pge_lit:
8188 AssignEpsilon (true, f->followinfo, pge_false);
8189 break;
8190
8191 case pge_sub:
8192 CalcEpsilonExpression (f->expr);
8193 AssignEpsilon ((GetEpsilon (f->expr->followinfo)) != pge_unknown, f->followinfo, GetEpsilon (f->expr->followinfo));
8194 break;
8195
8196 case pge_m2:
8197 AssignEpsilon (true, f->followinfo, pge_true);
8198 break;
8199
8200 case pge_opt:
8201 case pge_mult:
8202 CalcEpsilonExpression (f->expr);
8203 AssignEpsilon (true, f->followinfo, pge_true);
8204 break;
8205
8206
8207 default:
8208 break;
8209 }
8210 f = f->next;
8211 }
8212 }
8213
8214
8215 /*
8216 CalcEpsilonTerm -
8217 */
8218
8219 static void CalcEpsilonTerm (pge_TermDesc t)
8220 {
8221 if (t != NULL)
8222 {
8223 while (t != NULL)
8224 {
8225 if (t->factor != NULL)
8226 {
8227 switch (GetReachEnd (t->factor->followinfo))
8228 {
8229 case pge_true:
8230 AssignEpsilon (true, t->followinfo, pge_true);
8231 break;
8232
8233 case pge_false:
8234 AssignEpsilon (true, t->followinfo, pge_false);
8235 break;
8236
8237 case pge_unknown:
8238 break;
8239
8240
8241 default:
8242 break;
8243 }
8244 }
8245 CalcEpsilonFactor (t->factor); /* { '|' Term } */
8246 t = t->next;
8247 }
8248 }
8249 }
8250
8251
8252 /*
8253 CalcEpsilonExpression -
8254 */
8255
8256 static void CalcEpsilonExpression (pge_ExpressionDesc e)
8257 {
8258 pge_TermDesc t;
8259 pge_TraverseResult result;
8260
8261 if (e != NULL)
8262 {
8263 CalcEpsilonTerm (e->term);
8264 if ((GetEpsilon (e->followinfo)) == pge_unknown)
8265 {
8266 result = pge_unknown;
8267 t = e->term;
8268 while (t != NULL)
8269 {
8270 if ((GetEpsilon (t->followinfo)) != pge_unknown)
8271 {
8272 stop ();
8273 }
8274 switch (GetEpsilon (t->followinfo))
8275 {
8276 case pge_unknown:
8277 break;
8278
8279 case pge_true:
8280 result = pge_true;
8281 break;
8282
8283 case pge_false:
8284 if (result != pge_true)
8285 {
8286 result = pge_false;
8287 }
8288 break;
8289
8290
8291 default:
8292 break;
8293 }
8294 t = t->next;
8295 }
8296 AssignEpsilon (result != pge_unknown, e->followinfo, result);
8297 }
8298 }
8299 }
8300
8301
8302 /*
8303 CalcEpsilonStatement - given a bnf statement generate the follow set.
8304 */
8305
8306 static void CalcEpsilonStatement (pge_StatementDesc s)
8307 {
8308 if (s != NULL)
8309 {
8310 if (s->expr != NULL)
8311 {
8312 AssignEpsilon ((GetEpsilon (s->expr->followinfo)) != pge_unknown, s->followinfo, GetEpsilon (s->expr->followinfo));
8313 }
8314 CalcEpsilonExpression (s->expr);
8315 }
8316 }
8317
8318
8319 /*
8320 CalcEpsilonProduction -
8321 */
8322
8323 static void CalcEpsilonProduction (pge_ProductionDesc p)
8324 {
8325 if (p != NULL)
8326 {
8327 /*
8328 IF p^.statement^.ident^.name=MakeKey('DefinitionModule')
8329 THEN
8330 stop
8331 END ;
8332 */
8333 if (Debugging)
8334 {
8335 NameKey_WriteKey (p->statement->ident->name);
8336 StrIO_WriteString ((const char *) " calculating epsilon", 21);
8337 StrIO_WriteLn ();
8338 }
8339 AssignEpsilon ((GetEpsilon (p->statement->followinfo)) != pge_unknown, p->followinfo, GetEpsilon (p->statement->followinfo));
8340 CalcEpsilonStatement (p->statement);
8341 }
8342 }
8343
8344
8345 /*
8346 CalcReachEndFactor -
8347 */
8348
8349 static pge_TraverseResult CalcReachEndFactor (pge_FactorDesc f)
8350 {
8351 pge_TraverseResult canreachend;
8352 pge_TraverseResult result;
8353
8354 if (f == NULL)
8355 {
8356 return pge_true; /* we have reached the end of this factor list */
8357 }
8358 else
8359 {
8360 /* we need to traverse all factors even if we can short cut the answer to this list of factors */
8361 result = CalcReachEndFactor (f->next);
8362 switch (f->type)
8363 {
8364 case pge_id:
8365 if (f->ident->definition == NULL)
8366 {
8367 WarnError1 ((const char *) "definition for %s is absent (assuming epsilon is false for this production)", 75, f->ident->name);
8368 result = pge_false;
8369 }
8370 else if (result != pge_false)
8371 {
8372 /* avoid dangling else. */
8373 switch (GetReachEnd (f->ident->definition->followinfo))
8374 {
8375 case pge_false:
8376 result = pge_false;
8377 break;
8378
8379 case pge_true:
8380 break;
8381
8382 case pge_unknown:
8383 result = pge_unknown;
8384 break;
8385
8386
8387 default:
8388 break;
8389 }
8390 }
8391 break;
8392
8393 case pge_lit:
8394 result = pge_false;
8395 break;
8396
8397 case pge_sub:
8398 CalcReachEndExpression (f->expr);
8399 if ((f->expr != NULL) && (result == pge_true))
8400 {
8401 result = GetReachEnd (f->expr->followinfo);
8402 }
8403 break;
8404
8405 case pge_mult:
8406 case pge_opt:
8407 if (f->expr != NULL)
8408 {
8409 /* not interested in the result as expression is optional */
8410 CalcReachEndExpression (f->expr);
8411 }
8412 break;
8413
8414 case pge_m2:
8415 break;
8416
8417
8418 default:
8419 break;
8420 }
8421 AssignReachEnd (result != pge_unknown, f->followinfo, result);
8422 return result;
8423 }
8424 /* static analysis guarentees a RETURN statement will be used before here. */
8425 __builtin_unreachable ();
8426 }
8427
8428
8429 /*
8430 CalcReachEndTerm -
8431 */
8432
8433 static pge_TraverseResult CalcReachEndTerm (pge_TermDesc t)
8434 {
8435 pge_TraverseResult canreachend;
8436 pge_TraverseResult result;
8437
8438 if (t != NULL)
8439 {
8440 canreachend = pge_false;
8441 while (t != NULL)
8442 {
8443 result = CalcReachEndFactor (t->factor);
8444 AssignReachEnd (result != pge_unknown, t->followinfo, result);
8445 switch (result)
8446 {
8447 case pge_true:
8448 canreachend = pge_true;
8449 break;
8450
8451 case pge_false:
8452 break;
8453
8454 case pge_unknown:
8455 if (canreachend == pge_false)
8456 {
8457 canreachend = pge_unknown;
8458 }
8459 break;
8460
8461
8462 default:
8463 break;
8464 }
8465 t = t->next; /* { '|' Term } */
8466 }
8467 return canreachend;
8468 }
8469 /* static analysis guarentees a RETURN statement will be used before here. */
8470 __builtin_unreachable ();
8471 }
8472
8473
8474 /*
8475 CalcReachEndExpression -
8476 */
8477
8478 static void CalcReachEndExpression (pge_ExpressionDesc e)
8479 {
8480 pge_TraverseResult result;
8481
8482 if (e == NULL)
8483 {} /* empty. */
8484 else
8485 {
8486 /* no expression, thus reached the end of this sentance */
8487 result = CalcReachEndTerm (e->term);
8488 AssignReachEnd (result != pge_unknown, e->followinfo, result);
8489 }
8490 }
8491
8492
8493 /*
8494 CalcReachEndStatement -
8495 */
8496
8497 static void CalcReachEndStatement (pge_StatementDesc s)
8498 {
8499 if (s != NULL)
8500 {
8501 if (s->expr != NULL)
8502 {
8503 CalcReachEndExpression (s->expr);
8504 AssignReachEnd ((GetReachEnd (s->expr->followinfo)) != pge_unknown, s->followinfo, GetReachEnd (s->expr->followinfo));
8505 }
8506 }
8507 }
8508
8509
8510 /*
8511 CalcReachEndStatement -
8512 */
8513
8514 static void stop (void)
8515 {
8516 }
8517
8518
8519 /*
8520 CalcReachEndProduction -
8521 */
8522
8523 static void CalcReachEndProduction (pge_ProductionDesc p)
8524 {
8525 if (p != NULL)
8526 {
8527 CalcReachEndStatement (p->statement);
8528 if ((GetReachEnd (p->followinfo)) != pge_unknown)
8529 {
8530 if (Debugging)
8531 {
8532 StrIO_WriteString ((const char *) "already calculated reach end for: ", 34);
8533 NameKey_WriteKey (p->statement->ident->name);
8534 StrIO_WriteString ((const char *) " its value is ", 14);
8535 if ((GetReachEnd (p->followinfo)) == pge_true)
8536 {
8537 StrIO_WriteString ((const char *) "reachable", 9);
8538 }
8539 else
8540 {
8541 StrIO_WriteString ((const char *) "non reachable", 13);
8542 }
8543 StrIO_WriteLn ();
8544 }
8545 }
8546 AssignReachEnd ((GetReachEnd (p->statement->followinfo)) != pge_unknown, p->followinfo, GetReachEnd (p->statement->followinfo));
8547 }
8548 }
8549
8550
8551 /*
8552 EmptyFactor -
8553 */
8554
8555 static bool EmptyFactor (pge_FactorDesc f)
8556 {
8557 while (f != NULL)
8558 {
8559 switch (f->type)
8560 {
8561 case pge_id:
8562 if (! (EmptyProduction (f->ident->definition)))
8563 {
8564 return false;
8565 }
8566 break;
8567
8568 case pge_lit:
8569 return false;
8570 break;
8571
8572 case pge_sub:
8573 if (! (EmptyExpression (f->expr)))
8574 {
8575 return false;
8576 }
8577 break;
8578
8579 case pge_opt:
8580 case pge_mult:
8581 return true;
8582 break;
8583
8584 case pge_m2:
8585 break;
8586
8587
8588 default:
8589 break;
8590 }
8591 f = f->next;
8592 }
8593 return true;
8594 /* static analysis guarentees a RETURN statement will be used before here. */
8595 __builtin_unreachable ();
8596 }
8597
8598
8599 /*
8600 EmptyTerm - returns TRUE if the term maybe empty.
8601 */
8602
8603 static bool EmptyTerm (pge_TermDesc t)
8604 {
8605 while (t != NULL)
8606 {
8607 if (EmptyFactor (t->factor))
8608 {
8609 return true;
8610 }
8611 else
8612 {
8613 t = t->next;
8614 }
8615 }
8616 return false;
8617 /* static analysis guarentees a RETURN statement will be used before here. */
8618 __builtin_unreachable ();
8619 }
8620
8621
8622 /*
8623 EmptyExpression -
8624 */
8625
8626 static bool EmptyExpression (pge_ExpressionDesc e)
8627 {
8628 if (e == NULL)
8629 {
8630 return true;
8631 }
8632 else
8633 {
8634 return EmptyTerm (e->term);
8635 }
8636 /* static analysis guarentees a RETURN statement will be used before here. */
8637 __builtin_unreachable ();
8638 }
8639
8640
8641 /*
8642 EmptyStatement - returns TRUE if statement, s, is empty.
8643 */
8644
8645 static bool EmptyStatement (pge_StatementDesc s)
8646 {
8647 if (s == NULL)
8648 {
8649 return true;
8650 }
8651 else
8652 {
8653 return EmptyExpression (s->expr);
8654 }
8655 /* static analysis guarentees a RETURN statement will be used before here. */
8656 __builtin_unreachable ();
8657 }
8658
8659
8660 /*
8661 EmptyProduction - returns if production, p, maybe empty.
8662 */
8663
8664 static bool EmptyProduction (pge_ProductionDesc p)
8665 {
8666 if (p == NULL)
8667 {
8668 PushBackInput_WarnError ((const char *) "unknown production", 18);
8669 return true;
8670 }
8671 else if (p->firstsolved && (p->first != NULL))
8672 {
8673 /* avoid dangling else. */
8674 /* predefined but first set to something - thus not empty */
8675 return false;
8676 }
8677 else
8678 {
8679 /* avoid dangling else. */
8680 return EmptyStatement (p->statement);
8681 }
8682 /* static analysis guarentees a RETURN statement will be used before here. */
8683 __builtin_unreachable ();
8684 }
8685
8686
8687 /*
8688 EmitFDLNotice -
8689 */
8690
8691 static void EmitFDLNotice (void)
8692 {
8693 Output_WriteString ((const char *) "@c Copyright (C) 2000-2025 Free Software Foundation, Inc.", 57);
8694 Output_WriteLn ();
8695 Output_WriteLn ();
8696 Output_WriteString ((const char *) "@c This file is part of GCC.", 28);
8697 Output_WriteLn ();
8698 Output_WriteString ((const char *) "@c Permission is granted to copy, distribute and/or modify this document", 72);
8699 Output_WriteLn ();
8700 Output_WriteString ((const char *) "@c under the terms of the GNU Free Documentation License, Version 1.2 or", 72);
8701 Output_WriteLn ();
8702 Output_WriteString ((const char *) "@c any later version published by the Free Software Foundation.", 63);
8703 Output_WriteLn ();
8704 }
8705
8706
8707 /*
8708 EmitRules - generates the BNF rules.
8709 */
8710
8711 static void EmitRules (void)
8712 {
8713 if (Texinfo && FreeDocLicense)
8714 {
8715 EmitFDLNotice ();
8716 }
8717 ForeachRuleDo ((pge_DoProcedure) {(pge_DoProcedure_t) EmitRule});
8718 }
8719
8720
8721 /*
8722 DescribeElement -
8723 */
8724
8725 static void DescribeElement (unsigned int name)
8726 {
8727 NameKey_Name lit;
8728
8729 if (InitialElement)
8730 {
8731 InitialElement = false;
8732 }
8733 else
8734 {
8735 Output_WriteString ((const char *) " |", 2);
8736 }
8737 Output_WriteLn ();
8738 Indent = 3;
8739 IndentString ((const char *) "", 0);
8740 Output_WriteKey (name);
8741 Output_WriteString ((const char *) ": ", 2);
8742 lit = static_cast<NameKey_Name> (SymbolKey_GetSymKey (ReverseAliases, name));
8743 if ((NameKey_MakeKey ((const char *) "\"", 1)) == lit)
8744 {
8745 Output_WriteString ((const char *) "str := ConCat(ConCatChar(ConCatChar(InitString(\"syntax error, found ", 68);
8746 Output_Write ('\'');
8747 Output_WriteString ((const char *) "\"), ", 4);
8748 Output_Write ('\'');
8749 Output_Write ('"');
8750 Output_Write ('\'');
8751 Output_WriteString ((const char *) "), ", 3);
8752 Output_Write ('"');
8753 Output_Write ('\'');
8754 Output_Write ('"');
8755 Output_WriteString ((const char *) "), Mark(str))", 13);
8756 }
8757 else if ((NameKey_MakeKey ((const char *) "'", 1)) == lit)
8758 {
8759 /* avoid dangling else. */
8760 Output_WriteString ((const char *) "str := ConCat(ConCatChar(ConCatChar(InitString('syntax error, found ", 68);
8761 Output_Write ('"');
8762 Output_WriteString ((const char *) "'), ", 4);
8763 Output_Write ('"');
8764 Output_Write ('\'');
8765 Output_Write ('"');
8766 Output_WriteString ((const char *) "), ", 3);
8767 Output_Write ('\'');
8768 Output_Write ('"');
8769 Output_Write ('\'');
8770 Output_WriteString ((const char *) "), Mark(str))", 13);
8771 }
8772 else
8773 {
8774 /* avoid dangling else. */
8775 Output_WriteString ((const char *) "str := ConCat(InitString(", 25);
8776 Output_Write ('"');
8777 Output_WriteString ((const char *) "syntax error, found ", 20);
8778 KeyWord (lit);
8779 Output_WriteString ((const char *) "\"), Mark(str))", 14);
8780 }
8781 }
8782
8783
8784 /*
8785 EmitInTestStop - construct a test for stop element, name.
8786 */
8787
8788 static void EmitInTestStop (NameKey_Name name)
8789 {
8790 unsigned int i;
8791 unsigned int value;
8792
8793 if (LargestValue <= MaxElementsInSet)
8794 {
8795 Output_WriteKey (name);
8796 Output_WriteString ((const char *) " IN stopset", 11);
8797 ParametersUsed |= (1 << (0 ));
8798 }
8799 else
8800 {
8801 value = static_cast<unsigned int> (SymbolKey_GetSymKey (Values, name));
8802 i = value / MaxElementsInSet;
8803 Output_WriteKey (name);
8804 Output_WriteString ((const char *) " IN stopset", 11);
8805 Output_WriteCard (i, 0);
8806 ParametersUsed |= (1 << (i ));
8807 }
8808 }
8809
8810
8811 /*
8812 DescribeStopElement -
8813 */
8814
8815 static void DescribeStopElement (unsigned int name)
8816 {
8817 NameKey_Name lit;
8818
8819 Indent = 3;
8820 IndentString ((const char *) "IF ", 3);
8821 EmitInTestStop (name);
8822 Output_WriteLn ();
8823 IndentString ((const char *) "THEN", 4);
8824 Output_WriteLn ();
8825 Indent = 6;
8826 lit = static_cast<NameKey_Name> (SymbolKey_GetSymKey (ReverseAliases, name));
8827 if ((lit == NameKey_NulName) || (lit == (NameKey_MakeKey ((const char *) "", 0))))
8828 {
8829 IndentString ((const char *) "(* ", 3);
8830 Output_WriteKey (name);
8831 Output_WriteString ((const char *) " has no token name (needed to generate error messages) *)", 57);
8832 }
8833 else if ((NameKey_MakeKey ((const char *) "'", 1)) == lit)
8834 {
8835 /* avoid dangling else. */
8836 IndentString ((const char *) "message := ConCatChar(ConCatChar(ConCatChar(ConCatChar(ConCatChar(message, ", 75);
8837 Output_WriteString ((const char *) "' '), ", 6);
8838 Output_Write ('\'');
8839 Output_Write ('"');
8840 Output_WriteString ((const char *) "'), ", 4);
8841 Output_Write ('"');
8842 Output_Write ('\'');
8843 Output_WriteString ((const char *) "\"), ", 4);
8844 Output_Write ('\'');
8845 Output_Write ('"');
8846 Output_WriteString ((const char *) "'), ',') ; INC(n) ; ", 20);
8847 }
8848 else if ((NameKey_MakeKey ((const char *) "\"", 1)) == lit)
8849 {
8850 /* avoid dangling else. */
8851 IndentString ((const char *) "message := ConCatChar(ConCatChar(ConCatChar(ConCatChar(ConCatChar(message, ", 75);
8852 Output_WriteString ((const char *) "\" \"), ", 6);
8853 Output_Write ('"');
8854 Output_Write ('`');
8855 Output_WriteString ((const char *) "\"), ", 4);
8856 Output_Write ('\'');
8857 Output_Write ('"');
8858 Output_WriteString ((const char *) "'), ", 4);
8859 Output_Write ('"');
8860 Output_Write ('\'');
8861 Output_WriteString ((const char *) "\"), \",\") ; INC(n) ; ", 20);
8862 }
8863 else
8864 {
8865 /* avoid dangling else. */
8866 IndentString ((const char *) "message := ConCat(ConCatChar(message, ' ", 40);
8867 Output_WriteString ((const char *) "'), ", 4);
8868 Output_WriteString ((const char *) "Mark(InitString(\"", 17);
8869 KeyWord (lit);
8870 Output_Write ('"');
8871 Output_WriteString ((const char *) "))) ; INC(n)", 12);
8872 }
8873 Output_WriteLn ();
8874 Indent = 3;
8875 IndentString ((const char *) "END ;", 5);
8876 Output_WriteLn ();
8877 }
8878
8879
8880 /*
8881 EmitDescribeStop -
8882 */
8883
8884 static void EmitDescribeStop (void)
8885 {
8886 DynamicStrings_String s;
8887
8888 Output_WriteLn ();
8889 Indent = 0;
8890 IndentString ((const char *) "(*", 2);
8891 Indent = 3;
8892 Output_WriteLn ();
8893 IndentString ((const char *) "DescribeStop - issues a message explaining what tokens were expected", 68);
8894 Output_WriteLn ();
8895 Output_WriteString ((const char *) "*)", 2);
8896 Output_WriteLn ();
8897 Output_WriteLn ();
8898 Indent = 0;
8899 IndentString ((const char *) "PROCEDURE DescribeStop (", 24);
8900 ParametersUsed = (unsigned int) 0;
8901 Output_StartBuffer ();
8902 Output_WriteString ((const char *) ") : String ;", 12);
8903 Output_WriteLn ();
8904 IndentString ((const char *) "VAR", 3);
8905 Output_WriteLn ();
8906 Indent = 3;
8907 IndentString ((const char *) "n : CARDINAL ;", 19);
8908 Output_WriteLn ();
8909 IndentString ((const char *) "str,", 4);
8910 Output_WriteLn ();
8911 IndentString ((const char *) "message: String ;", 17);
8912 Output_WriteLn ();
8913 Indent = 0;
8914 IndentString ((const char *) "BEGIN", 5);
8915 Output_WriteLn ();
8916 Indent = 3;
8917 IndentString ((const char *) "n := 0 ;", 8);
8918 Output_WriteLn ();
8919 IndentString ((const char *) "message := InitString('') ;", 27);
8920 Output_WriteLn ();
8921 SymbolKey_ForeachNodeDo (Aliases, (SymbolKey_PerformOperation) {(SymbolKey_PerformOperation_t) DescribeStopElement});
8922 Output_WriteLn ();
8923 Indent = 3;
8924 IndentString ((const char *) "IF n=0", 6);
8925 Output_WriteLn ();
8926 IndentString ((const char *) "THEN", 4);
8927 Output_WriteLn ();
8928 Indent = 6;
8929 IndentString ((const char *) "str := InitString(' syntax error') ; ", 37);
8930 Output_WriteLn ();
8931 IndentString ((const char *) "message := KillString(message) ; ", 33);
8932 Output_WriteLn ();
8933 Indent = 3;
8934 IndentString ((const char *) "ELSIF n=1", 9);
8935 Output_WriteLn ();
8936 IndentString ((const char *) "THEN", 4);
8937 Output_WriteLn ();
8938 Indent = 6;
8939 IndentString ((const char *) "str := ConCat(message, Mark(InitString(' missing '))) ;", 55);
8940 Output_WriteLn ();
8941 Indent = 3;
8942 IndentString ((const char *) "ELSE", 4);
8943 Output_WriteLn ();
8944 Indent = 6;
8945 IndentString ((const char *) "str := ConCat(InitString(' expecting one of'), message) ;", 57);
8946 Output_WriteLn ();
8947 IndentString ((const char *) "message := KillString(message) ;", 32);
8948 Output_WriteLn ();
8949 Indent = 3;
8950 IndentString ((const char *) "END ;", 5);
8951 Output_WriteLn ();
8952 IndentString ((const char *) "RETURN( str )", 13);
8953 Output_WriteLn ();
8954 Indent = 0;
8955 IndentString ((const char *) "END DescribeStop ;", 18);
8956 Output_WriteLn ();
8957 Output_WriteLn ();
8958 s = Output_EndBuffer ();
8959 EmitStopParameters (true);
8960 Output_KillWriteS (s);
8961 }
8962
8963
8964 /*
8965 EmitDescribeError -
8966 */
8967
8968 static void EmitDescribeError (void)
8969 {
8970 Output_WriteLn ();
8971 Indent = 0;
8972 IndentString ((const char *) "(*", 2);
8973 Output_WriteLn ();
8974 Indent = 3;
8975 IndentString ((const char *) "DescribeError - issues a message explaining what tokens were expected", 69);
8976 Output_WriteLn ();
8977 Indent = 0;
8978 IndentString ((const char *) "*)", 2);
8979 Output_WriteLn ();
8980 Output_WriteLn ();
8981 IndentString ((const char *) "PROCEDURE DescribeError ;", 25);
8982 Output_WriteLn ();
8983 IndentString ((const char *) "VAR", 3);
8984 Output_WriteLn ();
8985 Indent = 3;
8986 IndentString ((const char *) "str: String ;", 13);
8987 Output_WriteLn ();
8988 Indent = 0;
8989 IndentString ((const char *) "BEGIN", 5);
8990 Output_WriteLn ();
8991 Indent = 3;
8992 IndentString ((const char *) "str := InitString('') ;", 23);
8993 Output_WriteLn ();
8994 /* was
8995 IndentString('str := DescribeStop(') ; EmitStopParameters(FALSE) ; Output.WriteString(') ;') ; Output.WriteLn ;
8996 */
8997 IndentString ((const char *) "CASE ", 5);
8998 WriteGetTokenType ();
8999 Output_WriteString ((const char *) " OF", 3);
9000 NewLine (3);
9001 InitialElement = true;
9002 SymbolKey_ForeachNodeDo (Aliases, (SymbolKey_PerformOperation) {(SymbolKey_PerformOperation_t) DescribeElement});
9003 Output_WriteLn ();
9004 Indent = 3;
9005 IndentString ((const char *) "ELSE", 4);
9006 Output_WriteLn ();
9007 IndentString ((const char *) "END ;", 5);
9008 Output_WriteLn ();
9009 IndentString ((const char *) "", 0);
9010 Output_WriteKey (ErrorProcString);
9011 Output_WriteString ((const char *) "(str) ;", 7);
9012 Output_WriteLn ();
9013 Indent = 0;
9014 IndentString ((const char *) "END DescribeError ;", 19);
9015 Output_WriteLn ();
9016 }
9017
9018
9019 /*
9020 EmitSetTypes - write out the set types used during error recovery
9021 */
9022
9023 static void EmitSetTypes (void)
9024 {
9025 unsigned int i;
9026 unsigned int j;
9027 unsigned int m;
9028 unsigned int n;
9029
9030 Output_WriteString ((const char *) "(*", 2);
9031 NewLine (3);
9032 Output_WriteString ((const char *) "expecting token set defined as an enumerated type", 49);
9033 NewLine (3);
9034 Output_WriteString ((const char *) "(", 1);
9035 i = 0;
9036 while (i < LargestValue)
9037 {
9038 Output_WriteKey (SymbolKey_GetSymKey (ReverseValues, (unsigned int ) (i)));
9039 i += 1;
9040 if (i < LargestValue)
9041 {
9042 Output_WriteString ((const char *) ", ", 2);
9043 }
9044 }
9045 Output_WriteString ((const char *) ") ;", 3);
9046 NewLine (0);
9047 Output_WriteString ((const char *) "*)", 2);
9048 NewLine (0);
9049 Output_WriteString ((const char *) "TYPE", 4);
9050 NewLine (3);
9051 if (LargestValue > MaxElementsInSet)
9052 {
9053 i = 0;
9054 n = LargestValue / MaxElementsInSet;
9055 while (i <= n)
9056 {
9057 j = i*MaxElementsInSet;
9058 if (LargestValue < (((i+1)*MaxElementsInSet)-1))
9059 {
9060 m = LargestValue-1;
9061 }
9062 else
9063 {
9064 m = ((i+1)*MaxElementsInSet)-1;
9065 }
9066 Output_WriteString ((const char *) "stop", 4);
9067 Output_WriteCard (i, 0);
9068 Output_WriteString ((const char *) " = [", 4);
9069 Output_WriteKey (SymbolKey_GetSymKey (ReverseValues, (unsigned int ) (j)));
9070 Output_WriteString ((const char *) "..", 2);
9071 Output_WriteKey (SymbolKey_GetSymKey (ReverseValues, (unsigned int ) (m)));
9072 Output_WriteString ((const char *) "] ;", 3);
9073 NewLine (3);
9074 Output_WriteString ((const char *) "SetOfStop", 9);
9075 Output_WriteCard (i, 0);
9076 Output_WriteString ((const char *) " = SET OF stop", 14);
9077 Output_WriteCard (i, 0);
9078 Output_WriteString ((const char *) " ;", 2);
9079 NewLine (3);
9080 i += 1;
9081 }
9082 }
9083 else
9084 {
9085 Output_WriteString ((const char *) "SetOfStop", 9);
9086 Output_WriteString ((const char *) " = SET OF [", 11);
9087 Output_WriteKey (SymbolKey_GetSymKey (ReverseValues, (unsigned int ) (0)));
9088 Output_WriteString ((const char *) "..", 2);
9089 Output_WriteKey (SymbolKey_GetSymKey (ReverseValues, (unsigned int ) (LargestValue-1)));
9090 Output_WriteString ((const char *) "] ;", 3);
9091 }
9092 NewLine (0);
9093 }
9094
9095
9096 /*
9097 EmitSupport - generates the support routines.
9098 */
9099
9100 static void EmitSupport (void)
9101 {
9102 if (ErrorRecovery)
9103 {
9104 EmitSetTypes ();
9105 EmitDescribeStop ();
9106 EmitDescribeError ();
9107 }
9108 }
9109
9110
9111 /*
9112 DisposeSetDesc - dispose of the set list, s.
9113 */
9114
9115 static void DisposeSetDesc (pge_SetDesc *s)
9116 {
9117 pge_SetDesc h;
9118 pge_SetDesc n;
9119
9120 if ((*s) != NULL)
9121 {
9122 h = (*s);
9123 n = (*s)->next;
9124 do {
9125 Storage_DEALLOCATE ((void **) &h, sizeof (pge__T7));
9126 h = n;
9127 if (n != NULL)
9128 {
9129 n = n->next;
9130 }
9131 } while (! (h == NULL));
9132 (*s) = NULL;
9133 }
9134 }
9135
9136
9137 /*
9138 OptionalFactor -
9139 */
9140
9141 static bool OptionalFactor (pge_FactorDesc f)
9142 {
9143 while (f != NULL)
9144 {
9145 switch (f->type)
9146 {
9147 case pge_id:
9148 break;
9149
9150 case pge_lit:
9151 break;
9152
9153 case pge_sub:
9154 case pge_opt:
9155 case pge_mult:
9156 if (OptionalExpression (f->expr))
9157 {
9158 return true;
9159 }
9160 break;
9161
9162 case pge_m2:
9163 break;
9164
9165
9166 default:
9167 break;
9168 }
9169 f = f->next;
9170 }
9171 return false;
9172 /* static analysis guarentees a RETURN statement will be used before here. */
9173 __builtin_unreachable ();
9174 }
9175
9176
9177 /*
9178 OptionalTerm - returns TRUE if the term maybe empty.
9179 */
9180
9181 static bool OptionalTerm (pge_TermDesc t)
9182 {
9183 pge_TermDesc u;
9184 pge_TermDesc v;
9185 pge_SetDesc tov;
9186 pge_SetDesc tou;
9187
9188 u = t;
9189 while (u != NULL)
9190 {
9191 if (OptionalFactor (u->factor))
9192 {
9193 return true;
9194 }
9195 v = t;
9196 tou = NULL;
9197 CalcFirstFactor (u->factor, NULL, &tou);
9198 while (v != NULL)
9199 {
9200 if (v != u)
9201 {
9202 tov = NULL;
9203 CalcFirstFactor (v->factor, NULL, &tov);
9204 if (IntersectionIsNil (tov, tou))
9205 {
9206 DisposeSetDesc (&tov);
9207 }
9208 else
9209 {
9210 StrIO_WriteString ((const char *) "problem with two first sets. Set 1: ", 36);
9211 EmitSet (tou, static_cast<unsigned int> (0), static_cast<unsigned int> (0));
9212 StrIO_WriteLn ();
9213 StrIO_WriteString ((const char *) " Set 2: ", 36);
9214 EmitSet (tov, static_cast<unsigned int> (0), static_cast<unsigned int> (0));
9215 StrIO_WriteLn ();
9216 DisposeSetDesc (&tou);
9217 DisposeSetDesc (&tov);
9218 return true;
9219 }
9220 }
9221 v = v->next;
9222 }
9223 DisposeSetDesc (&tou);
9224 u = u->next;
9225 }
9226 return false;
9227 /* static analysis guarentees a RETURN statement will be used before here. */
9228 __builtin_unreachable ();
9229 }
9230
9231
9232 /*
9233 OptionalExpression -
9234 */
9235
9236 static bool OptionalExpression (pge_ExpressionDesc e)
9237 {
9238 if (e == NULL)
9239 {
9240 return false;
9241 }
9242 else
9243 {
9244 return OptionalTerm (e->term);
9245 }
9246 /* static analysis guarentees a RETURN statement will be used before here. */
9247 __builtin_unreachable ();
9248 }
9249
9250
9251 /*
9252 OptionalStatement - returns FALSE if statement, s, does not have a optional ambiguity.
9253 */
9254
9255 static bool OptionalStatement (pge_StatementDesc s)
9256 {
9257 if (s == NULL)
9258 {
9259 return false;
9260 }
9261 else
9262 {
9263 return OptionalExpression (s->expr);
9264 }
9265 /* static analysis guarentees a RETURN statement will be used before here. */
9266 __builtin_unreachable ();
9267 }
9268
9269
9270 /*
9271 OptionalProduction -
9272 */
9273
9274 static bool OptionalProduction (pge_ProductionDesc p)
9275 {
9276 if (p == NULL)
9277 {
9278 return false;
9279 }
9280 else
9281 {
9282 return OptionalStatement (p->statement);
9283 }
9284 /* static analysis guarentees a RETURN statement will be used before here. */
9285 __builtin_unreachable ();
9286 }
9287
9288
9289 /*
9290 CheckFirstFollow -
9291 */
9292
9293 static bool CheckFirstFollow (pge_FactorDesc f, pge_FactorDesc after)
9294 {
9295 pge_SetDesc first;
9296 pge_SetDesc follow;
9297
9298 first = NULL;
9299 CalcFirstFactor (f, NULL, &first);
9300 follow = NULL;
9301 follow = GetFollow (f->followinfo);
9302 if (IntersectionIsNil (first, follow))
9303 {
9304 DisposeSetDesc (&first);
9305 DisposeSetDesc (&follow);
9306 return false;
9307 }
9308 else
9309 {
9310 PrettyCommentFactor (f, 3);
9311 NewLine (3);
9312 StrIO_WriteString ((const char *) "first: ", 7);
9313 EmitSet (first, static_cast<unsigned int> (0), static_cast<unsigned int> (0));
9314 NewLine (3);
9315 StrIO_WriteString ((const char *) "follow: ", 8);
9316 EmitSet (follow, static_cast<unsigned int> (0), static_cast<unsigned int> (0));
9317 NewLine (3);
9318 DisposeSetDesc (&first);
9319 DisposeSetDesc (&follow);
9320 return true;
9321 }
9322 /* static analysis guarentees a RETURN statement will be used before here. */
9323 __builtin_unreachable ();
9324 }
9325
9326
9327 /*
9328 ConstrainedEmptyFactor -
9329 */
9330
9331 static bool ConstrainedEmptyFactor (pge_FactorDesc f)
9332 {
9333 while (f != NULL)
9334 {
9335 switch (f->type)
9336 {
9337 case pge_id:
9338 break;
9339
9340 case pge_lit:
9341 break;
9342
9343 case pge_sub:
9344 case pge_opt:
9345 case pge_mult:
9346 if (ConstrainedEmptyExpression (f->expr))
9347 {
9348 return true;
9349 }
9350 break;
9351
9352 case pge_m2:
9353 break;
9354
9355
9356 default:
9357 break;
9358 }
9359 if (((f->type != pge_m2) && (EmptyFactor (f))) && (CheckFirstFollow (f, f->next)))
9360 {
9361 return true;
9362 }
9363 f = f->next;
9364 }
9365 return false;
9366 /* static analysis guarentees a RETURN statement will be used before here. */
9367 __builtin_unreachable ();
9368 }
9369
9370
9371 /*
9372 ConstrainedEmptyTerm - returns TRUE if the term maybe empty.
9373 */
9374
9375 static bool ConstrainedEmptyTerm (pge_TermDesc t)
9376 {
9377 pge_SetDesc first;
9378 pge_SetDesc follow;
9379
9380 while (t != NULL)
9381 {
9382 if (ConstrainedEmptyFactor (t->factor))
9383 {
9384 return true;
9385 }
9386 else if (((t->factor->type != pge_m2) && (EmptyFactor (t->factor))) && (CheckFirstFollow (t->factor, t->factor->next)))
9387 {
9388 /* avoid dangling else. */
9389 return true;
9390 }
9391 t = t->next;
9392 }
9393 return false;
9394 /* static analysis guarentees a RETURN statement will be used before here. */
9395 __builtin_unreachable ();
9396 }
9397
9398
9399 /*
9400 ConstrainedEmptyExpression -
9401 */
9402
9403 static bool ConstrainedEmptyExpression (pge_ExpressionDesc e)
9404 {
9405 if (e == NULL)
9406 {
9407 return false;
9408 }
9409 else
9410 {
9411 return ConstrainedEmptyTerm (e->term);
9412 }
9413 /* static analysis guarentees a RETURN statement will be used before here. */
9414 __builtin_unreachable ();
9415 }
9416
9417
9418 /*
9419 ConstrainedEmptyStatement - returns FALSE if statement, s, does not have a optional ambiguity.
9420 */
9421
9422 static bool ConstrainedEmptyStatement (pge_StatementDesc s)
9423 {
9424 if (s == NULL)
9425 {
9426 return false;
9427 }
9428 else
9429 {
9430 return ConstrainedEmptyExpression (s->expr);
9431 }
9432 /* static analysis guarentees a RETURN statement will be used before here. */
9433 __builtin_unreachable ();
9434 }
9435
9436
9437 /*
9438 ConstrainedEmptyProduction - returns TRUE if a problem exists with, p.
9439 */
9440
9441 static bool ConstrainedEmptyProduction (pge_ProductionDesc p)
9442 {
9443 if (p == NULL)
9444 {
9445 return false;
9446 }
9447 else
9448 {
9449 return ConstrainedEmptyStatement (p->statement);
9450 }
9451 /* static analysis guarentees a RETURN statement will be used before here. */
9452 __builtin_unreachable ();
9453 }
9454
9455
9456 /*
9457 TestForLALR1 -
9458 */
9459
9460 static void TestForLALR1 (pge_ProductionDesc p)
9461 {
9462 if (OptionalProduction (p))
9463 {
9464 WarnError1 ((const char *) "production %s has two optional sentances using | which both have the same start symbols", 87, p->statement->ident->name);
9465 WasNoError = false;
9466 PrettyCommentProduction (p);
9467 }
9468 }
9469
9470
9471 /*
9472 DoEpsilon - runs the epsilon interrelated rules
9473 */
9474
9475 static void DoEpsilon (pge_ProductionDesc p)
9476 {
9477 CalcEpsilonProduction (p);
9478 CalcReachEndProduction (p);
9479 }
9480
9481
9482 /*
9483 CheckComplete - checks that production, p, is complete.
9484 */
9485
9486 static void CheckComplete (pge_ProductionDesc p)
9487 {
9488 if ((GetReachEnd (p->followinfo)) == pge_unknown)
9489 {
9490 PrettyCommentProduction (p);
9491 WarnError1 ((const char *) "cannot determine epsilon, probably a left recursive rule in %s and associated rules (hint rewrite using ebnf and eliminate left recursion)", 138, p->statement->ident->name);
9492 WasNoError = false;
9493 }
9494 }
9495
9496
9497 /*
9498 PostProcessRules - backpatch the ident to rule definitions and emit comments and code.
9499 */
9500
9501 static void PostProcessRules (void)
9502 {
9503 ForeachRuleDo ((pge_DoProcedure) {(pge_DoProcedure_t) BackPatchIdentToDefinitions});
9504 if (! WasNoError)
9505 {
9506 M2RTS_HALT (-1);
9507 __builtin_unreachable ();
9508 }
9509 WhileNotCompleteDo ((pge_DoProcedure) {(pge_DoProcedure_t) DoEpsilon});
9510 if (! WasNoError)
9511 {
9512 M2RTS_HALT (-1);
9513 __builtin_unreachable ();
9514 }
9515 ForeachRuleDo ((pge_DoProcedure) {(pge_DoProcedure_t) CheckComplete});
9516 if (! WasNoError)
9517 {
9518 M2RTS_HALT (-1);
9519 __builtin_unreachable ();
9520 }
9521 WhileNotCompleteDo ((pge_DoProcedure) {(pge_DoProcedure_t) CalculateFirstAndFollow});
9522 if (! WasNoError)
9523 {
9524 M2RTS_HALT (-1);
9525 __builtin_unreachable ();
9526 }
9527 ForeachRuleDo ((pge_DoProcedure) {(pge_DoProcedure_t) TestForLALR1});
9528 if (! WasNoError)
9529 {
9530 ForeachRuleDo ((pge_DoProcedure) {(pge_DoProcedure_t) PrettyCommentProduction});
9531 }
9532 }
9533
9534
9535 /*
9536 DisplayHelp - display a summary help and then exit (0).
9537 */
9538
9539 static void DisplayHelp (void)
9540 {
9541 StrIO_WriteString ((const char *) "Usage: pge [-l] [-c] [-d] [-e] [-k] [-t] [-k] [-p] [-x] [-f] [-o outputfile] filename", 85);
9542 StrIO_WriteLn ();
9543 StrIO_WriteString ((const char *) " -l suppress file and line source information", 59);
9544 StrIO_WriteLn ();
9545 StrIO_WriteString ((const char *) " -c do not generate any Modula-2 code within the parser rules", 75);
9546 StrIO_WriteLn ();
9547 StrIO_WriteString ((const char *) " -h or --help generate this help message", 44);
9548 StrIO_WriteLn ();
9549 StrIO_WriteString ((const char *) " -e do not generate a parser with error recovery", 62);
9550 StrIO_WriteLn ();
9551 StrIO_WriteString ((const char *) " -k generate keyword errors with GCC formatting directives", 72);
9552 StrIO_WriteLn ();
9553 StrIO_WriteString ((const char *) " -d generate internal debugging information", 57);
9554 StrIO_WriteLn ();
9555 StrIO_WriteString ((const char *) " -p only display the ebnf rules", 45);
9556 StrIO_WriteLn ();
9557 StrIO_WriteString ((const char *) " -t generate texinfo formating for pretty printing (-p)", 69);
9558 StrIO_WriteLn ();
9559 StrIO_WriteString ((const char *) " -x generate sphinx formating for pretty printing (-p)", 68);
9560 StrIO_WriteLn ();
9561 StrIO_WriteString ((const char *) " -f generate GNU Free Documentation header before pretty printing in texinfo", 90);
9562 StrIO_WriteLn ();
9563 StrIO_WriteString ((const char *) " -o write output to filename", 42);
9564 StrIO_WriteLn ();
9565 libc_exit (0);
9566 }
9567
9568
9569 /*
9570 ParseArgs -
9571 */
9572
9573 static void ParseArgs (void)
9574 {
9575 unsigned int n;
9576 unsigned int i;
9577
9578 ErrorRecovery = true; /* DefaultRecovery ; */
9579 Debugging = false; /* DefaultRecovery ; */
9580 PrettyPrint = false;
9581 KeywordFormatting = false;
9582 i = 1;
9583 n = Args_Narg ();
9584 while (i < n)
9585 {
9586 if (Args_GetArg ((char *) &ArgName.array[0], MaxFileName, i))
9587 {
9588 /* avoid gcc warning by using compound statement even if not strictly necessary. */
9589 if (StrLib_StrEqual ((const char *) &ArgName.array[0], MaxFileName, (const char *) "-e", 2))
9590 {
9591 ErrorRecovery = false;
9592 }
9593 else if (StrLib_StrEqual ((const char *) &ArgName.array[0], MaxFileName, (const char *) "-d", 2))
9594 {
9595 /* avoid dangling else. */
9596 Debugging = true;
9597 bnflex_SetDebugging (true);
9598 }
9599 else if (StrLib_StrEqual ((const char *) &ArgName.array[0], MaxFileName, (const char *) "-c", 2))
9600 {
9601 /* avoid dangling else. */
9602 EmitCode = false;
9603 }
9604 else if (StrLib_StrEqual ((const char *) &ArgName.array[0], MaxFileName, (const char *) "-k", 2))
9605 {
9606 /* avoid dangling else. */
9607 KeywordFormatting = true;
9608 }
9609 else if (StrLib_StrEqual ((const char *) &ArgName.array[0], MaxFileName, (const char *) "-l", 2))
9610 {
9611 /* avoid dangling else. */
9612 SuppressFileLineTag = true;
9613 }
9614 else if ((StrLib_StrEqual ((const char *) &ArgName.array[0], MaxFileName, (const char *) "-h", 2)) || (StrLib_StrEqual ((const char *) &ArgName.array[0], MaxFileName, (const char *) "--help", 6)))
9615 {
9616 /* avoid dangling else. */
9617 DisplayHelp ();
9618 }
9619 else if (StrLib_StrEqual ((const char *) &ArgName.array[0], MaxFileName, (const char *) "-p", 2))
9620 {
9621 /* avoid dangling else. */
9622 PrettyPrint = true;
9623 }
9624 else if (StrLib_StrEqual ((const char *) &ArgName.array[0], MaxFileName, (const char *) "-t", 2))
9625 {
9626 /* avoid dangling else. */
9627 Texinfo = true;
9628 }
9629 else if (StrLib_StrEqual ((const char *) &ArgName.array[0], MaxFileName, (const char *) "-x", 2))
9630 {
9631 /* avoid dangling else. */
9632 Sphinx = true;
9633 }
9634 else if (StrLib_StrEqual ((const char *) &ArgName.array[0], MaxFileName, (const char *) "-f", 2))
9635 {
9636 /* avoid dangling else. */
9637 FreeDocLicense = true;
9638 }
9639 else if (StrLib_StrEqual ((const char *) &ArgName.array[0], MaxFileName, (const char *) "-o", 2))
9640 {
9641 /* avoid dangling else. */
9642 i += 1;
9643 if (Args_GetArg ((char *) &ArgName.array[0], MaxFileName, i))
9644 {
9645 if (! (Output_Open ((const char *) &ArgName.array[0], MaxFileName)))
9646 {
9647 StrIO_WriteString ((const char *) "cannot open ", 12);
9648 StrIO_WriteString ((const char *) &ArgName.array[0], MaxFileName);
9649 StrIO_WriteString ((const char *) " for writing", 12);
9650 StrIO_WriteLn ();
9651 libc_exit (1);
9652 }
9653 }
9654 }
9655 else if (bnflex_OpenSource ((const char *) &ArgName.array[0], MaxFileName))
9656 {
9657 /* avoid dangling else. */
9658 StrLib_StrCopy ((const char *) &ArgName.array[0], MaxFileName, (char *) &FileName.array[0], MaxFileName);
9659 bnflex_AdvanceToken ();
9660 }
9661 else
9662 {
9663 /* avoid dangling else. */
9664 StrIO_WriteString ((const char *) "cannot open ", 12);
9665 StrIO_WriteString ((const char *) &ArgName.array[0], MaxFileName);
9666 StrIO_WriteString ((const char *) " for reading", 12);
9667 StrIO_WriteLn ();
9668 libc_exit (1);
9669 }
9670 }
9671 i += 1;
9672 }
9673 if (n == 1)
9674 {
9675 DisplayHelp ();
9676 }
9677 }
9678
9679
9680 /*
9681 Init - initialize the modules data structures
9682 */
9683
9684 static void Init (void)
9685 {
9686 WasNoError = true;
9687 Texinfo = false;
9688 Sphinx = false;
9689 FreeDocLicense = false;
9690 EmitCode = true;
9691 LargestValue = 0;
9692 HeadProduction = NULL;
9693 CurrentProduction = NULL;
9694 SymbolKey_InitTree (&Aliases);
9695 SymbolKey_InitTree (&ReverseAliases);
9696 SymbolKey_InitTree (&Values);
9697 SymbolKey_InitTree (&ReverseValues);
9698 LastLineNo = 0;
9699 CodePrologue = NULL;
9700 CodeEpilogue = NULL;
9701 CodeDeclaration = NULL;
9702 ErrorProcArray = NameKey_MakeKey ((const char *) "Error", 5);
9703 ErrorProcString = NameKey_MakeKey ((const char *) "ErrorS", 6);
9704 TokenTypeProc = NameKey_MakeKey ((const char *) "GetCurrentTokenType()", 21);
9705 SymIsProc = NameKey_MakeKey ((const char *) "SymIs", 5);
9706 OnLineStart = true;
9707 ParseArgs ();
9708 Main (static_cast<pge_SetOfStop> ((unsigned int) ((1 << (bnflex_eoftok))))); /* this line will be manipulated by sed in buildpg */
9709 if (WasNoError) /* this line will be manipulated by sed in buildpg */
9710 {
9711 PostProcessRules ();
9712 if (WasNoError)
9713 {
9714 /* avoid gcc warning by using compound statement even if not strictly necessary. */
9715 if (Debugging)
9716 {
9717 EmitRules ();
9718 }
9719 else if (PrettyPrint)
9720 {
9721 /* avoid dangling else. */
9722 EmitRules ();
9723 }
9724 else
9725 {
9726 /* avoid dangling else. */
9727 Output_WriteString ((const char *) "(* it is advisable not to edit this file as it was automatically generated from the grammer file ", 97);
9728 Output_WriteString ((const char *) &FileName.array[0], MaxFileName);
9729 Output_WriteString ((const char *) " *)", 3);
9730 Output_WriteLn ();
9731 OnLineStart = false;
9732 EmitFileLineTag (LinePrologue);
9733 BeginningOfLine = true;
9734 WriteCodeHunkList (CodePrologue);
9735 EmitSupport ();
9736 EmitFileLineTag (LineDeclaration);
9737 WriteCodeHunkList (CodeDeclaration);
9738 EmitRules ();
9739 /* code rules */
9740 EmitFileLineTag (LineEpilogue);
9741 WriteCodeHunkList (CodeEpilogue);
9742 }
9743 }
9744 }
9745 Output_Close ();
9746 }
9747
9748 extern "C" void _M2_pge_init (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
9749 {
9750 Init ();
9751 }
9752
9753 extern "C" void _M2_pge_fini (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
9754 {
9755 }