comparison tests/agcl/contrib/parse.syn @ 0:13d2b8934445

Import AnaGram (near-)release tree into Mercurial.
author David A. Holland
date Sat, 22 Dec 2007 17:52:45 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:13d2b8934445
1 {
2 /*
3 * AnaGram, A System for Syntax Directed Programming
4 * Copyright 1993-1994 Steve Holland. All rights reserved.
5 * See the file COPYING for license and usage terms.
6 */
7 /* Embedded C code */
8 #include <exec/lists.h>
9 #include <intuition/classes.h>
10 #include <proto/intuition.h>
11 #include "OOLib.h"
12 #include "Expr.h"
13 #include "PushString.h"
14 #include "ClassManager.h"
15 #include "Driver/VariableClass.h"
16 #include "Number.h"
17 #include "Util.h"
18
19 struct List ExprList;
20
21 #define SYNTAX_ERROR fprintf(stderr,"%s, line %d, column %d\n", (PCB).error_message, (PCB).line, (PCB).column)
22 #define PARSER_STACK_OVERFLOW {fprintf(stderr, "\nParser stack overflow, line %d, column %d\n", (PCB).line, (PCB).column);}
23 #define REDUCTION_TOKEN_ERROR {fprintf(stderr, "\nReduction token error, line %d, column %d\n", (PCB).line, (PCB).column);}
24
25 Object *VariableProto=NULL; /* remember to initialize this */
26
27 void Prepare_parse(void)
28 {
29 VariableProto=NewObject(FindOOClass("numberclass"),NULL,NM_ID,VARIABLECLASS_ID,TAG_END);
30 if (!VariableProto) Warning(183,"Unable to create prototype variable object");
31 if (!ExprList.lh_Head) NewList(&ExprList);
32 }
33
34 void End_parse(void)
35 {
36 if (VariableProto) DisposeObject(VariableProto);
37 VariableProto=NULL;
38 }
39
40 } /* End of embedded C */
41 EOL = '\n'+'\r'
42 DIGIT= '0-9'
43 ALPHA='a-z'+'A-Z'
44 GREEK=256..308
45 RESERVED=','+';'+'='+'('+')'+'<'+'>'+'*'+'/'+'+'+'-'+0
46 WS=' '+'\t'
47 WSDISREGARD=WS+'\r'+'\n'
48 /*ALNUM=ALPHA+DIGIT+GREEK*/
49 ANYTHING=1..255+GREEK
50 ALNUM=~(RESERVED+WSDISREGARD)
51
52 [
53 disregard WhiteSpace
54 lexeme {VariNumStr}
55 parser stack size = 1000
56 sticky {StringBody}
57 ~allow macros
58 ~case sensitive
59 escape backslashes
60 line numbers path = "parse.syn"
61 line numbers
62 ]
63
64 (void)LineParse $
65 -> Terminated Line..., 0
66
67 Terminated Line
68 -> Line, ';'
69
70 (void)Line
71 -> "DEFINE",VariNumStr:n,'(',VariableList:List,')',VariableList:LocList,'(',Group,')'
72 -> "PRINT",VariNumStr:func
73 -> "VAR", VariableDecl
74 -> "QUIT" = PCB.exit_flag=AG_SUCCESS_CODE;
75 -> Comparison = {
76 Object *Result;
77 struct ExprNode *Node;
78 char *String=NULL;
79 Result=EvalExpr(&ExprList);
80 if (Result) {
81 GetAttr(NM_NumberString,Result,(ULONG *)&String);
82 if (String) printf("%s\n",String);
83 DisposeObject(Result);
84 }
85
86 while (Node=(struct ExprNode *)RemHead(&ExprList)) {
87 if (Node->Op != OP_FUNC && Node->Data) DisposeObject(Node->Data);
88 Free(Node);
89 }
90 }
91
92 (void)Group
93 -> Comparisons:a = {ReverseTopExprStack(&ExprList,a);Push(&ExprList,OP_GROUP,a,NULL);}
94
95 (struct LocaleNode *)VariableList
96 -> !{struct Locale *Locale;DoMethod(VariableProto,VCM_NEWLOCALE,&Locale);},VariableListWork = {struct LocaleNode *Locale;DoMethod(VariableProto,VCM_TAKELOCALE,NULL,&Locale);return Locale;}
97
98 VariableListWork
99 ->
100 -> VariListMemb
101 -> VariableListWork,',',VariListMemb
102
103 VariableDecl
104 -> VariNumStr:Str ={
105 if (Str && GetString(Str)) {
106 NewObject(FindOOClass("numberclass"),NULL,
107 NM_ID,VARIABLECLASS_ID,
108 VA_Create,TRUE,
109 NM_NumberString,GetString(Str),
110 VA_NoObject,TRUE,
111 TAG_END);
112 DeleteString(Str);
113 }
114 else ParsePanic(PP_MEM);
115 }
116
117 -> "const",VariNumStr:Str,'=',VariNum:Num ={
118 Object *Const;
119 if (Str && GetString(Str)) {
120 Const=NewObject(FindOOClass("numberclass"),NULL,
121 NM_ID,VARIABLECLASS_ID,
122 NM_NumberString,GetString(Str),
123 VA_Constant,TRUE,
124 VA_Create,TRUE,
125 TAG_END);
126 DeleteString(Str);
127 if (Const && Num) DoMethod(Const,VCM_ASSIGNFORCE,Num);
128 if (Const) DisposeObject(Const);
129 }
130 else ParsePanic(PP_MEM);
131 if (Num) DisposeObject(Num);
132 }
133
134
135 (void)VariListMemb
136 -> VariNumStr:Str ={
137 if (Str && GetString(Str)) {
138 NewObject(FindOOClass("numberclass"),NULL,
139 NM_ID,VARIABLECLASS_ID,
140 VA_Create,TRUE,
141 NM_NumberString,GetString(Str),
142 VA_NoObject,TRUE,
143 TAG_END);
144 DeleteString(Str);
145 }
146 else ParsePanic(PP_MEM);
147 }
148
149 -> "var",VariNumStr:Str ={
150 if (Str && GetString(Str)) {
151 NewObject(FindOOClass("numberclass"),NULL,
152 NM_ID,VARIABLECLASS_ID,
153 NM_NumberString,GetString(Str),
154 VA_ByReference,TRUE,
155 VA_Create,TRUE,
156 VA_NoObject,TRUE,
157 TAG_END);
158 DeleteString(Str);
159 }
160 else ParsePanic(PP_MEM);
161 }
162 -> "const",VariNumStr:Str,VariNum:Num ={
163 Object *Const;
164 if (Str && GetString(Str)) {
165 Const=NewObject(FindOOClass("numberclass"),NULL,
166 NM_ID,VARIABLECLASS_ID,
167 NM_NumberString,GetString(Str),
168 VA_Constant,TRUE,
169 VA_Create,TRUE,
170 TAG_END);
171 if (Const && Num) DoMethod(Const,VCM_ASSIGNFORCE,Num);
172 if (Const) DisposeObject(Const);
173 DeleteString(Str);
174
175 }
176 else ParsePanic(PP_MEM);
177 if (Num) DisposeObject(Num);
178 }
179
180
181 Comparison
182 -> Assignment
183 -> Comparison,'>',Assignment ={ReverseTopExprStack(&ExprList,2);Push(&ExprList,OP_FUNC,2,FUNC_GT);}
184 -> Comparison,'<',Assignment ={ReverseTopExprStack(&ExprList,2);Push(&ExprList,OP_FUNC,2,FUNC_LT);}
185 -> Comparison,"==",Assignment ={ReverseTopExprStack(&ExprList,2);Push(&ExprList,OP_FUNC,2,FUNC_COMP);}
186 -> Comparison,'>','=',Assignment ={ReverseTopExprStack(&ExprList,2);Push(&ExprList,OP_FUNC,2,FUNC_GTEQ);}
187 -> Comparison,'<','=',Assignment ={ReverseTopExprStack(&ExprList,2);Push(&ExprList,OP_FUNC,2,FUNC_LTEQ);}
188
189 (int) Comparisons
190 -> Comparison =1;
191 -> Comparison, ';' =1;
192 -> Comparison, ';',Comparisons:n =n+1;
193
194 Assignment
195 -> Expression
196 -> VariNum:s,'=',Expression ={Push(&ExprList,OP_ASSIGN,0,s);}
197
198 (void)FunctionCall
199 -> VariNumStr:f,'(',ArgumentList:n,')' = {ReverseTopExprStack(&ExprList,n);Push(&ExprList,OP_FUNC,n,GetString(f));DeleteString(f);}
200
201 (void)Expression
202 -> Product
203 -> Expression,'+',Product ={ReverseTopExprStack(&ExprList,2);Push(&ExprList,OP_FUNC,2,FUNC_ADD);}
204 -> Expression,'-',Product ={ReverseTopExprStack(&ExprList,2);Push(&ExprList,OP_FUNC,2,FUNC_SUB);}
205
206 (unsigned)ArgumentList
207 -> =0;
208 -> Group ={return 1;}
209 -> Group,',',ArgumentList:a =a+1;
210
211 (void)Product
212 -> PowerFactor
213 -> Product,'*',Factor = {ReverseTopExprStack(&ExprList,2);Push(&ExprList,OP_FUNC,2,FUNC_MUL);}
214 -> Product,'/',Factor = {ReverseTopExprStack(&ExprList,2);Push(&ExprList,OP_FUNC,2,FUNC_DIV);}
215
216 (void)PowerFactor
217 -> Factor
218 -> '-',PowerFactor = Push(&ExprList,OP_FUNC,1,FUNC_OPPOSITE);
219 -> PowerFactor,'^',Factor ={ReverseTopExprStack(&ExprList,2);Push(&ExprList,OP_FUNC,2,FUNC_POW);}
220
221 (void)Factor
222 -> '(',Group,')'
223 -> VariNum:Vari = Push(&ExprList,OP_VAR,1,Vari);
224 -> FunctionCall
225
226
227 (Object *)VariNum
228 -> VariNumStr:Str ={
229 Object *Result;
230 if (Str && GetString(Str)) {
231 DiscardBlanks(Str);
232 Result=NewObject(FindOOClass("numberclass"),NULL,
233 NM_NumberString,GetString(Str),
234 TAG_END);
235 DeleteString(Str);
236 if (Result) return Result;
237 }
238 ParsePanic(PP_MEM);
239 return NULL;
240 }
241
242
243 (struct String *)VariNumStr
244 -> StringBody:s = s;
245
246 (struct String *) StringBody
247 -> ALNUM:c ={struct String *String;String=CreateString();StringInsChar(String,strlen(GetString(String)),c);return String;}
248 -> StringBody:String,ALNUM:c =StringInsChar(String,strlen(GetString(String)),c);
249 -> StringBody:String,WS:c =StringInsChar(String,strlen(GetString(String)),c);
250
251 WhiteSpace
252 -> WSDISREGARD
253 -> "/*",ANYTHING...,"*/"
254