genparser.java

来自「有关编译器的编译器.」· Java 代码 · 共 894 行 · 第 1/2 页

JAVA
894
字号
                           current++;                           macros.apply(finalFile, "ParserNewBodyParams",                                        new String[] {(current == 1) ? "" : ", ",                                                      current + ""});                         }                         catch(IOException e)                         {                           throw new RuntimeException("An error occured while writing to " +                                                      new File(pkgDir, "Parser.java").getAbsolutePath());                         }                       }                     }                    );          macros.apply(file, "ParserNewTail", new String[] { "" + productions[i].leftside});        }      }      macros.apply(file, "ParserActionHeader");      StringBuffer table = new StringBuffer();      DataOutputStream out = new DataOutputStream(                               new BufferedOutputStream(                                 new FileOutputStream(                                   new File(pkgDir, "parser.dat"))));      Vector outerArray = new Vector();      for(int i = 0; i < Grammar.action_.length; i++)      {        Vector innerArray = new Vector();        String mostFrequentAction = "ERROR";        int mostFrequentDestination = i;        int frequence = 0;        Map map = new TreeMap(IntegerComparator.instance);        for(int j = 0; j < Grammar.action_[i].length; j++)        {          if(Grammar.action_[i][j] != null)          {            if(Grammar.action_[i][j][0] == 1)            {              Integer index = new Integer(Grammar.action_[i][j][1]);              Integer count = (Integer) map.get(index);              int freq = count == null ? 0 : count.intValue();              map.put(index, new Integer(++freq));              if(freq > frequence)              {                frequence = freq;                mostFrequentAction = "REDUCE";                mostFrequentDestination = Grammar.action_[i][j][1];              }            }          }        }        table.append("\t\t\t{");        table.append("{" + -1 + ", " +                     mostFrequentAction + ", " +                     mostFrequentDestination + "}, ");        innerArray.addElement(          new int[] {-1,                     mostFrequentAction.equals("ERROR") ? 3 : 1,                     mostFrequentDestination});        for(int j = 0; j < Grammar.action_[i].length; j++)        {          if(Grammar.action_[i][j] != null)          {            switch(Grammar.action_[i][j][0])            {            case 0:              table.append("{" + j + ", SHIFT, " + Grammar.action_[i][j][1] + "}, ");              innerArray.addElement(new int[] {j, 0, Grammar.action_[i][j][1]});              break;            case 1:              if(Grammar.action_[i][j][1] != mostFrequentDestination)              {                table.append("{" + j + ", REDUCE, " + Grammar.action_[i][j][1] + "}, ");                innerArray.addElement(new int[] {j, 1, Grammar.action_[i][j][1]});              }              break;            case 2:              table.append("{" + j + ", ACCEPT, -1}, ");              innerArray.addElement(new int[] {j, 2, -1});              break;            }          }        }        table.append("}," + System.getProperty("line.separator"));        outerArray.addElement(innerArray);      }      file.write("" + table);      out.writeInt(outerArray.size());      for(Enumeration e = outerArray.elements(); e.hasMoreElements();)      {        Vector innerArray = (Vector) e.nextElement();        out.writeInt(innerArray.size());        for(Enumeration n = innerArray.elements(); n.hasMoreElements();)        {          int[] array = (int[]) n.nextElement();          for(int i = 0; i < 3; i++)          {            out.writeInt(array[i]);          }        }      }      macros.apply(file, "ParserActionTail");      macros.apply(file, "ParserGotoHeader");      table = new StringBuffer();      outerArray = new Vector();      for(int j = 0; j < nonterminals.length - 1; j++)      {        Vector innerArray = new Vector();        int mostFrequent = -1;        int frequence = 0;        Map map = new TreeMap(IntegerComparator.instance);        for(int i = 0; i < Grammar.goto_.length; i++)        {          if(Grammar.goto_[i][j] != -1)          {            Integer index = new Integer(Grammar.goto_[i][j]);            Integer count = (Integer) map.get(index);            int freq = count == null ? 0 : count.intValue();            map.put(index, new Integer(++freq));            if(freq > frequence)            {              frequence = freq;              mostFrequent = Grammar.goto_[i][j];            }          }        }        table.append("\t\t\t{");        table.append("{" + (-1) + ", " + mostFrequent + "}, ");        innerArray.addElement(new int[] {-1, mostFrequent});        for(int i = 0; i < Grammar.goto_.length; i++)        {          if((Grammar.goto_[i][j] != -1) &&              (Grammar.goto_[i][j] != mostFrequent))          {            table.append("{" + i + ", " + Grammar.goto_[i][j] + "}, ");            innerArray.addElement(new int[] {i, Grammar.goto_[i][j]});          }        }        table.append("}," + System.getProperty("line.separator"));        outerArray.addElement(innerArray);      }      file.write("" + table);      out.writeInt(outerArray.size());      for(Enumeration e = outerArray.elements(); e.hasMoreElements();)      {        Vector innerArray = (Vector) e.nextElement();        out.writeInt(innerArray.size());        for(Enumeration n = innerArray.elements(); n.hasMoreElements();)        {          int[] array = (int[]) n.nextElement();          for(int i = 0; i < 2; i++)          {            out.writeInt(array[i]);          }        }      }      macros.apply(file, "ParserGotoTail");      macros.apply(file, "ParserErrorsHeader");      table = new StringBuffer();      StringBuffer index = new StringBuffer();      int nextIndex = 0;      Map errorIndex = new TypedTreeMap(                         StringComparator.instance,                         StringCast.instance,                         IntegerCast.instance);      outerArray = new Vector();      Vector indexArray = new Vector();      index.append("\t\t\t");      for(int i = 0; i < Grammar.action_.length; i++)      {        StringBuffer s = new StringBuffer();        s.append("expecting: ");        boolean comma = false;        for(int j = 0; j < Grammar.action_[i].length; j++)        {          if(Grammar.action_[i][j] != null)          {            if(comma)            {              s.append(", ");            }            else            {              comma = true;            }            s.append(Symbol.symbol(j, true).errorName);          }        }        if(errorIndex.containsKey(s.toString()))        {          index.append(errorIndex.get(s.toString()) + ", ");          indexArray.addElement(errorIndex.get(s.toString()));        }        else        {          table.append("\t\t\t\"" + s + "\"," + System.getProperty("line.separator"));          outerArray.addElement(s.toString());          errorIndex.put(s.toString(), new Integer(nextIndex));          indexArray.addElement(new Integer(nextIndex));          index.append(nextIndex++ + ", ");        }      }      file.write("" + table);      out.writeInt(outerArray.size());      for(Enumeration e = outerArray.elements(); e.hasMoreElements();)      {        String s = (String) e.nextElement();        out.writeInt(s.length());        int length = s.length();        for(int i = 0; i < length; i++)        {          out.writeChar(s.charAt(i));        }      }      out.writeInt(indexArray.size());      for(Enumeration e = indexArray.elements(); e.hasMoreElements();)      {        Integer n = (Integer) e.nextElement();        out.writeInt(n.intValue());      }      out.close();      macros.apply(file, "ParserErrorsTail");      macros.apply(file, "ParserErrorIndexHeader");      file.write("" + index);      macros.apply(file, "ParserErrorIndexTail");      macros.apply(file, "ParserTail");    }    catch(IOException e)    {      throw new RuntimeException("An error occured while writing to " +                                 new File(pkgDir, "Parser.java").getAbsolutePath());    }    try    {      file.close();    }    catch(IOException e)    {}  }  private void createTokenIndex()  {    BufferedWriter file;    try    {      file = new BufferedWriter(               new FileWriter(                 new File(pkgDir, "TokenIndex.java")));    }    catch(IOException e)    {      throw new RuntimeException("Unable to create " + new File(pkgDir, "TokenIndex.java").getAbsolutePath());    }    try    {      Symbol[] terminals = Symbol.terminals();      macros.apply(file, "TokenIndexHeader", new String[] {pkgName,                   ids.pkgName.equals("") ? "node" : ids.pkgName + ".node",                   ids.pkgName.equals("") ? "analysis" : ids.pkgName + ".analysis"});      for(int i = 0; i < (terminals.length - 2); i++)      {        macros.apply(file, "TokenIndexBody", new String[] {terminals[i].name, "" + i});      }      macros.apply(file, "TokenIndexTail", new String[] {"" + (terminals.length - 2)});    }    catch(IOException e)    {      throw new RuntimeException("An error occured while writing to " +                                 new File(pkgDir, "TokenIndex.java").getAbsolutePath());    }    try    {      file.close();    }    catch(IOException e)    {}  }  private void createParserException()  {    BufferedWriter file;    try    {      file = new BufferedWriter(               new FileWriter(                 new File(pkgDir, "ParserException.java")));    }    catch(IOException e)    {      throw new RuntimeException("Unable to create " + new File(pkgDir, "ParserException.java").getAbsolutePath());    }    try    {      macros.apply(file, "ParserException", new String[] {pkgName,                   ids.pkgName.equals("") ? "node" : ids.pkgName + ".node"});    }    catch(IOException e)    {      throw new RuntimeException("An error occured while writing to " +                                 new File(pkgDir, "ParserException.java").getAbsolutePath());    }    try    {      file.close();    }    catch(IOException e)    {}  }  private void createState()  {    BufferedWriter file;    try    {      file = new BufferedWriter(               new FileWriter(                 new File(pkgDir, "State.java")));    }    catch(IOException e)    {      throw new RuntimeException("Unable to create " + new File(pkgDir, "State.java").getAbsolutePath());    }    try    {      macros.apply(file, "State", new String[] {pkgName});    }    catch(IOException e)    {      throw new RuntimeException("An error occured while writing to " +                                 new File(pkgDir, "State.java").getAbsolutePath());    }    try    {      file.close();    }    catch(IOException e)    {}  }  private int count(String name)  {    if(name.charAt(0) != 'X')    {      return 0;    }    StringBuffer s = new StringBuffer();    int i = 1;    while((i < name.length()) &&          (name.charAt(i) >= '0') &&          (name.charAt(i) <= '9'))    {      s.append(name.charAt(i++));    }    return Integer.parseInt(s.toString());  }  private String name(String name)  {    if(name.charAt(0) != 'X')    {      return name;    }    int i = 1;    while((i < name.length()) &&          (name.charAt(i) >= '0') &&          (name.charAt(i) <= '9'))    {      i++;    }    return name.substring(i);  }  static class Element  {    String macro;    String[] arguments;    Element(String macro, String[] arguments)    {      this.macro = macro;      this.arguments = arguments;    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?