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

📄 exprparser.java.svn-base

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

//----------------------------------------------------
// The following code was generated by CUP v0.11a beta 20060608
// Mon Apr 07 13:58:14 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 Mon Apr 07 13:58:14 CST 2008
  */
public class ExprParser extends java_cup.runtime.lr_parser implements ParserInterface{

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

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

  /** Constructor which sets the default scanner. */
  public ExprParser(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\021\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\007\003\000" +
    "\002\007\005\000\002\007\005\000\002\010\003\000\002" +
    "\010\005\000\002\010\005\000\002\011\003\000\002\011" +
    "\003\000\002\011\005" });

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

  /** Parse-action table. */
  protected static final short[][] _action_table = 
    unpackFromStrings(new String[] {
    "\000\033\000\006\002\uffff\015\007\001\002\000\006\002" +
    "\ufffe\015\007\001\002\000\004\002\034\001\002\000\006" +
    "\002\ufffd\015\ufffd\001\002\000\004\011\013\001\002\000" +
    "\004\004\012\001\002\000\004\002\000\001\002\000\006" +
    "\002\ufffb\015\ufffb\001\002\000\010\012\020\014\014\015" +
    "\017\001\002\000\016\004\ufff2\005\ufff2\006\ufff2\007\ufff2" +
    "\010\ufff2\013\ufff2\001\002\000\016\004\ufff9\005\ufff9\006" +
    "\ufff9\007\025\010\026\013\ufff9\001\002\000\016\004\ufff6" +
    "\005\ufff6\006\ufff6\007\ufff6\010\ufff6\013\ufff6\001\002\000" +
    "\016\004\ufff3\005\ufff3\006\ufff3\007\ufff3\010\ufff3\013\ufff3" +
    "\001\002\000\010\012\020\014\014\015\017\001\002\000" +
    "\010\004\ufffa\005\022\006\023\001\002\000\010\012\020" +
    "\014\014\015\017\001\002\000\010\012\020\014\014\015" +
    "\017\001\002\000\016\004\ufff7\005\ufff7\006\ufff7\007\025" +
    "\010\026\013\ufff7\001\002\000\010\012\020\014\014\015" +
    "\017\001\002\000\010\012\020\014\014\015\017\001\002" +
    "\000\016\004\ufff4\005\ufff4\006\ufff4\007\ufff4\010\ufff4\013" +
    "\ufff4\001\002\000\016\004\ufff5\005\ufff5\006\ufff5\007\ufff5" +
    "\010\ufff5\013\ufff5\001\002\000\016\004\ufff8\005\ufff8\006" +
    "\ufff8\007\025\010\026\013\ufff8\001\002\000\010\005\022" +
    "\006\023\013\033\001\002\000\016\004\ufff1\005\ufff1\006" +
    "\ufff1\007\ufff1\010\ufff1\013\ufff1\001\002\000\004\002\001" +
    "\001\002\000\006\002\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\033\000\014\002\004\003\010\004\003\005\005\006" +
    "\007\001\001\000\006\005\034\006\007\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\020" +
    "\010\014\011\015\001\001\000\002\001\001\000\002\001" +
    "\001\000\002\001\001\000\002\001\001\000\010\007\031" +
    "\010\014\011\015\001\001\000\002\001\001\000\006\010" +
    "\030\011\015\001\001\000\006\010\023\011\015\001\001" +
    "\000\002\001\001\000\004\011\027\001\001\000\004\011" +
    "\026\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" });

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

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

  /** Action encapsulation object initializer. */
  protected void init_actions()
    {
      action_obj = new CUP$ExprParser$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$ExprParser$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 = false;    public InterRepresent doParse(File src) throws ParserException {		ExprLexer lexer = null;        try {            lexer = new ExprLexer( 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();        }                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$ExprParser$actions {

    private ASTNode root = null;    private AST ast = AST.newAST(AST.JLS3);    public ASTNode getAST() {        return root;    }
  private final ExprParser parser;

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

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

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

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

          /*. . . . . . . . . . . . . . . . . . . .*/
          case 14: // factor ::= IDENTIFIER 
            {
              Expression RESULT =null;

⌨️ 快捷键说明

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