root/trunk/src/dmachine.d

Revision 43, 1.9 kB (checked in by svanleent, 6 years ago)

Stack Machine

Line 
1 /*
2  * DGrammar - An OOP grammar compiler for D
3  * Copyright (C) 2004  Sjoerd van Leent
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19 module dmachine;
20
21 import org.dsource.dmachine.stackmachine;
22 import std.stdio;
23
24  /++
25   + Example bootstrap routine
26   +/
27 int main(char[][] args) {
28     if(args.length < 2) {
29         writefln("Usage: %s <string>", args[0]);
30         return 1;
31     }
32     wchar[] string;
33
34     foreach(char c; args[1]) {
35         string ~= cast(wchar)(c);
36     }
37
38     StackMachine machine = new StackMachine(string);
39     StackState q0 = new StackState();
40     StackState q1 = new StackState();
41     StackState q2 = new StackState(true);
42    
43     StackRule r0 = new StackRule('a', '\0', 'A');
44     StackRule r1 = new StackRule('b', '\0', '\0');
45     StackRule r2 = new StackRule('a', 'A', '\0');
46
47     StackTransition t0 = new StackTransition(q0);
48     StackTransition t1 = new StackTransition(q1);
49     StackTransition t2 = new StackTransition(q2);
50
51     r0.addTransition(t0);
52     r1.addTransition(t1);
53     r2.addTransition(t2);
54
55     q0.addRule(r0);
56     q0.addRule(r1);
57     q1.addRule(r1);
58     q1.addRule(r2);
59     q2.addRule(r2);
60
61     StackParsedCollection spc = machine.run(q0);
62     if(spc !is null) {
63         writefln("Parsed!");
64     } else {
65         writefln("Parse Error!");
66     }
67     return 0;
68 }
Note: See TracBrowser for help on using the browser.