[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [enh][patch] Multiple sablecc on single JVM



Hi,

Actually SableCC _is_ dependent on some class attributes and static
structures, and the previous patch only partially was correct. Here is the
*REAL* working and tested patch for multiple SableCC 2.13 to work
correctly on single JVM.

The changes I have made are cosmetic, in particular the change of some
static final variables to only static should not influence performance at
all (at least I have not noticed it for my grammar files). 

Everything I said in the previous post apart from the above still holds.

I am sorry for not noticing the problems before submitting previous patch.

best regards
Mariusz
diff -U 5 -r old/ca/mcgill/sable/sablecc/Grammar.java new/ca/mcgill/sable/sablecc/Grammar.java
--- old/ca/mcgill/sable/sablecc/Grammar.java	Sat Mar  4 21:43:00 2000
+++ new/ca/mcgill/sable/sablecc/Grammar.java	Wed May  3 17:12:49 2000
@@ -9,11 +9,27 @@
 
 import java.util.Vector;
 import java.util.*;
 
 public final class Grammar
-{
+{    
+    private static TreeMap fastLr0Closure = new TreeMap();
+    private static TreeMap fastLr1Closure = new TreeMap();
+
+    static int startSymbol;
+    static int startProduction;
+    static int eof ;
+    static int dummy ;
+
+    static int[][][] action_;
+    static int[][] goto_;
+
+    static 
+    { 
+       reinit(); 
+    }
+
     private Grammar()
     {
     }
 
     public static int addTerminal(String name)
@@ -42,17 +58,21 @@
     public static void addSymbolToProduction(String symbol, int production)
     {
         Production.production(production).addSymbol(Symbol.symbol(symbol));
     }
 
-    static int startSymbol = 0;
-    static int startProduction = -1;
-    static int eof = -1;
-    static int dummy = -1;
-
-    static int[][][] action_;
-    static int[][] goto_;
+    public static void reinit()
+    {
+       fastLr0Closure = new TreeMap();
+       fastLr1Closure = new TreeMap();
+       startSymbol = 0;
+       startProduction = -1;
+       eof = -1;
+       dummy = -1;
+       action_ = null;
+       goto_ = null;
+    }
 
     public static void computeLALR()
     {
         // Add EOF to terminals
         eof = addTerminal("EOF");
@@ -388,12 +408,10 @@
     static SymbolSet FOLLOW(int nonterminal)
     {
         return FOLLOW[nonterminal];
     }
 
-    private static final TreeMap fastLr0Closure = new TreeMap();
-
     static LR0ItemSet CLOSURE(LR0Item item)
     {
         LR0ItemSet result = (LR0ItemSet) fastLr0Closure.get(item);
 
         if(result != null)
@@ -474,12 +492,10 @@
 
 //        fastLr0SetClosure.put(set, result);
 
         return result;
     }
-
-    private static final TreeMap fastLr1Closure = new TreeMap();
 
     static LR1ItemSet CLOSURE(LR1Item item)
     {
         LR1ItemSet result = (LR1ItemSet) fastLr1Closure.get(item);
 
diff -U 5 -r old/ca/mcgill/sable/sablecc/LR0Collection.java new/ca/mcgill/sable/sablecc/LR0Collection.java
--- old/ca/mcgill/sable/sablecc/LR0Collection.java	Sat Mar  4 21:43:00 2000
+++ new/ca/mcgill/sable/sablecc/LR0Collection.java	Wed May  3 16:46:23 2000
@@ -56,11 +56,16 @@
         }
 
         return result.intValue();
     }
 
-    private static final LR0ItemSet empty = new LR0ItemSet();
+    private static LR0ItemSet empty = new LR0ItemSet();
+
+    public static void reinit()
+    {
+        empty = new LR0ItemSet();
+    }
 
     private void addGoto(int from, Symbol symbol, LR0ItemSet to)
     {
         if(!to.equals(empty))
         {
diff -U 5 -r old/ca/mcgill/sable/sablecc/Production.java new/ca/mcgill/sable/sablecc/Production.java
--- old/ca/mcgill/sable/sablecc/Production.java	Sat Mar  4 21:43:00 2000
+++ new/ca/mcgill/sable/sablecc/Production.java	Wed May  3 17:10:06 2000
@@ -11,20 +11,30 @@
 import java.util.Vector;
 import java.util.Enumeration;
 
 final class Production
 {
-    private static final Vector productions = new Vector(0);
 
     final int leftside;
-    private final Vector rightside = new Vector(0);
+    private final Vector rightside = new Vector();
     final int index;
     final String name;
 
+    private static final Vector productions = new Vector();    
+    private static TreeMap alternatives_ = new TreeMap(IntegerComparator.instance);
     private static boolean modified_ = true;
     private static Production[] productions_;
 
+    public static void reinit()
+    {
+       productions.removeAllElements();
+       alternatives_ = new TreeMap(IntegerComparator.instance);
+       productions_ = null;
+       modified_ = true;
+       productions_ = null;
+    }
+
     private static void computeArray_()
     {
         productions_ = new Production[productions.size()];
         productions.copyInto(productions_);
         modified_ = false;
@@ -74,12 +84,10 @@
 
     static Production production(int index)
     {
         return (Production) productions.elementAt(index);
     }
-
-    private static TreeMap alternatives_ = new TreeMap(IntegerComparator.instance);
 
     static Production[] alternatives(int nonterminal)
     {
         if(modified_)
         {
diff -U 5 -r old/ca/mcgill/sable/sablecc/SableCC.java new/ca/mcgill/sable/sablecc/SableCC.java
--- old/ca/mcgill/sable/sablecc/SableCC.java	Sat Mar  4 21:43:00 2000
+++ new/ca/mcgill/sable/sablecc/SableCC.java	Wed May  3 16:46:46 2000
@@ -39,10 +39,11 @@
     {
         System.out.println("Usage:");
         System.out.println("  java SableCC [-d destination] filename");
         System.out.println("  java SableCC -license");
     }
+
     public static void main(String[] arguments)
     {
         String d_option = null;
         String filename = null;
 
@@ -97,77 +98,12 @@
             }
         }
 
         try
         {
-            // Get the grammar description file
-
-            File in;
-            File dir;
-
-            in = new File(filename);
-            in = new File(in.getAbsolutePath());
-
-            if(d_option == null)
-            {
-                dir = new File(in.getParent());
-            }
-            else
-            {
-                dir = new File(d_option);
-                dir = new File(dir.getAbsolutePath());
-            }
-
-            FileReader temp;
-
-            // Build the AST
-            Start tree = new Parser(new Lexer(new BufferedReader(
-                temp = new FileReader(in)))).parse();
-
-            temp.close();
-
-            System.out.println("Verifying identifiers.");
-            ResolveIds ids = new ResolveIds(dir);
-            tree.apply(ids);
-
-            // Create the node.* and analysis.* files
-            System.out.println("Generating token classes.");
-            tree.apply(new GenTokens(ids));
-
-            System.out.println("Generating production classes.");
-            tree.apply(new GenProds(ids));
-
-            System.out.println("Generating alternative classes.");
-            tree.apply(new GenAlts(ids));
-
-            System.out.println("Generating analysis classes.");
-            tree.apply(new GenAnalyses(ids));
-
-            System.out.println("Generating utility classes.");
-            tree.apply(new GenUtils(ids));
-
-            try
-            {
-                System.out.println("Generating the lexer.");
-                tree.apply(new GenLexer(ids));
-            }
-            catch(Exception e)
-            {
-                System.out.println(e.getMessage());
-            }
-
-            try
-            {
-                System.out.println("Generating the parser.");
-                tree.apply(new GenParser(ids));
-            }
-            catch(Exception e)
-            {
-                System.out.println(e.getMessage());
-            }
-        }
-        catch(Exception e)
+          processGrammar(filename, d_option);
+        }catch(Exception e)
         {
             System.out.println(e);
             System.exit(1);
         }
         catch(Throwable e)
@@ -178,7 +114,99 @@
         finally
         {
             System.exit(0);
         }
     }
+
+  /**
+   * The main method for processing grammar file and generating the parser/lexer. 
+   * @param in input grammar file
+   * @param dir output directory
+   */
+  public static void processGrammar(String grammar, String destDir) throws Exception, Throwable 
+  {
+      File in;
+      File dir;
+
+      in = new File(grammar);
+      in = new File(in.getAbsolutePath());
+    
+      if(destDir == null)
+      {
+          dir = new File(in.getParent());
+      }
+      else
+      {
+          dir = new File(destDir);
+          dir = new File(dir.getAbsolutePath());
+      }
+    
+      processGrammar(in, dir);
+  }
+
+  /**
+   * The main method for processing grammar file and generating the parser/lexer. 
+   * @param in input grammar file
+   * @param dir output directory
+   */
+  public static void processGrammar(File in,  File dir) throws Exception, Throwable 
+  {
+      // reinit static structures
+      LR0Collection.reinit();
+      Symbol.reinit();
+      Production.reinit();
+      Grammar.reinit();
+
+
+      FileReader temp;
+    
+      // Build the AST
+      Start tree = new Parser(new Lexer(new BufferedReader(
+                 temp = new FileReader(in)))).parse();
+      temp.close();
+
+      System.out.println("Verifying identifiers.");
+      ResolveIds ids = new ResolveIds(dir);
+      tree.apply(ids);
+
+      // Create the node.* and analysis.* files
+      System.out.println("Generating token classes.");
+      tree.apply(new GenTokens(ids));
+
+      System.out.println("Generating production classes.");
+      tree.apply(new GenProds(ids));
+
+      System.out.println("Generating alternative classes.");
+      tree.apply(new GenAlts(ids));
+
+      System.out.println("Generating analysis classes.");
+      tree.apply(new GenAnalyses(ids));
+
+      System.out.println("Generating utility classes.");
+      tree.apply(new GenUtils(ids));
+
+      try
+      {
+          System.out.println("Generating the lexer.");
+          tree.apply(new GenLexer(ids));
+      }
+      catch(Exception e)
+      {
+          System.out.println(e.getMessage());
+          throw e;
+      }
+
+      try
+      {
+          System.out.println("Generating the parser.");
+          tree.apply(new GenParser(ids));
+      }
+      catch(Exception e)
+      {
+          System.out.println(e.getMessage());
+          throw e;
+      }
+  }
+  
+
 }
 
diff -U 5 -r old/ca/mcgill/sable/sablecc/Symbol.java new/ca/mcgill/sable/sablecc/Symbol.java
--- old/ca/mcgill/sable/sablecc/Symbol.java	Fri Mar  3 20:30:00 2000
+++ new/ca/mcgill/sable/sablecc/Symbol.java	Wed May  3 16:17:36 2000
@@ -10,24 +10,27 @@
 import java.util.*;
 import java.util.Vector;
 
 final class Symbol implements Comparable
 {
-    private static final Vector terminals = new Vector(0);
-    private static final Vector nonterminals = new Vector(0);
-    private static final TreeMap names = new TreeMap(StringComparator.instance);
+    private static Vector terminals;
+    private static Vector nonterminals;
+    private static TreeMap names;
 
     private static boolean modified_ = true;
     private static Symbol[] symbols_;
     private static Symbol[] terminals_;
     private static Symbol[] nonterminals_;
 
-
     final String name;
     final boolean terminal;
     final int index;
 
+    static {
+        reinit();
+    }
+
     Symbol(String name, boolean terminal)
     {
         if(names.get(name) != null)
         {
             throw new IllegalArgumentException("The symbol " + name + " aready exists.");
@@ -46,10 +49,21 @@
 
         this.name = name;
         this.terminal = terminal;
         names.put(name, this);
         modified_ = true;
+     }
+
+    public static void reinit()
+    {
+        terminals = new Vector();
+        nonterminals = new Vector();
+        names = new TreeMap(StringComparator.instance);
+        modified_ = true;
+        symbols_ = null;
+        terminals_ = null;
+        nonterminals_ = null;
     }
 
     static Symbol symbol(String name)
     {
         return (Symbol) names.get(name);