move JFlex to CE source
[fedora-idea.git] / tools / lexer / jflex-1.4 / examples / interpreter / Main.java
blob4fbaf1a7a6e1371bbe674d8eba97db8d14c2736a
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Copyright (C) 2001 Gerwin Klein <lsf@jflex.de> *
3 * Copyright (C) 2001 Bernhard Rumpe <rumpe@in.tum.de> *
4 * All rights reserved. *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License. See the file *
8 * COPYRIGHT for more information. *
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 along *
16 * with this program; if not, write to the Free Software Foundation, Inc., *
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
18 * *
19 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
22 import java.io.File;
23 import java.io.FileReader;
24 import java.io.InputStreamReader;
25 import java.io.Reader;
27 /**
28 * Main program of the interpreter for the AS programming language.
29 * Based on JFlex/CUP.
31 * Steps:
32 * - scanning (Yylex)
33 * - context free parsing and AST building (yyparse)
34 * - build up symbol table (setSymtabs)
35 * - check context conditions (checkcontext)
36 * - prepare interpretation (prepInterp)
37 * - start interpretation (interpret)
38 */
39 public class Main {
41 public static void main(String [] args) throws Exception {
42 Reader reader = null;
44 if (args.length == 1) {
45 File input = new File(args[0]);
46 if (!input.canRead()) {
47 System.out.println("Error: could not read ["+input+"]");
49 reader = new FileReader(input);
51 else {
52 reader = new InputStreamReader(System.in);
55 Yylex scanner = new Yylex(reader); // create scanner
56 SymTab symtab = new SymTab(); // set global symbol table
57 scanner.setSymtab(symtab);
59 parser parser = new parser(scanner); // create parser
60 Tprogram syntaxbaum = null;
62 try {
63 syntaxbaum = (Tprogram) parser.parse().value; // parse
65 catch (Exception e) {
66 e.printStackTrace();
69 // System.out.println(symtab);
70 System.out.println(syntaxbaum);
72 syntaxbaum.setSymtabs(); // set symbol table
73 // syntaxbaum.printSymtabs(); // print symbol table
75 syntaxbaum.checkcontext(); // CoCo (DefVar, DefFun, Arity)
76 if(contexterror>0) return;
78 syntaxbaum.prepInterp(); // var. indices and function pointers
79 // im Syntaxbaum setzen
80 syntaxbaum.interpret(); // interpretation
83 static int contexterror = 0; // number of errors in context conditions
85 public static void error(String s) {
86 System.out.println((contexterror++)+". "+s);