Debian Codegraph @

gcc/m2/gm2-compiler/ppg.mod

raw
1 (* ppg.mod master source file of the ebnf parser generator.
2
3 Copyright (C) 2003-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Modula-2; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. *)
21
22 MODULE ppg ;
23
24 FROM PushBackInput IMPORT WarnError, WarnString, GetColumnPosition, GetCurrentLine ;
25 FROM bnflex IMPORT IsSym, SymIs, TokenType, GetCurrentToken, GetCurrentTokenType, GetChar, PutChar,
26 SkipWhite, SkipUntilEoln, AdvanceToken, IsReserved, OpenSource, CloseSource,
27 PushBackToken, SetDebugging ;
28 FROM StrLib IMPORT StrCopy, StrEqual, StrLen, StrConCat ;
29 FROM Storage IMPORT ALLOCATE, DEALLOCATE ;
30 FROM NameKey IMPORT Name, MakeKey, WriteKey, LengthKey, GetKey, KeyToCharStar, NulName ;
31 FROM NumberIO IMPORT CardToStr, WriteCard ;
32 FROM SymbolKey IMPORT InitTree, SymbolTree, PutSymKey, GetSymKey, ForeachNodeDo, ContainsSymKey, NulKey ;
33 FROM Lists IMPORT InitList, IsItemInList, IncludeItemIntoList, RemoveItemFromList, KillList, List ;
34 FROM DynamicStrings IMPORT String, InitString, KillString, ConCat, Mark, ConCatChar,
35 InitStringCharStar, char, Length ;
36 FROM ASCII IMPORT nul, lf, tab ;
37 FROM StrIO IMPORT WriteString, WriteLn ;
38 FROM StdIO IMPORT Write ;
39 FROM Debug IMPORT Halt ;
40 FROM Args IMPORT GetArg, Narg ;
41 FROM SYSTEM IMPORT WORD ;
42 FROM libc IMPORT exit ;
43 IMPORT Output ;
44
45
46 CONST
47 MaxCodeHunkLength = 8192 ;
48 MaxFileName = 8192 ;
49 MaxString = 8192 ;
50 DefaultRecovery = TRUE ; (* default is to generate a parser which will recover from errors. *)
51 MaxElementsInSet = 32 ;
52
53 (* formatting constants *)
54 BaseRightLimit = 75 ;
55 BaseRightMargin = 50 ;
56 BaseNewLine = 3 ;
57
58 TYPE
59 ElementType = (idel, tokel, litel) ;
60
61 m2condition = (m2none, m2if, m2elsif, m2while) ;
62
63 TraverseResult = (unknown, true, false) ;
64
65 IdentDesc = POINTER TO RECORD
66 definition: ProductionDesc ; (* where this idents production is defined *)
67 name : Name ;
68 line : CARDINAL ;
69 END ;
70
71 SetDesc = POINTER TO RECORD
72 next : SetDesc ;
73 CASE type: ElementType OF
74
75 idel : ident : IdentDesc |
76 tokel,
77 litel : string: Name
78
79 END
80 END ;
81
82 (* note that epsilon refers to whether we can satisfy this component part
83 of a sentance without consuming a token. Reachend indicates we can get
84 to the end of the sentance without consuming a token.
85
86 For expression, statement, productions, terms: the epsilon value should
87 equal the reachend value but for factors the two may differ.
88 *)
89
90 FollowDesc = POINTER TO RECORD
91 calcfollow : BOOLEAN ; (* have we solved the follow set yet? *)
92 follow : SetDesc ; (* the follow set *)
93 reachend : TraverseResult ; (* can we see the end of the sentance (due to multiple epsilons) *)
94 epsilon : TraverseResult ; (* potentially no token may be consumed within this component of the sentance *)
95 line : CARDINAL ;
96 END ;
97
98 TermDesc = POINTER TO termdesc ;
99
100 ExpressionDesc = POINTER TO RECORD
101 term : TermDesc ;
102 followinfo: FollowDesc ;
103 line : CARDINAL ;
104 END ;
105
106 StatementDesc = POINTER TO RECORD
107 ident : IdentDesc ;
108 expr : ExpressionDesc ;
109 followinfo : FollowDesc ;
110 line : CARDINAL ;
111 END ;
112
113 CodeHunk = POINTER TO RECORD
114 codetext : ARRAY [0..MaxCodeHunkLength] OF CHAR ;
115 next : CodeHunk ;
116 END ;
117
118 CodeDesc = POINTER TO RECORD
119 code : CodeHunk ;
120 indent : CARDINAL ; (* column of the first % *)
121 line : CARDINAL ;
122 END ;
123
124 FactorType = (id, lit, sub, opt, mult, m2) ;
125
126 FactorDesc = POINTER TO RECORD
127 followinfo: FollowDesc ;
128 next : FactorDesc ; (* chain of successive factors *)
129 line : CARDINAL ;
130 pushed : FactorDesc ; (* chain of pushed code factors *)
131 CASE type: FactorType OF
132
133 id : ident : IdentDesc |
134 lit : string: Name |
135 sub,
136 opt,
137 mult: expr : ExpressionDesc |
138 m2 : code : CodeDesc ;
139
140 END
141 END ;
142
143 termdesc = RECORD
144 factor : FactorDesc ;
145 next : TermDesc ; (* chain of alternative terms *)
146 followinfo: FollowDesc ;
147 line : CARDINAL ;
148 END ;
149
150 ProductionDesc = POINTER TO RECORD
151 next : ProductionDesc ; (* the chain of productions *)
152 statement : StatementDesc ;
153 first : SetDesc ; (* the first set *)
154 firstsolved : BOOLEAN ;
155 followinfo : FollowDesc ;
156 line : CARDINAL ;
157 description : Name ;
158 END ;
159
160 DoProcedure = PROCEDURE (ProductionDesc) ;
161
162
163 VAR
164 LastLineNo : CARDINAL ;
165 Finished,
166 SuppressFileLineTag,
167 KeywordFormatting,
168 PrettyPrint,
169 EmitCode,
170 Texinfo,
171 Sphinx,
172 FreeDocLicense,
173 Debugging,
174 WasNoError : BOOLEAN ;
175 LinePrologue,
176 LineEpilogue,
177 LineDeclaration : CARDINAL ;
178 CodePrologue,
179 CodeEpilogue,
180 CodeDeclaration : CodeHunk ;
181 CurrentProduction,
182 TailProduction,
183 HeadProduction : ProductionDesc ;
184 CurrentExpression : ExpressionDesc ;
185 CurrentTerm : TermDesc ;
186 CurrentFactor : FactorDesc ;
187 CurrentIdent : IdentDesc ;
188 CurrentStatement : StatementDesc ;
189 CurrentSetDesc : SetDesc ;
190 ReverseValues,
191 Values, (* tree of tokens and their ORD value *)
192 ReverseAliases,
193 Aliases : SymbolTree ;
194 ModuleName : Name ;
195 LastLiteral : Name ;
196 LastIdent : Name ;
197 SymIsProc, (* the name of the SymIs function tests and consumes token *)
198 TokenTypeProc, (* the name of the function which yields the current token type *)
199 ErrorProcArray,
200 ErrorProcString : Name ; (* the name of the error procedures *)
201 ArgName,
202 FileName : ARRAY [0..MaxFileName] OF CHAR ;
203 OnLineStart,
204 BeginningOfLine : BOOLEAN ;
205 Indent : CARDINAL ;
206 EmittedVar : BOOLEAN ; (* have we written VAR yet? *)
207 ErrorRecovery : BOOLEAN ; (* do we want to recover from parsing errors? *)
208 LargestValue : CARDINAL ; (* the number of tokens we are using. *)
209 InitialElement : BOOLEAN ; (* used to determine whether we are writing *)
210 (* the first element of a case statement. *)
211 ParametersUsed : BITSET ; (* which parameters have been used? *)
212
213
214 (* % declaration *)
215
216 (*
217 AddEntry - adds an entry into, t, containing [def:value].
218 *)
219
220 PROCEDURE AddEntry (VAR t: SymbolTree; def, value: Name) ;
221 BEGIN
222 IF ContainsSymKey(t, def)
223 THEN
224 WarnError1("already seen a definition for token '%s'", def)
225 ELSE
226 PutSymKey(t, def, value)
227 END
228 END AddEntry ;
229
230
231 (*
232 Format1 - converts string, src, into, dest, together with encapsulated
233 entity, n. It only formats the first %s or %d with n.
234 *)
235
236 PROCEDURE Format1 (src: ARRAY OF CHAR; n: WORD; VAR dest: ARRAY OF CHAR) ;
237 VAR
238 HighSrc,
239 HighDest,
240 i, j : CARDINAL ;
241 str : ARRAY [0..MaxString] OF CHAR ;
242 BEGIN
243 HighSrc := StrLen(src) ;
244 HighDest := HIGH(dest) ;
245 i := 0 ;
246 j := 0 ;
247 WHILE (i<HighSrc) AND (src[i]#nul) AND (j<HighDest) AND (src[i]#'%') DO
248 dest[j] := src[i] ;
249 INC(i) ;
250 INC(j)
251 END ;
252 IF (i+1<HighSrc) AND (src[i]='%') AND (j<HighDest)
253 THEN
254 IF src[i+1]='s'
255 THEN
256 dest[j] := nul ;
257 GetKey(n, str) ;
258 StrConCat(dest, str, dest) ;
259 j := StrLen(dest) ;
260 INC(i, 2)
261 ELSIF src[i+1]='d'
262 THEN
263 dest[j] := nul ;
264 CardToStr(n, 0, str) ;
265 StrConCat(dest, str, dest) ;
266 j := StrLen(dest) ;
267 INC(i, 2)
268 ELSE
269 dest[j] := src[i] ;
270 INC(i) ;
271 INC(j)
272 END
273 END ;
274 (* and finish off copying src into dest *)
275 WHILE (i<HighSrc) AND (src[i]#nul) AND (j<HighDest) DO
276 dest[j] := src[i] ;
277 INC(i) ;
278 INC(j)
279 END ;
280 IF j<HighDest
281 THEN
282 dest[j] := nul
283 END ;
284 END Format1 ;
285
286
287 (*
288 WarnError1 -
289 *)
290
291 PROCEDURE WarnError1 (a: ARRAY OF CHAR; n: WORD) ;
292 VAR
293 line: ARRAY [0..MaxString] OF CHAR ;
294 BEGIN
295 Format1(a, n, line) ;
296 WarnError(line)
297 END WarnError1 ;
298
299
300 (*
301 PrettyFollow -
302 *)
303
304 PROCEDURE PrettyFollow (start, end: ARRAY OF CHAR; f: FollowDesc) ;
305 BEGIN
306 IF Debugging
307 THEN
308 Output.WriteString(start) ;
309 IF f#NIL
310 THEN
311 WITH f^ DO
312 IF calcfollow
313 THEN
314 Output.WriteString('followset defined as:') ;
315 EmitSet(follow, 0, 0)
316 END ;
317 CASE reachend OF
318
319 true : Output.WriteString(' [E]') |
320 false: Output.WriteString(' [C]') |
321 unknown: Output.WriteString(' [U]')
322
323 ELSE
324 END ;
325 CASE epsilon OF
326
327 true : Output.WriteString(' [e]') |
328 false : |
329 unknown: Output.WriteString(' [u]')
330
331 ELSE
332 END
333 END
334 END ;
335 Output.WriteString(end)
336 END
337 END PrettyFollow ;
338
339
340 (*
341 NewFollow - creates a new follow descriptor and returns the data structure.
342 *)
343
344 PROCEDURE NewFollow () : FollowDesc ;
345 VAR
346 f: FollowDesc ;
347 BEGIN
348 NEW(f) ;
349 WITH f^ DO
350 follow := NIL ;
351 reachend := unknown ;
352 epsilon := unknown ;
353 END ;
354 RETURN( f )
355 END NewFollow ;
356
357
358 (*
359 AssignEpsilon - assigns the epsilon value and sets the epsilon to value,
360 providing condition is TRUE.
361 *)
362
363 PROCEDURE AssignEpsilon (condition: BOOLEAN; f: FollowDesc; value: TraverseResult) ;
364 BEGIN
365 WITH f^ DO
366 IF condition AND (value#unknown) AND (epsilon=unknown)
367 THEN
368 epsilon := value ;
369 Finished := FALSE
370 END
371 END
372 END AssignEpsilon ;
373
374
375 (*
376 GetEpsilon - returns the value of epsilon
377 *)
378
379 PROCEDURE GetEpsilon (f: FollowDesc) : TraverseResult ;
380 BEGIN
381 IF f=NIL
382 THEN
383 Halt('why is the follow info NIL?',
384 __FILE__, __FUNCTION__, __LINE__)
385 ELSE
386 RETURN( f^.epsilon )
387 END
388 END GetEpsilon ;
389
390
391 (*
392 AssignReachEnd - assigns the reachend value providing that, condition, is TRUE.
393 *)
394
395 PROCEDURE AssignReachEnd (condition: BOOLEAN; f: FollowDesc; value: TraverseResult) ;
396 BEGIN
397 IF condition
398 THEN
399 WITH f^ DO
400 IF (reachend=unknown) AND (value#unknown)
401 THEN
402 reachend := value ;
403 Finished := FALSE
404 END
405 END
406 END
407 END AssignReachEnd ;
408
409
410 (*
411 GetReachEnd - returns the value of reachend
412 *)
413
414 PROCEDURE GetReachEnd (f: FollowDesc) : TraverseResult ;
415 BEGIN
416 IF f=NIL
417 THEN
418 Halt('why is the follow info NIL?',
419 __FILE__, __FUNCTION__, __LINE__)
420 ELSE
421 RETURN( f^.reachend )
422 END
423 END GetReachEnd ;
424
425
426 (*
427 AssignFollow - assigns the follow set and sets the calcfollow to TRUE.
428 *)
429
430 PROCEDURE AssignFollow (f: FollowDesc; s: SetDesc) ;
431 BEGIN
432 WITH f^ DO
433 IF calcfollow
434 THEN
435 Halt('why are we reassigning this follow set?',
436 __FILE__, __FUNCTION__, __LINE__)
437 END ;
438 follow := s ;
439 calcfollow := TRUE
440 END
441 END AssignFollow ;
442
443
444 (*
445 GetFollow - returns the follow set.
446 *)
447
448 PROCEDURE GetFollow (f: FollowDesc) : SetDesc ;
449 BEGIN
450 IF f=NIL
451 THEN
452 Halt ('why is the follow info NIL?',
453 __FILE__, __FUNCTION__, __LINE__)
454 ELSE
455 WITH f^ DO
456 IF calcfollow
457 THEN
458 RETURN( follow )
459 ELSE
460 Halt('not calculated the follow set yet..',
461 __FILE__, __FUNCTION__, __LINE__)
462 END
463 END
464 END
465 END GetFollow ;
466
467
468 (*
469 NewProduction - creates a new production and returns the data structure.
470 *)
471
472 PROCEDURE NewProduction () : ProductionDesc ;
473 VAR
474 p: ProductionDesc ;
475 BEGIN
476 NEW(p) ;
477 IF TailProduction#NIL
478 THEN
479 TailProduction^.next := p
480 END ;
481 TailProduction := p ;
482 IF HeadProduction=NIL
483 THEN
484 HeadProduction := p
485 END ;
486 WITH p^ DO
487 next := NIL ;
488 statement := NIL ;
489 first := NIL ;
490 firstsolved := FALSE ;
491 followinfo := NewFollow() ;
492 line := GetCurrentLine() ;
493 description := NulName
494 END ;
495 RETURN( p )
496 END NewProduction ;
497
498
499 (*
500 NewFactor -
501 *)
502
503 PROCEDURE NewFactor () : FactorDesc ;
504 VAR
505 f: FactorDesc ;
506 BEGIN
507 NEW(f) ;
508 WITH f^ DO
509 next := NIL ;
510 followinfo := NewFollow() ;
511 line := GetCurrentLine()
512 END ;
513 RETURN( f )
514 END NewFactor ;
515
516
517 (*
518 NewTerm - returns a new term.
519 *)
520
521 PROCEDURE NewTerm () : TermDesc ;
522 VAR
523 t: TermDesc ;
524 BEGIN
525 NEW(t) ;
526 WITH t^ DO
527 factor := NIL ;
528 followinfo := NewFollow() ;
529 next := NIL ;
530 line := GetCurrentLine()
531 END ;
532 RETURN( t )
533 END NewTerm ;
534
535
536 (*
537 NewExpression - returns a new expression.
538 *)
539
540 PROCEDURE NewExpression () : ExpressionDesc ;
541 VAR
542 e: ExpressionDesc ;
543 BEGIN
544 NEW(e) ;
545 WITH e^ DO
546 term := NIL ;
547 followinfo := NewFollow() ;
548 line := GetCurrentLine()
549 END ;
550 RETURN( e )
551 END NewExpression ;
552
553
554 (*
555 NewStatement - returns a new statement.
556 *)
557
558 PROCEDURE NewStatement () : StatementDesc ;
559 VAR
560 s: StatementDesc ;
561 BEGIN
562 NEW(s) ;
563 WITH s^ DO
564 ident := NIL ;
565 expr := NIL ;
566 followinfo := NewFollow() ;
567 line := GetCurrentLine()
568 END ;
569 RETURN( s )
570 END NewStatement ;
571
572
573 (*
574 NewSetDesc - creates a new set description and returns the data structure.
575 *)
576
577 PROCEDURE NewSetDesc () : SetDesc ;
578 VAR
579 s: SetDesc ;
580 BEGIN
581 NEW(s) ;
582 WITH s^ DO
583 next := NIL
584 END ;
585 RETURN( s )
586 END NewSetDesc ;
587
588
589 (*
590 NewCodeDesc - creates a new code descriptor and initializes all fields to zero.
591 *)
592
593 PROCEDURE NewCodeDesc () : CodeDesc ;
594 VAR
595 c: CodeDesc ;
596 BEGIN
597 NEW(c) ;
598 WITH c^ DO
599 code := NIL ;
600 indent := 0 ;
601 line := GetCurrentLine()
602 END ;
603 RETURN( c )
604 END NewCodeDesc ;
605
606
607 (*
608 CodeFragmentPrologue - consumes code text up to a "%" after a newline.
609 *)
610
611 PROCEDURE CodeFragmentPrologue ;
612 BEGIN
613 LinePrologue := GetCurrentLine() ;
614 GetCodeFragment(CodePrologue)
615 END CodeFragmentPrologue ;
616
617
618 (*
619 CodeFragmentEpilogue - consumes code text up to a "%" after a newline.
620 *)
621
622 PROCEDURE CodeFragmentEpilogue ;
623 BEGIN
624 LineEpilogue := GetCurrentLine() ;
625 GetCodeFragment(CodeEpilogue)
626 END CodeFragmentEpilogue ;
627
628
629 (*
630 CodeFragmentDeclaration - consumes code text up to a "%" after a newline.
631 *)
632
633 PROCEDURE CodeFragmentDeclaration ;
634 BEGIN
635 LineDeclaration := GetCurrentLine() ;
636 GetCodeFragment(CodeDeclaration)
637 END CodeFragmentDeclaration ;
638
639
640 (*
641 GetCodeFragment - collects the code fragment up until ^ %
642 *)
643
644 PROCEDURE GetCodeFragment (VAR h: CodeHunk) ;
645 VAR
646 i : CARDINAL ;
647 ch: CHAR ;
648 BEGIN
649 h := NIL ;
650 i := 0 ;
651 WHILE (PutChar(GetChar())#'%') AND (PutChar(GetChar())#nul) DO
652 REPEAT
653 WHILE (PutChar(GetChar())#nul) AND (PutChar(GetChar())#lf) DO
654 h := Add(h, GetChar(), i)
655 END ;
656 IF PutChar(GetChar())=lf
657 THEN
658 (* consume line feed *)
659 h := Add(h, GetChar(), i) ;
660 ch := PutChar(lf)
661 ELSIF PutChar(GetChar())=nul
662 THEN
663 ch := PutChar(nul) ;
664 ch := PutChar(lf)
665 ELSE
666 ch := PutChar(PutChar(GetChar()))
667 END
668 UNTIL GetChar()=lf
669 END ;
670 IF PutChar(GetChar())='%'
671 THEN
672 h := Add(h, nul, i) ;
673 ch := PutChar(' ') ; (* to give the following token % a delimiter infront of it *)
674 AdvanceToken
675 ELSE
676 WarnError('expecting % to terminate code fragment, found end of file')
677 END
678 END GetCodeFragment ;
679
680
681 (*
682 WriteCodeHunkList - writes the CodeHunk list in the correct order.
683 *)
684
685 PROCEDURE WriteCodeHunkList (l: CodeHunk) ;
686 BEGIN
687 IF l#NIL
688 THEN
689 OnLineStart := FALSE ;
690 (* recursion *)
691 WITH l^ DO
692 WriteCodeHunkList(next) ;
693 Output.WriteString(codetext)
694 END
695 END
696 END WriteCodeHunkList ;
697
698
699 (*
700 WriteIndent - writes, n, spaces.
701 *)
702
703 PROCEDURE WriteIndent (n: CARDINAL) ;
704 BEGIN
705 WHILE n>0 DO
706 Output.Write(' ') ;
707 DEC(n)
708 END ;
709 OnLineStart := FALSE
710 END WriteIndent ;
711
712
713 (*
714 CheckWrite -
715 *)
716
717 PROCEDURE CheckWrite (ch: CHAR; VAR curpos: CARDINAL; left: CARDINAL; VAR seentext: BOOLEAN) ;
718 BEGIN
719 IF ch=lf
720 THEN
721 NewLine(left) ;
722 curpos := 0 ;
723 seentext := FALSE
724 ELSE
725 Output.Write(ch) ;
726 INC(curpos)
727 END
728 END CheckWrite ;
729
730
731 (*
732 WriteStringIndent - writes a string but it will try and remove upto indent spaces
733 if they exist.
734 *)
735
736 PROCEDURE WriteStringIndent (a: ARRAY OF CHAR; indent: CARDINAL;
737 VAR curpos: CARDINAL;
738 left: CARDINAL; VAR seentext: BOOLEAN) ;
739 VAR
740 l, i: CARDINAL ;
741 BEGIN
742 i := 0 ;
743 l := StrLen(a) ;
744 WHILE i<l DO
745 IF seentext
746 THEN
747 CheckWrite(a[i], curpos, left, seentext)
748 ELSE
749 IF a[i]=' '
750 THEN
751 (* ignore space for now *)
752 INC(curpos)
753 ELSE
754 IF curpos>=indent
755 THEN
756 WriteIndent(curpos-indent)
757 END ;
758 seentext := TRUE ;
759 CheckWrite(a[i], curpos, left, seentext)
760 END
761 END ;
762 INC(i)
763 END
764 END WriteStringIndent ;
765
766
767 (*
768 WriteCodeHunkListIndent - writes the CodeHunk list in the correct order
769 but it removes up to indent spaces if they exist.
770 *)
771
772 PROCEDURE WriteCodeHunkListIndent (l: CodeHunk; indent: CARDINAL;
773 VAR curpos: CARDINAL;
774 left: CARDINAL; VAR seentext: BOOLEAN) ;
775 BEGIN
776 IF l#NIL
777 THEN
778 (* recursion *)
779 WITH l^ DO
780 WriteCodeHunkListIndent(next, indent, curpos, left, seentext) ;
781 WriteStringIndent(codetext, indent, curpos, left, seentext)
782 END
783 END
784 END WriteCodeHunkListIndent ;
785
786
787 (*
788 Add - adds a character to a code hunk and creates another code hunk if necessary.
789 *)
790
791 PROCEDURE Add (VAR p: CodeHunk; ch: CHAR; VAR i: CARDINAL) : CodeHunk ;
792 VAR
793 q: CodeHunk ;
794 BEGIN
795 IF (p=NIL) OR (i>MaxCodeHunkLength)
796 THEN
797 NEW(q) ;
798 q^.next := p ;
799 q^.codetext[0] := ch ;
800 i := 1 ;
801 RETURN( q )
802 ELSE
803 p^.codetext[i] := ch ;
804 INC(i) ;
805 RETURN( p )
806 END
807 END Add ;
808
809
810 (*
811 ConsHunk - combine two possible code hunks.
812 *)
813
814 PROCEDURE ConsHunk (VAR p: CodeHunk; q: CodeHunk) ;
815 VAR
816 r: CodeHunk ;
817 BEGIN
818 IF p#NIL
819 THEN
820 r := q ;
821 WHILE r^.next#NIL DO
822 r := r^.next
823 END ;
824 r^.next := p ;
825 END ;
826 p := q
827 END ConsHunk ;
828
829
830 (*
831 GetName - returns the next symbol which is checked for a legal name.
832 *)
833
834 PROCEDURE GetName () : Name ;
835 VAR
836 name: Name ;
837 BEGIN
838 IF IsReserved(GetCurrentToken())
839 THEN
840 WarnError('expecting a name and found a reserved word') ;
841 AdvanceToken ; (* move on to another token *)
842 RETURN( NulName )
843 ELSE
844 name := GetCurrentToken() ;
845 AdvanceToken ;
846 RETURN( name )
847 END
848 END GetName ;
849
850
851 (* % rules *)
852
853 (*
854 Note that all the code from here down to the end of the module as
855 delimited by the comment will all be hidden when the buildpg
856 script is invoked. Also be careful not to duplicate or remove these
857 critical comments below..
858 Check buildpg for sed details.
859 *)
860
861 (* StartNonErrorChecking *)
862
863 (* actually these two are not strictly rules but hand built primitives *)
864
865
866 (*
867 Ident - non error checking varient of Ident
868 *)
869
870 PROCEDURE Ident () : BOOLEAN ;
871 BEGIN
872 IF GetCurrentTokenType()=identtok
873 THEN
874 NEW(CurrentIdent) ;
875 WITH CurrentIdent^ DO
876 definition := NIL ;
877 name := GetName() ;
878 line := GetCurrentLine()
879 END ;
880 RETURN( TRUE )
881 ELSE
882 RETURN( FALSE )
883 END
884 END Ident ;
885
886
887 (*
888 Modula2Code - non error checking varient of Modula2Code
889 *)
890
891 PROCEDURE Modula2Code () : BOOLEAN ;
892 VAR
893 p : CodeHunk ;
894 i : CARDINAL ;
895 quote : BOOLEAN ;
896 line,
897 position: CARDINAL ;
898 BEGIN
899 line := GetCurrentLine() ;
900 PushBackToken(GetCurrentToken()) ;
901 position := GetColumnPosition() ;
902 p := NIL ;
903 SkipWhite ;
904 WHILE (PutChar(GetChar())#'%') AND (PutChar(GetChar())#nul) DO
905 IF PutChar(GetChar())='"'
906 THEN
907 REPEAT
908 p := Add(p, GetChar(), i)
909 UNTIL (PutChar(GetChar())='"') OR (PutChar(GetChar())=nul) ;
910 p := Add(p, '"', i) ;
911 IF (PutChar(GetChar())='"') AND (GetChar()='"')
912 THEN
913 END
914 ELSIF PutChar(GetChar())="'"
915 THEN
916 REPEAT
917 p := Add(p, GetChar(), i)
918 UNTIL (PutChar(GetChar())="'") OR (PutChar(GetChar())=nul) ;
919 p := Add(p, "'", i) ;
920 IF (PutChar(GetChar())="'") AND (GetChar()="'")
921 THEN
922 END
923 ELSIF (PutChar(GetChar())='\') AND (GetChar()='\')
924 THEN
925 p := Add(p, GetChar(), i)
926 ELSIF PutChar(GetChar())#'%'
927 THEN
928 p := Add(p, GetChar(), i)
929 END
930 END ;
931 p := Add(p, nul, i) ;
932 WITH CurrentFactor^ DO
933 type := m2 ;
934 code := NewCodeDesc() ;
935 WITH code^ DO
936 code := p ;
937 indent := position
938 END
939 END ;
940 IF PutChar(' ')=' '
941 THEN
942 END ;
943 AdvanceToken ; (* read the next token ready for the parser *)
944 IF NOT WasNoError
945 THEN
946 WarnError1('error probably occurred before the start of inline code on line %d', line)
947 END ;
948 RETURN( TRUE )
949 END Modula2Code ;
950
951
952 (*
953 StartModName := % ModuleName := GetName() ; (* ignore begintok *) CodeFragmentPrologue % =:
954 *)
955
956 PROCEDURE StartModName () : BOOLEAN ;
957 BEGIN
958 ModuleName := GetName() ;
959 CodeFragmentPrologue ;
960 RETURN( TRUE )
961 END StartModName ;
962
963 (*
964 EndModName :=
965 *)
966
967 PROCEDURE EndModName () : BOOLEAN ;
968 BEGIN
969 IF ModuleName#GetName()
970 THEN
971 WarnError('expecting same module name at end as beginning')
972 END ;
973 (* ignore endtok as it consumes the token afterwards *)
974 CodeFragmentEpilogue ;
975 RETURN( TRUE )
976 END EndModName ;
977
978 (*
979 DoDeclaration := % CodeFragmentDeclaration % =:
980 *)
981
982 PROCEDURE DoDeclaration () : BOOLEAN ;
983 BEGIN
984 IF ModuleName#GetName()
985 THEN
986 WarnError('expecting same module name in declaration as in the beginning')
987 END ;
988 (* ignore begintok as it consumes the token afterwards *)
989 CodeFragmentDeclaration ;
990 RETURN( TRUE )
991 END DoDeclaration ;
992
993 (* EndNonErrorChecking now for the real ebnf rules *)
994
995 TYPE
996 SetOfStop = SET OF TokenType ;
997
998 (* **************************************************************************
999 E r r o r R e c o v e r y I d e n t & M o d u l a 2 C o d e
1000 **************************************************************************
1001
1002 (* StartErrorChecking *)
1003
1004
1005 (*
1006 SyntaxError - after a syntax error we skip all tokens up until we reach
1007 a stop symbol.
1008 *)
1009
1010 PROCEDURE SyntaxError (stop: SetOfStop) ;
1011 BEGIN
1012 DescribeError ;
1013 IF Debugging
1014 THEN
1015 WriteLn ;
1016 WriteString('skipping token *** ')
1017 END ;
1018 WHILE NOT (GetCurrentTokenType() IN stop) DO
1019 AdvanceToken
1020 END ;
1021 IF Debugging
1022 THEN
1023 WriteString(' ***') ; WriteLn
1024 END ;
1025 WasNoError := FALSE
1026 END SyntaxError ;
1027
1028
1029 (*
1030 SyntaxCheck -
1031 *)
1032
1033 PROCEDURE SyntaxCheck (stop: SetOfStop) ;
1034 BEGIN
1035 IF NOT (GetCurrentTokenType() IN stop)
1036 THEN
1037 SyntaxError(stop)
1038 END
1039 END SyntaxCheck ;
1040
1041
1042 (*
1043 Expect -
1044 *)
1045
1046 PROCEDURE Expect (t: TokenType; stop: SetOfStop) ;
1047 BEGIN
1048 IF GetCurrentTokenType()=t
1049 THEN
1050 AdvanceToken
1051 ELSE
1052 SyntaxError(stop)
1053 END ;
1054 SyntaxCheck(stop)
1055 END Expect ;
1056
1057
1058 (*
1059 Ident - error checking varient of Ident
1060 *)
1061
1062 PROCEDURE Ident (stop: SetOfStop) ;
1063 BEGIN
1064 IF GetCurrentTokenType()=identtok
1065 THEN
1066 NEW(CurrentIdent) ;
1067 WITH CurrentIdent^ DO
1068 definition := NIL ;
1069 name := GetName() ;
1070 line := GetCurrentLine()
1071 END ;
1072 END
1073 END Ident ;
1074
1075
1076 (*
1077 Modula2Code - error checking varient of Modula2Code
1078 *)
1079
1080 PROCEDURE Modula2Code (stop: SetOfStop) ;
1081 VAR
1082 p : CodeHunk ;
1083 i : CARDINAL ;
1084 quote : BOOLEAN ;
1085 line,
1086 position: CARDINAL ;
1087 BEGIN
1088 line := GetCurrentLine() ;
1089 PushBackToken(GetCurrentToken()) ;
1090 position := GetColumnPosition() ;
1091 p := NIL ;
1092 SkipWhite ;
1093 WHILE (PutChar(GetChar())#'%') AND (PutChar(GetChar())#nul) DO
1094 IF PutChar(GetChar())='"'
1095 THEN
1096 REPEAT
1097 p := Add(p, GetChar(), i)
1098 UNTIL (PutChar(GetChar())='"') OR (PutChar(GetChar())=nul) ;
1099 p := Add(p, '"', i) ;
1100 IF (PutChar(GetChar())='"') AND (GetChar()='"')
1101 THEN
1102 END
1103 ELSIF PutChar(GetChar())="'"
1104 THEN
1105 REPEAT
1106 p := Add(p, GetChar(), i)
1107 UNTIL (PutChar(GetChar())="'") OR (PutChar(GetChar())=nul) ;
1108 p := Add(p, "'", i) ;
1109 IF (PutChar(GetChar())="'") AND (GetChar()="'")
1110 THEN
1111 END
1112 ELSIF (PutChar(GetChar())='\') AND (GetChar()='\')
1113 THEN
1114 p := Add(p, GetChar(), i)
1115 ELSIF PutChar(GetChar())#'%'
1116 THEN
1117 p := Add(p, GetChar(), i)
1118 END
1119 END ;
1120 p := Add(p, nul, i) ;
1121 WITH CurrentFactor^ DO
1122 type := m2 ;
1123 code := NewCodeDesc() ;
1124 WITH code^ DO
1125 code := p ;
1126 indent := position
1127 END
1128 END ;
1129 IF PutChar(' ')=' '
1130 THEN
1131 END ;
1132 AdvanceToken ; (* read the next token ready for the parser *)
1133 IF NOT WasNoError
1134 THEN
1135 WarnError1('error probably occurred before the start of inline code on line %d', line)
1136 END
1137 END Modula2Code ;
1138
1139
1140 (*
1141 StartModName := % ModuleName := GetName() ; (* ignore begintok *) CodeFragmentPrologue % =:
1142 *)
1143
1144 PROCEDURE StartModName (stop: SetOfStop) ;
1145 BEGIN
1146 ModuleName := GetName() ;
1147 CodeFragmentPrologue
1148 END StartModName ;
1149
1150
1151 (*
1152 EndModName :=
1153 *)
1154
1155 PROCEDURE EndModName (stop: SetOfStop) ;
1156 BEGIN
1157 IF ModuleName#GetName()
1158 THEN
1159 WarnError('expecting same module name at end as beginning')
1160 END ;
1161 (* ignore endtok as it consumes the token afterwards *)
1162 CodeFragmentEpilogue
1163 END EndModName ;
1164
1165
1166 (*
1167 DoDeclaration := % CodeFragmentDeclaration % =:
1168 *)
1169
1170 PROCEDURE DoDeclaration (stop: SetOfStop) ;
1171 BEGIN
1172 IF ModuleName#GetName()
1173 THEN
1174 WarnError('expecting same module name in declaration as in the beginning')
1175 END ;
1176 (* ignore begintok as it consumes the token afterwards *)
1177 CodeFragmentDeclaration
1178 END DoDeclaration ;
1179
1180
1181 (* EndErrorChecking now for the real ebnf rules *)
1182
1183 *****************************************************************
1184 l e a v e a b o v e c o d e a l o n e (f o r S E D)
1185 ***************************************************************** *)
1186
1187 (* this code below will be recreated by ppg *)
1188
1189 PROCEDURE DescribeError ;
1190 BEGIN
1191 WarnError('syntax error')
1192 END DescribeError ;
1193
1194 PROCEDURE Main () : BOOLEAN ;
1195 BEGIN
1196 IF Header()
1197 THEN
1198 IF Decls()
1199 THEN
1200 IF Footer()
1201 THEN
1202 IF Rules()
1203 THEN
1204 RETURN( TRUE )
1205 END
1206 END
1207 END
1208 END ;
1209 RETURN( FALSE )
1210 END Main ;
1211
1212 PROCEDURE Header () : BOOLEAN ;
1213 BEGIN
1214 IF SymIs(codetok)
1215 THEN
1216 IF SymIs(moduletok)
1217 THEN
1218 ModuleName := GetName() ;
1219 (* ignore the begintok as we are looking one symbol ahead and we dont want to move over MODULE *)
1220 CodeFragmentPrologue ;
1221 RETURN( TRUE )
1222 ELSE
1223 WarnError('expecting module')
1224 END
1225 END ;
1226 RETURN( FALSE )
1227 END Header ;
1228
1229 PROCEDURE Footer () : BOOLEAN ;
1230 BEGIN
1231 IF SymIs(codetok)
1232 THEN
1233 IF SymIs(moduletok)
1234 THEN
1235 IF ModuleName#GetName()
1236 THEN
1237 WarnError('expecting same module name at end as beginning')
1238 END ;
1239 (* ignore endtok as it consumes the token afterwards *)
1240 CodeFragmentEpilogue ;
1241 RETURN( TRUE )
1242 ELSE
1243 WarnError('expecting module')
1244 END
1245 END ;
1246 RETURN( FALSE )
1247 END Footer ;
1248
1249 PROCEDURE Decls () : BOOLEAN ;
1250 BEGIN
1251 IF SymIs(codetok)
1252 THEN
1253 IF SymIs(declarationtok)
1254 THEN
1255 RETURN( DoDeclaration() )
1256 ELSE
1257 WarnError('expecting declaration')
1258 END
1259 END ;
1260 RETURN( FALSE )
1261 END Decls ;
1262
1263
1264 (*
1265 Rules := " % " " rules " { Defs } ExtBNF =:
1266 *)
1267
1268 PROCEDURE Rules () : BOOLEAN ;
1269 BEGIN
1270 IF SymIs(codetok)
1271 THEN
1272 IF SymIs(rulestok)
1273 THEN
1274 WHILE Defs() DO
1275 END ;
1276 IF ExtBNF()
1277 THEN
1278 RETURN( TRUE )
1279 ELSE
1280 WarnError('expecting some BNF rules to be present')
1281 END
1282 END
1283 END ;
1284 RETURN( FALSE )
1285 END Rules ;
1286
1287
1288 (*
1289 Defs := " special " Special | " token " Token | " error " ErrorProcedures |
1290 "tokenfunc" TokenProcedure =:
1291 *)
1292
1293 PROCEDURE Defs () : BOOLEAN ;
1294 BEGIN
1295 IF SymIs(specialtok)
1296 THEN
1297 IF Special()
1298 THEN
1299 RETURN( TRUE )
1300 END
1301 ELSIF SymIs(tokentok)
1302 THEN
1303 IF Token()
1304 THEN
1305 RETURN( TRUE )
1306 END
1307 ELSIF SymIs(errortok)
1308 THEN
1309 IF ErrorProcedures()
1310 THEN
1311 RETURN( TRUE )
1312 END
1313 ELSIF SymIs(tfunctok)
1314 THEN
1315 IF TokenProcedure()
1316 THEN
1317 RETURN( TRUE )
1318 END
1319 ELSIF SymIs(symfunctok)
1320 THEN
1321 IF SymProcedure()
1322 THEN
1323 RETURN( TRUE )
1324 END
1325 END ;
1326 RETURN( FALSE )
1327 END Defs ;
1328
1329
1330 (*
1331 Special := Name First Follow [ "epsilon" ] =:
1332 *)
1333
1334 PROCEDURE Special () : BOOLEAN ;
1335 VAR
1336 p: ProductionDesc ;
1337 BEGIN
1338 IF Ident()
1339 THEN
1340 p := NewProduction() ;
1341 p^.statement := NewStatement() ;
1342 p^.statement^.followinfo^.calcfollow := TRUE ;
1343 p^.statement^.followinfo^.epsilon := false ;
1344 p^.statement^.followinfo^.reachend := false ;
1345 p^.statement^.ident := CurrentIdent ;
1346 p^.statement^.expr := NIL ;
1347 p^.firstsolved := TRUE ;
1348 p^.followinfo^.calcfollow := TRUE ;
1349 p^.followinfo^.epsilon := false ;
1350 p^.followinfo^.reachend := false ;
1351 IF First()
1352 THEN
1353 IF Follow()
1354 THEN
1355 IF SymIs(epsilontok)
1356 THEN
1357 p^.statement^.followinfo^.epsilon := true ; (* these are not used - but they are displayed when debugging *)
1358 p^.statement^.followinfo^.reachend := true ;
1359 p^.followinfo^.epsilon := true ;
1360 p^.followinfo^.reachend := true
1361 END ;
1362 IF Literal()
1363 THEN
1364 p^.description := LastLiteral
1365 END ;
1366 RETURN( TRUE )
1367 ELSE
1368 WarnError('Follow - expected') ;
1369 RETURN( FALSE )
1370 END ;
1371 ELSE
1372 WarnError('First - expected') ;
1373 RETURN( FALSE )
1374 END
1375 ELSE
1376 RETURN( FALSE )
1377 END
1378 END Special ;
1379
1380
1381 (*
1382 First := 'first' '{' { LitOrTokenOrIdent % WITH LastSetDesc^ DO
1383 next := HeadProduction^.first ;
1384 END ;
1385 TailProduction^.first := LastSetDesc ;
1386 %
1387 } '}'
1388 *)
1389
1390 PROCEDURE First () : BOOLEAN ;
1391 BEGIN
1392 IF SymIs(firsttok)
1393 THEN
1394 IF SymIs(lcparatok)
1395 THEN
1396 WHILE LitOrTokenOrIdent() DO
1397 WITH CurrentSetDesc^ DO
1398 next := TailProduction^.first ;
1399 END ;
1400 TailProduction^.first := CurrentSetDesc
1401 END ; (* while *)
1402 IF SymIs(rcparatok)
1403 THEN
1404 RETURN( TRUE )
1405 ELSE
1406 WarnError("'}' - expected") ;
1407 RETURN( FALSE )
1408 END ;
1409 ELSE
1410 WarnError("'{' - expected") ;
1411 RETURN( FALSE )
1412 END ;
1413 ELSE
1414 RETURN( FALSE )
1415 END ;
1416 END First ;
1417
1418
1419 (*
1420 Follow := 'follow' '{' { LitOrTokenOrIdent } '}'
1421 *)
1422
1423 PROCEDURE Follow () : BOOLEAN ;
1424 BEGIN
1425 IF SymIs(followtok)
1426 THEN
1427 IF SymIs(lcparatok)
1428 THEN
1429 WHILE LitOrTokenOrIdent() DO
1430 WITH CurrentSetDesc^ DO
1431 next := TailProduction^.followinfo^.follow ;
1432 END ;
1433 TailProduction^.followinfo^.follow := CurrentSetDesc
1434 END ; (* while *)
1435 IF SymIs(rcparatok)
1436 THEN
1437 RETURN( TRUE )
1438 ELSE
1439 WarnError("'}' - expected") ;
1440 RETURN( FALSE )
1441 END ;
1442 ELSE
1443 WarnError("'{' - expected") ;
1444 RETURN( FALSE )
1445 END ;
1446 ELSE
1447 RETURN( FALSE )
1448 END ;
1449 END Follow ;
1450
1451
1452 (*
1453 LitOrTokenOrIdent := Literal % CurrentSetDesc := NewSetDesc() ;
1454 WITH CurrentSetDesc^ DO
1455 type := litel ;
1456 string := LastLiteral ;
1457 END ;
1458 %
1459 | '<' % CurrentSetDesc := NewSetDesc() ;
1460 WITH CurrentSetDesc^ DO
1461 type := tokel ;
1462 string := GetCurrentToken() ;
1463 END ;
1464 AdvanceToken() ;
1465 %
1466 '>' | Ident % CurrentSetDesc := NewSetDesc() ;
1467 WITH CurrentSetDesc^ DO
1468 type := idel ;
1469 ident := CurrentIdent ;
1470 END ;
1471 %
1472
1473 *)
1474
1475 PROCEDURE LitOrTokenOrIdent () : BOOLEAN ;
1476 BEGIN
1477 IF Literal()
1478 THEN
1479 CurrentSetDesc := NewSetDesc() ;
1480 WITH CurrentSetDesc^ DO
1481 type := litel ;
1482 string := LastLiteral
1483 END ;
1484 RETURN( TRUE )
1485 ELSIF SymIs(lesstok)
1486 THEN
1487 CurrentSetDesc := NewSetDesc() ;
1488 WITH CurrentSetDesc^ DO
1489 type := tokel ;
1490 string := GetCurrentToken() ;
1491 END ;
1492 IF GetSymKey(Aliases, GetCurrentToken())=NulKey
1493 THEN
1494 (*
1495 PutSymKey(Values, GetCurrentToken(), LargestValue) ;
1496 PutSymKey(Aliases, GetCurrentToken(), GetCurrentToken()) ;
1497 PutSymKey(ReverseAliases, GetCurrentToken(), GetCurrentToken()) ;
1498 INC(LargestValue) ;
1499 *)
1500 END ;
1501 AdvanceToken() ;
1502 IF SymIs(gretok)
1503 THEN
1504 RETURN( TRUE )
1505 ELSE
1506 WarnError("'>' - expected") ;
1507 RETURN( FALSE )
1508 END ;
1509 ELSIF Ident()
1510 THEN
1511 CurrentSetDesc := NewSetDesc() ;
1512 WITH CurrentSetDesc^ DO
1513 type := idel ;
1514 ident := CurrentIdent ;
1515 END ;
1516 RETURN( TRUE )
1517 ELSE
1518 RETURN( FALSE )
1519 END ; (* elsif *)
1520 END LitOrTokenOrIdent ;
1521
1522
1523 (*
1524 Literal -
1525 *)
1526
1527 PROCEDURE Literal () : BOOLEAN ;
1528 BEGIN
1529 IF SymIs(squotetok)
1530 THEN
1531 LastLiteral := GetCurrentToken() ;
1532 AdvanceToken ;
1533 IF SymIs(squotetok)
1534 THEN
1535 RETURN( TRUE )
1536 END
1537 ELSIF SymIs(dquotetok)
1538 THEN
1539 LastLiteral := GetCurrentToken() ;
1540 AdvanceToken ;
1541 IF SymIs(dquotetok)
1542 THEN
1543 RETURN( TRUE )
1544 END
1545 END ;
1546 RETURN( FALSE )
1547 END Literal ;
1548
1549
1550 (*
1551 Token := Literal % VAR l: CARDINAL ;
1552 l := GetCurrentToken() ; %
1553 Name % PutSymKey(Aliases, l, GetCurrentToken()) ; % =:
1554 *)
1555
1556 PROCEDURE Token () : BOOLEAN ;
1557 BEGIN
1558 IF Literal()
1559 THEN
1560 AddEntry(Aliases, LastLiteral, GetCurrentToken()) ;
1561 AddEntry(ReverseAliases, GetCurrentToken(), LastLiteral) ;
1562 AddEntry(Values, GetCurrentToken(), LargestValue) ;
1563 AddEntry(ReverseValues, Name(LargestValue), GetCurrentToken()) ;
1564 INC(LargestValue) ;
1565 AdvanceToken ;
1566 RETURN( TRUE )
1567 ELSE
1568 RETURN( FALSE )
1569 END
1570 END Token ;
1571
1572
1573 (*
1574 ErrorProcedures := Literal % ErrorProcArray := LastLiteral %
1575 Literal % ErrorProcString := LastLiteral % =:
1576 *)
1577
1578 PROCEDURE ErrorProcedures () : BOOLEAN ;
1579 BEGIN
1580 IF Literal()
1581 THEN
1582 ErrorProcArray := LastLiteral ;
1583 IF Literal()
1584 THEN
1585 ErrorProcString := LastLiteral ;
1586 RETURN( TRUE )
1587 END
1588 END ;
1589 RETURN( FALSE )
1590 END ErrorProcedures ;
1591
1592
1593 (*
1594 TokenProcedure := Literal % TokenTypeProc := LastLiteral % =:
1595 *)
1596
1597 PROCEDURE TokenProcedure () : BOOLEAN ;
1598 BEGIN
1599 IF Literal()
1600 THEN
1601 TokenTypeProc := LastLiteral ;
1602 RETURN( TRUE )
1603 ELSE
1604 RETURN( FALSE )
1605 END
1606 END TokenProcedure ;
1607
1608
1609 (*
1610 SymProcedure := Literal % SymIsProc := LastLiteral % =:
1611 *)
1612
1613 PROCEDURE SymProcedure () : BOOLEAN ;
1614 BEGIN
1615 IF Literal()
1616 THEN
1617 SymIsProc := LastLiteral ;
1618 RETURN( TRUE )
1619 ELSE
1620 RETURN( FALSE )
1621 END
1622 END SymProcedure ;
1623
1624
1625 (*
1626 ExtBNF := " BNF " { Production } " FNB " =:
1627 *)
1628
1629 PROCEDURE ExtBNF () : BOOLEAN ;
1630 BEGIN
1631 IF SymIs(BNFtok)
1632 THEN
1633 WHILE Production() DO
1634 END ;
1635 IF SymIs(FNBtok)
1636 THEN
1637 RETURN( TRUE )
1638 END
1639 END ;
1640 RETURN( FALSE )
1641 END ExtBNF ;
1642
1643
1644 (*
1645 Production := Statement =:
1646 *)
1647
1648 PROCEDURE Production () : BOOLEAN ;
1649 BEGIN
1650 IF Statement()
1651 THEN
1652 RETURN( TRUE )
1653 END ;
1654 RETURN( FALSE )
1655 END Production ;
1656
1657
1658 (*
1659 Statement := % VAR i: IdentDesc ; %
1660 Ident
1661 % i := CurrentIdent ; %
1662 " := "
1663 % VAR e: ExpressionDesc ;
1664 e := NewExpression() ; %
1665 Expression
1666 % WITH CurrentStatement^ DO
1667 ident := i ;
1668 expr := e ;
1669 first := NIL ;
1670 END ;
1671 %
1672 " =: " =:
1673 *)
1674
1675 PROCEDURE Statement () : BOOLEAN ;
1676 VAR
1677 i: IdentDesc ;
1678 s: StatementDesc ;
1679 e: ExpressionDesc ;
1680 p: ProductionDesc ;
1681 BEGIN
1682 IF Ident()
1683 THEN
1684 p := FindDefinition(CurrentIdent^.name) ;
1685 IF p=NIL
1686 THEN
1687 p := NewProduction()
1688 ELSE
1689 IF NOT ((p^.statement=NIL) OR (p^.statement^.expr=NIL))
1690 THEN
1691 WarnError1('already declared rule %s', CurrentIdent^.name)
1692 END
1693 END ;
1694 i := CurrentIdent ;
1695 IF SymIs(lbecomestok)
1696 THEN
1697 e := NewExpression() ;
1698 CurrentExpression := e ;
1699 s := NewStatement() ;
1700 WITH s^ DO
1701 ident := i ;
1702 expr := e ;
1703 END ;
1704 IF Expression()
1705 THEN
1706 p^.statement := s ;
1707 IF SymIs(rbecomestok)
1708 THEN
1709 RETURN( TRUE )
1710 END
1711 END
1712 END
1713 END ;
1714 RETURN( FALSE )
1715 END Statement ;
1716
1717
1718 (*
1719 Expression := % CurrentTerm := NIL %
1720 Term { " | " % CurrentTerm := NewTerm() % Term } =:
1721 *)
1722
1723 PROCEDURE Expression () : BOOLEAN ;
1724 VAR
1725 t1, t2: TermDesc ;
1726 e : ExpressionDesc ;
1727 BEGIN
1728 e := CurrentExpression ;
1729 t1 := NewTerm() ;
1730 CurrentTerm := t1 ;
1731 IF Term()
1732 THEN
1733 e^.term := t1 ;
1734 WHILE SymIs(bartok) DO
1735 t2 := NewTerm() ;
1736 CurrentTerm := t2 ;
1737 IF Term()
1738 THEN
1739 t1^.next := t2 ;
1740 t1 := t2
1741 ELSE
1742 WarnError('term expected')
1743 END
1744 END ;
1745 RETURN( TRUE )
1746 ELSE
1747 (* DISPOSE(t1) ; *)
1748 RETURN( FALSE )
1749 END
1750 END Expression ;
1751
1752
1753 (*
1754 Term := Factor { Factor } =:
1755 *)
1756
1757 PROCEDURE Term () : BOOLEAN ;
1758 VAR
1759 t1: TermDesc ;
1760 f1, f2: FactorDesc ;
1761 BEGIN
1762 CurrentFactor := NewFactor() ;
1763 f1 := CurrentFactor ;
1764 t1 := CurrentTerm ;
1765 IF Factor()
1766 THEN
1767 t1^.factor := f1 ;
1768 f2 := NewFactor() ;
1769 CurrentFactor := f2 ;
1770 WHILE Factor() DO
1771 f1^.next := f2 ;
1772 f1 := f2 ;
1773 f2 := NewFactor() ;
1774 CurrentFactor := f2 ;
1775 END ;
1776 (* DISPOSE(f2) ; *)
1777 RETURN( TRUE )
1778 ELSE
1779 (* DISPOSE(f1) ; *)
1780 RETURN( FALSE )
1781 END
1782 END Term ;
1783
1784
1785 (*
1786 Factor := " % " Modula2Code " % " % AssignCode ; % |
1787 ( Ident | Literal | " { " Expression " } " |
1788 " [ " Expression " ] " | " ( " Expression " ) " ) =:
1789 *)
1790
1791 PROCEDURE Factor () : BOOLEAN ;
1792 BEGIN
1793 IF SymIs(codetok)
1794 THEN
1795 IF Modula2Code()
1796 THEN
1797 IF SymIs(codetok)
1798 THEN
1799 RETURN( TRUE )
1800 END
1801 END
1802 ELSE
1803 IF Ident()
1804 THEN
1805 WITH CurrentFactor^ DO
1806 type := id ;
1807 ident := CurrentIdent
1808 END ;
1809 RETURN( TRUE )
1810 ELSIF Literal()
1811 THEN
1812 WITH CurrentFactor^ DO
1813 type := lit ;
1814 string := LastLiteral ;
1815 IF GetSymKey(Aliases, LastLiteral)=NulKey
1816 THEN
1817 WarnError1('no token defined for literal %s', LastLiteral)
1818 END
1819 END ;
1820 RETURN( TRUE )
1821 ELSIF SymIs(lcparatok)
1822 THEN
1823 WITH CurrentFactor^ DO
1824 type := mult ;
1825 expr := NewExpression() ;
1826 CurrentExpression := expr ;
1827 IF Expression()
1828 THEN
1829 IF SymIs(rcparatok)
1830 THEN
1831 RETURN( TRUE )
1832 ELSE
1833 WarnError('} expected')
1834 END
1835 END
1836 END
1837 ELSIF SymIs(lsparatok)
1838 THEN
1839 WITH CurrentFactor^ DO
1840 type := opt ;
1841 expr := NewExpression() ;
1842 CurrentExpression := expr ;
1843 IF Expression()
1844 THEN
1845 IF SymIs(rsparatok)
1846 THEN
1847 RETURN( TRUE )
1848 ELSE
1849 WarnError('] expected')
1850 END
1851 END
1852 END
1853 ELSIF SymIs(lparatok)
1854 THEN
1855 WITH CurrentFactor^ DO
1856 type := sub ;
1857 expr := NewExpression() ;
1858 CurrentExpression := expr ;
1859 IF Expression()
1860 THEN
1861 IF SymIs(rparatok)
1862 THEN
1863 RETURN( TRUE )
1864 ELSE
1865 WarnError(') expected')
1866 END
1867 END
1868 END
1869 END
1870 END ;
1871 RETURN( FALSE )
1872 END Factor ;
1873
1874 (* % module pg end *)
1875
1876
1877 (*
1878 GetDefinitionName - returns the name of the rule inside, p.
1879 *)
1880
1881 PROCEDURE GetDefinitionName (p: ProductionDesc) : Name ;
1882 BEGIN
1883 IF p#NIL
1884 THEN
1885 WITH p^ DO
1886 IF (statement#NIL) AND (statement^.ident#NIL)
1887 THEN
1888 RETURN( statement^.ident^.name )
1889 END
1890 END
1891 END ;
1892 RETURN( NulName )
1893 END GetDefinitionName ;
1894
1895
1896 (*
1897 FindDefinition - searches and returns the rule which defines, n.
1898 *)
1899
1900 PROCEDURE FindDefinition (n: Name) : ProductionDesc ;
1901 VAR
1902 p, f: ProductionDesc ;
1903 BEGIN
1904 p := HeadProduction ;
1905 f := NIL ;
1906 WHILE p#NIL DO
1907 IF GetDefinitionName(p)=n
1908 THEN
1909 IF f=NIL
1910 THEN
1911 f := p
1912 ELSE
1913 WriteString('multiple definition for rule: ') ; WriteKey(n) ; WriteLn
1914 END
1915 END ;
1916 p := p^.next
1917 END ;
1918 RETURN( f )
1919 END FindDefinition ;
1920
1921
1922 (*
1923 BackPatchIdent - found an ident, i, we must look for the corresponding rule and
1924 set the definition accordingly.
1925 *)
1926
1927 PROCEDURE BackPatchIdent (i: IdentDesc) ;
1928 BEGIN
1929 IF i#NIL
1930 THEN
1931 WITH i^ DO
1932 definition := FindDefinition(name) ;
1933 IF definition=NIL
1934 THEN
1935 WarnError1('unable to find production %s', name) ;
1936 WasNoError := FALSE
1937 END
1938 END
1939 END
1940 END BackPatchIdent ;
1941
1942
1943 (*
1944 BackPatchFactor - runs through the factor looking for an ident
1945 *)
1946
1947 PROCEDURE BackPatchFactor (f: FactorDesc) ;
1948 BEGIN
1949 WHILE f#NIL DO
1950 WITH f^ DO
1951 CASE type OF
1952
1953 id : BackPatchIdent(ident) |
1954 sub ,
1955 opt ,
1956 mult: BackPatchExpression(expr)
1957
1958 ELSE
1959 END
1960 END ;
1961 f := f^.next
1962 END
1963 END BackPatchFactor ;
1964
1965
1966 (*
1967 BackPatchTerm - runs through all terms to find idents.
1968 *)
1969
1970 PROCEDURE BackPatchTerm (t: TermDesc) ;
1971 BEGIN
1972 WHILE t#NIL DO
1973 BackPatchFactor(t^.factor) ;
1974 t := t^.next
1975 END
1976 END BackPatchTerm ;
1977
1978
1979 (*
1980 BackPatchExpression - runs through the term to find any idents.
1981 *)
1982
1983 PROCEDURE BackPatchExpression (e: ExpressionDesc) ;
1984 BEGIN
1985 IF e#NIL
1986 THEN
1987 BackPatchTerm(e^.term)
1988 END
1989 END BackPatchExpression ;
1990
1991
1992 (*
1993 BackPatchSet -
1994 *)
1995
1996 PROCEDURE BackPatchSet (s: SetDesc) ;
1997 BEGIN
1998 WHILE s#NIL DO
1999 WITH s^ DO
2000 CASE type OF
2001
2002 idel: BackPatchIdent(ident)
2003
2004 ELSE
2005 END
2006 END ;
2007 s := s^.next
2008 END
2009 END BackPatchSet ;
2010
2011
2012 (*
2013 BackPatchIdentToDefinitions - search through all the rules and add a link from any ident
2014 to the definition.
2015 *)
2016
2017 PROCEDURE BackPatchIdentToDefinitions (d: ProductionDesc) ;
2018 BEGIN
2019 IF (d#NIL) AND (d^.statement#NIL)
2020 THEN
2021 BackPatchExpression(d^.statement^.expr)
2022 END
2023 END BackPatchIdentToDefinitions ;
2024
2025
2026 (*
2027 CalculateFirstAndFollow -
2028 *)
2029
2030 PROCEDURE CalculateFirstAndFollow (p: ProductionDesc) ;
2031 BEGIN
2032 IF Debugging
2033 THEN
2034 WriteLn ;
2035 WriteKey(p^.statement^.ident^.name) ; WriteLn ;
2036 WriteString(' calculating first')
2037 END ;
2038 CalcFirstProduction(p, p, p^.first) ;
2039 BackPatchSet(p^.first) ;
2040 IF Debugging
2041 THEN
2042 WriteString(' calculating follow set')
2043 END ;
2044 IF p^.followinfo^.follow=NIL
2045 THEN
2046 CalcFollowProduction(p)
2047 END ;
2048 BackPatchSet(p^.followinfo^.follow)
2049 END CalculateFirstAndFollow ;
2050
2051
2052 (*
2053 ForeachRuleDo -
2054 *)
2055
2056 PROCEDURE ForeachRuleDo (p: DoProcedure) ;
2057 BEGIN
2058 CurrentProduction := HeadProduction ;
2059 WHILE CurrentProduction#NIL DO
2060 p(CurrentProduction) ;
2061 CurrentProduction := CurrentProduction^.next
2062 END
2063 END ForeachRuleDo ;
2064
2065
2066 (*
2067 WhileNotCompleteDo -
2068 *)
2069
2070 PROCEDURE WhileNotCompleteDo (p: DoProcedure) ;
2071 BEGIN
2072 REPEAT
2073 Finished := TRUE ;
2074 ForeachRuleDo(p) ;
2075 UNTIL Finished
2076 END WhileNotCompleteDo ;
2077
2078
2079 (*
2080 NewLine - generate a newline and indent.
2081 *)
2082
2083 PROCEDURE NewLine (Left: CARDINAL) ;
2084 BEGIN
2085 Output.WriteLn ;
2086 BeginningOfLine := TRUE ;
2087 Indent := 0 ;
2088 WHILE Indent<Left DO
2089 Output.Write(' ') ;
2090 INC(Indent)
2091 END
2092 END NewLine ;
2093
2094
2095 (*
2096 CheckNewLine -
2097 *)
2098
2099 PROCEDURE CheckNewLine (Left: CARDINAL) ;
2100 BEGIN
2101 IF Indent=Left
2102 THEN
2103 Left := BaseNewLine
2104 END ;
2105 IF Indent>BaseRightMargin
2106 THEN
2107 NewLine(Left)
2108 END
2109 END CheckNewLine ;
2110
2111
2112 (*
2113 IndentString - writes out a string with a preceeding indent.
2114 *)
2115
2116 PROCEDURE IndentString (a: ARRAY OF CHAR) ;
2117 VAR
2118 i: CARDINAL ;
2119 BEGIN
2120 i := 0 ;
2121 WHILE i<Indent DO
2122 Output.Write(' ') ;
2123 INC(i)
2124 END ;
2125 Output.WriteString(a) ;
2126 LastLineNo := 0
2127 END IndentString ;
2128
2129
2130 (*
2131 KeyWord - writes out a keywork with optional formatting directives.
2132 *)
2133
2134 PROCEDURE KeyWord (n: Name) ;
2135 BEGIN
2136 IF KeywordFormatting
2137 THEN
2138 Output.WriteString('{%K') ;
2139 IF (n = MakeKey('}')) OR (n = MakeKey('{')) OR (n = MakeKey('%'))
2140 THEN
2141 Output.Write('%') (* escape }, { or % *)
2142 END ;
2143 Output.WriteKey(n) ;
2144 Output.Write('}')
2145 ELSE
2146 Output.WriteKey(n)
2147 END
2148 END KeyWord ;
2149
2150
2151 (*
2152 PrettyPara -
2153 *)
2154
2155 PROCEDURE PrettyPara (c1, c2: ARRAY OF CHAR; e: ExpressionDesc; Left: CARDINAL) ;
2156 BEGIN
2157 Output.WriteString(c1) ;
2158 INC(Indent, StrLen(c1)) ;
2159 Left := Indent ;
2160 PrettyCommentExpression(e, Left) ;
2161 Output.WriteString(c2) ;
2162 INC(Indent, StrLen(c2))
2163 END PrettyPara ;
2164
2165
2166 (*
2167 WriteKeyTexinfo -
2168 *)
2169
2170 PROCEDURE WriteKeyTexinfo (s: Name) ;
2171 VAR
2172 ds : String ;
2173 ch : CHAR ;
2174 i, l: CARDINAL ;
2175 BEGIN
2176 IF Texinfo
2177 THEN
2178 ds := InitStringCharStar(KeyToCharStar(s)) ;
2179 l := Length(ds) ;
2180 i := 0 ;
2181 WHILE i<l DO
2182 ch := char(ds, i) ;
2183 IF (ch='{') OR (ch='}')
2184 THEN
2185 Output.Write('@')
2186 END ;
2187 Output.Write(ch) ;
2188 INC(i)
2189 END
2190 ELSE
2191 Output.WriteKey(s)
2192 END
2193 END WriteKeyTexinfo ;
2194
2195
2196 (*
2197 PrettyCommentFactor -
2198 *)
2199
2200 PROCEDURE PrettyCommentFactor (f: FactorDesc; Left: CARDINAL) ;
2201 VAR
2202 curpos : CARDINAL ;
2203 seentext: BOOLEAN ;
2204 BEGIN
2205 WHILE f#NIL DO
2206 CheckNewLine(Left) ;
2207 WITH f^ DO
2208 CASE type OF
2209
2210 id : Output.WriteKey(ident^.name) ; Output.WriteString(' ') ;
2211 INC(Indent, LengthKey(ident^.name)+1) |
2212 lit : IF MakeKey("'")=string
2213 THEN
2214 Output.Write('"') ; WriteKeyTexinfo(string) ; Output.WriteString('" ')
2215 ELSE
2216 Output.Write("'") ; WriteKeyTexinfo(string) ; Output.WriteString("' ")
2217 END ;
2218 INC(Indent, LengthKey(string)+3) |
2219 sub: PrettyPara('( ', ' ) ', expr, Left) |
2220 opt: PrettyPara('[ ', ' ] ', expr, Left) |
2221 mult: IF Texinfo
2222 THEN
2223 PrettyPara('@{ ', ' @} ', expr, Left)
2224 ELSE
2225 PrettyPara('{ ', ' } ', expr, Left)
2226 END |
2227 m2 : IF EmitCode
2228 THEN
2229 NewLine(Left) ; Output.WriteString('% ') ;
2230 seentext := FALSE ;
2231 curpos := 0 ;
2232 WriteCodeHunkListIndent(code^.code, code^.indent, curpos, Left+2, seentext) ;
2233 Output.WriteString(' %') ;
2234 NewLine(Left)
2235 END
2236
2237 ELSE
2238 END ;
2239 PrettyFollow('<f:', ':f>', followinfo)
2240 END ;
2241 f := f^.next
2242 END
2243 END PrettyCommentFactor ;
2244
2245
2246 (*
2247 PeepTerm - returns the length of characters in term.
2248 *)
2249
2250 PROCEDURE PeepTerm (t: TermDesc) : CARDINAL ;
2251 VAR
2252 l: CARDINAL ;
2253 BEGIN
2254 l := 0 ;
2255 WHILE t#NIL DO
2256 INC(l, PeepFactor(t^.factor)) ;
2257 IF t^.next#NIL
2258 THEN
2259 INC(l, 3)
2260 END ;
2261 t := t^.next
2262 END ;
2263 RETURN( l )
2264 END PeepTerm ;
2265
2266
2267 (*
2268 PeepExpression - returns the length of the expression.
2269 *)
2270
2271 PROCEDURE PeepExpression (e: ExpressionDesc) : CARDINAL ;
2272 BEGIN
2273 IF e=NIL
2274 THEN
2275 RETURN( 0 )
2276 ELSE
2277 RETURN( PeepTerm(e^.term) )
2278 END
2279 END PeepExpression ;
2280
2281
2282 (*
2283 PeepFactor - returns the length of character in the factor
2284 *)
2285
2286 PROCEDURE PeepFactor (f: FactorDesc) : CARDINAL ;
2287 VAR
2288 l: CARDINAL ;
2289 BEGIN
2290 l := 0 ;
2291 WHILE f#NIL DO
2292 WITH f^ DO
2293 CASE type OF
2294
2295 id : INC(l, LengthKey(ident^.name)+1) |
2296 lit : INC(l, LengthKey(string)+3) |
2297 opt ,
2298 mult,
2299 sub : INC(l, PeepExpression(expr)) |
2300 m2 : (* empty *)
2301
2302 ELSE
2303 END
2304 END ;
2305 f := f^.next
2306 END ;
2307 RETURN( l )
2308 END PeepFactor ;
2309
2310
2311 (*
2312 PrettyCommentTerm -
2313 *)
2314
2315 PROCEDURE PrettyCommentTerm (t: TermDesc; Left: CARDINAL) ;
2316 BEGIN
2317 WHILE t#NIL DO
2318 CheckNewLine(Left) ;
2319 PrettyCommentFactor(t^.factor, Left) ;
2320 IF t^.next#NIL
2321 THEN
2322 Output.WriteString(' | ') ;
2323 INC(Indent, 3) ;
2324 IF PeepFactor(t^.factor)+Indent>BaseRightMargin
2325 THEN
2326 NewLine(Left)
2327 END
2328 END ;
2329 PrettyFollow('<t:', ':t>', t^.followinfo) ;
2330 t := t^.next
2331 END
2332 END PrettyCommentTerm ;
2333
2334
2335 (*
2336 PrettyCommentExpression -
2337 *)
2338
2339 PROCEDURE PrettyCommentExpression (e: ExpressionDesc; Left: CARDINAL) ;
2340 BEGIN
2341 IF e#NIL
2342 THEN
2343 PrettyCommentTerm(e^.term, Left) ;
2344 PrettyFollow('<e:', ':e>', e^.followinfo)
2345 END
2346 END PrettyCommentExpression ;
2347
2348
2349 (*
2350 PrettyCommentStatement -
2351 *)
2352
2353 PROCEDURE PrettyCommentStatement (s: StatementDesc; Left: CARDINAL) ;
2354 BEGIN
2355 IF s#NIL
2356 THEN
2357 PrettyCommentExpression(s^.expr, Left) ;
2358 PrettyFollow('<s:', ':s>', s^.followinfo)
2359 END
2360 END PrettyCommentStatement ;
2361
2362
2363 (*
2364 PrettyCommentProduction - generates the comment for rule, p.
2365 *)
2366
2367 PROCEDURE PrettyCommentProduction (p: ProductionDesc) ;
2368 VAR
2369 to: SetDesc ;
2370 BEGIN
2371 IF p#NIL
2372 THEN
2373 BeginningOfLine := TRUE ;
2374 Indent := 0 ;
2375 Output.WriteString('(*') ; NewLine(3) ;
2376 Output.WriteKey(GetDefinitionName(p)) ;
2377 Output.WriteString(' := ') ;
2378 INC(Indent, LengthKey(GetDefinitionName(p))+4) ;
2379 PrettyCommentStatement(p^.statement, Indent) ;
2380 NewLine(0) ;
2381 IF ErrorRecovery
2382 THEN
2383 NewLine(3) ;
2384 Output.WriteString('first symbols:') ;
2385 EmitSet(p^.first, 0, 0) ;
2386 NewLine(3) ;
2387 PrettyFollow('<p:', ':p>', p^.followinfo) ;
2388 NewLine(3) ;
2389 CASE GetReachEnd(p^.followinfo) OF
2390
2391 true : Output.WriteString('reachend') |
2392 false : Output.WriteString('cannot reachend') |
2393 unknown: Output.WriteString('unknown...')
2394
2395 ELSE
2396 END ;
2397 NewLine(0)
2398 END ;
2399 Output.WriteString('*)') ; NewLine(0) ;
2400 END
2401 END PrettyCommentProduction ;
2402
2403
2404 (*
2405 PrettyPrintProduction - pretty prints the ebnf rule, p.
2406 *)
2407
2408 PROCEDURE PrettyPrintProduction (p: ProductionDesc) ;
2409 VAR
2410 to: SetDesc ;
2411 BEGIN
2412 IF p#NIL
2413 THEN
2414 BeginningOfLine := TRUE ;
2415 Indent := 0 ;
2416 IF Texinfo
2417 THEN
2418 Output.WriteString('@example') ; NewLine(0)
2419 ELSIF Sphinx
2420 THEN
2421 Output.WriteString('.. code-block:: ebnf') ; NewLine(0)
2422 END ;
2423 Output.WriteKey(GetDefinitionName(p)) ;
2424 Output.WriteString(' := ') ;
2425 INC(Indent, LengthKey(GetDefinitionName(p))+4) ;
2426 PrettyCommentStatement(p^.statement, Indent) ;
2427 IF p^.description#NulName
2428 THEN
2429 Output.WriteKey(p^.description)
2430 END ;
2431 NewLine(0) ;
2432 WriteIndent(LengthKey(GetDefinitionName(p))+1) ;
2433 Output.WriteString(' =: ') ;
2434 NewLine(0) ;
2435 IF Texinfo
2436 THEN
2437 Output.WriteString('@findex ') ; Output.WriteKey(GetDefinitionName(p)) ; Output.WriteString(' (ebnf)') ; NewLine(0) ;
2438 Output.WriteString('@end example') ; NewLine(0)
2439 ELSIF Sphinx
2440 THEN
2441 Output.WriteString('.. index::') ; NewLine(0) ;
2442 Output.WriteString(' pair: ') ; Output.WriteKey(GetDefinitionName(p)) ; Output.WriteString('; (ebnf)') ; NewLine(0)
2443 END ;
2444 NewLine(0)
2445 END
2446 END PrettyPrintProduction ;
2447
2448
2449 (*
2450 EmitFileLineTag - emits a line and file tag using the C preprocessor syntax.
2451 *)
2452
2453 PROCEDURE EmitFileLineTag (line: CARDINAL) ;
2454 BEGIN
2455 IF (NOT SuppressFileLineTag) AND (line#LastLineNo)
2456 THEN
2457 LastLineNo := line ;
2458 IF NOT OnLineStart
2459 THEN
2460 Output.WriteLn
2461 END ;
2462 Output.WriteString('# ') ; Output.WriteCard(line, 0) ; Output.WriteString(' "') ; Output.WriteString(FileName) ; Output.Write('"') ;
2463 Output.WriteLn ;
2464 OnLineStart := TRUE
2465 END
2466 END EmitFileLineTag ;
2467
2468
2469 (*
2470 EmitRule - generates a comment and code for rule, p.
2471 *)
2472
2473 PROCEDURE EmitRule (p: ProductionDesc) ;
2474 BEGIN
2475 IF PrettyPrint
2476 THEN
2477 PrettyPrintProduction(p)
2478 ELSE
2479 PrettyCommentProduction(p) ;
2480 IF ErrorRecovery
2481 THEN
2482 RecoverProduction(p)
2483 ELSE
2484 CodeProduction(p)
2485 END
2486 END
2487 END EmitRule ;
2488
2489
2490 (*
2491 CodeCondition -
2492 *)
2493
2494 PROCEDURE CodeCondition (m: m2condition) ;
2495 BEGIN
2496 CASE m OF
2497
2498 m2if,
2499 m2none : IndentString('IF ') |
2500 m2elsif: IndentString('ELSIF ') |
2501 m2while: IndentString('WHILE ')
2502
2503 ELSE
2504 Halt('unrecognised m2condition',
2505 __FILE__, __FUNCTION__, __LINE__)
2506 END
2507 END CodeCondition ;
2508
2509
2510 (*
2511 CodeThenDo - codes a "THEN" or "DO" depending upon, m.
2512 *)
2513
2514 PROCEDURE CodeThenDo (m: m2condition) ;
2515 BEGIN
2516 CASE m OF
2517
2518 m2if,
2519 m2none,
2520 m2elsif: IF LastLineNo=0
2521 THEN
2522 Output.WriteLn
2523 END ;
2524 IndentString('THEN') ;
2525 Output.WriteLn |
2526 m2while: Output.WriteString(' DO') ;
2527 Output.WriteLn
2528
2529 ELSE
2530 Halt('unrecognised m2condition',
2531 __FILE__, __FUNCTION__, __LINE__)
2532 END ;
2533 OnLineStart := TRUE
2534 END CodeThenDo ;
2535
2536
2537 (*
2538 CodeElseEnd - builds an ELSE END statement using string, end.
2539 *)
2540
2541 PROCEDURE CodeElseEnd (end: ARRAY OF CHAR; consumed: BOOLEAN; f: FactorDesc; inopt: BOOLEAN) ;
2542 BEGIN
2543 Output.WriteLn ;
2544 OnLineStart := TRUE ;
2545 EmitFileLineTag(f^.line) ;
2546 IF NOT inopt
2547 THEN
2548 IndentString('ELSE') ; WriteLn ;
2549 INC(Indent, 3) ;
2550 IF consumed
2551 THEN
2552 IndentString('') ;
2553 Output.WriteKey(ErrorProcArray) ;
2554 Output.Write('(') ;
2555 WITH f^ DO
2556 CASE type OF
2557
2558 id : Output.Write("'") ; Output.WriteKey(ident^.name) ; Output.WriteString(' - expected') ; Output.WriteString("') ;") |
2559 lit : IF MakeKey("'")=string
2560 THEN
2561 Output.Write('"') ;
2562 KeyWord(string) ;
2563 Output.WriteString(' - expected') ; Output.WriteString('") ;')
2564 ELSIF MakeKey('"')=string
2565 THEN
2566 Output.Write("'") ; KeyWord(string) ;
2567 Output.WriteString(' - expected') ; Output.WriteString("') ;")
2568 ELSE
2569 Output.Write('"') ; Output.Write("'") ; KeyWord(string) ; Output.WriteString("' - expected") ;
2570 Output.WriteString('") ;')
2571 END
2572
2573 ELSE
2574 END
2575 END ;
2576 Output.WriteLn
2577 END ;
2578 IndentString('RETURN( FALSE )') ;
2579 DEC(Indent, 3) ;
2580 Output.WriteLn
2581 END ;
2582 IndentString(end) ;
2583 Output.WriteLn ;
2584 OnLineStart := TRUE
2585 END CodeElseEnd ;
2586
2587
2588 (*
2589 CodeEnd - codes a "END" depending upon, m.
2590 *)
2591
2592 PROCEDURE CodeEnd (m: m2condition; t: TermDesc; consumed: BOOLEAN; f: FactorDesc; inopt: BOOLEAN) ;
2593 BEGIN
2594 DEC(Indent, 3) ;
2595 Output.WriteLn ;
2596 OnLineStart := TRUE ;
2597 CASE m OF
2598
2599 m2none : IF t=NIL
2600 THEN
2601 CodeElseEnd('END ;', consumed, f, inopt)
2602 END |
2603 m2if : IF t=NIL
2604 THEN
2605 CodeElseEnd('END ; (* if *)', consumed, f, inopt)
2606 END |
2607 m2elsif: IF t=NIL
2608 THEN
2609 CodeElseEnd('END ; (* elsif *)', consumed, f, inopt)
2610 END |
2611 m2while: IndentString('END ; (* while *)')
2612
2613 ELSE
2614 Halt('unrecognised m2condition',
2615 __FILE__, __FUNCTION__, __LINE__)
2616 END ;
2617 OnLineStart := FALSE
2618 END CodeEnd ;
2619
2620
2621 (*
2622 EmitNonVarCode - writes out, code, providing it is not a variable declaration.
2623 *)
2624
2625 PROCEDURE EmitNonVarCode (code: CodeDesc; curpos, left: CARDINAL) ;
2626 VAR
2627 i : CARDINAL ;
2628 t : CodeHunk ;
2629 seentext: BOOLEAN ;
2630 BEGIN
2631 t := code^.code ;
2632 IF (NOT FindStr(t, i, 'VAR')) AND EmitCode
2633 THEN
2634 seentext := FALSE ;
2635 curpos := 0 ;
2636 EmitFileLineTag(code^.line) ;
2637 IndentString('') ;
2638 WriteCodeHunkListIndent(code^.code, code^.indent, curpos, left, seentext) ;
2639 Output.WriteString(' ;') ;
2640 Output.WriteLn ;
2641 OnLineStart := TRUE
2642 END
2643 END EmitNonVarCode ;
2644
2645
2646 (*
2647 ChainOn -
2648 *)
2649
2650 PROCEDURE ChainOn (codeStack, f: FactorDesc) : FactorDesc ;
2651 VAR
2652 s: FactorDesc ;
2653 BEGIN
2654 f^.pushed := NIL ;
2655 IF codeStack=NIL
2656 THEN
2657 RETURN( f )
2658 ELSE
2659 s := codeStack ;
2660 WHILE s^.pushed#NIL DO
2661 s := s^.pushed
2662 END ;
2663 s^.pushed := f ;
2664 RETURN( codeStack )
2665 END
2666 END ChainOn ;
2667
2668
2669 (*
2670 FlushCode -
2671 *)
2672
2673 PROCEDURE FlushCode (VAR codeStack: FactorDesc) ;
2674 BEGIN
2675 IF codeStack#NIL
2676 THEN
2677 NewLine(Indent) ; Output.WriteString('(* begin flushing code *)') ;
2678 OnLineStart := FALSE ;
2679 WHILE codeStack#NIL DO
2680 NewLine(Indent) ; EmitNonVarCode(codeStack^.code, 0, Indent) ; NewLine(Indent) ;
2681 codeStack := codeStack^.pushed ;
2682 IF codeStack#NIL
2683 THEN
2684 Output.WriteString(' (* again flushing code *)') ; Output.WriteLn ;
2685 OnLineStart := TRUE
2686 END
2687 END ;
2688 NewLine(Indent) ;
2689 Output.WriteString('(* end flushing code *)') ;
2690 OnLineStart := FALSE
2691 END
2692 END FlushCode ;
2693
2694
2695 (*
2696 CodeFactor -
2697 *)
2698
2699 PROCEDURE CodeFactor (f: FactorDesc; t: TermDesc; l, n: m2condition; inopt, inwhile, consumed: BOOLEAN; codeStack: FactorDesc) ;
2700 BEGIN
2701 IF f=NIL
2702 THEN
2703 IF (* ((l=m2elsif) OR (l=m2if) OR (l=m2none)) AND *) (NOT inwhile) AND (NOT inopt)
2704 THEN
2705 Output.WriteLn ;
2706 IndentString('RETURN( TRUE )') ;
2707 OnLineStart := FALSE
2708 END
2709 ELSE
2710 WITH f^ DO
2711 EmitFileLineTag(line) ;
2712 CASE type OF
2713
2714 id : FlushCode(codeStack) ;
2715 CodeCondition(n) ;
2716 Output.WriteKey(ident^.name) ; Output.WriteString('()') ;
2717 CodeThenDo(n) ;
2718 INC(Indent, 3) ;
2719 CodeFactor(f^.next, NIL, n, m2none, inopt, inwhile, TRUE, NIL) ;
2720 CodeEnd(n, t, consumed, f, inopt) |
2721 lit : FlushCode(codeStack) ;
2722 CodeCondition(n) ;
2723 Output.WriteKey(SymIsProc) ; Output.Write('(') ;
2724 Output.WriteKey(GetSymKey(Aliases, string)) ; Output.Write(')') ;
2725 CodeThenDo(n) ;
2726 INC(Indent, 3) ;
2727 CodeFactor(f^.next, NIL, n, m2none, inopt, inwhile, TRUE, NIL) ;
2728 CodeEnd(n, t, consumed, f, inopt) |
2729 sub: FlushCode(codeStack) ;
2730 CodeExpression(expr, m2none, inopt, inwhile, consumed, NIL) ;
2731 IF f^.next#NIL
2732 THEN
2733 (*
2734 * the test above makes sure that we don't emit a RETURN( TRUE )
2735 * after a subexpression. Remember sub expressions are not conditional
2736 *)
2737 CodeFactor(f^.next, t, n, m2none, inopt, inwhile, TRUE, NIL)
2738 END |
2739 opt: FlushCode(codeStack) ;
2740 CodeExpression(expr, m2if, TRUE, inwhile, FALSE, NIL) ;
2741 CodeFactor(f^.next, t, n, m2none, inopt, inwhile, consumed, NIL) |
2742 mult: FlushCode(codeStack) ;
2743 CodeExpression(expr, m2while, FALSE, TRUE, consumed, NIL) ;
2744 CodeFactor(f^.next, t, n, m2none, inopt, inwhile, consumed, NIL) |
2745 m2 : codeStack := ChainOn(codeStack, f) ;
2746 IF consumed OR (f^.next=NIL)
2747 THEN
2748 FlushCode(codeStack)
2749 END ;
2750 CodeFactor(f^.next, t, n, m2none, inopt, inwhile, consumed, codeStack)
2751
2752 ELSE
2753 END
2754 END
2755 END
2756 END CodeFactor ;
2757
2758
2759 (*
2760 CodeTerm -
2761 *)
2762
2763 PROCEDURE CodeTerm (t: TermDesc; m: m2condition; inopt, inwhile, consumed: BOOLEAN; codeStack: FactorDesc) ;
2764 VAR
2765 l: m2condition ;
2766 BEGIN
2767 l := m ;
2768 WHILE t#NIL DO
2769 EmitFileLineTag(t^.line) ;
2770 IF (t^.factor^.type=m2) AND (m=m2elsif)
2771 THEN
2772 m := m2if ;
2773 IndentString('ELSE') ; Output.WriteLn ;
2774 OnLineStart := TRUE ;
2775 INC(Indent, 3) ;
2776 CodeFactor(t^.factor, t^.next, m2none, m2none, inopt, inwhile, consumed, codeStack) ;
2777 DEC(Indent, 3) ;
2778 IndentString('END ;') ; Output.WriteLn ;
2779 OnLineStart := TRUE
2780 ELSE
2781 CodeFactor(t^.factor, t^.next, m2none, m, inopt, inwhile, consumed, codeStack)
2782 END ;
2783 l := m ;
2784 IF t^.next#NIL
2785 THEN
2786 m := m2elsif
2787 END ;
2788 t := t^.next
2789 END
2790 END CodeTerm ;
2791
2792
2793 (*
2794 CodeExpression -
2795 *)
2796
2797 PROCEDURE CodeExpression (e: ExpressionDesc; m: m2condition; inopt, inwhile, consumed: BOOLEAN; codeStack: FactorDesc) ;
2798 BEGIN
2799 IF e#NIL
2800 THEN
2801 EmitFileLineTag(e^.line) ;
2802 CodeTerm(e^.term, m, inopt, inwhile, consumed, codeStack)
2803 END
2804 END CodeExpression ;
2805
2806
2807 (*
2808 CodeStatement -
2809 *)
2810
2811 PROCEDURE CodeStatement (s: StatementDesc; m: m2condition) ;
2812 BEGIN
2813 IF s#NIL
2814 THEN
2815 EmitFileLineTag(s^.line) ;
2816 CodeExpression(s^.expr, m, FALSE, FALSE, FALSE, NIL)
2817 END
2818 END CodeStatement ;
2819
2820
2821 (*
2822 CodeProduction - only encode grammer rules which are not special.
2823 *)
2824
2825 PROCEDURE CodeProduction (p: ProductionDesc) ;
2826 BEGIN
2827 IF (p#NIL) AND ((NOT p^.firstsolved) OR ((p^.statement#NIL) AND (p^.statement^.expr#NIL)))
2828 THEN
2829 BeginningOfLine := TRUE ;
2830 Indent := 0 ;
2831 Output.WriteLn ;
2832 EmitFileLineTag(p^.line) ;
2833 IndentString('PROCEDURE ') ;
2834 Output.WriteKey(GetDefinitionName(p)) ;
2835 Output.WriteString(' () : BOOLEAN ;') ;
2836 VarProduction(p) ;
2837 Output.WriteLn ;
2838 OnLineStart := TRUE ;
2839 EmitFileLineTag(p^.line) ;
2840 IndentString('BEGIN') ; WriteLn ;
2841 OnLineStart := FALSE ;
2842 EmitFileLineTag(p^.line) ;
2843 Indent := 3 ;
2844 CodeStatement(p^.statement, m2none) ;
2845 Output.WriteLn ;
2846 Indent := 0 ;
2847 IndentString('END ') ; WriteKey(GetDefinitionName(p)) ; Output.WriteString(' ;') ;
2848 Output.WriteLn ;
2849 Output.WriteLn ;
2850 Output.WriteLn
2851 END
2852 END CodeProduction ;
2853
2854
2855 (* and now for the production of code which will recover from syntax errors *)
2856
2857
2858 (*
2859 RecoverCondition -
2860 *)
2861
2862 PROCEDURE RecoverCondition (m: m2condition) ;
2863 BEGIN
2864 CASE m OF
2865
2866 m2if : IndentString('IF ') |
2867 m2none : IndentString('IF ') |
2868 m2elsif: IndentString('ELSIF ') |
2869 m2while: IndentString('WHILE ')
2870
2871 ELSE
2872 Halt('unrecognised m2condition',
2873 __FILE__, __FUNCTION__, __LINE__)
2874 END
2875 END RecoverCondition ;
2876
2877
2878 (*
2879 ConditionIndent - returns the number of spaces indentation created via, m.
2880 *)
2881
2882 PROCEDURE ConditionIndent (m: m2condition) : CARDINAL ;
2883 BEGIN
2884 CASE m OF
2885
2886 m2if : RETURN( 3 ) |
2887 m2none : RETURN( 3 ) |
2888 m2elsif: RETURN( 6 ) |
2889 m2while: RETURN( 6 )
2890
2891 ELSE
2892 Halt('unrecognised m2condition',
2893 __FILE__, __FUNCTION__, __LINE__)
2894 END
2895 END ConditionIndent ;
2896
2897
2898 (*
2899 WriteGetTokenType - writes out the method of determining the token type.
2900 *)
2901
2902 PROCEDURE WriteGetTokenType ;
2903 BEGIN
2904 Output.WriteKey(TokenTypeProc)
2905 END WriteGetTokenType ;
2906
2907
2908 (*
2909 NumberOfElements - returns the number of elements in set, to, which lie between low..high
2910 *)
2911
2912 PROCEDURE NumberOfElements (to: SetDesc; low, high: WORD) : CARDINAL ;
2913 VAR
2914 n: CARDINAL ;
2915 BEGIN
2916 n := 0 ;
2917 WHILE to#NIL DO
2918 WITH to^ DO
2919 CASE type OF
2920
2921 tokel: IF (high=0) OR IsBetween(string, low, high)
2922 THEN
2923 INC(n)
2924 END |
2925 litel: IF (high=0) OR IsBetween(GetSymKey(Aliases, string), low, high)
2926 THEN
2927 INC(n)
2928 END |
2929 idel : WarnError('not expecting ident in first symbol list') ;
2930 WasNoError := FALSE
2931
2932 ELSE
2933 WarnError('unknown enuneration element') ;
2934 WasNoError := FALSE
2935 END
2936 END ;
2937 to := to^.next ;
2938 END ;
2939 RETURN( n )
2940 END NumberOfElements ;
2941
2942
2943 (*
2944 WriteElement - writes the literal name for element, e.
2945 *)
2946
2947 PROCEDURE WriteElement (e: WORD) ;
2948 BEGIN
2949 Output.WriteKey(GetSymKey(ReverseValues, e))
2950 END WriteElement ;
2951
2952
2953 (*
2954 EmitIsInSet - writes out the equivalent of GetTokenType() IN { toset }
2955 *)
2956
2957 PROCEDURE EmitIsInSet (to: SetDesc; low, high: Name) ;
2958 BEGIN
2959 IF NumberOfElements(to, low, high)=1
2960 THEN
2961 WriteGetTokenType ; Output.Write('=') ; EmitSet(to, low, high)
2962 ELSE
2963 WriteGetTokenType ;
2964 Output.WriteString(' IN SetOfStop') ;
2965 IF LargestValue > MaxElementsInSet
2966 THEN
2967 Output.WriteCard(CARDINAL(low) DIV MaxElementsInSet, 0)
2968 END ;
2969 Output.WriteString(' {') ; EmitSet(to, low, high) ; Output.WriteString('}')
2970 END
2971 END EmitIsInSet ;
2972
2973
2974 (*
2975 EmitIsInSubSet - writes out a test to see whether GetTokenype() is in { subset }
2976 *)
2977
2978 PROCEDURE EmitIsInSubSet (to: SetDesc; low, high: WORD) ;
2979 BEGIN
2980 IF NumberOfElements(to, low, high)=1
2981 THEN
2982 Output.Write('(') ; EmitIsInSet(to, low, high) ; Output.Write(')')
2983 ELSIF low=0
2984 THEN
2985 (* no need to check whether GetTokenType > low *)
2986 Output.WriteString('((') ; WriteGetTokenType ; Output.Write('<') ; WriteElement(INTEGER(high)+1) ;
2987 Output.WriteString(') AND (') ; EmitIsInSet(to, low, high) ; Output.WriteString('))')
2988 ELSIF CARDINAL(high)>LargestValue
2989 THEN
2990 (* no need to check whether GetTokenType < high *)
2991 Output.WriteString('((') ; WriteGetTokenType ; Output.WriteString('>=') ; WriteElement(low) ;
2992 Output.WriteString(') AND (') ; EmitIsInSet(to, low, high) ; Output.WriteString('))')
2993 ELSE
2994 Output.WriteString('((') ; WriteGetTokenType ; Output.WriteString('>=') ; WriteElement(low) ;
2995 Output.WriteString(') AND (') ; WriteGetTokenType ; Output.Write('<') ; WriteElement(INTEGER(high)+1) ;
2996 Output.WriteString(') AND (') ; EmitIsInSet(to, low, high) ;
2997 Output.WriteString('))')
2998 END
2999 END EmitIsInSubSet ;
3000
3001
3002 (*
3003 EmitIsInFirst -
3004 *)
3005
3006 PROCEDURE EmitIsInFirst (to: SetDesc; m: m2condition) ;
3007 VAR
3008 i : CARDINAL ;
3009 first: BOOLEAN ;
3010 BEGIN
3011 IF NumberOfElements(to, 0, 0)=1
3012 THEN
3013 (* only one element *)
3014 WriteGetTokenType ;
3015 Output.Write('=') ;
3016 EmitSet(to, 0, 0)
3017 ELSE
3018 IF LargestValue<=MaxElementsInSet
3019 THEN
3020 Output.Write('(') ; WriteGetTokenType ; Output.WriteString(' IN ') ; EmitSetAsParameters(to) ; Output.WriteString(')')
3021 ELSE
3022 i := 0 ;
3023 first := TRUE ;
3024 REPEAT
3025 IF NOT IsEmptySet(to, i*MaxElementsInSet, (i+1)*MaxElementsInSet-1)
3026 THEN
3027 IF NOT first
3028 THEN
3029 Output.WriteString(' OR') ;
3030 NewLine(Indent+ConditionIndent(m)) ;
3031 DEC(Indent, ConditionIndent(m))
3032 END ;
3033 EmitIsInSubSet(to, i*MaxElementsInSet, (i+1)*MaxElementsInSet-1) ;
3034 first := FALSE
3035 END ;
3036 INC(i) ;
3037 UNTIL i*MaxElementsInSet>LargestValue
3038 END
3039 END
3040 END EmitIsInFirst ;
3041
3042
3043 (*
3044 FlushCode -
3045 *)
3046
3047 PROCEDURE FlushRecoverCode (VAR codeStack: FactorDesc) ;
3048 BEGIN
3049 IF codeStack#NIL
3050 THEN
3051 WHILE codeStack#NIL DO
3052 EmitNonVarCode(codeStack^.code, 0, Indent) ;
3053 codeStack := codeStack^.pushed
3054 END
3055 END
3056 END FlushRecoverCode ;
3057
3058
3059 (*
3060 RecoverFactor -
3061 *)
3062
3063 PROCEDURE RecoverFactor (f: FactorDesc; m: m2condition; codeStack: FactorDesc) ;
3064 VAR
3065 to: SetDesc ;
3066 BEGIN
3067 IF f=NIL
3068 THEN
3069 ELSE
3070 EmitFileLineTag(f^.line) ;
3071 WITH f^ DO
3072 CASE type OF
3073
3074 id : to := NIL ;
3075 CalcFirstFactor(f, NIL, to) ;
3076 IF (to#NIL) AND (m#m2none)
3077 THEN
3078 RecoverCondition(m) ;
3079 EmitIsInFirst(to, m) ;
3080 CodeThenDo(m) ;
3081 INC(Indent, 3)
3082 END ;
3083 FlushRecoverCode(codeStack) ;
3084 IndentString('') ;
3085 Output.WriteKey(ident^.name) ; Output.Write('(') ;
3086 EmitStopParametersAndFollow(f, m) ; Output.WriteString(') ;') ; Output.WriteLn ;
3087 RecoverFactor(f^.next, m2none, codeStack) ;
3088 IF (to#NIL) AND (m#m2none)
3089 THEN
3090 DEC(Indent, 3)
3091 END |
3092 lit : IF m=m2none
3093 THEN
3094 FlushRecoverCode(codeStack) ;
3095 IndentString('Expect(') ;
3096 Output.WriteKey(GetSymKey(Aliases, string)) ; Output.WriteString(', ') ;
3097 EmitStopParametersAndFollow(f, m) ; Output.WriteString(') ;') ; Output.WriteLn ;
3098 RecoverFactor(f^.next, m2none, codeStack)
3099 ELSE
3100 RecoverCondition(m) ;
3101 WriteGetTokenType ;
3102 Output.Write('=') ;
3103 Output.WriteKey(GetSymKey(Aliases, string)) ;
3104 CodeThenDo(m) ;
3105 INC(Indent, 3) ;
3106 IndentString('Expect(') ;
3107 Output.WriteKey(GetSymKey(Aliases, string)) ; Output.WriteString(', ') ;
3108 EmitStopParametersAndFollow(f, m) ; Output.WriteString(') ;') ;
3109 Output.WriteLn ;
3110 FlushRecoverCode(codeStack) ;
3111 RecoverFactor(f^.next, m2none, codeStack) ;
3112 DEC(Indent, 3)
3113 END |
3114 sub: FlushRecoverCode(codeStack) ;
3115 RecoverExpression(expr, m2none, m) ;
3116 RecoverFactor(f^.next, m2none, codeStack) |
3117 opt: FlushRecoverCode(codeStack) ;
3118 IF OptExpSeen(f)
3119 THEN
3120 to := NIL ;
3121 CalcFirstExpression(expr, NIL, to) ;
3122 RecoverCondition(m) ;
3123 EmitIsInFirst(to, m) ;
3124 CodeThenDo(m) ;
3125 INC(Indent, 3) ;
3126 IndentString('(* seen optional [ | ] expression *)') ; Output.WriteLn ;
3127 stop();
3128 RecoverExpression(expr, m2none, m2if) ;
3129 IndentString('(* end of optional [ | ] expression *)') ; Output.WriteLn ;
3130 DEC(Indent, 3) ;
3131 IndentString('END ;') ; Output.WriteLn
3132 ELSE
3133 RecoverExpression(expr, m2if, m)
3134 END ;
3135 RecoverFactor(f^.next, m2none, codeStack) |
3136 mult: FlushRecoverCode(codeStack) ;
3137 IF OptExpSeen(f) OR (m=m2if) OR (m=m2elsif)
3138 THEN
3139 to := NIL ;
3140 CalcFirstExpression(expr, NIL, to) ;
3141 RecoverCondition(m) ;
3142 EmitIsInFirst(to, m) ;
3143 CodeThenDo(m) ;
3144 INC(Indent, 3) ;
3145 IndentString('(* seen optional { | } expression *)') ; Output.WriteLn ;
3146 RecoverCondition(m2while) ;
3147 EmitIsInFirst(to, m2while) ;
3148 CodeThenDo(m2while) ;
3149 INC(Indent, 3) ;
3150 RecoverExpression(expr, m2none, m2while) ;
3151 IndentString('(* end of optional { | } expression *)') ; Output.WriteLn ;
3152 DEC(Indent, 3) ;
3153 IndentString('END ;') ; Output.WriteLn ;
3154 DEC(Indent, 3) ;
3155 IF m=m2none
3156 THEN
3157 IndentString('END ;') ; Output.WriteLn ;
3158 DEC(Indent, 3)
3159 END
3160 ELSE
3161 RecoverExpression(expr, m2while, m)
3162 END ;
3163 RecoverFactor(f^.next, m2none, codeStack) |
3164 m2 : codeStack := ChainOn(codeStack, f) ;
3165 IF f^.next=NIL
3166 THEN
3167 FlushRecoverCode(codeStack)
3168 ELSE
3169 RecoverFactor(f^.next, m, codeStack) (* was m2none *)
3170 END
3171
3172 ELSE
3173 END
3174 END
3175 END
3176 END RecoverFactor ;
3177
3178
3179 (*
3180 OptExpSeen - returns TRUE if we can see an optional expression in the factor.
3181 This is not the same as epsilon. Example { '+' } matches epsilon as
3182 well as { '+' | '-' } but OptExpSeen returns TRUE in the second case
3183 and FALSE in the first.
3184 *)
3185
3186 PROCEDURE OptExpSeen (f: FactorDesc) : BOOLEAN ;
3187 BEGIN
3188 IF f=NIL
3189 THEN
3190 RETURN( FALSE )
3191 ELSE
3192 WITH f^ DO
3193 CASE type OF
3194
3195 id ,
3196 lit : RETURN( FALSE ) |
3197 sub : RETURN( FALSE ) | (* is this correct? *)
3198 opt ,
3199 mult: RETURN( (expr#NIL) AND (expr^.term#NIL) AND (expr^.term^.next#NIL) ) |
3200 m2 : RETURN( TRUE )
3201
3202 ELSE
3203 END
3204 END
3205 END ;
3206 WarnError('all cases were not handled') ;
3207 WasNoError := FALSE
3208 END OptExpSeen ;
3209
3210
3211 (*
3212 RecoverTerm -
3213 *)
3214
3215 PROCEDURE RecoverTerm (t: TermDesc; new, old: m2condition) ;
3216 VAR
3217 LastWasM2Only, (* does the factor only contain inline code? *)
3218 alternative : BOOLEAN ;
3219 to : SetDesc ;
3220 BEGIN
3221 LastWasM2Only := (t^.factor^.type = m2) AND (t^.factor^.next = NIL) ;
3222 to := NIL ;
3223 CalcFirstTerm(t, NIL, to) ;
3224 alternative := FALSE ;
3225 IF t^.next#NIL
3226 THEN
3227 new := m2if
3228 END ;
3229 WHILE t#NIL DO
3230 EmitFileLineTag(t^.line) ;
3231 LastWasM2Only := (t^.factor^.type = m2) AND (t^.factor^.next = NIL) ;
3232 IF (t^.factor^.type=m2) AND (new=m2elsif)
3233 THEN
3234 new := m2if ;
3235 IndentString('ELSE') ; Output.WriteLn ;
3236 INC(Indent, 3) ;
3237 RecoverFactor(t^.factor, m2none, NIL) ;
3238 alternative := FALSE
3239 ELSE
3240 RecoverFactor(t^.factor, new, NIL)
3241 END ;
3242 IF t^.next#NIL
3243 THEN
3244 new := m2elsif ;
3245 alternative := TRUE
3246 END ;
3247 t := t^.next
3248 END ;
3249 IF (new=m2if) OR (new=m2elsif)
3250 THEN
3251 IF alternative AND (old#m2while)
3252 THEN
3253 IndentString('ELSE') ; Output.WriteLn ;
3254 INC(Indent, 3) ;
3255 IndentString('') ;
3256 Output.WriteKey(ErrorProcArray) ;
3257 Output.WriteString("('expecting one of: ") ;
3258 EmitSetName(to, 0, 0) ;
3259 Output.WriteString("')") ;
3260 Output.WriteLn ;
3261 DEC(Indent, 3)
3262 ELSIF LastWasM2Only
3263 THEN
3264 DEC(Indent, 3)
3265 END ;
3266 IndentString('END ;') ; Output.WriteLn
3267 ELSIF new=m2while
3268 THEN
3269 IndentString('END (* while *) ;') ; Output.WriteLn
3270 ELSIF LastWasM2Only
3271 THEN
3272 DEC(Indent, 3)
3273 END
3274 END RecoverTerm ;
3275
3276
3277 (*
3278 RecoverExpression -
3279 *)
3280
3281 PROCEDURE RecoverExpression (e: ExpressionDesc; new, old: m2condition) ;
3282 BEGIN
3283 IF e#NIL
3284 THEN
3285 EmitFileLineTag(e^.line) ;
3286 RecoverTerm(e^.term, new, old)
3287 END
3288 END RecoverExpression ;
3289
3290
3291 (*
3292 RecoverStatement -
3293 *)
3294
3295 PROCEDURE RecoverStatement (s: StatementDesc; m: m2condition) ;
3296 BEGIN
3297 IF s#NIL
3298 THEN
3299 EmitFileLineTag(s^.line) ;
3300 RecoverExpression(s^.expr, m, m2none)
3301 END
3302 END RecoverStatement ;
3303
3304
3305 (*
3306 EmitFirstFactor - generate a list of all first tokens between the range: low..high.
3307 *)
3308
3309 PROCEDURE EmitFirstFactor (f: FactorDesc; low, high: CARDINAL) ;
3310 BEGIN
3311
3312 END EmitFirstFactor ;
3313
3314
3315 (*
3316 EmitUsed -
3317 *)
3318
3319 PROCEDURE EmitUsed (wordno: CARDINAL) ;
3320 BEGIN
3321 IF NOT (wordno IN ParametersUsed)
3322 THEN
3323 Output.WriteString (" (* <* unused *> *) ")
3324 END
3325 END EmitUsed ;
3326
3327
3328 (*
3329 EmitStopParameters - generate the stop set.
3330 *)
3331
3332 PROCEDURE EmitStopParameters (FormalParameters: BOOLEAN) ;
3333 VAR
3334 i: CARDINAL ;
3335 BEGIN
3336 IF LargestValue<=MaxElementsInSet
3337 THEN
3338 Output.WriteString('stopset') ;
3339 IF FormalParameters
3340 THEN
3341 Output.WriteString(': SetOfStop') ;
3342 EmitUsed (0)
3343 ELSE
3344 INCL (ParametersUsed, 0)
3345 END
3346 ELSE
3347 i := 0 ;
3348 REPEAT
3349 Output.WriteString('stopset') ; Output.WriteCard(i, 0) ;
3350 IF FormalParameters
3351 THEN
3352 Output.WriteString(': SetOfStop') ; Output.WriteCard(i, 0) ;
3353 EmitUsed (i)
3354 ELSE
3355 INCL (ParametersUsed, i)
3356 END ;
3357 INC (i) ;
3358 IF i*MaxElementsInSet<LargestValue
3359 THEN
3360 IF FormalParameters
3361 THEN
3362 Output.WriteString('; ')
3363 ELSE
3364 Output.WriteString(', ')
3365 END
3366 END
3367 UNTIL i*MaxElementsInSet>=LargestValue ;
3368 END
3369 END EmitStopParameters ;
3370
3371
3372 (*
3373 IsBetween - returns TRUE if the value of the token, string, is
3374 in the range: low..high
3375 *)
3376
3377 PROCEDURE IsBetween (string: Name; low, high: WORD) : BOOLEAN ;
3378 BEGIN
3379 RETURN( (GetSymKey(Values, string) >= low) AND (GetSymKey(Values, string) <= high) )
3380 END IsBetween ;
3381
3382
3383 (*
3384 IsEmptySet - returns TRUE if no elements exist in set, to, with values, low..high.
3385 *)
3386
3387 PROCEDURE IsEmptySet (to: SetDesc; low, high: WORD) : BOOLEAN ;
3388 BEGIN
3389 WHILE to#NIL DO
3390 WITH to^ DO
3391 CASE type OF
3392
3393 tokel: IF IsBetween(string, low, high)
3394 THEN
3395 RETURN( FALSE )
3396 END |
3397 litel: IF IsBetween(GetSymKey(Aliases, string), low, high)
3398 THEN
3399 RETURN( FALSE )
3400 END |
3401 idel : WarnError('not expecting ident in first symbol list') ;
3402 WasNoError := FALSE
3403
3404 ELSE
3405 WarnError('unknown enuneration element') ;
3406 WasNoError := FALSE
3407 END
3408 END ;
3409 to := to^.next ;
3410 END ;
3411 RETURN( TRUE )
3412 END IsEmptySet ;
3413
3414
3415 (*
3416 EmitSet - emits the tokens in the set, to, which have values low..high
3417 *)
3418
3419 PROCEDURE EmitSet (to: SetDesc; low, high: WORD) ;
3420 VAR
3421 first: BOOLEAN ;
3422 BEGIN
3423 first := TRUE ;
3424 WHILE to#NIL DO
3425 WITH to^ DO
3426 CASE type OF
3427
3428 tokel: IF (high=0) OR IsBetween(string, low, high)
3429 THEN
3430 IF NOT first
3431 THEN
3432 Output.WriteString(', ')
3433 END ;
3434 Output.WriteKey(string) ;
3435 first := FALSE
3436 END |
3437 litel: IF (high=0) OR IsBetween(GetSymKey(Aliases, string), low, high)
3438 THEN
3439 IF NOT first
3440 THEN
3441 Output.WriteString(', ')
3442 END ;
3443 Output.WriteKey(GetSymKey(Aliases, string)) ;
3444 first := FALSE
3445 END |
3446 idel : WarnError('not expecting ident in first symbol list') ;
3447 WasNoError := FALSE
3448
3449 ELSE
3450 WarnError('unknown enuneration element') ;
3451 WasNoError := FALSE
3452 END
3453 END ;
3454 to := to^.next
3455 END
3456 END EmitSet ;
3457
3458
3459 (*
3460 EmitSetName - emits the tokens in the set, to, which have values low..high, using
3461 their names.
3462 *)
3463
3464 PROCEDURE EmitSetName (to: SetDesc; low, high: WORD) ;
3465 BEGIN
3466 WHILE to#NIL DO
3467 WITH to^ DO
3468 CASE type OF
3469
3470 tokel: IF (high=0) OR IsBetween(string, low, high)
3471 THEN
3472 IF MakeKey("'")=GetSymKey(ReverseAliases, string)
3473 THEN
3474 Output.WriteString('single quote')
3475 ELSE
3476 KeyWord(GetSymKey(ReverseAliases, string))
3477 END
3478 END |
3479 litel: IF (high=0) OR IsBetween(GetSymKey(Aliases, string), low, high)
3480 THEN
3481 Output.WriteKey(string)
3482 END |
3483 idel : WarnError('not expecting ident in first symbol list') ;
3484 WasNoError := FALSE
3485
3486 ELSE
3487 WarnError('unknown enuneration element') ;
3488 WasNoError := FALSE
3489 END
3490 END ;
3491 to := to^.next ;
3492 IF to#NIL
3493 THEN
3494 Output.Write(' ')
3495 END
3496 END
3497 END EmitSetName ;
3498
3499
3500 (*
3501 EmitStopParametersAndSet - generates the stop parameters together with a set
3502 inclusion of all the symbols in set, to.
3503 *)
3504
3505 PROCEDURE EmitStopParametersAndSet (to: SetDesc) ;
3506 VAR
3507 i : CARDINAL ;
3508 BEGIN
3509 IF LargestValue<=MaxElementsInSet
3510 THEN
3511 Output.WriteString('stopset') ;
3512 INCL (ParametersUsed, 0) ;
3513 IF (to#NIL) AND (NumberOfElements(to, 0, MaxElementsInSet-1)>0)
3514 THEN
3515 Output.WriteString(' + SetOfStop') ;
3516 Output.Write('{') ;
3517 EmitSet(to, 0, MaxElementsInSet-1) ;
3518 Output.Write('}')
3519 END
3520 ELSE
3521 i := 0 ;
3522 REPEAT
3523 Output.WriteString('stopset') ; Output.WriteCard(i, 0) ;
3524 INCL (ParametersUsed, i) ;
3525 IF (to#NIL) AND (NumberOfElements(to, i*MaxElementsInSet, (i+1)*MaxElementsInSet-1)>0)
3526 THEN
3527 Output.WriteString(' + SetOfStop') ; Output.WriteCard(i, 0) ;
3528 Output.Write('{') ;
3529 EmitSet(to, i*MaxElementsInSet, (i+1)*MaxElementsInSet-1) ;
3530 Output.Write('}')
3531 END ;
3532 INC(i) ;
3533 IF i*MaxElementsInSet<LargestValue
3534 THEN
3535 Output.WriteString(', ')
3536 END
3537 UNTIL i*MaxElementsInSet>=LargestValue
3538 END
3539 END EmitStopParametersAndSet ;
3540
3541
3542 (*
3543 EmitSetAsParameters - generates the first symbols as parameters to a set function.
3544 *)
3545
3546 PROCEDURE EmitSetAsParameters (to: SetDesc) ;
3547 VAR
3548 i : CARDINAL ;
3549 BEGIN
3550 IF LargestValue<=MaxElementsInSet
3551 THEN
3552 Output.Write('{') ;
3553 EmitSet(to, 0, MaxElementsInSet-1)
3554 ELSE
3555 i := 0 ;
3556 REPEAT
3557 Output.Write('{') ;
3558 EmitSet(to, i*MaxElementsInSet, (i+1)*MaxElementsInSet-1) ;
3559 INC(i) ;
3560 IF (i+1)*MaxElementsInSet>LargestValue
3561 THEN
3562 Output.WriteString('}, ')
3563 END
3564 UNTIL (i+1)*MaxElementsInSet>=LargestValue ;
3565 END ;
3566 Output.Write('}')
3567 END EmitSetAsParameters ;
3568
3569
3570 (*
3571 EmitStopParametersAndFollow - generates the stop parameters together with a set
3572 inclusion of all the follow symbols for subsequent
3573 sentances.
3574 *)
3575
3576 PROCEDURE EmitStopParametersAndFollow (f: FactorDesc; m: m2condition) ;
3577 VAR
3578 to: SetDesc ;
3579 BEGIN
3580 to := NIL ;
3581 (*
3582 IF m=m2while
3583 THEN
3584 CalcFirstFactor(f, NIL, to)
3585 END ;
3586 *)
3587 CollectFollow(to, f^.followinfo) ;
3588 EmitStopParametersAndSet(to) ;
3589 IF Debugging
3590 THEN
3591 Output.WriteLn ;
3592 Output.WriteString('factor is: ') ;
3593 PrettyCommentFactor(f, StrLen('factor is: ')) ;
3594 Output.WriteLn ;
3595 Output.WriteString('follow set:') ;
3596 EmitSet(to, 0, 0) ;
3597 Output.WriteLn
3598 END
3599 END EmitStopParametersAndFollow ;
3600
3601
3602 (*
3603 EmitFirstAsParameters -
3604 *)
3605
3606 PROCEDURE EmitFirstAsParameters (f: FactorDesc) ;
3607 VAR
3608 to: SetDesc ;
3609 BEGIN
3610 to := NIL ;
3611 CalcFirstFactor(f, NIL, to) ;
3612 EmitSetAsParameters(to)
3613 END EmitFirstAsParameters ;
3614
3615
3616 (*
3617 RecoverProduction - only encode grammer rules which are not special.
3618 Generate error recovery code.
3619 *)
3620
3621 PROCEDURE RecoverProduction (p: ProductionDesc) ;
3622 VAR
3623 s: String ;
3624 BEGIN
3625 IF (p#NIL) AND ((NOT p^.firstsolved) OR ((p^.statement#NIL) AND (p^.statement^.expr#NIL)))
3626 THEN
3627 BeginningOfLine := TRUE ;
3628 Indent := 0 ;
3629 Output.WriteLn ;
3630 OnLineStart := FALSE ;
3631 EmitFileLineTag(p^.line) ;
3632 IndentString('PROCEDURE ') ;
3633 Output.WriteKey(GetDefinitionName(p)) ;
3634 Output.WriteString(' (') ;
3635 ParametersUsed := {} ;
3636 Output.StartBuffer ;
3637 Output.WriteString(') ;') ;
3638 VarProduction(p) ;
3639 Output.WriteLn ;
3640 OnLineStart := FALSE ;
3641 EmitFileLineTag(p^.line) ;
3642 Indent := 0 ;
3643 IndentString('BEGIN') ; Output.WriteLn ;
3644 OnLineStart := FALSE ;
3645 EmitFileLineTag(p^.line) ;
3646 Indent := 3 ;
3647 RecoverStatement(p^.statement, m2none) ;
3648 Indent := 0 ;
3649 IndentString('END ') ; Output.WriteKey(GetDefinitionName(p)) ; Output.WriteString(' ;') ;
3650 Output.WriteLn ;
3651 Output.WriteLn ;
3652 Output.WriteLn ;
3653 s := Output.EndBuffer () ;
3654 EmitStopParameters (TRUE) ;
3655 Output.KillWriteS (s)
3656 END
3657 END RecoverProduction ;
3658
3659
3660 (*
3661 IsWhite - returns TRUE if, ch, is a space or a tab.
3662 *)
3663
3664 PROCEDURE IsWhite (ch: CHAR) : BOOLEAN ;
3665 BEGIN
3666 RETURN( (ch=' ') OR (ch=tab) OR (ch=lf) )
3667 END IsWhite ;
3668
3669
3670 (*
3671 FindStr - returns TRUE if, str, was seen inside the code hunk
3672 *)
3673
3674 PROCEDURE FindStr (VAR code: CodeHunk; VAR i: CARDINAL; str: ARRAY OF CHAR) : BOOLEAN ;
3675 VAR
3676 j, k: CARDINAL ;
3677 t : CodeHunk ;
3678 BEGIN
3679 t := code ;
3680 k := StrLen(code^.codetext)+1 ;
3681 j := StrLen(str) ;
3682 WHILE t#NIL DO
3683 REPEAT
3684 WHILE (k>0) AND IsWhite(t^.codetext[k-1]) DO
3685 DEC(k)
3686 END ;
3687 IF k=0
3688 THEN
3689 t := t^.next ;
3690 k := MaxCodeHunkLength+1
3691 END
3692 UNTIL (t=NIL) OR (NOT IsWhite(t^.codetext[k-1])) ;
3693
3694 (* found another word check it *)
3695
3696 IF t#NIL
3697 THEN
3698 j := StrLen(str) ;
3699 i := k ;
3700 WHILE (t#NIL) AND (j>0) AND ((str[j-1]=t^.codetext[k-1]) OR
3701 (IsWhite(str[j-1]) AND IsWhite(t^.codetext[k-1]))) DO
3702 DEC(j) ;
3703 DEC(k) ;
3704 IF j=0
3705 THEN
3706 (* found word remember position *)
3707 code := t
3708 END ;
3709 IF k=0
3710 THEN
3711 t := t^.next ;
3712 k := MaxCodeHunkLength+1
3713 END
3714 END ;
3715 IF k>0
3716 THEN
3717 DEC(k)
3718 ELSE
3719 t := t^.next
3720 END
3721 END ;
3722 END ;
3723 RETURN( (t=NIL) AND (j=0) )
3724 END FindStr ;
3725
3726
3727 (*
3728 WriteUpto -
3729 *)
3730
3731 PROCEDURE WriteUpto (code, upto: CodeHunk; limit: CARDINAL) ;
3732 BEGIN
3733 IF code#upto
3734 THEN
3735 WriteUpto(code^.next, upto, limit) ;
3736 Output.WriteString(code^.codetext)
3737 ELSE
3738 WHILE (limit<=MaxCodeHunkLength) AND (code^.codetext[limit]#nul) DO
3739 Output.Write(code^.codetext[limit]) ;
3740 INC(limit)
3741 END
3742 END
3743 END WriteUpto ;
3744
3745
3746 (*
3747 CheckForVar - checks for any local variables which need to be emitted during
3748 this production.
3749 *)
3750
3751 PROCEDURE CheckForVar (code: CodeHunk) ;
3752 VAR
3753 i: CARDINAL ;
3754 t: CodeHunk ;
3755 BEGIN
3756 t := code ;
3757 IF FindStr(t, i, 'VAR') AND EmitCode
3758 THEN
3759 IF NOT EmittedVar
3760 THEN
3761 Output.WriteLn ;
3762 Indent := 0 ;
3763 IndentString('VAR') ;
3764 INC(Indent, 3) ;
3765 Output.WriteLn ;
3766 EmittedVar := TRUE ;
3767 END ;
3768 WriteUpto(code, t, i)
3769 END
3770 END CheckForVar ;
3771
3772
3773 (*
3774 VarFactor -
3775 *)
3776
3777 PROCEDURE VarFactor (f: FactorDesc) ;
3778 BEGIN
3779 WHILE f#NIL DO
3780 WITH f^ DO
3781 CASE type OF
3782
3783 id : |
3784 lit : |
3785 sub ,
3786 opt ,
3787 mult: VarExpression(expr) |
3788 m2 : CheckForVar(code^.code)
3789
3790 ELSE
3791 END
3792 END ;
3793 f := f^.next
3794 END
3795 END VarFactor ;
3796
3797
3798 (*
3799 VarTerm -
3800 *)
3801
3802 PROCEDURE VarTerm (t: TermDesc) ;
3803 BEGIN
3804 WHILE t#NIL DO
3805 VarFactor(t^.factor) ;
3806 t := t^.next
3807 END
3808 END VarTerm ;
3809
3810
3811 (*
3812 VarExpression -
3813 *)
3814
3815 PROCEDURE VarExpression (e: ExpressionDesc) ;
3816 BEGIN
3817 IF e#NIL
3818 THEN
3819 VarTerm(e^.term)
3820 END
3821 END VarExpression ;
3822
3823
3824 (*
3825 VarStatement -
3826 *)
3827
3828 PROCEDURE VarStatement (s: StatementDesc) ;
3829 BEGIN
3830 IF s#NIL
3831 THEN
3832 VarExpression(s^.expr)
3833 END
3834 END VarStatement ;
3835
3836
3837 (*
3838 VarProduction - writes out all variable declarations.
3839 *)
3840
3841 PROCEDURE VarProduction (p: ProductionDesc) ;
3842 BEGIN
3843 EmittedVar := FALSE ;
3844 IF p#NIL
3845 THEN
3846 VarStatement(p^.statement)
3847 END
3848 END VarProduction ;
3849
3850
3851 (*
3852 In - returns TRUE if token, s, is already in the set, to.
3853 *)
3854
3855 PROCEDURE In (to: SetDesc; s: Name) : BOOLEAN ;
3856 BEGIN
3857 WHILE to#NIL DO
3858 WITH to^ DO
3859 CASE type OF
3860
3861 idel : IF s=ident^.name
3862 THEN
3863 RETURN( TRUE )
3864 END |
3865 tokel,
3866 litel : IF s=string
3867 THEN
3868 RETURN( TRUE )
3869 END
3870
3871 ELSE
3872 WarnError('internal error CASE type not known') ;
3873 WasNoError := FALSE
3874 END
3875 END ;
3876 to := to^.next
3877 END ;
3878 RETURN( FALSE )
3879 END In ;
3880
3881
3882 (*
3883 IntersectionIsNil - given two set lists, s1, s2, return TRUE if the
3884 s1 * s2 = {}
3885 *)
3886
3887 PROCEDURE IntersectionIsNil (s1, s2: SetDesc) : BOOLEAN ;
3888 BEGIN
3889 WHILE s1#NIL DO
3890 WITH s1^ DO
3891 CASE type OF
3892
3893 idel : IF In(s2, ident^.name)
3894 THEN
3895 RETURN( FALSE )
3896 END |
3897 tokel,
3898 litel: IF In(s2, string)
3899 THEN
3900 RETURN( FALSE )
3901 END
3902
3903 ELSE
3904 WarnError('internal error CASE type not known') ;
3905 WasNoError := FALSE
3906 END
3907 END ;
3908 s1 := s1^.next
3909 END ;
3910 RETURN( TRUE )
3911 END IntersectionIsNil ;
3912
3913
3914 (*
3915 AddSet - adds a first symbol to a production.
3916 *)
3917
3918 PROCEDURE AddSet (VAR to: SetDesc; s: Name) ;
3919 VAR
3920 d: SetDesc ;
3921 BEGIN
3922 IF NOT In(to, s)
3923 THEN
3924 d := NewSetDesc() ;
3925 WITH d^ DO
3926 type := tokel ;
3927 string := s ;
3928 next := to ;
3929 END ;
3930 to := d ;
3931 Finished := FALSE
3932 END
3933 END AddSet ;
3934
3935
3936 (*
3937 OrSet -
3938 *)
3939
3940 PROCEDURE OrSet (VAR to: SetDesc; from: SetDesc) ;
3941 BEGIN
3942 WHILE from#NIL DO
3943 WITH from^ DO
3944 CASE type OF
3945
3946 tokel: AddSet(to, string) |
3947 litel: AddSet(to, GetSymKey(Aliases, string)) |
3948 idel : WarnError('not expecting ident in first symbol list') ;
3949 WasNoError := FALSE
3950
3951 ELSE
3952 Halt('unknown element in enumeration type',
3953 __FILE__, __FUNCTION__, __LINE__)
3954 END
3955 END ;
3956 from := from^.next
3957 END
3958 END OrSet ;
3959
3960
3961 (*
3962 CalcFirstFactor -
3963 *)
3964
3965 PROCEDURE CalcFirstFactor (f: FactorDesc; from: ProductionDesc; VAR to: SetDesc) ;
3966 BEGIN
3967 WHILE f#NIL DO
3968 WITH f^ DO
3969 CASE type OF
3970
3971 id : IF ident^.definition=NIL
3972 THEN
3973 WarnError1("no rule found for an 'ident' called '%s'", ident^.name) ;
3974 HALT
3975 END ;
3976 OrSet(to, ident^.definition^.first) ;
3977 IF GetReachEnd(ident^.definition^.followinfo)=false
3978 THEN
3979 RETURN
3980 END |
3981 lit : IF GetSymKey(Aliases, string)=NulKey
3982 THEN
3983 WarnError1("unknown token for '%s'", string) ;
3984 WasNoError := FALSE
3985 ELSE
3986 AddSet(to, GetSymKey(Aliases, string))
3987 END ;
3988 RETURN |
3989 sub ,
3990 opt ,
3991 mult: CalcFirstExpression(expr, from, to) |
3992 m2 :
3993
3994 ELSE
3995 END
3996 END ;
3997 f := f^.next
3998 END
3999 END CalcFirstFactor ;
4000
4001
4002 (*
4003 CalcFirstTerm -
4004 *)
4005
4006 PROCEDURE CalcFirstTerm (t: TermDesc; from: ProductionDesc; VAR to: SetDesc) ;
4007 BEGIN
4008 WHILE t#NIL DO
4009 CalcFirstFactor(t^.factor, from, to) ;
4010 t := t^.next
4011 END
4012 END CalcFirstTerm ;
4013
4014
4015 (*
4016 CalcFirstExpression -
4017 *)
4018
4019 PROCEDURE CalcFirstExpression (e: ExpressionDesc; from: ProductionDesc; VAR to: SetDesc) ;
4020 BEGIN
4021 IF e#NIL
4022 THEN
4023 CalcFirstTerm(e^.term, from, to)
4024 END
4025 END CalcFirstExpression ;
4026
4027
4028 (*
4029 CalcFirstStatement -
4030 *)
4031
4032 PROCEDURE CalcFirstStatement (s: StatementDesc; from: ProductionDesc; VAR to: SetDesc) ;
4033 BEGIN
4034 IF s#NIL
4035 THEN
4036 CalcFirstExpression(s^.expr, from, to)
4037 END
4038 END CalcFirstStatement ;
4039
4040
4041 (*
4042 CalcFirstProduction - calculates all of the first symbols for the grammer
4043 *)
4044
4045 PROCEDURE CalcFirstProduction (p: ProductionDesc; from: ProductionDesc; VAR to: SetDesc) ;
4046 VAR
4047 s: SetDesc ;
4048 BEGIN
4049 IF p#NIL
4050 THEN
4051 IF p^.firstsolved
4052 THEN
4053 s := p^.first ;
4054 WHILE s#NIL DO
4055 CASE s^.type OF
4056
4057 idel : CalcFirstProduction(s^.ident^.definition, from, to) |
4058 tokel,
4059 litel: AddSet(to, s^.string)
4060
4061 ELSE
4062 END ;
4063 s := s^.next
4064 END
4065 ELSE
4066 CalcFirstStatement(p^.statement, from, to)
4067 END
4068 END
4069 END CalcFirstProduction ;
4070
4071
4072 (*
4073 WorkOutFollow -
4074 *)
4075
4076 PROCEDURE WorkOutFollowFactor (f: FactorDesc; VAR followset: SetDesc; after: SetDesc) ;
4077 VAR
4078 foundepsilon,
4079 canreachend : TraverseResult ;
4080 BEGIN
4081 foundepsilon := true ;
4082 canreachend := true ;
4083 WHILE (f#NIL) AND (foundepsilon=true) DO
4084 WITH f^ DO
4085 CASE type OF
4086
4087 id : IF ident^.definition=NIL
4088 THEN
4089 WarnError1("no rule found for an 'ident' called '%s'", ident^.name) ;
4090 HALT
4091 END ;
4092 OrSet(followset, ident^.definition^.first) |
4093 lit : AddSet(followset, GetSymKey(Aliases, string)) |
4094 sub : WorkOutFollowExpression(expr, followset, NIL) |
4095 opt : WorkOutFollowExpression(expr, followset, NIL) |
4096 mult: WorkOutFollowExpression(expr, followset, NIL) |
4097 m2 :
4098
4099 ELSE
4100 END
4101 END ;
4102 IF GetEpsilon(f^.followinfo)=unknown
4103 THEN
4104 WarnError('internal error: epsilon unknown') ;
4105 PrettyCommentFactor(f, 3) ;
4106 WasNoError := FALSE
4107 END ;
4108 foundepsilon := GetEpsilon(f^.followinfo) ;
4109 canreachend := GetReachEnd(f^.followinfo) ; (* only goes from FALSE -> TRUE *)
4110 f := f^.next
4111 END ;
4112 IF canreachend=true
4113 THEN
4114 OrSet(followset, after)
4115 END
4116 END WorkOutFollowFactor ;
4117
4118
4119 (*
4120 WorkOutFollowTerm -
4121 *)
4122
4123 PROCEDURE WorkOutFollowTerm (t: TermDesc; VAR followset: SetDesc; after: SetDesc) ;
4124 BEGIN
4125 IF t#NIL
4126 THEN
4127 WHILE t#NIL DO
4128 WITH t^ DO
4129 WorkOutFollowFactor(factor, followset, after) ; (* { '|' Term } *)
4130 END ;
4131 t := t^.next
4132 END
4133 END
4134 END WorkOutFollowTerm ;
4135
4136
4137 (*
4138 WorkOutFollowExpression -
4139 *)
4140
4141 PROCEDURE WorkOutFollowExpression (e: ExpressionDesc; VAR followset: SetDesc; after: SetDesc) ;
4142 BEGIN
4143 IF e#NIL
4144 THEN
4145 WITH e^ DO
4146 WorkOutFollowTerm(term, followset, after)
4147 END
4148 END
4149 END WorkOutFollowExpression ;
4150
4151
4152 (*
4153 CollectFollow - collects the follow set from, f, into, to.
4154 *)
4155
4156 PROCEDURE CollectFollow (VAR to: SetDesc; f: FollowDesc) ;
4157 BEGIN
4158 OrSet(to, f^.follow)
4159 END CollectFollow ;
4160
4161
4162 (*
4163 CalcFollowFactor -
4164 *)
4165
4166 PROCEDURE CalcFollowFactor (f: FactorDesc; after: SetDesc) ;
4167 BEGIN
4168 WHILE f#NIL DO
4169 WITH f^ DO
4170 CASE type OF
4171
4172 id : WorkOutFollowFactor(next, followinfo^.follow, after) |
4173 lit : WorkOutFollowFactor(next, followinfo^.follow, after) |
4174 opt ,
4175 sub : CalcFirstFactor(next, NIL, followinfo^.follow) ;
4176 IF (next=NIL) OR (GetReachEnd(next^.followinfo)=true)
4177 THEN
4178 OrSet(followinfo^.follow, after) ;
4179 CalcFollowExpression(expr, followinfo^.follow)
4180 ELSE
4181 CalcFollowExpression(expr, followinfo^.follow)
4182 END |
4183 mult: CalcFirstFactor(f, NIL, followinfo^.follow) ;
4184 (* include first as we may repeat this sentance *)
4185 IF Debugging
4186 THEN
4187 WriteLn ;
4188 WriteString('found mult: and first is: ') ; EmitSet(followinfo^.follow, 0, 0) ; WriteLn
4189 END ;
4190 IF (next=NIL) OR (GetReachEnd(next^.followinfo)=true)
4191 THEN
4192 OrSet(followinfo^.follow, after) ;
4193 CalcFollowExpression(expr, followinfo^.follow)
4194 ELSE
4195 CalcFollowExpression(expr, followinfo^.follow)
4196 END
4197
4198 ELSE
4199 END
4200 END ;
4201 f := f^.next
4202 END
4203 END CalcFollowFactor ;
4204
4205
4206 (*
4207 CalcFollowTerm -
4208 *)
4209
4210 PROCEDURE CalcFollowTerm (t: TermDesc; after: SetDesc) ;
4211 BEGIN
4212 IF t#NIL
4213 THEN
4214 WHILE t#NIL DO
4215 WITH t^ DO
4216 CalcFollowFactor(factor, after) ; (* { '|' Term } *)
4217 END ;
4218 t := t^.next
4219 END
4220 END
4221 END CalcFollowTerm ;
4222
4223
4224 (*
4225 CalcFollowExpression -
4226 *)
4227
4228 PROCEDURE CalcFollowExpression (e: ExpressionDesc; after: SetDesc) ;
4229 BEGIN
4230 IF e#NIL
4231 THEN
4232 WITH e^ DO
4233 CalcFollowTerm(term, after)
4234 END
4235 END
4236 END CalcFollowExpression ;
4237
4238
4239 (*
4240 CalcFollowStatement - given a bnf statement generate the follow set.
4241 *)
4242
4243 PROCEDURE CalcFollowStatement (s: StatementDesc) ;
4244 BEGIN
4245 IF s#NIL
4246 THEN
4247 WITH s^ DO
4248 CalcFollowExpression(expr, NIL)
4249 END
4250 END
4251 END CalcFollowStatement ;
4252
4253
4254 (*
4255 CalcFollowProduction -
4256 *)
4257
4258 PROCEDURE CalcFollowProduction (p: ProductionDesc) ;
4259 BEGIN
4260 IF p#NIL
4261 THEN
4262 WITH p^ DO
4263 CalcFollowStatement(statement)
4264 END
4265 END
4266 END CalcFollowProduction ;
4267
4268
4269 (*
4270 CalcEpsilonFactor -
4271 *)
4272
4273 PROCEDURE CalcEpsilonFactor (f: FactorDesc) ;
4274 BEGIN
4275 WHILE f#NIL DO
4276 WITH f^ DO
4277 CASE type OF
4278
4279 id : AssignEpsilon(GetEpsilon(ident^.definition^.followinfo)#unknown,
4280 followinfo, GetEpsilon(ident^.definition^.followinfo)) |
4281 lit : AssignEpsilon(TRUE, followinfo, false) |
4282 sub : CalcEpsilonExpression(expr) ;
4283 AssignEpsilon(GetEpsilon(expr^.followinfo)#unknown,
4284 followinfo, GetEpsilon(expr^.followinfo)) |
4285 m2 : AssignEpsilon(TRUE, followinfo, true) |
4286 opt ,
4287 mult: CalcEpsilonExpression(expr) ;
4288 AssignEpsilon(TRUE, followinfo, true)
4289
4290 ELSE
4291 END
4292 END ;
4293 f := f^.next
4294 END
4295 END CalcEpsilonFactor ;
4296
4297
4298 (*
4299 CalcEpsilonTerm -
4300 *)
4301
4302 PROCEDURE CalcEpsilonTerm (t: TermDesc) ;
4303 BEGIN
4304 IF t#NIL
4305 THEN
4306 WHILE t#NIL DO
4307 WITH t^ DO
4308 IF factor#NIL
4309 THEN
4310 CASE GetReachEnd(factor^.followinfo) OF
4311
4312 true : AssignEpsilon(TRUE, followinfo, true) |
4313 false: AssignEpsilon(TRUE, followinfo, false) |
4314 unknown:
4315
4316 ELSE
4317 END
4318 END ;
4319 CalcEpsilonFactor(factor) (* { '|' Term } *)
4320 END ;
4321 t := t^.next
4322 END
4323 END
4324 END CalcEpsilonTerm ;
4325
4326
4327 (*
4328 CalcEpsilonExpression -
4329 *)
4330
4331 PROCEDURE CalcEpsilonExpression (e: ExpressionDesc) ;
4332 VAR
4333 t : TermDesc ;
4334 result: TraverseResult ;
4335 BEGIN
4336 IF e#NIL
4337 THEN
4338 CalcEpsilonTerm(e^.term) ;
4339 IF GetEpsilon(e^.followinfo)=unknown
4340 THEN
4341 result := unknown ;
4342 WITH e^ DO
4343 t := term ;
4344 WHILE t#NIL DO
4345 IF GetEpsilon(t^.followinfo)#unknown
4346 THEN
4347 stop
4348 END ;
4349 CASE GetEpsilon(t^.followinfo) OF
4350
4351 unknown: |
4352 true : result := true |
4353 false : IF result#true
4354 THEN
4355 result := false
4356 END
4357
4358 ELSE
4359 END ;
4360 t := t^.next
4361 END
4362 END ;
4363 AssignEpsilon(result#unknown, e^.followinfo, result)
4364 END
4365 END
4366 END CalcEpsilonExpression ;
4367
4368
4369 (*
4370 CalcEpsilonStatement - given a bnf statement generate the follow set.
4371 *)
4372
4373 PROCEDURE CalcEpsilonStatement (s: StatementDesc) ;
4374 BEGIN
4375 IF s#NIL
4376 THEN
4377 WITH s^ DO
4378 IF expr#NIL
4379 THEN
4380 AssignEpsilon(GetEpsilon(expr^.followinfo)#unknown,
4381 followinfo, GetEpsilon(expr^.followinfo))
4382 END ;
4383 CalcEpsilonExpression(expr)
4384 END
4385 END
4386 END CalcEpsilonStatement ;
4387
4388
4389 (*
4390 CalcEpsilonProduction -
4391 *)
4392
4393 PROCEDURE CalcEpsilonProduction (p: ProductionDesc) ;
4394 BEGIN
4395 IF p#NIL
4396 THEN
4397 (*
4398 IF p^.statement^.ident^.name=MakeKey('DefinitionModule')
4399 THEN
4400 stop
4401 END ;
4402 *)
4403
4404 IF Debugging
4405 THEN
4406 WriteKey(p^.statement^.ident^.name) ;
4407 WriteString(' calculating epsilon') ;
4408 WriteLn
4409 END ;
4410
4411 WITH p^ DO
4412 AssignEpsilon(GetEpsilon(statement^.followinfo)#unknown,
4413 followinfo, GetEpsilon(statement^.followinfo)) ;
4414 CalcEpsilonStatement(statement)
4415 END
4416 END
4417 END CalcEpsilonProduction ;
4418
4419
4420 (*
4421 CalcReachEndFactor -
4422 *)
4423
4424 PROCEDURE CalcReachEndFactor (f: FactorDesc) : TraverseResult ;
4425 VAR
4426 canreachend,
4427 result : TraverseResult ;
4428 BEGIN
4429 IF f=NIL
4430 THEN
4431 RETURN( true ) (* we have reached the end of this factor list *)
4432 ELSE
4433 WITH f^ DO
4434 (* we need to traverse all factors even if we can short cut the answer to this list of factors *)
4435 result := CalcReachEndFactor(next) ;
4436 CASE type OF
4437
4438 id : IF ident^.definition=NIL
4439 THEN
4440 WarnError1('definition for %s is absent (assuming epsilon is false for this production)', ident^.name) ;
4441 result := false
4442 ELSIF result#false
4443 THEN
4444 CASE GetReachEnd(ident^.definition^.followinfo) OF
4445
4446 false : result := false |
4447 true : |
4448 unknown: result := unknown
4449
4450 ELSE
4451 END
4452 END |
4453 lit : result := false |
4454 sub : CalcReachEndExpression(expr) ;
4455 IF (expr#NIL) AND (result=true)
4456 THEN
4457 result := GetReachEnd(expr^.followinfo)
4458 END |
4459 mult,
4460 opt : IF expr#NIL
4461 THEN
4462 (* not interested in the result as expression is optional *)
4463 CalcReachEndExpression(expr)
4464 END |
4465 m2 :
4466
4467 ELSE
4468 END ;
4469 AssignReachEnd(result#unknown, followinfo, result)
4470 END ;
4471 RETURN( result )
4472 END
4473 END CalcReachEndFactor ;
4474
4475
4476 (*
4477 CalcReachEndTerm -
4478 *)
4479
4480 PROCEDURE CalcReachEndTerm (t: TermDesc) : TraverseResult ;
4481 VAR
4482 canreachend,
4483 result : TraverseResult ;
4484 BEGIN
4485 IF t#NIL
4486 THEN
4487 canreachend := false ;
4488 WHILE t#NIL DO
4489 WITH t^ DO
4490 result := CalcReachEndFactor(factor) ;
4491 AssignReachEnd(result#unknown, followinfo, result) ;
4492 CASE result OF
4493
4494 true : canreachend := true |
4495 false : |
4496 unknown: IF canreachend=false
4497 THEN
4498 canreachend := unknown
4499 END
4500
4501 ELSE
4502 END
4503 END ;
4504 t := t^.next (* { '|' Term } *)
4505 END ;
4506 RETURN( canreachend )
4507 END
4508 END CalcReachEndTerm ;
4509
4510
4511 (*
4512 CalcReachEndExpression -
4513 *)
4514
4515 PROCEDURE CalcReachEndExpression (e: ExpressionDesc) ;
4516 VAR
4517 result: TraverseResult ;
4518 BEGIN
4519 IF e=NIL
4520 THEN
4521 (* no expression, thus reached the end of this sentance *)
4522 ELSE
4523 WITH e^ DO
4524 result := CalcReachEndTerm(term) ;
4525 AssignReachEnd(result#unknown, followinfo, result)
4526 END
4527 END
4528 END CalcReachEndExpression ;
4529
4530
4531 (*
4532 CalcReachEndStatement -
4533 *)
4534
4535 PROCEDURE CalcReachEndStatement (s: StatementDesc) ;
4536 BEGIN
4537 IF s#NIL
4538 THEN
4539 WITH s^ DO
4540 IF expr#NIL
4541 THEN
4542 CalcReachEndExpression(expr) ;
4543 AssignReachEnd(GetReachEnd(expr^.followinfo)#unknown,
4544 followinfo, GetReachEnd(expr^.followinfo))
4545 END
4546 END
4547 END
4548 END CalcReachEndStatement ;
4549
4550
4551 PROCEDURE stop ; BEGIN END stop ;
4552
4553 (*
4554 CalcReachEndProduction -
4555 *)
4556
4557 PROCEDURE CalcReachEndProduction (p: ProductionDesc) ;
4558 BEGIN
4559 IF p#NIL
4560 THEN
4561 WITH p^ DO
4562 CalcReachEndStatement(statement) ;
4563 IF GetReachEnd(followinfo)#unknown
4564 THEN
4565 IF Debugging
4566 THEN
4567 WriteString('already calculated reach end for: ') ;
4568 WriteKey(p^.statement^.ident^.name) ; WriteString(' its value is ') ;
4569 IF GetReachEnd(followinfo)=true
4570 THEN
4571 WriteString('reachable')
4572 ELSE
4573 WriteString('non reachable')
4574 END ;
4575 WriteLn
4576 END
4577 END ;
4578 AssignReachEnd(GetReachEnd(statement^.followinfo)#unknown, followinfo, GetReachEnd(statement^.followinfo)) ;
4579 END
4580 END
4581 END CalcReachEndProduction ;
4582
4583
4584 (*
4585 EmptyFactor -
4586 *)
4587
4588 PROCEDURE EmptyFactor (f: FactorDesc) : BOOLEAN ;
4589 BEGIN
4590 WHILE f#NIL DO
4591 WITH f^ DO
4592 CASE type OF
4593
4594 id : IF NOT EmptyProduction(ident^.definition)
4595 THEN
4596 RETURN( FALSE )
4597 END |
4598 lit : RETURN( FALSE ) |
4599 sub : IF NOT EmptyExpression(expr)
4600 THEN
4601 RETURN( FALSE )
4602 END |
4603 opt ,
4604 mult: RETURN( TRUE ) |
4605 m2 :
4606
4607 ELSE
4608 END
4609 END ;
4610 f := f^.next
4611 END ;
4612 RETURN( TRUE )
4613 END EmptyFactor ;
4614
4615
4616 (*
4617 EmptyTerm - returns TRUE if the term maybe empty.
4618 *)
4619
4620 PROCEDURE EmptyTerm (t: TermDesc) : BOOLEAN ;
4621 BEGIN
4622 WHILE t#NIL DO
4623 IF EmptyFactor(t^.factor)
4624 THEN
4625 RETURN( TRUE )
4626 ELSE
4627 t := t^.next
4628 END
4629 END ;
4630 RETURN( FALSE )
4631 END EmptyTerm ;
4632
4633
4634 (*
4635 EmptyExpression -
4636 *)
4637
4638 PROCEDURE EmptyExpression (e: ExpressionDesc) : BOOLEAN ;
4639 BEGIN
4640 IF e=NIL
4641 THEN
4642 RETURN( TRUE )
4643 ELSE
4644 RETURN( EmptyTerm(e^.term) )
4645 END
4646 END EmptyExpression ;
4647
4648
4649 (*
4650 EmptyStatement - returns TRUE if statement, s, is empty.
4651 *)
4652
4653 PROCEDURE EmptyStatement (s: StatementDesc) : BOOLEAN ;
4654 BEGIN
4655 IF s=NIL
4656 THEN
4657 RETURN( TRUE )
4658 ELSE
4659 RETURN( EmptyExpression(s^.expr) )
4660 END
4661 END EmptyStatement ;
4662
4663
4664 (*
4665 EmptyProduction - returns if production, p, maybe empty.
4666 *)
4667
4668 PROCEDURE EmptyProduction (p: ProductionDesc) : BOOLEAN ;
4669 BEGIN
4670 IF p=NIL
4671 THEN
4672 WarnError('unknown production') ;
4673 RETURN( TRUE )
4674 ELSIF (p^.firstsolved) AND (p^.first#NIL)
4675 THEN
4676 (* predefined but first set to something - thus not empty *)
4677 RETURN( FALSE )
4678 ELSE
4679 RETURN( EmptyStatement(p^.statement) )
4680 END
4681 END EmptyProduction ;
4682
4683
4684 (*
4685 EmitFDLNotice -
4686 *)
4687
4688 PROCEDURE EmitFDLNotice ;
4689 BEGIN
4690 Output.WriteString('@c Copyright (C) 2000-2025 Free Software Foundation, Inc.') ; Output.WriteLn ;
4691 Output.WriteLn ;
4692 Output.WriteString('@c This file is part of GCC.') ; Output.WriteLn ;
4693 Output.WriteString('@c Permission is granted to copy, distribute and/or modify this document') ; Output.WriteLn ;
4694 Output.WriteString('@c under the terms of the GNU Free Documentation License, Version 1.2 or') ; Output.WriteLn ;
4695 Output.WriteString('@c any later version published by the Free Software Foundation.') ; Output.WriteLn
4696 END EmitFDLNotice ;
4697
4698
4699 (*
4700 EmitRules - generates the BNF rules.
4701 *)
4702
4703 PROCEDURE EmitRules ;
4704 BEGIN
4705 IF Texinfo AND FreeDocLicense
4706 THEN
4707 EmitFDLNotice
4708 END ;
4709 ForeachRuleDo(EmitRule)
4710 END EmitRules ;
4711
4712
4713 (*
4714 DescribeElement -
4715 *)
4716
4717 PROCEDURE DescribeElement (name: WORD) ;
4718 VAR
4719 lit: Name ;
4720 BEGIN
4721 IF InitialElement
4722 THEN
4723 InitialElement := FALSE
4724 ELSE
4725 Output.WriteString(' |')
4726 END ;
4727 Output.WriteLn ;
4728 Indent := 3 ;
4729 IndentString('') ;
4730 Output.WriteKey(name) ;
4731 Output.WriteString(': ') ;
4732 lit := GetSymKey(ReverseAliases, name) ;
4733 IF MakeKey('"')=lit
4734 THEN
4735 Output.WriteString('str := ConCat(ConCatChar(ConCatChar(InitString("syntax error, found ') ;
4736 Output.Write("'") ; Output.WriteString('"), ') ;
4737 Output.Write("'") ; Output.Write('"') ; Output.Write("'") ; Output.WriteString("), ") ;
4738 Output.Write('"') ; Output.Write("'") ; Output.Write('"') ; Output.WriteString("), Mark(str))")
4739 ELSIF MakeKey("'")=lit
4740 THEN
4741 Output.WriteString("str := ConCat(ConCatChar(ConCatChar(InitString('syntax error, found ") ;
4742 Output.Write('"') ; Output.WriteString("'), ") ;
4743 Output.Write('"') ; Output.Write("'") ; Output.Write('"') ; Output.WriteString('), ') ;
4744 Output.Write("'") ; Output.Write('"') ; Output.Write("'") ; Output.WriteString('), Mark(str))')
4745 ELSE
4746 Output.WriteString("str := ConCat(InitString(") ; Output.Write('"') ;
4747 Output.WriteString("syntax error, found ") ; KeyWord(lit) ; Output.WriteString('"), Mark(str))')
4748 END
4749 END DescribeElement ;
4750
4751
4752 (*
4753 EmitInTestStop - construct a test for stop element, name.
4754 *)
4755
4756 PROCEDURE EmitInTestStop (name: Name) ;
4757 VAR
4758 i, value: CARDINAL ;
4759 BEGIN
4760 IF LargestValue<=MaxElementsInSet
4761 THEN
4762 Output.WriteKey(name) ; Output.WriteString(' IN stopset') ;
4763 INCL (ParametersUsed, 0)
4764 ELSE
4765 value := GetSymKey(Values, name) ;
4766 i := value DIV MaxElementsInSet ;
4767 Output.WriteKey(name) ; Output.WriteString(' IN stopset') ; Output.WriteCard(i, 0) ;
4768 INCL (ParametersUsed, i)
4769 END
4770 END EmitInTestStop ;
4771
4772
4773 (*
4774 DescribeStopElement -
4775 *)
4776
4777 PROCEDURE DescribeStopElement (name: WORD) ;
4778 VAR
4779 lit: Name ;
4780 BEGIN
4781 Indent := 3 ;
4782 IndentString('IF ') ; EmitInTestStop(name) ; Output.WriteLn ;
4783 IndentString('THEN') ; Output.WriteLn ;
4784 Indent := 6 ;
4785 lit := GetSymKey(ReverseAliases, name) ;
4786 IF (lit=NulName) OR (lit=MakeKey(''))
4787 THEN
4788 IndentString('(* ') ;
4789 Output.WriteKey(name) ;
4790 Output.WriteString(' has no token name (needed to generate error messages) *)')
4791 ELSIF MakeKey("'")=lit
4792 THEN
4793 IndentString('message := ConCatChar(ConCatChar(ConCatChar(ConCatChar(ConCatChar(message, ') ;
4794 Output.WriteString("' '), ") ;
4795 Output.Write("'") ; Output.Write('"') ; Output.WriteString("'), ") ;
4796 Output.Write('"') ; Output.Write("'") ; Output.WriteString('"), ') ;
4797 Output.Write("'") ; Output.Write('"') ; Output.WriteString("'), ',') ; INC(n) ; ")
4798 ELSIF MakeKey('"')=lit
4799 THEN
4800 IndentString("message := ConCatChar(ConCatChar(ConCatChar(ConCatChar(ConCatChar(message, ") ;
4801 Output.WriteString('" "), ') ;
4802 Output.Write('"') ; Output.Write("`") ; Output.WriteString('"), ') ;
4803 Output.Write("'") ; Output.Write('"') ; Output.WriteString("'), ") ;
4804 Output.Write('"') ; Output.Write("'") ; Output.WriteString('"), ",") ; INC(n) ; ')
4805 ELSE
4806 IndentString("message := ConCat(ConCatChar(message, ' ") ; Output.WriteString("'), ") ;
4807 Output.WriteString('Mark(InitString("') ; KeyWord(lit) ; Output.Write('"') ;
4808 Output.WriteString('))) ; INC(n)')
4809 END ;
4810 Output.WriteLn ;
4811 Indent := 3 ;
4812 IndentString('END ;') ; Output.WriteLn
4813 END DescribeStopElement ;
4814
4815
4816 (*
4817 EmitDescribeStop -
4818 *)
4819
4820 PROCEDURE EmitDescribeStop ;
4821 VAR
4822 s: String ;
4823 BEGIN
4824 Output.WriteLn ;
4825 Indent := 0 ;
4826 IndentString('(*') ;
4827 Indent := 3 ;
4828 Output.WriteLn ;
4829 IndentString('DescribeStop - issues a message explaining what tokens were expected') ;
4830 Output.WriteLn ;
4831 Output.WriteString('*)') ;
4832 Output.WriteLn ;
4833 Output.WriteLn ;
4834 Indent := 0 ;
4835 IndentString('PROCEDURE DescribeStop (') ;
4836 ParametersUsed := {} ;
4837 Output.StartBuffer ;
4838 Output.WriteString(') : String ;') ;
4839 Output.WriteLn ;
4840 IndentString('VAR') ; Output.WriteLn ;
4841 Indent := 3 ;
4842 IndentString('n : CARDINAL ;') ; Output.WriteLn ;
4843 IndentString('str,') ; Output.WriteLn ;
4844 IndentString('message: String ;') ; Output.WriteLn ;
4845 Indent := 0 ;
4846 IndentString('BEGIN') ; Output.WriteLn ;
4847 Indent := 3 ;
4848 IndentString('n := 0 ;') ; Output.WriteLn ;
4849 IndentString("message := InitString('') ;") ;
4850 Output.WriteLn ;
4851 ForeachNodeDo(Aliases, DescribeStopElement) ; Output.WriteLn ;
4852 Indent := 3 ;
4853 IndentString('IF n=0') ; Output.WriteLn ;
4854 IndentString('THEN') ; Output.WriteLn ;
4855 Indent := 6 ;
4856 IndentString("str := InitString(' syntax error') ; ") ; Output.WriteLn ;
4857 IndentString('message := KillString(message) ; ') ; Output.WriteLn ;
4858 Indent := 3 ;
4859 IndentString('ELSIF n=1') ; Output.WriteLn ;
4860 IndentString('THEN') ; Output.WriteLn ;
4861 Indent := 6 ;
4862 IndentString("str := ConCat(message, Mark(InitString(' missing '))) ;") ; Output.WriteLn ;
4863 Indent := 3 ;
4864 IndentString('ELSE') ; Output.WriteLn ;
4865 Indent := 6 ;
4866 IndentString("str := ConCat(InitString(' expecting one of'), message) ;") ; Output.WriteLn ;
4867 IndentString("message := KillString(message) ;") ; Output.WriteLn ;
4868 Indent := 3 ;
4869 IndentString('END ;') ; Output.WriteLn ;
4870 IndentString('RETURN( str )') ; Output.WriteLn ;
4871 Indent := 0 ;
4872 IndentString('END DescribeStop ;') ; Output.WriteLn ;
4873 Output.WriteLn ;
4874 s := Output.EndBuffer () ;
4875 EmitStopParameters(TRUE) ;
4876 Output.KillWriteS (s)
4877 END EmitDescribeStop ;
4878
4879
4880 (*
4881 EmitDescribeError -
4882 *)
4883
4884 PROCEDURE EmitDescribeError ;
4885 BEGIN
4886 Output.WriteLn ;
4887 Indent := 0 ;
4888 IndentString('(*') ; Output.WriteLn ;
4889 Indent := 3 ;
4890 IndentString('DescribeError - issues a message explaining what tokens were expected') ; Output.WriteLn ;
4891 Indent := 0 ;
4892 IndentString('*)') ;
4893 Output.WriteLn ;
4894 Output.WriteLn ;
4895 IndentString('PROCEDURE DescribeError ;') ;
4896 Output.WriteLn ;
4897 IndentString('VAR') ; Output.WriteLn ;
4898 Indent := 3 ;
4899 IndentString('str: String ;') ; Output.WriteLn ;
4900 Indent := 0 ;
4901 IndentString('BEGIN') ; Output.WriteLn ;
4902 Indent := 3 ;
4903 IndentString("str := InitString('') ;") ; Output.WriteLn ;
4904 (* was
4905 IndentString('str := DescribeStop(') ; EmitStopParameters(FALSE) ; Output.WriteString(') ;') ; Output.WriteLn ;
4906 *)
4907 IndentString('CASE ') ; WriteGetTokenType ; Output.WriteString(' OF') ; NewLine(3) ;
4908 InitialElement := TRUE ;
4909 ForeachNodeDo(Aliases, DescribeElement) ;
4910 Output.WriteLn ;
4911 Indent := 3 ;
4912 IndentString('ELSE') ; Output.WriteLn ;
4913 IndentString('END ;') ; Output.WriteLn ;
4914 IndentString('') ;
4915 Output.WriteKey(ErrorProcString) ; Output.WriteString('(str) ;') ; Output.WriteLn ;
4916 Indent := 0 ;
4917 IndentString('END DescribeError ;') ; Output.WriteLn
4918 END EmitDescribeError ;
4919
4920
4921 (*
4922 EmitSetTypes - write out the set types used during error recovery
4923 *)
4924
4925 PROCEDURE EmitSetTypes ;
4926 VAR
4927 i, j, m, n: CARDINAL ;
4928 BEGIN
4929 Output.WriteString('(*') ; NewLine(3) ;
4930 Output.WriteString('expecting token set defined as an enumerated type') ; NewLine(3) ;
4931 Output.WriteString('(') ;
4932 i := 0 ;
4933 WHILE i<LargestValue DO
4934 Output.WriteKey(GetSymKey(ReverseValues, WORD(i))) ;
4935 INC(i) ;
4936 IF i<LargestValue
4937 THEN
4938 Output.WriteString(', ')
4939 END
4940 END ;
4941 Output.WriteString(') ;') ; NewLine(0) ;
4942 Output.WriteString('*)') ; NewLine(0) ;
4943 Output.WriteString('TYPE') ; NewLine(3) ;
4944 IF LargestValue>MaxElementsInSet
4945 THEN
4946 i := 0 ;
4947 n := LargestValue DIV MaxElementsInSet ;
4948 WHILE i<=n DO
4949 j := (i*MaxElementsInSet) ;
4950 IF LargestValue<(i+1)*MaxElementsInSet-1
4951 THEN
4952 m := LargestValue-1
4953 ELSE
4954 m := (i+1)*MaxElementsInSet-1
4955 END ;
4956 Output.WriteString('stop') ; Output.WriteCard(i, 0) ;
4957 Output.WriteString(' = [') ;
4958 Output.WriteKey(GetSymKey(ReverseValues, WORD(j))) ;
4959 Output.WriteString('..') ;
4960 Output.WriteKey(GetSymKey(ReverseValues, WORD(m))) ;
4961 Output.WriteString('] ;') ;
4962 NewLine(3) ;
4963 Output.WriteString('SetOfStop') ; Output.WriteCard(i, 0) ;
4964 Output.WriteString(' = SET OF stop') ; Output.WriteCard(i, 0) ;
4965 Output.WriteString(' ;') ;
4966 NewLine(3) ;
4967 INC(i)
4968 END
4969 ELSE
4970 Output.WriteString('SetOfStop') ;
4971 Output.WriteString(' = SET OF [') ;
4972 Output.WriteKey(GetSymKey(ReverseValues, WORD(0))) ;
4973 Output.WriteString('..') ;
4974 Output.WriteKey(GetSymKey(ReverseValues, WORD(LargestValue-1))) ;
4975 Output.WriteString('] ;')
4976 END ;
4977 NewLine(0)
4978 END EmitSetTypes ;
4979
4980
4981 (*
4982 EmitSupport - generates the support routines.
4983 *)
4984
4985 PROCEDURE EmitSupport ;
4986 BEGIN
4987 IF ErrorRecovery
4988 THEN
4989 EmitSetTypes ;
4990 EmitDescribeStop ;
4991 EmitDescribeError
4992 END
4993 END EmitSupport ;
4994
4995
4996 (*
4997 DisposeSetDesc - dispose of the set list, s.
4998 *)
4999
5000 PROCEDURE DisposeSetDesc (VAR s: SetDesc) ;
5001 VAR
5002 h, n: SetDesc ;
5003 BEGIN
5004 IF s#NIL
5005 THEN
5006 h := s ;
5007 n := s^.next ;
5008 REPEAT
5009 DISPOSE(h) ;
5010 h := n ;
5011 IF n#NIL
5012 THEN
5013 n := n^.next
5014 END
5015 UNTIL h=NIL ;
5016 s := NIL
5017 END
5018 END DisposeSetDesc ;
5019
5020
5021 (*
5022 OptionalFactor -
5023 *)
5024
5025 PROCEDURE OptionalFactor (f: FactorDesc) : BOOLEAN ;
5026 BEGIN
5027 WHILE f#NIL DO
5028 WITH f^ DO
5029 CASE type OF
5030
5031 id : |
5032 lit : |
5033 sub ,
5034 opt ,
5035 mult: IF OptionalExpression(expr)
5036 THEN
5037 RETURN( TRUE )
5038 END |
5039 m2 :
5040
5041 ELSE
5042 END
5043 END ;
5044 f := f^.next
5045 END ;
5046 RETURN( FALSE )
5047 END OptionalFactor ;
5048
5049
5050 (*
5051 OptionalTerm - returns TRUE if the term maybe empty.
5052 *)
5053
5054 PROCEDURE OptionalTerm (t: TermDesc) : BOOLEAN ;
5055 VAR
5056 u, v : TermDesc ;
5057 tov, tou: SetDesc ;
5058 BEGIN
5059 u := t ;
5060 WHILE u#NIL DO
5061 IF OptionalFactor(u^.factor)
5062 THEN
5063 RETURN( TRUE )
5064 END ;
5065 v := t ;
5066 tou := NIL ;
5067 CalcFirstFactor(u^.factor, NIL, tou) ;
5068 WHILE v#NIL DO
5069 IF v#u
5070 THEN
5071 tov := NIL ;
5072 CalcFirstFactor(v^.factor, NIL, tov) ;
5073 IF IntersectionIsNil(tov, tou)
5074 THEN
5075 DisposeSetDesc(tov) ;
5076 ELSE
5077 WriteString('problem with two first sets. Set 1: ') ;
5078 EmitSet(tou, 0, 0) ; WriteLn ;
5079 WriteString(' Set 2: ') ;
5080 EmitSet(tov, 0, 0) ; WriteLn ;
5081 DisposeSetDesc(tou) ;
5082 DisposeSetDesc(tov) ;
5083 RETURN( TRUE )
5084 END
5085 END ;
5086 v := v^.next
5087 END ;
5088 DisposeSetDesc(tou) ;
5089 u := u^.next
5090 END ;
5091 RETURN( FALSE )
5092 END OptionalTerm ;
5093
5094
5095 (*
5096 OptionalExpression -
5097 *)
5098
5099 PROCEDURE OptionalExpression (e: ExpressionDesc) : BOOLEAN ;
5100 BEGIN
5101 IF e=NIL
5102 THEN
5103 RETURN( FALSE )
5104 ELSE
5105 RETURN( OptionalTerm(e^.term) )
5106 END
5107 END OptionalExpression ;
5108
5109
5110 (*
5111 OptionalStatement - returns FALSE if statement, s, does not have a optional ambiguity.
5112 *)
5113
5114 PROCEDURE OptionalStatement (s: StatementDesc) : BOOLEAN ;
5115 BEGIN
5116 IF s=NIL
5117 THEN
5118 RETURN( FALSE )
5119 ELSE
5120 RETURN( OptionalExpression(s^.expr) )
5121 END
5122 END OptionalStatement ;
5123
5124
5125 (*
5126 OptionalProduction -
5127 *)
5128
5129 PROCEDURE OptionalProduction (p: ProductionDesc) : BOOLEAN ;
5130 BEGIN
5131 IF p=NIL
5132 THEN
5133 RETURN( FALSE )
5134 ELSE
5135 RETURN( OptionalStatement(p^.statement) )
5136 END
5137 END OptionalProduction ;
5138
5139
5140 (*
5141 CheckFirstFollow -
5142 *)
5143
5144 PROCEDURE CheckFirstFollow (f: FactorDesc; after: FactorDesc) : BOOLEAN ;
5145 VAR
5146 first, follow: SetDesc ;
5147 BEGIN
5148 first := NIL ;
5149 CalcFirstFactor(f, NIL, first) ;
5150 follow := NIL ;
5151 follow := GetFollow(f^.followinfo) ;
5152 IF IntersectionIsNil(first, follow)
5153 THEN
5154 DisposeSetDesc(first) ;
5155 DisposeSetDesc(follow) ;
5156 RETURN( FALSE )
5157 ELSE
5158 PrettyCommentFactor(f, 3) ;
5159 NewLine(3) ;
5160 WriteString('first: ') ;
5161 EmitSet(first, 0, 0) ;
5162 NewLine(3) ;
5163 WriteString('follow: ') ;
5164 EmitSet(follow, 0, 0) ;
5165 NewLine(3) ;
5166 DisposeSetDesc(first) ;
5167 DisposeSetDesc(follow) ;
5168 RETURN( TRUE )
5169 END
5170 END CheckFirstFollow ;
5171
5172
5173 (*
5174 ConstrainedEmptyFactor -
5175 *)
5176
5177 PROCEDURE ConstrainedEmptyFactor (f: FactorDesc) : BOOLEAN ;
5178 BEGIN
5179 WHILE f#NIL DO
5180 WITH f^ DO
5181 CASE type OF
5182
5183 id : |
5184 lit : |
5185 sub ,
5186 opt ,
5187 mult: IF ConstrainedEmptyExpression(expr)
5188 THEN
5189 RETURN( TRUE )
5190 END |
5191 m2 :
5192
5193 ELSE
5194 END
5195 END ;
5196 IF (f^.type#m2) AND EmptyFactor(f) AND CheckFirstFollow(f, f^.next)
5197 THEN
5198 RETURN( TRUE )
5199 END ;
5200 f := f^.next
5201 END ;
5202 RETURN( FALSE )
5203 END ConstrainedEmptyFactor ;
5204
5205
5206 (*
5207 ConstrainedEmptyTerm - returns TRUE if the term maybe empty.
5208 *)
5209
5210 PROCEDURE ConstrainedEmptyTerm (t: TermDesc) : BOOLEAN ;
5211 VAR
5212 first, follow: SetDesc ;
5213 BEGIN
5214 WHILE t#NIL DO
5215 IF ConstrainedEmptyFactor(t^.factor)
5216 THEN
5217 RETURN( TRUE )
5218 ELSIF (t^.factor^.type#m2) AND EmptyFactor(t^.factor) AND CheckFirstFollow(t^.factor, t^.factor^.next)
5219 THEN
5220 RETURN( TRUE )
5221 END ;
5222 t := t^.next
5223 END ;
5224 RETURN( FALSE )
5225 END ConstrainedEmptyTerm ;
5226
5227
5228 (*
5229 ConstrainedEmptyExpression -
5230 *)
5231
5232 PROCEDURE ConstrainedEmptyExpression (e: ExpressionDesc) : BOOLEAN ;
5233 BEGIN
5234 IF e=NIL
5235 THEN
5236 RETURN( FALSE )
5237 ELSE
5238 RETURN( ConstrainedEmptyTerm(e^.term) )
5239 END
5240 END ConstrainedEmptyExpression ;
5241
5242
5243 (*
5244 ConstrainedEmptyStatement - returns FALSE if statement, s, does not have a optional ambiguity.
5245 *)
5246
5247 PROCEDURE ConstrainedEmptyStatement (s: StatementDesc) : BOOLEAN ;
5248 BEGIN
5249 IF s=NIL
5250 THEN
5251 RETURN( FALSE )
5252 ELSE
5253 RETURN( ConstrainedEmptyExpression(s^.expr) )
5254 END
5255 END ConstrainedEmptyStatement ;
5256
5257
5258 (*
5259 ConstrainedEmptyProduction - returns TRUE if a problem exists with, p.
5260 *)
5261
5262 PROCEDURE ConstrainedEmptyProduction (p: ProductionDesc) : BOOLEAN ;
5263 BEGIN
5264 IF p=NIL
5265 THEN
5266 RETURN( FALSE )
5267 ELSE
5268 RETURN( ConstrainedEmptyStatement(p^.statement) )
5269 END
5270 END ConstrainedEmptyProduction ;
5271
5272
5273 (*
5274 TestForLALR1 -
5275 *)
5276
5277 PROCEDURE TestForLALR1 (p: ProductionDesc) ;
5278 BEGIN
5279 IF OptionalProduction(p)
5280 THEN
5281 WarnError1('production %s has two optional sentances using | which both have the same start symbols',
5282 p^.statement^.ident^.name) ;
5283 WasNoError := FALSE ;
5284 PrettyCommentProduction(p)
5285 END ;
5286 (*
5287 IF ConstrainedEmptyProduction(p)
5288 THEN
5289 WarnError1('production %s has an empty sentance and the first and follow symbols intersect',
5290 p^.statement^.ident^.name) ;
5291 WasNoError := FALSE
5292 END
5293 *)
5294 END TestForLALR1 ;
5295
5296
5297 (*
5298 DoEpsilon - runs the epsilon interrelated rules
5299 *)
5300
5301 PROCEDURE DoEpsilon (p: ProductionDesc) ;
5302 BEGIN
5303 CalcEpsilonProduction(p) ;
5304 CalcReachEndProduction(p)
5305 END DoEpsilon ;
5306
5307
5308 (*
5309 CheckComplete - checks that production, p, is complete.
5310 *)
5311
5312 PROCEDURE CheckComplete (p: ProductionDesc) ;
5313 BEGIN
5314 IF GetReachEnd(p^.followinfo)=unknown
5315 THEN
5316 PrettyCommentProduction(p) ;
5317 WarnError1('cannot determine epsilon, probably a left recursive rule in %s and associated rules (hint rewrite using ebnf and eliminate left recursion)',
5318 p^.statement^.ident^.name) ;
5319 WasNoError := FALSE
5320 END
5321 END CheckComplete ;
5322
5323
5324 (*
5325 PostProcessRules - backpatch the ident to rule definitions and emit comments and code.
5326 *)
5327
5328 PROCEDURE PostProcessRules ;
5329 BEGIN
5330 ForeachRuleDo(BackPatchIdentToDefinitions) ;
5331 IF NOT WasNoError
5332 THEN
5333 HALT
5334 END ;
5335 WhileNotCompleteDo(DoEpsilon) ;
5336 IF NOT WasNoError
5337 THEN
5338 HALT
5339 END ;
5340 ForeachRuleDo(CheckComplete) ;
5341 IF NOT WasNoError
5342 THEN
5343 HALT
5344 END ;
5345 WhileNotCompleteDo(CalculateFirstAndFollow) ;
5346 IF NOT WasNoError
5347 THEN
5348 HALT
5349 END ;
5350 ForeachRuleDo(TestForLALR1) ;
5351 IF NOT WasNoError
5352 THEN
5353 ForeachRuleDo(PrettyCommentProduction)
5354 END
5355 END PostProcessRules ;
5356
5357
5358 (*
5359 DisplayHelp - display a summary help and then exit (0).
5360 *)
5361
5362 PROCEDURE DisplayHelp ;
5363 BEGIN
5364 WriteString('Usage: ppg [-l] [-c] [-d] [-e] [-k] [-t] [-k] [-p] [-x] [-f] [-o outputfile] filename') ; WriteLn ;
5365 WriteString(' -l suppress file and line source information') ; WriteLn ;
5366 WriteString(' -c do not generate any Modula-2 code within the parser rules') ; WriteLn ;
5367 WriteString(' -h or --help generate this help message') ; WriteLn ;
5368 WriteString(' -e do not generate a parser with error recovery') ; WriteLn ;
5369 WriteString(' -k generate keyword errors with GCC formatting directives') ; WriteLn ;
5370 WriteString(' -d generate internal debugging information') ; WriteLn ;
5371 WriteString(' -p only display the ebnf rules') ; WriteLn ;
5372 WriteString(' -t generate texinfo formating for pretty printing (-p)') ; WriteLn ;
5373 WriteString(' -x generate sphinx formating for pretty printing (-p)') ; WriteLn ;
5374 WriteString(' -f generate GNU Free Documentation header before pretty printing in texinfo') ; WriteLn ;
5375 WriteString(' -o write output to filename') ; WriteLn ;
5376 exit (0)
5377 END DisplayHelp ;
5378
5379
5380 (*
5381 ParseArgs -
5382 *)
5383
5384 PROCEDURE ParseArgs ;
5385 VAR
5386 n, i: CARDINAL ;
5387 BEGIN
5388 ErrorRecovery := TRUE ; (* DefaultRecovery ; *)
5389 Debugging := FALSE ;
5390 PrettyPrint := FALSE ;
5391 KeywordFormatting := FALSE ;
5392 i := 1 ;
5393 n := Narg() ;
5394 WHILE i<n DO
5395 IF GetArg(ArgName, i)
5396 THEN
5397 IF StrEqual(ArgName, '-e')
5398 THEN
5399 ErrorRecovery := FALSE
5400 ELSIF StrEqual(ArgName, '-d')
5401 THEN
5402 Debugging := TRUE ;
5403 SetDebugging(TRUE)
5404 ELSIF StrEqual(ArgName, '-c')
5405 THEN
5406 EmitCode := FALSE
5407 ELSIF StrEqual(ArgName, '-k')
5408 THEN
5409 KeywordFormatting := TRUE
5410 ELSIF StrEqual(ArgName, '-l')
5411 THEN
5412 SuppressFileLineTag := TRUE
5413 ELSIF StrEqual(ArgName, '-h') OR StrEqual(ArgName, '--help')
5414 THEN
5415 DisplayHelp
5416 ELSIF StrEqual(ArgName, '-p')
5417 THEN
5418 PrettyPrint := TRUE
5419 ELSIF StrEqual(ArgName, '-t')
5420 THEN
5421 Texinfo := TRUE
5422 ELSIF StrEqual(ArgName, '-x')
5423 THEN
5424 Sphinx := TRUE
5425 ELSIF StrEqual(ArgName, '-f')
5426 THEN
5427 FreeDocLicense := TRUE
5428 ELSIF StrEqual(ArgName, '-o')
5429 THEN
5430 INC (i) ;
5431 IF GetArg(ArgName, i)
5432 THEN
5433 IF NOT Output.Open (ArgName)
5434 THEN
5435 WriteString('cannot open ') ; WriteString(ArgName) ;
5436 WriteString(' for writing') ; WriteLn ;
5437 exit (1)
5438 END
5439 END
5440 ELSIF OpenSource(ArgName)
5441 THEN
5442 StrCopy (ArgName, FileName) ;
5443 AdvanceToken
5444 ELSE
5445 WriteString('cannot open ') ; WriteString(ArgName) ;
5446 WriteString(' for reading') ; WriteLn ;
5447 exit (1)
5448 END
5449 END ;
5450 INC (i)
5451 END ;
5452 IF n=1
5453 THEN
5454 DisplayHelp
5455 END
5456 END ParseArgs ;
5457
5458
5459 (*
5460 Init - initialize the modules data structures
5461 *)
5462
5463 PROCEDURE Init ;
5464 BEGIN
5465 WasNoError := TRUE ;
5466 Texinfo := FALSE ;
5467 Sphinx := FALSE ;
5468 FreeDocLicense := FALSE ;
5469 EmitCode := TRUE ;
5470 LargestValue := 0 ;
5471 HeadProduction := NIL ;
5472 CurrentProduction := NIL ;
5473 InitTree(Aliases) ;
5474 InitTree(ReverseAliases) ;
5475 InitTree(Values) ;
5476 InitTree(ReverseValues) ;
5477 LastLineNo := 0 ;
5478 CodePrologue := NIL ;
5479 CodeEpilogue := NIL ;
5480 CodeDeclaration := NIL ;
5481 ErrorProcArray := MakeKey('Error') ;
5482 ErrorProcString := MakeKey('ErrorS') ;
5483 TokenTypeProc := MakeKey('GetCurrentTokenType()') ;
5484 SymIsProc := MakeKey('SymIs') ;
5485 OnLineStart := TRUE ;
5486 ParseArgs ;
5487 WasNoError := Main() ; (* this line will be manipulated by sed in buildpg *)
5488 IF WasNoError
5489 THEN
5490 PostProcessRules ;
5491 IF WasNoError
5492 THEN
5493 IF Debugging
5494 THEN
5495 EmitRules
5496 ELSIF PrettyPrint
5497 THEN
5498 EmitRules
5499 ELSE
5500 Output.WriteString('(* it is advisable not to edit this file as it was automatically generated from the grammer file ') ;
5501 Output.WriteString(FileName) ; Output.WriteString(' *)') ; Output.WriteLn ;
5502 OnLineStart := FALSE ;
5503 EmitFileLineTag(LinePrologue) ;
5504 BeginningOfLine := TRUE ;
5505 WriteCodeHunkList(CodePrologue) ;
5506 EmitSupport ;
5507 EmitFileLineTag(LineDeclaration) ;
5508 WriteCodeHunkList(CodeDeclaration) ;
5509 EmitRules ;
5510 (* code rules *)
5511 EmitFileLineTag(LineEpilogue) ;
5512 WriteCodeHunkList(CodeEpilogue)
5513 END
5514 END
5515 END ;
5516 Output.Close
5517 END Init ;
5518
5519
5520 BEGIN
5521 Init
5522 END ppg.
5523 (*
5524 * Local variables:
5525 * compile-command: "gm2 -I../gm2-libs:. -fbounds -freturn -c -g ppg.mod"
5526 * End:
5527 *)