编译原理中间代码生成–java实现

SecureCRT远程连接虚拟机CentOS——NAT模式
2017年12月6日
如何查看Linux是32位还是64位
2017年12月27日

本人博客内编译原理文章的配套资源jar包,包括词法分析,语法分析,中间代码生成,静态语义检查,代码解释执行以及抽象语法树的手动生成:https://download.csdn.net/download/as1072966956/10448935

转载请注明出处。

 

程序要求能自动生成AST抽象语法树。

Lab3Main.java

  1. package sch.cauc.edu.token;
  2.  
  3. import edu.ustc.cs.compile.platform.interfaces.InterRepresent;
  4. /**
  5. *
  6. *
  7. * Lab3Main
  8. * 创建人:xrzhang
  9. * 时间:2018年5月25日-上午8:13:05
  10. * @version 1.0.0
  11. *
  12. */
  13. public
    class
    Lab3Main {
  14.  
  15.     public
    static
    void
    main(String[] args) {
  16.         //String srcFileName=”test/expr3.txt”;
  17.         //String srcFileName=”test/expr221.txt”;
  18.         String srcFileName=“test/expr222.txt”;
  19.         //String srcFileName=”test/testown.txt”;
  20.         SyntaxDirectedTranslation parser=new SyntaxDirectedTranslation();
  21.         InterRepresent ir=parser.doParse(srcFileName);
  22.         ir.showIR();
  23.     }
  24. }

SyntaxDirectedTranslation.java

  1. package sch.cauc.edu.token;
  2. import java.util.LinkedList;
  3.  
  4.  
  5.  
  6. import org.eclipse.jdt.core.dom.*;
  7. import edu.ustc.cs.compile.platform.interfaces.InterRepresent;
  8. import edu.ustc.cs.compile.util.ir.HIRPack;
  9.  
  10. public
    class
    SyntaxDirectedTranslation {
  11.     /*抽象语法树的根节点*/
  12.     private AST ast=null;
  13.     private BlockLexer lexer=null;
  14.     private Token lookAhead=null;
  15.      public
    SyntaxDirectedTranslation() {
  16.         ast = AST.newAST(AST.JLS3);
  17.     }
  18.      public InterRepresent doParse(String filePath){
  19.          lexer=new BlockLexer(filePath);
  20.          Block mainBody = this.parse();
  21.          HIRPack ir = new HIRPack();
  22.          ir.setIR(mainBody);
  23.          return ir;
  24.      }
  25.      public Token matchToken(TokenType type,String functionName){
  26.         
  27.          if(lookAhead.getType()!=type){
  28.              parsingError(type.toString(),functionName);
  29.          }
  30.          Token matchedSymbol = lookAhead;
  31.          lookAhead = lexer.nextToken();
  32.          return matchedSymbol;
  33.      }
  34.      public
    void
    parsingError(String types,String functionName){
  35.             System.out.println(“Parsing Error! in”+functionName);
  36.             System.out.println(“encounter “+lookAhead.getLexeme());
  37.             System.out.println(“at line “+lookAhead.getLine()+“,column “+lookAhead.getColumn());
  38.             System.out.println(“while expecting “+types);
  39.             System.exit(1);
  40.         }
  41.      /**
  42.      *
  43.      * 调用开始符号对应的方法,进行语法分析
  44.      * 方法名:parse
  45.      * 创建人:xrzhang
  46.      * 时间:2018年5月16日-上午10:27:14
  47.      * 邮件:jmzhang_15_cauc@163.com void
  48.      * @exception

  49.      * @since 1.0.0
  50.      */
  51.     public Block parse() {
  52.         System.out.println(“In parse();—–jmzhang—–“);
  53.         lookAhead=lexer.nextToken();
  54.         Block mainBody=simpleblock();
  55.         System.out.println(“Parsing Success!”);
  56.         return mainBody;
  57.     }
  58.     /**
  59.      *
  60.      * simpleblock = LBRACE sequence RBRACE
  61.      * B->{S}
  62.      * 方法名:simpleblock
  63.      * 创建人:xrzhang
  64.      * 时间:2018年5月19日-下午6:59:57
  65.      * 邮件:jmzhang_15_cauc@163.com void
  66.      * @exception

  67.      * @since 1.0.0
  68.      */
  69.     public Block simpleblock() {
  70.         System.out.println(“In simpleblock();—–jmzhang—–“);
  71.         if(lookAhead.getType()==TokenType.LBRACKET){
  72.             matchToken(TokenType.LBRACKET, “simpleblock”);
  73.             System.out.println(“***********{*********”);
  74.             LinkedList seq =sequence();
  75.             matchToken(TokenType.RBRACKET, “simpleblock”);
  76.             System.out.println(“***********}*********”);
  77.             Block mainBody=ast.newBlock();
  78.             if (seq!=null) {
  79.                 for(int i=0;i<seq.size();i++){
  80.                     mainBody.statements().add(seq.get(i));
  81.                 }
  82.             }
  83.             return mainBody;
  84.         }else{
  85.             parsingError(TokenType.LBRACKET.toString(), “simpleblock”);
  86.             return
    null;
  87.         }
  88.     }
  89.     /**
  90.      *
  91.      * sequence=assognmentStatement sequence |
  92.      *             ifStatement sequence |
  93.      *             whileStatement sequence |
  94.      *             epsilon
  95.      * S->AS | IS |WS | ε
  96.      * 方法名:Sequence
  97.      * 创建人:xrzhang
  98.      * 时间:2018年5月16日-下午8:54:23
  99.      * 邮件:jmzhang_15_cauc@163.com void
  100.      * @exception

  101.      * @since 1.0.0
  102.      */
  103.     private LinkedList sequence() {
  104.         System.out.println(“In Sequence();—–jmzhang—–“);
  105.         if(lookAhead.getType()==TokenType.IDENTIFIER){
  106.         
  107.             ExpressionStatement es=assignmentStatement();
  108.             LinkedList seq=sequence();
  109.             if (seq==null) {
  110.                 seq=new LinkedList();
  111.                 }
  112.             seq.addFirst(es);
  113.             return seq;
  114.         }else
    if (lookAhead.getType()==TokenType.KEY_IF) {
  115.             
  116.             IfStatement es=ifStatement();
  117.             LinkedList seq=sequence();
  118.             if (seq==null) {
  119.                 seq=new LinkedList();
  120.                 }
  121.             seq.addFirst(es);
  122.             return seq;
  123.         }else
    if (lookAhead.getType()==TokenType.KEY_WHILE) {
  124.             System.out.println(“In Sequence();—–jmzhang—–WHILE”);
  125.             WhileStatement es=whileStatement();
  126.             LinkedList seq=sequence();
  127.             if (seq==null) {
  128.                 seq=new LinkedList();
  129.                 }
  130.             seq.addFirst(es);
  131.             return seq;
  132.         }else
    if (lookAhead.getType()==TokenType.RBRACKET) {
  133.             //match epslon
  134.             return
    null;
  135.         }else {
  136.             String errorTypes=TokenType.IDENTIFIER.toString()+“,”+TokenType.RBRACKET.toString();
  137.             parsingError(errorTypes, “sequence”);
  138.             return
    null;
  139.         }
  140.     }
  141.     /************************************>
  142.      * @return ************************************/
  143.     
     
  144.     
     
  145.     
     
  146.     
     
  147.     
  148.     private WhileStatement whileStatement() {
  149.         System.out.println(“In whileStatement();—–jmzhang—–“);
  150.         if (lookAhead.getType()==TokenType.KEY_WHILE) {
  151.             matchToken(TokenType.KEY_WHILE, “whileStatement”);
  152.             WhileStatement wStatement=ast.newWhileStatement();
  153.             
  154.             System.out.println(“***********while*********”);
  155.             matchToken(TokenType.LPAREN, “whileStatement”);
  156.             System.out.println(“***********(*********”);
  157.             Expression be=Boolexpression();
  158.             wStatement.setExpression(be);
  159.             matchToken(TokenType.RPAREN, “whileStatement”);
  160.             System.out.println(“***********)*********”);
  161.             matchToken(TokenType.LBRACKET, “whileStatement”);
  162.             System.out.println(“***********{*********”);
  163.             
  164.             Block ifbody=ast.newBlock();
  165.             LinkedList seq =sequence();
  166.             if (seq!=null) {
  167.                 for(int i=0;i<seq.size();i++){
  168.                     ifbody.statements().add(seq.get(i));
  169.                 }
  170.             }
  171.             wStatement.setBody(ifbody);
  172.             matchToken(TokenType.RBRACKET, “whileStatement”);
  173.             System.out.println(“***********}*********”);
  174.             
  175.             /*if (seq!=null) {
  176.                 for(int i=0;i<seq.size();i++){
  177.                     mainBody.statements().add(seq.get(i));
  178.                 }
  179.             }*/
  180.             return wStatement;
  181.         }else {
  182.             String errorTypes=TokenType.KEY_WHILE.toString();
  183.             parsingError(errorTypes, “whileStatement”);
  184.             return
    null;
  185.         }
  186.     }
  187.     /**
  188.      *
  189.      *
  190.      * 方法名:Boolexpression
  191.      * 创建人:xrzhang
  192.      * 时间:2018年5月19日-下午8:58:23
  193.      * 邮件:jmzhang_15_cauc@163.com
  194.      * @return Expression
  195.      * @exception

  196.      * @since 1.0.0
  197.      */
  198.     private Expression Boolexpression() {
  199.         System.out.println(“In Boolexpression();—–jmzhang—–“);
  200.         if (lookAhead.getType()==TokenType.BOOL_TRUE
  201.                 ||lookAhead.getType()==TokenType.BOOL_FALSE
  202.                 ||lookAhead.getType()==TokenType.LPAREN
  203.                 ||lookAhead.getType()==TokenType.IDENTIFIER
  204.                 ||lookAhead.getType()==TokenType.INTEGER_LITERAL) {
  205.             InfixExpression infix=ast.newInfixExpression();
  206.             
  207.             Expression left=Boolterm();
  208.             Expression right=Boolexpression_1(left);
  209.             return right;
  210.         }else {
  211.             String errorTypes =TokenType.BOOL_TRUE.toString()
  212.                     +“,”+TokenType.BOOL_FALSE.toString()
  213.                     +“,”+TokenType.LPAREN.toString()
  214.                     +“,”+TokenType.IDENTIFIER.toString()
  215.                     +“,”+TokenType.INTEGER_LITERAL.toString();
  216.             parsingError(errorTypes, “Boolexpression”);
  217.             return
    null;
  218.         }    
  219.     }
  220.     /**
  221.      *
  222.      * 方法名:Boolexpression_1
  223.      * 创建人:xrzhang
  224.      * 时间:2018年5月19日-下午9:02:26
  225.      * 邮件:jmzhang_15_cauc@163.com
  226.      * @param left
  227.      * @return Expression
  228.      * @exception

  229.      * @since 1.0.0
  230.      */
  231.     private Expression Boolexpression_1(Expression left) {
  232.         System.out.println(“In Boolexpression_1();—–jmzhang—–“);
  233.         if (lookAhead.getType()==TokenType.LOGICAL_OR) {
  234.             matchToken(TokenType.LOGICAL_OR, “Boolexpression_1”);
  235.             System.out.println(“***********||*********”);
  236.             Expression right=Boolterm();
  237.             InfixExpression infixEx=ast.newInfixExpression();
  238.             infixEx.setLeftOperand(left);
  239.             infixEx.setRightOperand(right);
  240.             Expression e=Boolexpression_1(infixEx);
  241.             return e;
  242.         }else
    if (lookAhead.getType()==TokenType.RPAREN) {
  243.             //match epslin
  244.             //follow(E’)={‘)’,’,’}
  245.             return left;
  246.         }else {
  247.             String errorTypes =TokenType.LOGICAL_OR.toString()
  248.                     +“,”+TokenType.RPAREN.toString();
  249.             parsingError(errorTypes, “Boolexpression_1”);
  250.             return
    null;
  251.         }    
  252.     }
  253.     /**
  254.      *
  255.      * 方法名:Boolterm
  256.      * 创建人:xrzhang
  257.      * 时间:2018年5月19日-下午9:03:36
  258.      * 邮件:jmzhang_15_cauc@163.com
  259.      * @return Expression
  260.      * @exception

  261.      * @since 1.0.0
  262.      */
  263.     private Expression Boolterm() {
  264.         System.out.println(“In Boolterm();—–jmzhang—–“);
  265.         if(lookAhead.getType()==TokenType.BOOL_TRUE
  266.                 ||lookAhead.getType()==TokenType.BOOL_FALSE
  267.                 ||lookAhead.getType()==TokenType.LPAREN
  268.                 ||lookAhead.getType()==TokenType.IDENTIFIER
  269.                 ||lookAhead.getType()==TokenType.INTEGER_LITERAL){
  270.             Expression f=Boolfactor();
  271.             Expression t=Boolterm_1(f);
  272.             return t;
  273.         }else {
  274.             String errorTypes =TokenType.BOOL_TRUE.toString()
  275.                     +“,”+TokenType.BOOL_FALSE.toString()
  276.                     +“,”+TokenType.LPAREN.toString()
  277.                     +“,”+TokenType.IDENTIFIER.toString()
  278.                     +“,”+TokenType.INTEGER_LITERAL.toString();
  279.             parsingError(errorTypes, “Boolterm”);
  280.             return
    null;
  281.         }
  282.     }
  283.     /**
  284.      *
  285.      * 方法名:Boolterm_1
  286.      * 创建人:xrzhang
  287.      * 时间:2018年5月19日-下午9:08:20
  288.      * 邮件:jmzhang_15_cauc@163.com
  289.      * @param left
  290.      * @return Expression
  291.      * @exception

  292.      * @since 1.0.0
  293.      */
  294.     private Expression Boolterm_1(Expression left) {
  295.         System.out.println(“In Boolterm_1();—–jmzhang—–“);
  296.         if (lookAhead.getType()==TokenType.LOGICAL_AND) {
  297.             matchToken(TokenType.LOGICAL_AND, “Boolterm_1”);
  298.             System.out.println(“***********&&*********”);
  299.             Expression right=Boolfactor();
  300.             InfixExpression infixEx=ast.newInfixExpression();
  301.             infixEx.setOperator(InfixExpression.Operator.AND);
  302.             infixEx.setLeftOperand(left);
  303.             infixEx.setRightOperand(right);
  304.             Expression t=Boolterm_1(infixEx);
  305.             return t;
  306.         }else
    if (lookAhead.getType()==TokenType.LOGICAL_OR
  307.                 ||lookAhead.getType()==TokenType.RPAREN) {
  308.             //match e[slion
  309.             //follow(T’)={‘+’,’-‘,’)’,’,’)
  310.             return left;
  311.         }else {
  312.             String errorTypes =TokenType.LOGICAL_AND.toString()
  313.                     +“,”+TokenType.LOGICAL_OR.toString()
  314.                     +“,”+TokenType.RPAREN.toString();
  315.             parsingError(errorTypes, “Boolterm_1”);
  316.             return
    null;
  317.         }
  318.         
  319.         
  320.     }
  321.  
  322.     private Expression Boolfactor() {
  323.         System.out.println(“In Boolfactor();—–jmzhang—–“);
  324.         if (lookAhead.getType()==TokenType.BOOL_TRUE) {
  325.             Token id=matchToken(TokenType.BOOL_TRUE, “Boolfactor”);
  326.             System.out.println(“***********true*********”);
  327.             SimpleName sn=ast.newSimpleName(id.getLexeme());
  328.             return sn;
  329.             
  330.         }else
    if (lookAhead.getType()==TokenType.BOOL_FALSE) {
  331.             
  332.             Token num=matchToken(TokenType.BOOL_FALSE, “Boolfactor”);
  333.             System.out.println(“***********flase*********”);
  334.             NumberLiteral nl=ast.newNumberLiteral(num.getLexeme());
  335.             return nl;
  336.         }else
    if (lookAhead.getType()==TokenType.LPAREN||
  337.                 lookAhead.getType()==TokenType.IDENTIFIER||
  338.                 lookAhead.getType()==TokenType.INTEGER_LITERAL){
  339.             Expression r=relationlExpression();
  340.             return r;
  341.         }else {
  342.             String errorTypes =TokenType.BOOL_TRUE.toString()
  343.                     +“,”+TokenType.BOOL_FALSE.toString()
  344.                     +“,”+TokenType.LPAREN.toString()
  345.                     +“,”+TokenType.IDENTIFIER.toString()
  346.                     +“,”+TokenType.INTEGER_LITERAL.toString();
  347.             parsingError(errorTypes, “relationlExpressionOperator”);
  348.             return
    null;
  349.         }        
  350.     }
  351.  
  352.     private Expression relationlExpression() {
  353.         System.out.println(“In relationlExpression();—–jmzhang—–“);
  354.         
  355.         if (lookAhead.getType()==TokenType.LPAREN||
  356.                 lookAhead.getType()==TokenType.IDENTIFIER||
  357.                 lookAhead.getType()==TokenType.INTEGER_LITERAL) {
  358.             InfixExpression infix=ast.newInfixExpression();
  359.             Expression left=expression();
  360.             infix.setLeftOperand(left);
  361.             //Expression r=relationlExpressionOperator();
  362.             infix.setOperator(relationlExpressionOperator());
  363.             Expression right=expression();
  364.             infix.setRightOperand(right);
  365.             return infix;
  366.         }else {
  367.             String errorTypes =TokenType.LPAREN.toString()
  368.                     +“,”+TokenType.IDENTIFIER.toString()
  369.                     +“,”+TokenType.INTEGER_LITERAL.toString();
  370.             parsingError(errorTypes, “relationlExpression”);
  371.             return
    null;
  372.         }
  373.     }
  374.  
  375.     private InfixExpression.Operator relationlExpressionOperator() {
  376.         System.out.println(“In relationlExpressionOperator();—–jmzhang—–“);
  377.         if (lookAhead.getType()==TokenType.LESS) {
  378.             matchToken(TokenType.LESS, “relationlExpressionOperator”);
  379.             System.out.println(“***********<*********”);
  380.             return InfixExpression.Operator.LESS;
  381.         }else
    if (lookAhead.getType()==TokenType.GREATER) {
  382.             matchToken(TokenType.GREATER, “relationlExpressionOperator”);
  383.             System.out.println(“***********>*********”);
  384.             return InfixExpression.Operator.GREATER;
  385.         }else
    if (lookAhead.getType()==TokenType.LESS_EQUAL) {
  386.             matchToken(TokenType.LESS_EQUAL, “relationlExpressionOperator”);
  387.             System.out.println(“***********<=*********”);
  388.             return InfixExpression.Operator.LESS_EQUALS;
  389.         }else
    if (lookAhead.getType()==TokenType.GREATER_EQUAL) {
  390.             matchToken(TokenType.GREATER_EQUAL, “relationlExpressionOperator”);
  391.             System.out.println(“***********>=*********”);
  392.             return InfixExpression.Operator.GREATER_EQUALS;
  393.         }else
    if (lookAhead.getType()==TokenType.NOT_EQUAL) {
  394.             matchToken(TokenType.NOT_EQUAL, “relationlExpressionOperator”);
  395.             System.out.println(“***********!=*********”);
  396.             return InfixExpression.Operator.NOT_EQUALS;
  397.         }else
    if(lookAhead.getType()==TokenType.EQUAL) {
  398.             matchToken(TokenType.EQUAL, “relationlExpressionOperator”);
  399.             System.out.println(“***********==*********”);
  400.             return InfixExpression.Operator.EQUALS;
  401.         }else {
  402.             String errorTypes =TokenType.LESS.toString()
  403.                     +“,”+TokenType.GREATER.toString()
  404.                     +“,”+TokenType.LESS_EQUAL.toString()
  405.                     +“,”+TokenType.GREATER_EQUAL.toString()
  406.                     +“,”+TokenType.NOT_EQUAL.toString()
  407.                     +“,”+TokenType.EQUAL.toString();
  408.             parsingError(errorTypes, “relationlExpressionOperator”);
  409.             return
    null;
  410.         }
  411.     }
  412.  
  413.     private IfStatement ifStatement() {
  414.         System.out.println(“In ifStatement();—–jmzhang—–“);
  415.         if (lookAhead.getType()==TokenType.KEY_IF) {
  416.             
  417.             matchToken(TokenType.KEY_IF, “ifStatement”);
  418.             IfStatement is = ast.newIfStatement();
  419.             System.out.println(“***********if*********”);
  420.             matchToken(TokenType.LPAREN, “ifStatement”);
  421.             System.out.println(“***********(*********”);
  422.             
  423.             Expression be=Boolexpression();
  424.             is.setExpression(be);
  425.             matchToken(TokenType.RPAREN, “ifStatement”);
  426.             System.out.println(“***********)*********”);
  427.             matchToken(TokenType.LBRACKET, “ifStatement”);
  428.             System.out.println(“***********{*********”);
  429.             
  430.             Block ifbody=ast.newBlock();
  431.             LinkedList seq =sequence();
  432.             if (seq!=null) {
  433.                 for(int i=0;i<seq.size();i++){
  434.                     ifbody.statements().add(seq.get(i));
  435.                 }
  436.             }
  437.             
  438.             is.setThenStatement(ifbody);
  439.             matchToken(TokenType.RBRACKET, “ifStatement”);
  440.             System.out.println(“***********}*********”);
  441.             if(lookAhead.getType()==TokenType.KEY_ELSE)
  442.             {
  443.                 
  444.                 Block o=OptionalElse();
  445.                 is.setElseStatement(o);
  446.             }
  447.             return is;
  448.         }else {
  449.             String errorTypes=TokenType.KEY_IF.toString();
  450.             parsingError(errorTypes, “ifStatement”);
  451.             return
    null;
  452.         }
  453.     }
  454.  
  455.     private Block OptionalElse() {
  456.         System.out.println(“In OptionalElse();—–jmzhang—–“);
  457.         if(lookAhead.getType()==TokenType.KEY_ELSE){
  458.             System.out.println(“In OptionalElse();—–jmzhang—–1”);
  459.             matchToken(TokenType.KEY_ELSE, “OptionalElse”);
  460.             IfStatement elIfStatement =ast.newIfStatement();
  461.             System.out.println(“***********else*********”);
  462.             matchToken(TokenType.LBRACKET, “OptionalElse”);
  463.             System.out.println(“***********{*********”);
  464.             Block ifbody=ast.newBlock();
  465.             LinkedList seq =sequence();
  466.             if (seq!=null) {
  467.                 for(int i=0;i<seq.size();i++){
  468.                     ifbody.statements().add(seq.get(i));
  469.                 }
  470.             }
  471.             
     
  472.             
     
  473.         
     
  474.             
  475.             matchToken(TokenType.RBRACKET, “OptionalElse”);
  476.             System.out.println(“***********}*********”);
  477.             return ifbody;
  478.         }else
    if (lookAhead.getType()==TokenType.RBRACKET
  479.                 ||lookAhead.getType()==TokenType.KEY_IF
  480.                 ||lookAhead.getType()==TokenType.KEY_WHILE
  481.                 ||lookAhead.getType()==TokenType.IDENTIFIER) {
  482.             //match epslion
  483.             return
    null;
  484.         }else {
  485.             String errorTypes =TokenType.KEY_ELSE.toString()
  486.                     +“,”+TokenType.RBRACKET.toString()
  487.                     +“,”+TokenType.KEY_IF.toString()
  488.                     +“,”+TokenType.KEY_WHILE.toString()
  489.                     +“,”+TokenType.IDENTIFIER.toString();
  490.             parsingError(errorTypes, “OptionalElse”);
  491.             return
    null;
  492.         }
  493.         
  494.     }
  495.     
     
  496.     
     
  497.     
     
  498.     
     
  499.     
     
  500.     
     
  501.     
     
  502.     
     
  503.     
  504.     /*************************************<**********************************/
  505.     /**
  506.      *
  507.      * assignmentStatement =IDENTIFIER ASSIGN expression SEMICOLON
  508.      * A->id = E;
  509.      * 方法名:assignmentStatement
  510.      * 创建人:xrzhang
  511.      * 时间:2018年5月16日-下午8:56:26
  512.      * 邮件:jmzhang_15_cauc@163.com void
  513.      * @exception

  514.      * @since 1.0.0
  515.      */
  516.     private ExpressionStatement assignmentStatement() {
  517.         System.out.println(“In assignmentStatement();—–jmzhang—–“);
  518.         if (lookAhead.getType()==TokenType.IDENTIFIER) {
  519.             
  520.             Token id =matchToken(TokenType.IDENTIFIER, “assignmentStatement”);
  521.             System.out.println(“***********id*********”);
  522.             matchToken(TokenType.ASSIGN, “assignmentStatement”);
  523.             System.out.println(“***********=*********”);
  524.             Expression e=expression();
  525.             matchToken(TokenType.SEMICOLON, “assignmentStatement”);
  526.             System.out.println(“***********;*********”);
  527.             SimpleName sn=ast.newSimpleName(id.getLexeme());
  528.             Assignment assign=ast.newAssignment();
  529.             assign.setLeftHandSide(sn);
  530.             assign.setOperator(Assignment.Operator.ASSIGN);
  531.             assign.setRightHandSide(e);
  532.             ExpressionStatement es=ast.newExpressionStatement(assign);
  533.             return es;
  534.         }else {
  535.             String errorTypes=TokenType.IDENTIFIER.toString();
  536.             parsingError(errorTypes, “assignmentStatement”);
  537.             return
    null;
  538.         }
  539.     }
  540.     /**
  541.      *
  542.      * expression = term expression_1
  543.      * E->TE’
  544.      * 方法名:expression
  545.      * 创建人:xrzhang
  546.      * 时间:2018年5月16日-下午9:00:40
  547.      * 邮件:jmzhang_15_cauc@163.com void
  548.      * @exception

  549.      * @since 1.0.0
  550.      */
  551.     private Expression expression() {
  552.         System.out.println(“In expression();—–jmzhang—–“);
  553.         if (lookAhead.getType()==TokenType.IDENTIFIER
  554.                 ||lookAhead.getType()==TokenType.LPAREN
  555.                 ||lookAhead.getType()==TokenType.INTEGER_LITERAL) {
  556.             Expression left=term();
  557.             Expression right=expression_1(left);
  558.             return right;
  559.         }else {
  560.             String errorTypes =TokenType.IDENTIFIER.toString()
  561.                     +“,”+TokenType.INTEGER_LITERAL.toString()
  562.                     +“,”+TokenType.LPAREN.toString();
  563.             parsingError(errorTypes, “expression”);
  564.             return
    null;
  565.         }
  566.     }
  567.     /**
  568.      *
  569.      * expression_1=PLUS term expression_1 | MINUS term expression_1 | epslin
  570.      * E’->TE’ | -TE’ | ε
  571.      * 方法名:expression_1
  572.      * 创建人:xrzhang
  573.      * 时间:2018年5月16日-下午9:06:26
  574.      * 邮件:jmzhang_15_cauc@163.com void
  575.      * @exception

  576.      * @since 1.0.0
  577.      */
  578.     private Expression expression_1(Expression left) {
  579.         System.out.println(“In expression_1();—–jmzhang—–“);
  580.         if (lookAhead.getType()==TokenType.PLUS) {
  581.             matchToken(TokenType.PLUS, “expression_1”);
  582.             System.out.println(“***********+*********”);
  583.             Expression right=term();
  584.             InfixExpression infixEx=ast.newInfixExpression();
  585.             infixEx.setLeftOperand(left);
  586.             infixEx.setRightOperand(right);
  587.             
  588.             Expression e=expression_1(infixEx);
  589.             return e;
  590.         }else
    if (lookAhead.getType()==TokenType.MINUS) {
  591.             
  592.             matchToken(TokenType.MINUS, “expression_1”);
  593.             System.out.println(“***********-*********”);
  594.             Expression right=term();
  595.             InfixExpression infixEx=ast.newInfixExpression();
  596.             infixEx.setOperator(InfixExpression.Operator.MINUS);
  597.             infixEx.setLeftOperand(left);
  598.             infixEx.setRightOperand(right);
  599.             Expression e=expression_1(infixEx);
  600.             return e;
  601.         }else
    if (lookAhead.getType()==TokenType.SEMICOLON
  602.                 ||lookAhead.getType()==TokenType.LESS
  603.                 ||lookAhead.getType()==TokenType.GREATER
  604.                 ||lookAhead.getType()==TokenType.LESS_EQUAL
  605.                 ||lookAhead.getType()==TokenType.GREATER_EQUAL
  606.                 ||lookAhead.getType()==TokenType.NOT_EQUAL
  607.                 ||lookAhead.getType()==TokenType.EQUAL
  608.                 ||lookAhead.getType()==TokenType.LOGICAL_AND
  609.                 ||lookAhead.getType()==TokenType.LOGICAL_OR
  610.                 ||lookAhead.getType()==TokenType.RPAREN) {
  611.             System.out.println(“In expression_1();—–jmzhang—–follow”);
  612.             //match epslin
  613.             //follow(E’)={‘)’,’,’}
  614.             return left;
  615.         }else {
  616.             System.out.println(“In expression_1();—–jmzhang—–else”);
  617.             String errorTypes =TokenType.PLUS.toString()
  618.                     +“,”+TokenType.MINUS.toString()
  619.                     +“,”+TokenType.SEMICOLON.toString()
  620.                     +“,”+TokenType.LESS.toString()
  621.                     +“,”+TokenType.GREATER.toString()
  622.                     +“,”+TokenType.LESS_EQUAL.toString()
  623.                     +“,”+TokenType.GREATER_EQUAL.toString()
  624.                     +“,”+TokenType.NOT_EQUAL.toString()
  625.                     +“,”+TokenType.EQUAL.toString();
  626.             parsingError(errorTypes, “expression_1”);
  627.             return
    null;
  628.         }
  629.     }
  630.     /**
  631.      *
  632.      * term=factor term_1
  633.      * T->FT’
  634.      * 方法名:term
  635.      * 创建人:xrzhang
  636.      * 时间:2018年5月16日-下午9:16:51
  637.      * 邮件:jmzhang_15_cauc@163.com void
  638.      * @exception

  639.      * @since 1.0.0
  640.      */
  641.     private Expression term() {
  642.         System.out.println(“In term();—–jmzhang—–“);
  643.         if(lookAhead.getType()==TokenType.IDENTIFIER
  644.                 ||lookAhead.getType()==TokenType.LPAREN
  645.                 ||lookAhead.getType()==TokenType.INTEGER_LITERAL){
  646.             Expression f=factor();
  647.             Expression t=term_1(f);
  648.             return t;
  649.         }else {
  650.             String errorTypes =TokenType.IDENTIFIER.toString()
  651.                     +“,”+TokenType.INTEGER_LITERAL.toString()
  652.                     +“,”+TokenType.LPAREN.toString();
  653.             parsingError(errorTypes, “term”);
  654.             return
    null;
  655.         }
  656.     }
  657.     /**
  658.      *
  659.      * term_1=MULT factor term_1 | DIV factor term_1 | MOD factor term_1 | epslin
  660.      * T’->*FT’ | /FT’ |%FT’|ε
  661.      * 方法名:term_1
  662.      * 创建人:xrzhang
  663.      * 时间:2018年5月16日-下午9:20:00
  664.      * 邮件:jmzhang_15_cauc@163.com void
  665.      * @exception

  666.      * @since 1.0.0
  667.      */
  668.     private Expression term_1(Expression left) {
  669.         System.out.println(“In term_1();—–jmzhang—–“);
  670.         if (lookAhead.getType()==TokenType.TIMES) {
  671.             matchToken(TokenType.TIMES, “term_1”);
  672.             System.out.println(“***********乘*********”);
  673.             Expression right=factor();
  674.             InfixExpression infixEx=ast.newInfixExpression();
  675.             infixEx.setOperator(InfixExpression.Operator.TIMES);
  676.             infixEx.setLeftOperand(left);
  677.             infixEx.setRightOperand(right);
  678.             Expression t=term_1(infixEx);
  679.             return t;
  680.         }else
    if (lookAhead.getType()==TokenType.DIVIDE) {
  681.             matchToken(TokenType.DIVIDE, “term_1”);
  682.             System.out.println(“***********/*********”);
  683.             Expression right=factor();
  684.             InfixExpression infixEx=ast.newInfixExpression();
  685.             infixEx.setOperator(InfixExpression.Operator.DIVIDE);
  686.             infixEx.setLeftOperand(left);
  687.             infixEx.setRightOperand(right);
  688.             Expression t=term_1(infixEx);
  689.             return t;
  690.         }else
    if (lookAhead.getType()==TokenType.REMAINDER) {
  691.             matchToken(TokenType.REMAINDER, “term_1”);
  692.             System.out.println(“***********%*********”);
  693.             Expression right=factor();
  694.             InfixExpression infixEx=ast.newInfixExpression();
  695.             infixEx.setOperator(InfixExpression.Operator.REMAINDER);
  696.             infixEx.setLeftOperand(left);
  697.             infixEx.setRightOperand(right);
  698.             Expression t=term_1(infixEx);
  699.             return t;
  700.         }else
    if (lookAhead.getType()==TokenType.PLUS
  701.                 ||lookAhead.getType()==TokenType.MINUS
  702.                 ||lookAhead.getType()==TokenType.RPAREN
  703.                 ||lookAhead.getType()==TokenType.SEMICOLON
  704.                 ||lookAhead.getType()==TokenType.LESS
  705.                 ||lookAhead.getType()==TokenType.GREATER
  706.                 ||lookAhead.getType()==TokenType.LESS_EQUAL
  707.                 ||lookAhead.getType()==TokenType.GREATER_EQUAL
  708.                 ||lookAhead.getType()==TokenType.NOT_EQUAL
  709.                 ||lookAhead.getType()==TokenType.EQUAL
  710.                 ||lookAhead.getType()==TokenType.LOGICAL_AND
  711.                 ||lookAhead.getType()==TokenType.LOGICAL_OR
  712.                 ||lookAhead.getType()==TokenType.RPAREN) {
  713.             //match e[slion
  714.             //follow(T’)={‘+’,’-‘,’)’,’,’)
  715.             return left;
  716.         }else {
  717.             String errorTypes =TokenType.TIMES.toString()
  718.                     +“,”+TokenType.DIVIDE.toString()
  719.                     +“,”+TokenType.REMAINDER.toString()
  720.                     +“,”+TokenType.PLUS.toString()
  721.                     +“,”+TokenType.MINUS.toString()
  722.                     +“,”+TokenType.SEMICOLON.toString()
  723.                     +“,”+TokenType.LESS.toString()
  724.                     +“,”+TokenType.GREATER.toString()
  725.                     +“,”+TokenType.LESS_EQUAL.toString()
  726.                     +“,”+TokenType.GREATER_EQUAL.toString()
  727.                     +“,”+TokenType.NOT_EQUAL.toString()
  728.                     +“,”+TokenType.EQUAL.toString()
  729.                     +“,”+TokenType.LOGICAL_AND.toString()
  730.                     +“,”+TokenType.LOGICAL_OR.toString();
  731.             parsingError(errorTypes, “term_1”);
  732.             return
    null;
  733.         }
  734.     }
  735.     /**
  736.      *
  737.      * factor = LPAREN expression RPAREN | IDENTIFER|INTEGER_LITERAL
  738.      * F->(E)|id| number
  739.      * 方法名:factor
  740.      * 创建人:xrzhang
  741.      * 时间:2018年5月16日-下午9:29:47
  742.      * 邮件:jmzhang_15_cauc@163.com void
  743.      * @exception

  744.      * @since 1.0.0
  745.      */
  746.     private Expression factor() {
  747.         System.out.println(“In factor();—–jmzhang—–“);
  748.         if (lookAhead.getType()==TokenType.LPAREN) {
  749.             matchToken(TokenType.LPAREN, “factor”);
  750.             System.out.println(“***********(*********”);
  751.             Expression e=expression();
  752.             matchToken(TokenType.RPAREN, “factor”);
  753.             System.out.println(“***********)*********”);
  754.             ParenthesizedExpression pe=ast.newParenthesizedExpression();
  755.             pe.setExpression(e);
  756.             return pe;
  757.         }else
    if (lookAhead.getType()==TokenType.IDENTIFIER) {
  758.             Token id=matchToken(TokenType.IDENTIFIER, “factor”);
  759.             System.out.println(“***********id*********”);
  760.             SimpleName sn=ast.newSimpleName(id.getLexeme());
  761.             return sn;
  762.         }else
    if (lookAhead.getType()==TokenType.INTEGER_LITERAL) {
  763.             Token num=matchToken(TokenType.INTEGER_LITERAL, “factor”);
  764.             System.out.println(“***********int*********”);
  765.             NumberLiteral nl=ast.newNumberLiteral(num.getLexeme());
  766.             return nl;
  767.         }else {
  768.             String errorTypes =TokenType.LPAREN.toString()
  769.                     +“,”+TokenType.IDENTIFIER.toString()
  770.                     +“,”+TokenType.INTEGER_LITERAL.toString();
  771.             parsingError(errorTypes, “factor”);
  772.             return
    null;
  773.         }
  774.     }
  775. }

 

测试文件expr3.txt

  1. {
  2.     i1=14;
  3.     i2=i1+2*3;
  4.     i3=i1-5*(i2%2)+5;
  5. }

 

测试文件expr221.txt

  1. {
  2.     i1=14;
  3.     i2=i1+2*3;
  4.     i3=i1-5*(i2%2)+5;
  5.     if(4<5){}
  6.     if(i1==i4&&i2>=20){
  7.         i3=i3+1;}
  8.     else{
  9.     i3=i3+2;}
  10. }

 

测试文件expr222.txt

  1. {
  2.     m=12;n=21;
  3.     if(m<n){
  4.         t=m;m=n;n=t;
  5.     }
  6.     r=1;
  7.     d=m%n;
  8.     while(r!=0){m=n;n=r;r=m%n;}
  9.     
  10. }

 


 

发表评论

电子邮件地址不会被公开。 必填项已用*标注