⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 expreparser.java.svn-base

📁 实现的一个简单的语言编译器,可以编译类JAVA 的小语言
💻 SVN-BASE
📖 第 1 页 / 共 2 页
字号:

//----------------------------------------------------
// The following code was generated by CUP v0.11a beta 20060608
// Fri Apr 11 09:26:36 CST 2008
//----------------------------------------------------

package edu.ustc.cs.compile.parser.expr;

import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.LinkedList;
import java_cup.runtime.*;
import org.eclipse.jdt.core.dom.*;
import edu.ustc.cs.compile.platform.interfaces.ParserInterface;
import edu.ustc.cs.compile.platform.interfaces.ParserException;
import edu.ustc.cs.compile.platform.interfaces.InterRepresent;
import edu.ustc.cs.compile.platform.util.ir.HIR;
import edu.ustc.cs.compile.platform.util.ASTView.core.*;
import edu.ustc.cs.compile.platform.util.ASTView.plugin.*;

/** CUP v0.11a beta 20060608 generated parser.
  * @version Fri Apr 11 09:26:36 CST 2008
  */
public class ExprEParser extends java_cup.runtime.lr_parser implements ParserInterface {

  /** Default constructor. */
  public ExprEParser() {super();}

  /** Constructor which sets the default scanner. */
  public ExprEParser(java_cup.runtime.Scanner s) {super(s);}

  /** Constructor which sets the default scanner. */
  public ExprEParser(java_cup.runtime.Scanner s, java_cup.runtime.SymbolFactory sf) {super(s,sf);}

  /** Production table. */
  protected static final short _production_table[][] = 
    unpackFromStrings(new String[] {
    "\000\025\000\002\002\004\000\002\002\003\000\002\003" +
    "\002\000\002\003\003\000\002\004\003\000\002\004\004" +
    "\000\002\005\004\000\002\006\005\000\002\006\005\000" +
    "\002\007\003\000\002\007\005\000\002\007\005\000\002" +
    "\010\003\000\002\010\004\000\002\010\004\000\002\010" +
    "\005\000\002\010\005\000\002\011\003\000\002\011\003" +
    "\000\002\011\005\000\002\012\004" });

  /** Access to production table. */
  public short[][] production_table() {return _production_table;}

  /** Parse-action table. */
  protected static final short[][] _action_table = 
    unpackFromStrings(new String[] {
    "\000\042\000\010\002\uffff\003\006\015\010\001\002\000" +
    "\010\002\ufffe\003\006\015\010\001\002\000\004\002\043" +
    "\001\002\000\004\011\041\001\002\000\010\002\ufffd\003" +
    "\ufffd\015\ufffd\001\002\000\004\011\014\001\002\000\004" +
    "\004\013\001\002\000\004\002\000\001\002\000\010\002" +
    "\ufffb\003\ufffb\015\ufffb\001\002\000\010\012\021\014\015" +
    "\015\020\001\002\000\022\004\uffef\005\uffef\006\uffef\007" +
    "\uffef\010\uffef\012\uffef\013\uffef\016\uffef\001\002\000\016" +
    "\004\ufff8\005\ufff8\006\ufff8\007\026\010\027\013\ufff8\001" +
    "\002\000\022\004\ufff5\005\ufff5\006\ufff5\007\ufff5\010\ufff5" +
    "\012\036\013\ufff5\016\035\001\002\000\022\004\ufff0\005" +
    "\ufff0\006\ufff0\007\ufff0\010\ufff0\012\ufff0\013\ufff0\016\ufff0" +
    "\001\002\000\010\012\021\014\015\015\020\001\002\000" +
    "\010\004\ufffa\005\023\006\024\001\002\000\010\012\021" +
    "\014\015\015\020\001\002\000\010\012\021\014\015\015" +
    "\020\001\002\000\016\004\ufff6\005\ufff6\006\ufff6\007\026" +
    "\010\027\013\ufff6\001\002\000\010\012\021\014\015\015" +
    "\020\001\002\000\010\012\021\014\015\015\020\001\002" +
    "\000\016\004\ufff1\005\ufff1\006\ufff1\007\ufff1\010\ufff1\013" +
    "\ufff1\001\002\000\016\004\ufff2\005\ufff2\006\ufff2\007\ufff2" +
    "\010\ufff2\013\ufff2\001\002\000\016\004\ufff7\005\ufff7\006" +
    "\ufff7\007\026\010\027\013\ufff7\001\002\000\010\005\023" +
    "\006\024\013\034\001\002\000\022\004\uffee\005\uffee\006" +
    "\uffee\007\uffee\010\uffee\012\uffee\013\uffee\016\uffee\001\002" +
    "\000\016\004\ufff4\005\ufff4\006\ufff4\007\ufff4\010\ufff4\013" +
    "\ufff4\001\002\000\004\013\040\001\002\000\016\004\ufff3" +
    "\005\ufff3\006\ufff3\007\ufff3\010\ufff3\013\ufff3\001\002\000" +
    "\016\004\uffed\005\uffed\006\uffed\007\uffed\010\uffed\013\uffed" +
    "\001\002\000\010\012\021\014\015\015\020\001\002\000" +
    "\010\004\ufff9\005\023\006\024\001\002\000\004\002\001" +
    "\001\002\000\010\002\ufffc\003\ufffc\015\ufffc\001\002" });

  /** Access to parse-action table. */
  public short[][] action_table() {return _action_table;}

  /** <code>reduce_goto</code> table. */
  protected static final short[][] _reduce_table = 
    unpackFromStrings(new String[] {
    "\000\042\000\014\002\004\003\011\004\003\005\006\006" +
    "\010\001\001\000\006\005\043\006\010\001\001\000\002" +
    "\001\001\000\002\001\001\000\002\001\001\000\002\001" +
    "\001\000\002\001\001\000\002\001\001\000\002\001\001" +
    "\000\010\007\021\010\015\011\016\001\001\000\002\001" +
    "\001\000\002\001\001\000\004\012\036\001\001\000\002" +
    "\001\001\000\010\007\032\010\015\011\016\001\001\000" +
    "\002\001\001\000\006\010\031\011\016\001\001\000\006" +
    "\010\024\011\016\001\001\000\002\001\001\000\004\011" +
    "\030\001\001\000\004\011\027\001\001\000\002\001\001" +
    "\000\002\001\001\000\002\001\001\000\002\001\001\000" +
    "\002\001\001\000\002\001\001\000\002\001\001\000\002" +
    "\001\001\000\002\001\001\000\010\007\041\010\015\011" +
    "\016\001\001\000\002\001\001\000\002\001\001\000\002" +
    "\001\001" });

  /** Access to <code>reduce_goto</code> table. */
  public short[][] reduce_table() {return _reduce_table;}

  /** Instance of action encapsulation class. */
  protected CUP$ExprEParser$actions action_obj;

  /** Action encapsulation object initializer. */
  protected void init_actions()
    {
      action_obj = new CUP$ExprEParser$actions(this);
    }

  /** Invoke a user supplied parse action. */
  public java_cup.runtime.Symbol do_action(
    int                        act_num,
    java_cup.runtime.lr_parser parser,
    java.util.Stack            stack,
    int                        top)
    throws java.lang.Exception
  {
    /* call code in generated class */
    return action_obj.CUP$ExprEParser$do_action(act_num, parser, stack, top);
  }

  /** Indicates start state. */
  public int start_state() {return 0;}
  /** Indicates start production. */
  public int start_production() {return 0;}

  /** <code>EOF</code> Symbol index. */
  public int EOF_sym() {return 0;}

  /** <code>error</code> Symbol index. */
  public int error_sym() {return 1;}


    private boolean success = false;    private boolean debug = true;        protected int _error_sync_size = 1;    public InterRepresent doParse(File src) throws ParserException {        ExprELexer lexer = null;        try {            lexer = new ExprELexer( new FileReader(src) );        } catch (FileNotFoundException e) {            throw new ParserException();        }                setScanner(lexer);        symbolFactory = new DefaultSymbolFactory();        try {            parse();        }catch (Exception e) {            System.err.println("Parser Exception.");            if (debug) {                e.printStackTrace();            }            throw new ParserException();        }                LinkedList<ExprError> errors = action_obj.getErrors();        for (ExprError error : errors) {            System.err.println(error);        }                if (!errors.isEmpty()) {            throw new ParserException();        }                if (action_obj.getAST() == null) {            throw new ParserException();        }                HIR ir = new HIR();        ir.setIR(action_obj.getAST());                success = true;        return ir;    }            public static void main(String[] argv)  throws ParserException{        String srcFileName = "E:/CompilerProj/student/lab/lab3/test/exp_list.txt";  // source file name        File srcFile = new File(srcFileName);                ExprParser parser = new ExprParser();        HIR ir = null;        try {            ir = (HIR)parser.doParse(srcFile);        } catch (ParserException e) {            System.err.println("ParserException");            e.printStackTrace();            System.exit(-1);        }                ASTViewer astviewer = new ASTViewer((ASTNode)ir.getIR(), new GenericPropertyDump());        astviewer.show();    }
}

/** Cup generated class to encapsulate user supplied action code.*/
class CUP$ExprEParser$actions {

    private ASTNode root = null;    private AST ast = AST.newAST(AST.JLS3);        private LinkedList<ExprError> errors = new LinkedList<ExprError>();        public ASTNode getAST() {        return root;    }        public LinkedList<ExprError> getErrors() {        return errors;    }        private void addError(int errNo, String info, int line, int column) {        ExprError error = new ExprError(errNo, info, line, column);        errors.add(error);    }    
  private final ExprEParser parser;

  /** Constructor */
  CUP$ExprEParser$actions(ExprEParser parser) {
    this.parser = parser;
  }

  /** Method with the actual generated action code. */
  public final java_cup.runtime.Symbol CUP$ExprEParser$do_action(
    int                        CUP$ExprEParser$act_num,
    java_cup.runtime.lr_parser CUP$ExprEParser$parser,
    java.util.Stack            CUP$ExprEParser$stack,
    int                        CUP$ExprEParser$top)
    throws java.lang.Exception
    {
      /* Symbol object for return from actions */
      java_cup.runtime.Symbol CUP$ExprEParser$result;

      /* select the action based on the action number */
      switch (CUP$ExprEParser$act_num)
        {
          /*. . . . . . . . . . . . . . . . . . . .*/
          case 20: // nop ::= LPAREN RPAREN 
            {
              Object RESULT =null;
		    
              CUP$ExprEParser$result = parser.getSymbolFactory().newSymbol("nop",8, ((java_cup.runtime.Symbol)CUP$ExprEParser$stack.elementAt(CUP$ExprEParser$top-1)), ((java_cup.runtime.Symbol)CUP$ExprEParser$stack.peek()), RESULT);
            }
          return CUP$ExprEParser$result;

          /*. . . . . . . . . . . . . . . . . . . .*/
          case 19: // factor ::= LPAREN expression RPAREN 
            {
              Expression RESULT =null;
		int eleft = ((java_cup.runtime.Symbol)CUP$ExprEParser$stack.elementAt(CUP$ExprEParser$top-1)).left;		int eright = ((java_cup.runtime.Symbol)CUP$ExprEParser$stack.elementAt(CUP$ExprEParser$top-1)).right;		Expression e = (Expression)((java_cup.runtime.Symbol) CUP$ExprEParser$stack.elementAt(CUP$ExprEParser$top-1)).value;		        ParenthesizedExpression pe = ast.newParenthesizedExpression();        pe.setExpression(e);        RESULT = pe;    
              CUP$ExprEParser$result = parser.getSymbolFactory().newSymbol("factor",7, ((java_cup.runtime.Symbol)CUP$ExprEParser$stack.elementAt(CUP$ExprEParser$top-2)), ((java_cup.runtime.Symbol)CUP$ExprEParser$stack.peek()), RESULT);
            }
          return CUP$ExprEParser$result;

          /*. . . . . . . . . . . . . . . . . . . .*/
          case 18: // factor ::= INTEGER_LITERAL 
            {
              Expression RESULT =null;
		int nleft = ((java_cup.runtime.Symbol)CUP$ExprEParser$stack.peek()).left;		int nright = ((java_cup.runtime.Symbol)CUP$ExprEParser$stack.peek()).right;		Integer n = (Integer)((java_cup.runtime.Symbol) CUP$ExprEParser$stack.peek()).value;		        RESULT = ast.newNumberLiteral(n.toString());    
              CUP$ExprEParser$result = parser.getSymbolFactory().newSymbol("factor",7, ((java_cup.runtime.Symbol)CUP$ExprEParser$stack.peek()), ((java_cup.runtime.Symbol)CUP$ExprEParser$stack.peek()), RESULT);
            }
          return CUP$ExprEParser$result;

          /*. . . . . . . . . . . . . . . . . . . .*/
          case 17: // factor ::= IDENTIFIER 
            {
              Expression RESULT =null;
		int idleft = ((java_cup.runtime.Symbol)CUP$ExprEParser$stack.peek()).left;		int idright = ((java_cup.runtime.Symbol)CUP$ExprEParser$stack.peek()).right;		String id = (String)((java_cup.runtime.Symbol) CUP$ExprEParser$stack.peek()).value;		        RESULT = ast.newSimpleName(id);

⌨️ 快捷键说明

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