Пример #1
0
  public static void main(String[] args) throws Exception {

    CharStream in = new ANTLRFileStream(args[0]);
    MicroLexer lexer = new MicroLexer(in);
    CommonTokenStream tks = new CommonTokenStream(lexer);
    MicroParser psr = new MicroParser(tks);
    ParseTreeWalker walker = new ParseTreeWalker();
    ExtractionListener extractor = new ExtractionListener(psr);
    psr.setErrorHandler(new BailErrorStrategy());
    ParseTree t;

    try {
      t = psr.program();
    } catch (Exception fpe) {
      fpe.printStackTrace();
      System.out.println("Not accepted");
      return;
    }

    walker.walk(extractor, t);
    TinyGenerator asmgen = new TinyGenerator(extractor.getIRList());

    // Utils.printSymbolTable(extractor.getRootSymbolTable());

    Utils.printIR(extractor.getIRList());
    asmgen.printAll();
  }
Пример #2
0
 public String execRecognizer() {
   try {
     String inputFile = new File(tmpdir, "input").getAbsolutePath();
     String[] args =
         new String[] {"java", "-classpath", tmpdir + pathSep + CLASSPATH, "Test", inputFile};
     // String cmdLine = "java -classpath "+CLASSPATH+pathSep+tmpdir+" Test " + new File(tmpdir,
     // "input").getAbsolutePath();
     // System.out.println("execParser: "+cmdLine);
     Process process = Runtime.getRuntime().exec(args, null, new File(tmpdir));
     StreamVacuum stdoutVacuum = new StreamVacuum(process.getInputStream());
     StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream());
     stdoutVacuum.start();
     stderrVacuum.start();
     process.waitFor();
     stdoutVacuum.join();
     stderrVacuum.join();
     String output = null;
     output = stdoutVacuum.toString();
     if (stderrVacuum.toString().length() > 0) {
       this.stderrDuringParse = stderrVacuum.toString();
       System.err.println("exec stderrVacuum: " + stderrVacuum);
     }
     return output;
   } catch (Exception e) {
     System.err.println("can't exec recognizer");
     e.printStackTrace(System.err);
   }
   return null;
 }
Пример #3
0
  public String execClass(String className) {
    /* HOW TO GET STDOUT?
    try {
    	ClassLoader cl_new = new DirectoryLoader(new File(tmpdir));
    	Class compiledClass = cl_new.loadClass(className);
    	Method m = compiledClass.getMethod("main");
    	m.invoke(null);
    } catch (Exception ex) {
    	ex.printStackTrace(System.err);
    }
    */

    try {
      String[] args =
          new String[] {
            "java",
            "-classpath",
            tmpdir + pathSep + CLASSPATH,
            className,
            new File(tmpdir, "input").getAbsolutePath()
          };
      // String cmdLine = "java -classpath "+CLASSPATH+pathSep+tmpdir+" Test " + new File(tmpdir,
      // "input").getAbsolutePath();
      // System.out.println("execParser: "+cmdLine);
      Process process = Runtime.getRuntime().exec(args, null, new File(tmpdir));
      StreamVacuum stdoutVacuum = new StreamVacuum(process.getInputStream());
      StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream());
      stdoutVacuum.start();
      stderrVacuum.start();
      process.waitFor();
      stdoutVacuum.join();
      stderrVacuum.join();
      String output = null;
      output = stdoutVacuum.toString();
      if (stderrVacuum.toString().length() > 0) {
        this.stderrDuringParse = stderrVacuum.toString();
        System.err.println("exec stderrVacuum: " + stderrVacuum);
      }
      return output;
    } catch (Exception e) {
      System.err.println("can't exec recognizer");
      e.printStackTrace(System.err);
    }
    return null;
  }
Пример #4
0
 /** Return true if all is ok, no errors */
 protected boolean antlr(
     String fileName, String grammarFileName, String grammarStr, boolean debug) {
   boolean allIsWell = true;
   mkdir(tmpdir);
   writeFile(tmpdir, fileName, grammarStr);
   try {
     final List options = new ArrayList();
     if (debug) {
       options.add("-debug");
     }
     options.add("-o");
     options.add(tmpdir);
     options.add("-lib");
     options.add(tmpdir);
     options.add(new File(tmpdir, grammarFileName).toString());
     final String[] optionsA = new String[options.size()];
     options.toArray(optionsA);
     ErrorQueue equeue = new ErrorQueue();
     Tool antlr = newTool(optionsA);
     antlr.addListener(equeue);
     antlr.processGrammarsOnCommandLine();
     if (equeue.errors.size() > 0) {
       allIsWell = false;
       System.err.println("antlr reports errors from " + options);
       for (int i = 0; i < equeue.errors.size(); i++) {
         ANTLRMessage msg = (ANTLRMessage) equeue.errors.get(i);
         System.err.println(msg);
       }
       System.out.println("!!!\ngrammar:");
       System.out.println(grammarStr);
       System.out.println("###");
     }
   } catch (Exception e) {
     allIsWell = false;
     System.err.println("problems building grammar: " + e);
     e.printStackTrace(System.err);
   }
   return allIsWell;
 }