source: devel-tools/trunk/LL1GrammarAnalyzers/LLAnalyzerInRfp2k7/GrRead.rf @ 2712

Last change on this file since 2712 was 2712, checked in by roman, 14 years ago

This version is for "Refal+ 2007".

  • Property svn:eol-style set to native
File size: 4.4 KB
Line 
1//
2// File GRREAD.RF
3//
4// Project: GR
5//
6
7$use Class;
8$use Convert;
9$use Dos;
10$use StdIO;
11
12$use GrScn;
13$use GrPrn;
14
15$func  Parse         e.Chl = (e.Tokens) (e.Rules);
16$func  Grammar       sC sI = sC sI (e.Tokens) (e.Rules);
17$func  TokenSeq     sC sI (e.Tokens) = sC sI (e.Tokens);
18$func  GroupSeq     sC sI (e.Groups) = sC sI (e.Groups);
19$func  Group         sC sI = sC sI (e.Group);
20$func  RhsSeq       sC sI (e.Group) = sC sI (e.Group);
21$func  RhsSeqRest  sC sI (e.Group) = sC sI (e.Group);
22$func  Rhs           sC sI (e.Rhs) = sC sI (e.Rhs);
23$func? IsKeyword      s.KW sC sI    = sC sI ;
24$func  Keyword       s.KW sC sI    = sC sI ;
25$func? IsName         sC sI         = sC sI s.Name;
26$func  Name          sC sI         = sC sI s.Name;
27
28$func CheckGrammar (e.Tokens) (e.Rules) = ;
29$func InsertTags   (e.Tokens) (e.Rules) = e.TaggedRules;
30$func InsertTagsRhsList
31                    (e.Tokens) (e.RhsList) = e.TaggedRhsList;
32$func InsertTagsRhs
33                    (e.Tokens) (e.Rhs) = e.TaggedRhs;
34
35$channel Input;
36
37ReadGrammar e.FileName =
38  {
39  <OpenFile &Input e.FileName "r">
40    = ;
41    = <PrintCh &StdErr "Unable to open file \"">,
42      <PrintCh &StdErr e.FileName>,
43      <PrintLnCh &StdErr "\"">,
44      <Exit 1>;
45  },
46  <Parse &Input> :: (e.Tokens) (e.Rules),
47  <CloseChannel &Input>,
48  <CheckGrammar (e.Tokens) (e.Rules)>,
49  (e.Tokens) (<InsertTags (e.Tokens) (e.Rules)>);
50
51Parse  s.Chl =
52  <InitScanner s.Chl>,
53  <Grammar <ReadToken>> :: sC sI (e.Tokens) (e.Rules),
54  <TermScanner>,
55  {
56  sC sI : Key Eof
57    = ;
58    = <WriteCh &StdErr sC sI>
59      <PrintLnCh &StdErr " instead of Eof after the grammar">
60      <Exit 1>;
61  },
62  = (e.Tokens) (e.Rules);
63
64Grammar sC sI =
65  <TokenSeq sC sI ()> :: sC sI (e.Tokens),
66  <Keyword "%%" sC sI> :: sC sI,
67  <GroupSeq sC sI ()> :: sC sI (e.Rules),
68    = sC sI (e.Tokens) (e.Rules);
69
70TokenSeq sC sI (e.Tokens) =
71  {
72  <IsName sC sI> :: sC sI s.Name
73    = <TokenSeq sC sI (e.Tokens s.Name)>;
74  <IsKeyword "%token" sC sI> :: sC sI
75    = <TokenSeq sC sI (e.Tokens)>;
76    = sC sI (e.Tokens);
77  };
78
79GroupSeq sC sI (e.Groups) =
80  {
81  sC sI : Key Eof
82    = sC sI (e.Groups);
83    = <Group sC sI> :: sC sI (e.Group),
84      = <GroupSeq sC sI (e.Groups (e.Group))>;
85  };
86
87Group sC sI =
88  <Name sC sI> :: sC sI s.Name,
89  <Keyword ":" sC sI> :: sC sI,
90  <RhsSeq sC sI (s.Name)> :: sC sI (e.Group),
91  <Keyword ";" sC sI> :: sC sI
92    = sC sI (e.Group);
93
94RhsSeq sC sI (e.Group) =
95  <Rhs sC sI ()> :: sC sI (e.Rhs)
96  = <RhsSeqRest sC sI (e.Group (e.Rhs))>;
97
98RhsSeqRest sC sI (e.Group) =
99  {
100  <IsKeyword "|" sC sI> :: sC sI
101    = <Rhs sC sI ()> :: sC sI (e.Rhs),
102      <RhsSeqRest sC sI (e.Group (e.Rhs))>;
103    = sC sI (e.Group);
104  };
105
106Rhs sC sI (e.Rhs) =
107  {
108  <IsName sC sI> :: sC sI s.Name
109    = <Rhs sC sI (e.Rhs s.Name)>;
110    = sC sI (e.Rhs);
111  };
112
113IsKeyword  s.KW_ Key s.KW_
114  = <ReadToken>;
115
116Keyword s.KW sC sI =
117  {
118  sC sI : Key s.KW
119    = ;
120    = <WriteCh &StdErr sC sI>
121      <PrintCh &StdErr " instead of ">
122      <WriteLnCh &StdErr Key s.KW>
123      <Exit 1>;
124  },
125  <ReadToken>;
126
127IsName  Name sI
128  = <ReadToken> sI;
129
130Name  sC sI =
131  {
132  sC : Name
133    = ;
134    = <WriteCh &StdErr sC sI>
135      <PrintLnCh &StdErr " instead of a name">
136      <Exit 1>;
137  },
138  = <ReadToken> sI;
139
140CheckGrammar (e.Tokens) (e.Rules) =
141  {
142  e.Rules : =
143    <PrintLnCh &StdErr " The grammar is empty">
144    <Exit 1>;
145  e.Rules : e (s.NT e) e, e.Tokens : e s.NT e =
146    <PrintCh &StdErr "Symbol "><WriteCh &StdErr s.NT>
147    <PrintLnCh &StdErr " is a token and a non-terminal at the same time">
148    <Exit 1>;
149  e.Rules : e (s.NT e) e (s.NT e) e =
150    <PrintCh &StdErr "Non-terminal "><WriteCh &StdErr s.NT>
151    <PrintLnCh &StdErr " is defined twice">
152    <Exit 1>;
153  e.Rules : e (s e (e s.NT e) e) e,
154    # \{ e.Tokens : e s.NT e; },
155    # \{ e.Rules : e (s.NT e) e; } =
156      <PrintCh &StdErr "Symbol "><WriteCh &StdErr s.NT>
157      <PrintLnCh &StdErr " is not a token and is not a non-terminal">
158      <Exit 1>;
159  = ;
160  };
161
162InsertTags
163  {
164  (e.Tokens) () = ;
165  (e.Tokens) ((s.N e.RhsList) e.Rules) =
166    (s.N <InsertTagsRhsList (e.Tokens) (e.RhsList)>)
167    <InsertTags (e.Tokens) (e.Rules)>;
168  };
169
170InsertTagsRhsList
171  {
172  (e.Tokens) () = ;
173  (e.Tokens) ((e.Rhs) e.Rest) =
174    (<InsertTagsRhs (e.Tokens) (e.Rhs)>)
175    <InsertTagsRhsList (e.Tokens) (e.Rest)>;
176  };
177
178InsertTagsRhs (e.Tokens) (e.Rhs) =
179  e.Rhs :
180  {
181  = ;
182  s.Sym e.Rest,
183    { e.Tokens : e s.Sym e = T; = N; } :: s.Tag,
184    = (s.Tag s.Sym) <InsertTagsRhs (e.Tokens) (e.Rest)>;
185  };
Note: See TracBrowser for help on using the repository browser.