程序要求能自动生成AST抽象语法树。
Lab3Main.java
package sch.cauc.edu.token; import edu.ustc.cs.compile.platform.interfaces.InterRepresent; /** * * * Lab3Main * 创建人:xrzhang * 时间:2018年5月25日-上午8:13:05 * @version 1.0.0 * */ public class Lab3Main { public static void main(String[] args) { //String srcFileName="test/expr3.txt"; //String srcFileName="test/expr221.txt"; String srcFileName="test/expr222.txt"; //String srcFileName="test/testown.txt"; SyntaxDirectedTranslation parser=new SyntaxDirectedTranslation(); InterRepresent ir=parser.doParse(srcFileName); ir.showIR(); } }
SyntaxDirectedTranslation.java
package sch.cauc.edu.token; import java.util.LinkedList; import org.eclipse.jdt.core.dom.*; import edu.ustc.cs.compile.platform.interfaces.InterRepresent; import edu.ustc.cs.compile.util.ir.HIRPack; public class SyntaxDirectedTranslation { /*抽象语法树的根节点*/ private AST ast=null; private BlockLexer lexer=null; private Token lookAhead=null; public SyntaxDirectedTranslation() { ast = AST.newAST(AST.JLS3); } public InterRepresent doParse(String filePath){ lexer=new BlockLexer(filePath); Block mainBody = this.parse(); HIRPack ir = new HIRPack(); ir.setIR(mainBody); return ir; } public Token matchToken(TokenType type,String functionName){ if(lookAhead.getType()!=type){ parsingError(type.toString(),functionName); } Token matchedSymbol = lookAhead; lookAhead = lexer.nextToken(); return matchedSymbol; } public void parsingError(String types,String functionName){ System.out.println("Parsing Error! in"+functionName); System.out.println("encounter "+lookAhead.getLexeme()); System.out.println("at line "+lookAhead.getLine()+",column "+lookAhead.getColumn()); System.out.println("while expecting "+types); System.exit(1); } /** * * 调用开始符号对应的方法,进行语法分析 * 方法名:parse * 创建人:xrzhang * 时间:2018年5月16日-上午10:27:14 * 邮件:jmzhang_15_cauc@163.com void * @exception * @since 1.0.0 */ public Block parse() { System.out.println("In parse();-----jmzhang-----"); lookAhead=lexer.nextToken(); Block mainBody=simpleblock(); System.out.println("Parsing Success!"); return mainBody; } /** * * simpleblock = LBRACE sequence RBRACE * B->{S} * 方法名:simpleblock * 创建人:xrzhang * 时间:2018年5月19日-下午6:59:57 * 邮件:jmzhang_15_cauc@163.com void * @exception * @since 1.0.0 */ public Block simpleblock() { System.out.println("In simpleblock();-----jmzhang-----"); if(lookAhead.getType()==TokenType.LBRACKET){ matchToken(TokenType.LBRACKET, "simpleblock"); System.out.println("***********{*********"); LinkedList seq =sequence(); matchToken(TokenType.RBRACKET, "simpleblock"); System.out.println("***********}*********"); Block mainBody=ast.newBlock(); if (seq!=null) { for(int i=0;i<seq.size();i++){ mainBody.statements().add(seq.get(i)); } } return mainBody; }else{ parsingError(TokenType.LBRACKET.toString(), "simpleblock"); return null; } } /** * * sequence=assognmentStatement sequence | * ifStatement sequence | * whileStatement sequence | * epsilon * S->AS | IS |WS | ε * 方法名:Sequence * 创建人:xrzhang * 时间:2018年5月16日-下午8:54:23 * 邮件:jmzhang_15_cauc@163.com void * @exception * @since 1.0.0 */ private LinkedList sequence() { System.out.println("In Sequence();-----jmzhang-----"); if(lookAhead.getType()==TokenType.IDENTIFIER){ ExpressionStatement es=assignmentStatement(); LinkedList seq=sequence(); if (seq==null) { seq=new LinkedList(); } seq.addFirst(es); return seq; }else if (lookAhead.getType()==TokenType.KEY_IF) { IfStatement es=ifStatement(); LinkedList seq=sequence(); if (seq==null) { seq=new LinkedList(); } seq.addFirst(es); return seq; }else if (lookAhead.getType()==TokenType.KEY_WHILE) { System.out.println("In Sequence();-----jmzhang-----WHILE"); WhileStatement es=whileStatement(); LinkedList seq=sequence(); if (seq==null) { seq=new LinkedList(); } seq.addFirst(es); return seq; }else if (lookAhead.getType()==TokenType.RBRACKET) { //match epslon return null; }else { String errorTypes=TokenType.IDENTIFIER.toString()+","+TokenType.RBRACKET.toString(); parsingError(errorTypes, "sequence"); return null; } } /************************************> * @return ************************************/ private WhileStatement whileStatement() { System.out.println("In whileStatement();-----jmzhang-----"); if (lookAhead.getType()==TokenType.KEY_WHILE) { matchToken(TokenType.KEY_WHILE, "whileStatement"); WhileStatement wStatement=ast.newWhileStatement(); System.out.println("***********while*********"); matchToken(TokenType.LPAREN, "whileStatement"); System.out.println("***********(*********"); Expression be=Boolexpression(); wStatement.setExpression(be); matchToken(TokenType.RPAREN, "whileStatement"); System.out.println("***********)*********"); matchToken(TokenType.LBRACKET, "whileStatement"); System.out.println("***********{*********"); Block ifbody=ast.newBlock(); LinkedList seq =sequence(); if (seq!=null) { for(int i=0;i<seq.size();i++){ ifbody.statements().add(seq.get(i)); } } wStatement.setBody(ifbody); matchToken(TokenType.RBRACKET, "whileStatement"); System.out.println("***********}*********"); /*if (seq!=null) { for(int i=0;i<seq.size();i++){ mainBody.statements().add(seq.get(i)); } }*/ return wStatement; }else { String errorTypes=TokenType.KEY_WHILE.toString(); parsingError(errorTypes, "whileStatement"); return null; } } /** * * * 方法名:Boolexpression * 创建人:xrzhang * 时间:2018年5月19日-下午8:58:23 * 邮件:jmzhang_15_cauc@163.com * @return Expression * @exception * @since 1.0.0 */ private Expression Boolexpression() { System.out.println("In Boolexpression();-----jmzhang-----"); if (lookAhead.getType()==TokenType.BOOL_TRUE ||lookAhead.getType()==TokenType.BOOL_FALSE ||lookAhead.getType()==TokenType.LPAREN ||lookAhead.getType()==TokenType.IDENTIFIER ||lookAhead.getType()==TokenType.INTEGER_LITERAL) { InfixExpression infix=ast.newInfixExpression(); Expression left=Boolterm(); Expression right=Boolexpression_1(left); return right; }else { String errorTypes =TokenType.BOOL_TRUE.toString() +","+TokenType.BOOL_FALSE.toString() +","+TokenType.LPAREN.toString() +","+TokenType.IDENTIFIER.toString() +","+TokenType.INTEGER_LITERAL.toString(); parsingError(errorTypes, "Boolexpression"); return null; } } /** * * 方法名:Boolexpression_1 * 创建人:xrzhang * 时间:2018年5月19日-下午9:02:26 * 邮件:jmzhang_15_cauc@163.com * @param left * @return Expression * @exception * @since 1.0.0 */ private Expression Boolexpression_1(Expression left) { System.out.println("In Boolexpression_1();-----jmzhang-----"); if (lookAhead.getType()==TokenType.LOGICAL_OR) { matchToken(TokenType.LOGICAL_OR, "Boolexpression_1"); System.out.println("***********||*********"); Expression right=Boolterm(); InfixExpression infixEx=ast.newInfixExpression(); infixEx.setLeftOperand(left); infixEx.setRightOperand(right); Expression e=Boolexpression_1(infixEx); return e; }else if (lookAhead.getType()==TokenType.RPAREN) { //match epslin //follow(E')={')',','} return left; }else { String errorTypes =TokenType.LOGICAL_OR.toString() +","+TokenType.RPAREN.toString(); parsingError(errorTypes, "Boolexpression_1"); return null; } } /** * * 方法名:Boolterm * 创建人:xrzhang * 时间:2018年5月19日-下午9:03:36 * 邮件:jmzhang_15_cauc@163.com * @return Expression * @exception * @since 1.0.0 */ private Expression Boolterm() { System.out.println("In Boolterm();-----jmzhang-----"); if(lookAhead.getType()==TokenType.BOOL_TRUE ||lookAhead.getType()==TokenType.BOOL_FALSE ||lookAhead.getType()==TokenType.LPAREN ||lookAhead.getType()==TokenType.IDENTIFIER ||lookAhead.getType()==TokenType.INTEGER_LITERAL){ Expression f=Boolfactor(); Expression t=Boolterm_1(f); return t; }else { String errorTypes =TokenType.BOOL_TRUE.toString() +","+TokenType.BOOL_FALSE.toString() +","+TokenType.LPAREN.toString() +","+TokenType.IDENTIFIER.toString() +","+TokenType.INTEGER_LITERAL.toString(); parsingError(errorTypes, "Boolterm"); return null; } } /** * * 方法名:Boolterm_1 * 创建人:xrzhang * 时间:2018年5月19日-下午9:08:20 * 邮件:jmzhang_15_cauc@163.com * @param left * @return Expression * @exception * @since 1.0.0 */ private Expression Boolterm_1(Expression left) { System.out.println("In Boolterm_1();-----jmzhang-----"); if (lookAhead.getType()==TokenType.LOGICAL_AND) { matchToken(TokenType.LOGICAL_AND, "Boolterm_1"); System.out.println("***********&&*********"); Expression right=Boolfactor(); InfixExpression infixEx=ast.newInfixExpression(); infixEx.setOperator(InfixExpression.Operator.AND); infixEx.setLeftOperand(left); infixEx.setRightOperand(right); Expression t=Boolterm_1(infixEx); return t; }else if (lookAhead.getType()==TokenType.LOGICAL_OR ||lookAhead.getType()==TokenType.RPAREN) { //match e[slion //follow(T')={'+','-',')',',') return left; }else { String errorTypes =TokenType.LOGICAL_AND.toString() +","+TokenType.LOGICAL_OR.toString() +","+TokenType.RPAREN.toString(); parsingError(errorTypes, "Boolterm_1"); return null; } } private Expression Boolfactor() { System.out.println("In Boolfactor();-----jmzhang-----"); if (lookAhead.getType()==TokenType.BOOL_TRUE) { Token id=matchToken(TokenType.BOOL_TRUE, "Boolfactor"); System.out.println("***********true*********"); SimpleName sn=ast.newSimpleName(id.getLexeme()); return sn; }else if (lookAhead.getType()==TokenType.BOOL_FALSE) { Token num=matchToken(TokenType.BOOL_FALSE, "Boolfactor"); System.out.println("***********flase*********"); NumberLiteral nl=ast.newNumberLiteral(num.getLexeme()); return nl; }else if (lookAhead.getType()==TokenType.LPAREN|| lookAhead.getType()==TokenType.IDENTIFIER|| lookAhead.getType()==TokenType.INTEGER_LITERAL){ Expression r=relationlExpression(); return r; }else { String errorTypes =TokenType.BOOL_TRUE.toString() +","+TokenType.BOOL_FALSE.toString() +","+TokenType.LPAREN.toString() +","+TokenType.IDENTIFIER.toString() +","+TokenType.INTEGER_LITERAL.toString(); parsingError(errorTypes, "relationlExpressionOperator"); return null; } } private Expression relationlExpression() { System.out.println("In relationlExpression();-----jmzhang-----"); if (lookAhead.getType()==TokenType.LPAREN|| lookAhead.getType()==TokenType.IDENTIFIER|| lookAhead.getType()==TokenType.INTEGER_LITERAL) { InfixExpression infix=ast.newInfixExpression(); Expression left=expression(); infix.setLeftOperand(left); //Expression r=relationlExpressionOperator(); infix.setOperator(relationlExpressionOperator()); Expression right=expression(); infix.setRightOperand(right); return infix; }else { String errorTypes =TokenType.LPAREN.toString() +","+TokenType.IDENTIFIER.toString() +","+TokenType.INTEGER_LITERAL.toString(); parsingError(errorTypes, "relationlExpression"); return null; } } private InfixExpression.Operator relationlExpressionOperator() { System.out.println("In relationlExpressionOperator();-----jmzhang-----"); if (lookAhead.getType()==TokenType.LESS) { matchToken(TokenType.LESS, "relationlExpressionOperator"); System.out.println("***********<*********"); return InfixExpression.Operator.LESS; }else if (lookAhead.getType()==TokenType.GREATER) { matchToken(TokenType.GREATER, "relationlExpressionOperator"); System.out.println("***********>*********"); return InfixExpression.Operator.GREATER; }else if (lookAhead.getType()==TokenType.LESS_EQUAL) { matchToken(TokenType.LESS_EQUAL, "relationlExpressionOperator"); System.out.println("***********<=*********"); return InfixExpression.Operator.LESS_EQUALS; }else if (lookAhead.getType()==TokenType.GREATER_EQUAL) { matchToken(TokenType.GREATER_EQUAL, "relationlExpressionOperator"); System.out.println("***********>=*********"); return InfixExpression.Operator.GREATER_EQUALS; }else if (lookAhead.getType()==TokenType.NOT_EQUAL) { matchToken(TokenType.NOT_EQUAL, "relationlExpressionOperator"); System.out.println("***********!=*********"); return InfixExpression.Operator.NOT_EQUALS; }else if(lookAhead.getType()==TokenType.EQUAL) { matchToken(TokenType.EQUAL, "relationlExpressionOperator"); System.out.println("***********==*********"); return InfixExpression.Operator.EQUALS; }else { String errorTypes =TokenType.LESS.toString() +","+TokenType.GREATER.toString() +","+TokenType.LESS_EQUAL.toString() +","+TokenType.GREATER_EQUAL.toString() +","+TokenType.NOT_EQUAL.toString() +","+TokenType.EQUAL.toString(); parsingError(errorTypes, "relationlExpressionOperator"); return null; } } private IfStatement ifStatement() { System.out.println("In ifStatement();-----jmzhang-----"); if (lookAhead.getType()==TokenType.KEY_IF) { matchToken(TokenType.KEY_IF, "ifStatement"); IfStatement is = ast.newIfStatement(); System.out.println("***********if*********"); matchToken(TokenType.LPAREN, "ifStatement"); System.out.println("***********(*********"); Expression be=Boolexpression(); is.setExpression(be); matchToken(TokenType.RPAREN, "ifStatement"); System.out.println("***********)*********"); matchToken(TokenType.LBRACKET, "ifStatement"); System.out.println("***********{*********"); Block ifbody=ast.newBlock(); LinkedList seq =sequence(); if (seq!=null) { for(int i=0;i<seq.size();i++){ ifbody.statements().add(seq.get(i)); } } is.setThenStatement(ifbody); matchToken(TokenType.RBRACKET, "ifStatement"); System.out.println("***********}*********"); if(lookAhead.getType()==TokenType.KEY_ELSE) { Block o=OptionalElse(); is.setElseStatement(o); } return is; }else { String errorTypes=TokenType.KEY_IF.toString(); parsingError(errorTypes, "ifStatement"); return null; } } private Block OptionalElse() { System.out.println("In OptionalElse();-----jmzhang-----"); if(lookAhead.getType()==TokenType.KEY_ELSE){ System.out.println("In OptionalElse();-----jmzhang-----1"); matchToken(TokenType.KEY_ELSE, "OptionalElse"); IfStatement elIfStatement =ast.newIfStatement(); System.out.println("***********else*********"); matchToken(TokenType.LBRACKET, "OptionalElse"); System.out.println("***********{*********"); Block ifbody=ast.newBlock(); LinkedList seq =sequence(); if (seq!=null) { for(int i=0;i<seq.size();i++){ ifbody.statements().add(seq.get(i)); } } matchToken(TokenType.RBRACKET, "OptionalElse"); System.out.println("***********}*********"); return ifbody; }else if (lookAhead.getType()==TokenType.RBRACKET ||lookAhead.getType()==TokenType.KEY_IF ||lookAhead.getType()==TokenType.KEY_WHILE ||lookAhead.getType()==TokenType.IDENTIFIER) { //match epslion return null; }else { String errorTypes =TokenType.KEY_ELSE.toString() +","+TokenType.RBRACKET.toString() +","+TokenType.KEY_IF.toString() +","+TokenType.KEY_WHILE.toString() +","+TokenType.IDENTIFIER.toString(); parsingError(errorTypes, "OptionalElse"); return null; } } /*************************************<**********************************/ /** * * assignmentStatement =IDENTIFIER ASSIGN expression SEMICOLON * A->id = E; * 方法名:assignmentStatement * 创建人:xrzhang * 时间:2018年5月16日-下午8:56:26 * 邮件:jmzhang_15_cauc@163.com void * @exception * @since 1.0.0 */ private ExpressionStatement assignmentStatement() { System.out.println("In assignmentStatement();-----jmzhang-----"); if (lookAhead.getType()==TokenType.IDENTIFIER) { Token id =matchToken(TokenType.IDENTIFIER, "assignmentStatement"); System.out.println("***********id*********"); matchToken(TokenType.ASSIGN, "assignmentStatement"); System.out.println("***********=*********"); Expression e=expression(); matchToken(TokenType.SEMICOLON, "assignmentStatement"); System.out.println("***********;*********"); SimpleName sn=ast.newSimpleName(id.getLexeme()); Assignment assign=ast.newAssignment(); assign.setLeftHandSide(sn); assign.setOperator(Assignment.Operator.ASSIGN); assign.setRightHandSide(e); ExpressionStatement es=ast.newExpressionStatement(assign); return es; }else { String errorTypes=TokenType.IDENTIFIER.toString(); parsingError(errorTypes, "assignmentStatement"); return null; } } /** * * expression = term expression_1 * E->TE' * 方法名:expression * 创建人:xrzhang * 时间:2018年5月16日-下午9:00:40 * 邮件:jmzhang_15_cauc@163.com void * @exception * @since 1.0.0 */ private Expression expression() { System.out.println("In expression();-----jmzhang-----"); if (lookAhead.getType()==TokenType.IDENTIFIER ||lookAhead.getType()==TokenType.LPAREN ||lookAhead.getType()==TokenType.INTEGER_LITERAL) { Expression left=term(); Expression right=expression_1(left); return right; }else { String errorTypes =TokenType.IDENTIFIER.toString() +","+TokenType.INTEGER_LITERAL.toString() +","+TokenType.LPAREN.toString(); parsingError(errorTypes, "expression"); return null; } } /** * * expression_1=PLUS term expression_1 | MINUS term expression_1 | epslin * E'->TE' | -TE' | ε * 方法名:expression_1 * 创建人:xrzhang * 时间:2018年5月16日-下午9:06:26 * 邮件:jmzhang_15_cauc@163.com void * @exception * @since 1.0.0 */ private Expression expression_1(Expression left) { System.out.println("In expression_1();-----jmzhang-----"); if (lookAhead.getType()==TokenType.PLUS) { matchToken(TokenType.PLUS, "expression_1"); System.out.println("***********+*********"); Expression right=term(); InfixExpression infixEx=ast.newInfixExpression(); infixEx.setLeftOperand(left); infixEx.setRightOperand(right); Expression e=expression_1(infixEx); return e; }else if (lookAhead.getType()==TokenType.MINUS) { matchToken(TokenType.MINUS, "expression_1"); System.out.println("***********-*********"); Expression right=term(); InfixExpression infixEx=ast.newInfixExpression(); infixEx.setOperator(InfixExpression.Operator.MINUS); infixEx.setLeftOperand(left); infixEx.setRightOperand(right); Expression e=expression_1(infixEx); return e; }else if (lookAhead.getType()==TokenType.SEMICOLON ||lookAhead.getType()==TokenType.LESS ||lookAhead.getType()==TokenType.GREATER ||lookAhead.getType()==TokenType.LESS_EQUAL ||lookAhead.getType()==TokenType.GREATER_EQUAL ||lookAhead.getType()==TokenType.NOT_EQUAL ||lookAhead.getType()==TokenType.EQUAL ||lookAhead.getType()==TokenType.LOGICAL_AND ||lookAhead.getType()==TokenType.LOGICAL_OR ||lookAhead.getType()==TokenType.RPAREN) { System.out.println("In expression_1();-----jmzhang-----follow"); //match epslin //follow(E')={')',','} return left; }else { System.out.println("In expression_1();-----jmzhang-----else"); String errorTypes =TokenType.PLUS.toString() +","+TokenType.MINUS.toString() +","+TokenType.SEMICOLON.toString() +","+TokenType.LESS.toString() +","+TokenType.GREATER.toString() +","+TokenType.LESS_EQUAL.toString() +","+TokenType.GREATER_EQUAL.toString() +","+TokenType.NOT_EQUAL.toString() +","+TokenType.EQUAL.toString(); parsingError(errorTypes, "expression_1"); return null; } } /** * * term=factor term_1 * T->FT' * 方法名:term * 创建人:xrzhang * 时间:2018年5月16日-下午9:16:51 * 邮件:jmzhang_15_cauc@163.com void * @exception * @since 1.0.0 */ private Expression term() { System.out.println("In term();-----jmzhang-----"); if(lookAhead.getType()==TokenType.IDENTIFIER ||lookAhead.getType()==TokenType.LPAREN ||lookAhead.getType()==TokenType.INTEGER_LITERAL){ Expression f=factor(); Expression t=term_1(f); return t; }else { String errorTypes =TokenType.IDENTIFIER.toString() +","+TokenType.INTEGER_LITERAL.toString() +","+TokenType.LPAREN.toString(); parsingError(errorTypes, "term"); return null; } } /** * * term_1=MULT factor term_1 | DIV factor term_1 | MOD factor term_1 | epslin * T'->*FT' | /FT' |%FT'|ε * 方法名:term_1 * 创建人:xrzhang * 时间:2018年5月16日-下午9:20:00 * 邮件:jmzhang_15_cauc@163.com void * @exception * @since 1.0.0 */ private Expression term_1(Expression left) { System.out.println("In term_1();-----jmzhang-----"); if (lookAhead.getType()==TokenType.TIMES) { matchToken(TokenType.TIMES, "term_1"); System.out.println("***********乘*********"); Expression right=factor(); InfixExpression infixEx=ast.newInfixExpression(); infixEx.setOperator(InfixExpression.Operator.TIMES); infixEx.setLeftOperand(left); infixEx.setRightOperand(right); Expression t=term_1(infixEx); return t; }else if (lookAhead.getType()==TokenType.DIVIDE) { matchToken(TokenType.DIVIDE, "term_1"); System.out.println("***********/*********"); Expression right=factor(); InfixExpression infixEx=ast.newInfixExpression(); infixEx.setOperator(InfixExpression.Operator.DIVIDE); infixEx.setLeftOperand(left); infixEx.setRightOperand(right); Expression t=term_1(infixEx); return t; }else if (lookAhead.getType()==TokenType.REMAINDER) { matchToken(TokenType.REMAINDER, "term_1"); System.out.println("***********%*********"); Expression right=factor(); InfixExpression infixEx=ast.newInfixExpression(); infixEx.setOperator(InfixExpression.Operator.REMAINDER); infixEx.setLeftOperand(left); infixEx.setRightOperand(right); Expression t=term_1(infixEx); return t; }else if (lookAhead.getType()==TokenType.PLUS ||lookAhead.getType()==TokenType.MINUS ||lookAhead.getType()==TokenType.RPAREN ||lookAhead.getType()==TokenType.SEMICOLON ||lookAhead.getType()==TokenType.LESS ||lookAhead.getType()==TokenType.GREATER ||lookAhead.getType()==TokenType.LESS_EQUAL ||lookAhead.getType()==TokenType.GREATER_EQUAL ||lookAhead.getType()==TokenType.NOT_EQUAL ||lookAhead.getType()==TokenType.EQUAL ||lookAhead.getType()==TokenType.LOGICAL_AND ||lookAhead.getType()==TokenType.LOGICAL_OR ||lookAhead.getType()==TokenType.RPAREN) { //match e[slion //follow(T')={'+','-',')',',') return left; }else { String errorTypes =TokenType.TIMES.toString() +","+TokenType.DIVIDE.toString() +","+TokenType.REMAINDER.toString() +","+TokenType.PLUS.toString() +","+TokenType.MINUS.toString() +","+TokenType.SEMICOLON.toString() +","+TokenType.LESS.toString() +","+TokenType.GREATER.toString() +","+TokenType.LESS_EQUAL.toString() +","+TokenType.GREATER_EQUAL.toString() +","+TokenType.NOT_EQUAL.toString() +","+TokenType.EQUAL.toString() +","+TokenType.LOGICAL_AND.toString() +","+TokenType.LOGICAL_OR.toString(); parsingError(errorTypes, "term_1"); return null; } } /** * * factor = LPAREN expression RPAREN | IDENTIFER|INTEGER_LITERAL * F->(E)|id| number * 方法名:factor * 创建人:xrzhang * 时间:2018年5月16日-下午9:29:47 * 邮件:jmzhang_15_cauc@163.com void * @exception * @since 1.0.0 */ private Expression factor() { System.out.println("In factor();-----jmzhang-----"); if (lookAhead.getType()==TokenType.LPAREN) { matchToken(TokenType.LPAREN, "factor"); System.out.println("***********(*********"); Expression e=expression(); matchToken(TokenType.RPAREN, "factor"); System.out.println("***********)*********"); ParenthesizedExpression pe=ast.newParenthesizedExpression(); pe.setExpression(e); return pe; }else if (lookAhead.getType()==TokenType.IDENTIFIER) { Token id=matchToken(TokenType.IDENTIFIER, "factor"); System.out.println("***********id*********"); SimpleName sn=ast.newSimpleName(id.getLexeme()); return sn; }else if (lookAhead.getType()==TokenType.INTEGER_LITERAL) { Token num=matchToken(TokenType.INTEGER_LITERAL, "factor"); System.out.println("***********int*********"); NumberLiteral nl=ast.newNumberLiteral(num.getLexeme()); return nl; }else { String errorTypes =TokenType.LPAREN.toString() +","+TokenType.IDENTIFIER.toString() +","+TokenType.INTEGER_LITERAL.toString(); parsingError(errorTypes, "factor"); return null; } } }
测试文件expr3.txt
{ i1=14; i2=i1+2*3; i3=i1-5*(i2%2)+5; }
测试文件expr221.txt
{ i1=14; i2=i1+2*3; i3=i1-5*(i2%2)+5; if(4<5){} if(i1==i4&&i2>=20){ i3=i3+1;} else{ i3=i3+2;} }
测试文件expr222.txt
{ m=12;n=21; if(m<n){ t=m;m=n;n=t; } r=1; d=m%n; while(r!=0){m=n;n=r;r=m%n;} }