Mercurial > ~dholland > hg > ag > index.cgi
comparison tests/agcl/oldagsrc/asilbug1.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 VM.SYN | |
4 | |
5 Virtual Machine Compiler | |
6 Copyright (c) 1997 Parsifal Software, All Rights Reserved. | |
7 | |
8 The expression syntax is borrowed from C but with the | |
9 addition of the FORTRAN exponentiation operator (**). | |
10 | |
11 The cast, increment, and decrement operators are not | |
12 implemented, nor are operations that are defined only | |
13 for integers: | |
14 Bitwise logical operators: &, |, ^, ~, &=, |=, ^= | |
15 Remainder operators: %, %= | |
16 Shift operators: <<, >>, >>=, <<= | |
17 | |
18 The supported operations are: | |
19 Assignment operators: =, +=, -=, *=, /= | |
20 Conditional expressions: ? : | |
21 Logical operators: !, &&, || | |
22 Comparison operators: ==, !=, <, <=, >, >= | |
23 Binary arithmetic operators: +, -, *, / | |
24 Exponentiation: ** | |
25 Unary arithmetic operators: +, - | |
26 Parentheses | |
27 Function calls | |
28 | |
29 All arithmetic is double precision floating point. | |
30 | |
31 Statements may include expression statements, blocks, if/else statements | |
32 or while statements, following the rules of C. | |
33 | |
34 The statement syntax has been written to avoid the conventional | |
35 if/else ambiguity. | |
36 | |
37 There are no declarations. All variables are presumed to be double. | |
38 | |
39 Input strings may contain any number of statements. White space may be | |
40 used freely, including both C and C++ style comments. | |
41 | |
42 vmCompile makes the following external calls: | |
43 void pushChar(int character); | |
44 Push the specified character onto a character stack. | |
45 | |
46 int locateVariable(int nameLength); | |
47 Pop the last nameLength characters from the character stack | |
48 and, treating them as the name of a variable, return an | |
49 index into the variable array. | |
50 | |
51 Overrides for macros defined by AnaGram, such as SYNTAX_ERROR | |
52 are included in VMDEFS.H | |
53 | |
54 VM.SYN is compiled with the AnaGram parser generator | |
55 yielding VM.H and VM.CPP. | |
56 | |
57 To build VM, compile VM.CPP and VMDEMO.CPP and link them | |
58 with your C++ compiler. | |
59 | |
60 For information about AnaGram, visit http://www.parsifalsoft.com. | |
61 */ | |
62 | |
63 #include <math.h> | |
64 #include "vmdefs.h" // defines external interface | |
65 | |
66 static VirtualMachine *vm; | |
67 | |
68 } | |
69 // -- CONFIGURATION SECTION ---------------------------- | |
70 [ | |
71 default token type = double | |
72 disregard white space | |
73 lexeme {real, name} | |
74 pointer input | |
75 parser name = asil | |
76 parser file name = "#.cpp" | |
77 line numbers | |
78 ] | |
79 | |
80 | |
81 //parse option $ | |
82 // -> parse script, statements?, eof | |
83 // -> skip statement, statement text?, ';' | |
84 // -> skip statement, balanced braces | |
85 // -> skip condition, balanced parens | |
86 // -> eval expression, expression | |
87 // -> exec statement, statement | |
88 | |
89 //parse script, skip statement, skip condition, eval expression, exec statement | |
90 // -> =asil_change_reduction(PCB, syntaxSelector); | |
91 | |
92 (void) input string $ // specify grammar token | |
93 -> statements?, eof | |
94 | |
95 statement text | |
96 -> statement char | |
97 -> balanced parens | |
98 -> statement text, statement char | |
99 -> statement text, balanced parens | |
100 -> statement text, balanced braces | |
101 | |
102 blank = ' ' + '\t' + '\f' + '\v' + '\r' + '\n' | |
103 statement char = 32..126 - blank - ';' - '(' - ')' - '{' - '}' | |
104 | |
105 balanced parens | |
106 -> '(', statement text?, ')' | |
107 | |
108 balanced braces | |
109 -> '{', statement text?, '}' | |
110 -> '{', balanced braces, '}' | |
111 | |
112 statement | |
113 -> unconditional statement | |
114 -> conditional statement | |
115 | |
116 statements | |
117 -> statement:x =x; | |
118 -> statements:x, statement =x; | |
119 | |
120 skip statement | |
121 -> skip unconditional statement | |
122 -> skip conditional statement | |
123 | |
124 /* | |
125 An unconditional statement is any statement that does not involve | |
126 an if statement | |
127 */ | |
128 | |
129 unconditional statement | |
130 -> expression:x, ';' | |
131 -> ';' | |
132 -> '{', '}' | |
133 -> '{', statements:s, '}' | |
134 -> gather unconditional while, while loop, false condition, skip statement | |
135 | |
136 gather unconditional while | |
137 -> "while", balanced parens, skip unconditional statement | |
138 | |
139 while loop | |
140 -> | |
141 -> while loop, true condition, statement | |
142 | |
143 skip unconditional statement | |
144 -> statement text?, ';' | |
145 -> balanced braces | |
146 -> "while", balanced parens, skip unconditional statement | |
147 | |
148 /* | |
149 Any statement with an if in it is a conditional statement | |
150 */ | |
151 | |
152 conditional statement | |
153 -> gather conditional while, while loop, false condition, skip statement | |
154 -> true if clause:x, statement | |
155 -> false if clause:x, skip statement | |
156 -> true if clause:x, unconditional statement:s1, "else", skip statement:s2 | |
157 -> false if clause:x, skip unconditional statement:s1, "else", statement:s2 | |
158 | |
159 gather conditional while | |
160 -> "while", balanced parens, skip conditional statement | |
161 | |
162 skip conditional statement | |
163 -> "if", balanced parens, skip statement | |
164 -> "if", balanced parens, skip unconditional statement, "else", skip statement | |
165 -> "while", balanced parens, skip conditional statement | |
166 | |
167 false if clause, true if clause | |
168 -> "if", '(', expression:x, ')' ={if (x) CHANGE_REDUCTION(true_if_clause);} | |
169 | |
170 expression | |
171 -> conditional expression | |
172 -> name:k, '=', expression:x =vm->code(STORE, k), x; | |
173 -> name:k, "+=", expression:x =vm->code(ADDM,k), x; | |
174 -> name:k, "-=", expression:x =vm->code(SUBM,k), x; | |
175 -> name:k, "*=", expression:x =vm->code(MULM,k), x; | |
176 -> name:k, "/=", expression:x =vm->code(DIVM,k), x; | |
177 | |
178 conditional expression | |
179 -> logical or expression | |
180 -> logical or expression:c, '?', | |
181 expression:x, ':', conditional expression:y =vm->codeIfElse(c,x,y); | |
182 | |
183 logical or expression | |
184 -> logical and expression | |
185 -> logical or expression:x, "||", | |
186 logical and expression:y =vm->insertCode(OR, vm->wx - y, y), x; | |
187 | |
188 logical and expression | |
189 -> equality expression | |
190 -> logical and expression:x, "&&", | |
191 equality expression:y =vm->insertCode(AND, vm->wx - y, y), x; | |
192 | |
193 equality expression | |
194 -> relational expression | |
195 -> equality expression:x, "==", relational expression:y =vm->code(EQ), x; | |
196 -> equality expression:x, "!=", relational expression:y =vm->code(NE), x; | |
197 | |
198 relational expression | |
199 -> additive expression | |
200 -> relational expression:x, '<', additive expression:y =vm->code(LT), x; | |
201 -> relational expression:x, "<=", additive expression:y =vm->code(LE), x; | |
202 -> relational expression:x, '>', additive expression:y =vm->code(GT), x; | |
203 -> relational expression:x, ">=", additive expression:y =vm->code(GE), x; | |
204 | |
205 additive expression | |
206 -> multiplicative expression | |
207 -> additive expression:x, '+', | |
208 multiplicative expression:y =vm->code(ADD), x; | |
209 -> additive expression:x, '-', | |
210 multiplicative expression:y =vm->code(SUB), x; | |
211 | |
212 multiplicative expression | |
213 -> factor | |
214 -> multiplicative expression:x, '*', factor: y =vm->code(MUL), x; | |
215 -> multiplicative expression:x, '/', factor:y =vm->code(DIV), x; | |
216 | |
217 factor | |
218 -> primary | |
219 -> primary:x, "**", factor:y =vm->code(POW), x; | |
220 | |
221 primary | |
222 -> real:x =vm->code(PUSHI, x); | |
223 -> name:k =vm->code(PUSH, k); | |
224 -> "log", '(', expression:x, ')' =vm->code(LOG), x; | |
225 -> "exp", '(', expression:x, ')' =vm->code(EXP), x; | |
226 -> '(', expression:x, ')' =x; | |
227 -> '-', primary:x =vm->code(NEG), x; | |
228 -> '+', primary:x =x; | |
229 -> '!', primary:x =vm->code(NOT), x; | |
230 | |
231 // -- LEXICAL UNITS ------------------------------------------------ | |
232 digit = '0-9' | |
233 eof = 0 | |
234 letter = 'a-z' + 'A-Z' + '_' | |
235 | |
236 (void) white space | |
237 -> blank | |
238 -> "/*", ~eof?..., "*/" // C style comment | |
239 -> "//", ~(eof+'\n')?..., '\n' // C++ style comment | |
240 | |
241 (double) real | |
242 -> simple real | |
243 -> simple real:x, 'e'+'E', '+'?,exponent:e =x*pow(10,e); | |
244 -> simple real:x, 'e'+'E', '-',exponent:e =x*pow(10,-e); | |
245 | |
246 (double) simple real | |
247 -> integer part:i, '.', fraction part:f = i+f; | |
248 -> integer part, '.'? | |
249 -> '.', fraction part:f = f; | |
250 | |
251 (double) integer part | |
252 -> digit:d = d-'0'; | |
253 -> integer part:x, digit:d = 10*x + d-'0'; | |
254 | |
255 (double) fraction part | |
256 -> digit:d =(d-'0')/10.; | |
257 -> digit:d, fraction part:f =(d-'0' + f)/10.; | |
258 | |
259 (int) exponent | |
260 -> digit:d = d-'0'; | |
261 -> exponent:x, digit:d = 10*x + d-'0'; | |
262 | |
263 (int) name | |
264 -> name string:k =locateVariable(k); | |
265 | |
266 (int) name string | |
267 -> letter: c =pushChar(c), 1; | |
268 -> name string:k, letter+digit: c =pushChar(c), k+1; | |
269 | |
270 { // begin embedded C | |
271 | |
272 | |
273 int compileStatements(VirtualMachine *v, char *text) { | |
274 vm = v; | |
275 resetCharStack(); | |
276 vmCompile_pcb.pointer = (unsigned char *) text; | |
277 vmCompile(); | |
278 return vmCompile_pcb.exit_flag != AG_SUCCESS_CODE; | |
279 } | |
280 } // end of embedded C | |
281 /********************* End of VM.SYN ************************/ |