语法分析器——java实现

词法分析器——java实现
2017年5月15日
遍历AST过程中解释执行代码–java实现
2017年5月20日

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

转载请注明出处。

 

部分代码见——词法分析器

expr221.txt

[html] view plain copy

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

 

expr222.txt

[html] view plain copy

  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. }  

 

Lab2Main.java

[java] view plain copy

  1. package sch.cauc.edu.token;  
  2. /** 
  3.  *  
  4.  *  
  5.  * Lab2Main 
  6.  * 创建人:xrzhang  
  7.  * 时间:2018516下午9:37:28  
  8.  * @version 1.0.0 
  9.  * 
  10.  */  
  11. public class Lab2Main {  
  12.     /** 
  13.      *  
  14.      * 方法名:main 
  15.      * 创建人:xrzhang  
  16.      * 时间:2018516下午9:37:34  
  17.      * 邮件:jmzhang_15_cauc@163.com 
  18.      * @param args void 
  19.      * @exception  
  20.      * @since  1.0.0 
  21.      */  
  22.     public static void main(String[] args) {  
  23.         String srcFileName=“test/expr221.txt”;  
  24.         //String srcFileName=”test/expr222.txt”;  
  25.         RecursionDescendParser parser = new RecursionDescendParser();  
  26.         parser.doParse(srcFileName);  
  27.     }  
  28. }  

 

RecursionDescendParser.java

[java] view plain copy

  1. package sch.cauc.edu.token;  
  2.   
     
  3. import java.beans.Expression;  
  4.   
     
  5. import javax.management.timer.TimerMBean;  
  6. import javax.sound.midi.Sequence;  
  7.   
     
  8.   
     
  9. /** 
  10.  *  
  11.  * SimpleBlock语言的递归下降分析 
  12.  * RecursionDescendParser 
  13.  * 创建人:xrzhang  
  14.  * 时间:2018516上午10:14:16  
  15.  * @version 1.0.0 
  16.  * 
  17.  */  
  18. public class RecursionDescendParser {  
  19.       
     
  20.     private BlockLexer lexer=null;  
  21.     private Token lookAhead = null;  
  22.       
     
  23.     public RecursionDescendParser(){  
  24.     }  
  25.   
     
  26.     public void doParse(String filePath){  
  27.         lexer = new BlockLexer(filePath);  
  28.         this.parse();  
  29.     }  
  30.       
     
  31.     public void matchToken(TokenType type,String functionName){  
  32.         System.out.println(“In matchToken();—–jmzhang—–“+lookAhead.getType());  
  33.         if(lookAhead.getType()!=type){  
  34.             parsingError(type.toString(),functionName);   
  35.         }  
  36.         lookAhead =lexer.nextToken();  
  37.     }  
  38.       
     
  39.     public void parsingError(String types,String functionName){  
  40.         System.out.println(“Parsing Error! in”+functionName);  
  41.         System.out.println(“encounter “+lookAhead.getLexeme());  
  42.         System.out.println(“at line “+lookAhead.getLine()+“,column “+lookAhead.getColumn());  
  43.         System.out.println(“while expecting “+types);  
  44.         System.exit(1);  
  45.     }  
  46.       
     
  47.     /** 
  48.      *  
  49.      * 调用开始符号对应的方法,进行语法分析 
  50.      * 方法名:parse 
  51.      * 创建人:xrzhang  
  52.      * 时间:2018516上午10:27:14  
  53.      * 邮件:jmzhang_15_cauc@163.com void 
  54.      * @exception  
  55.      * @since  1.0.0 
  56.      */  
  57.     public void parse() {  
  58.         System.out.println(“In parse();—–jmzhang—–“);  
  59.         lookAhead=lexer.nextToken();  
  60.         simpleblock();  
  61.         System.out.println(“Parsing Success!”);  
  62.     }  
  63.   
     
  64.     public void simpleblock() {  
  65.         System.out.println(“In simpleblock();—–jmzhang—–“);  
  66.         if(lookAhead.getType()==TokenType.LBRACKET){  
  67.             matchToken(TokenType.LBRACKET, “simpleblock”);  
  68.             System.out.println(“In simpleblock();—–jmzhang—–11”);  
  69.             Sequence();  
  70.             System.out.println(“In simpleblock();—–jmzhang—–12”);  
  71.             matchToken(TokenType.RBRACKET, “simpleblock”);  
  72.             System.out.println(“In simpleblock();—–jmzhang—–13”);  
  73.             if(lookAhead.getType()==TokenType.LBRACKET){  
  74.                 simpleblock();  
  75.             }  
  76.         }else{  
  77.             System.out.println(“In simpleblock();—–jmzhang—–2”);  
  78.             parsingError(TokenType.LBRACKET.toString(), “simpleblock”);  
  79.         }  
  80.     }  
  81.     /** 
  82.      *  
  83.      * sequence=assognmentStatement sequence | 
  84.      *          ifStatement sequence | 
  85.      *          whileStatement sequence | 
  86.      *          epsilon 
  87.      * S->AS | IS |WS | ε 
  88.      * 方法名:Sequence 
  89.      * 创建人:xrzhang  
  90.      * 时间:2018516下午8:54:23  
  91.      * 邮件:jmzhang_15_cauc@163.com void 
  92.      * @exception  
  93.      * @since  1.0.0 
  94.      */  
  95.     private void Sequence() {  
  96.         System.out.println(“In Sequence();—–jmzhang—–“);  
  97.         if(lookAhead.getType()==TokenType.IDENTIFIER){  
  98.             System.out.println(“In Sequence();—–jmzhang—–1”);  
  99.             assignmentStatement();  
  100.             Sequence();  
  101.         }else if (lookAhead.getType()==TokenType.KEY_IF) {  
  102.             System.out.println(“In Sequence();—–jmzhang—–2”);  
  103.             ifStatement();  
  104.             Sequence();  
  105.         }else if (lookAhead.getType()==TokenType.KEY_WHILE) {  
  106.             System.out.println(“In Sequence();—–jmzhang—–WHILE”);  
  107.             whileStatement();  
  108.             Sequence();  
  109.         }else if (lookAhead.getType()==TokenType.RBRACKET) {  
  110.             //match epslon  
  111.             System.out.println(“In Sequence();—–jmzhang—–3”);  
  112.         }else {  
  113.             System.out.println(“In Sequence();—–jmzhang—–4”);  
  114.             String errorTypes=TokenType.IDENTIFIER.toString()+“,”+TokenType.RBRACKET.toString();  
  115.             parsingError(errorTypes, “sequence”);  
  116.         }  
  117.     }  
  118.     /** 
  119.      *  
  120.      * 方法名:whileStatement 
  121.      * 创建人:xrzhang  
  122.      * 时间:2018523下午7:31:56  
  123.      * 邮件:jmzhang_15_cauc@163.com void 
  124.      * @exception  
  125.      * @since  1.0.0 
  126.      */  
  127.     private void whileStatement() {  
  128.         if (lookAhead.getType()==TokenType.KEY_WHILE){  
  129.             System.out.println(“In whileStatement();—–jmzhang—–“);  
  130.             matchToken(TokenType.KEY_WHILE, “whileStatement”);  
  131.             matchToken(TokenType.LPAREN, “whileStatement”);  
  132.             Boolexpression();  
  133.             matchToken(TokenType.RPAREN, “whileStatement”);  
  134.             matchToken(TokenType.LBRACKET, “whileStatement”);  
  135.             Sequence();  
  136.             matchToken(TokenType.RBRACKET, “whileStatement”);  
  137.         }else {  
  138.             String errorTypes=TokenType.KEY_WHILE.toString();  
  139.             parsingError(errorTypes, “whileStatement”);  
  140.         }  
  141.           
     
  142.     }  
  143.     /** 
  144.      *  
  145.      * 方法名:Boolexpression 
  146.      * 创建人:xrzhang  
  147.      * 时间:2018523下午7:23:48  
  148.      * 邮件:jmzhang_15_cauc@163.com void 
  149.      * @exception  
  150.      * @since  1.0.0 
  151.      */  
  152.     private void Boolexpression() {  
  153.         System.out.println(“In Boolexpression();—–jmzhang—–“);  
  154.         if (lookAhead.getType()==TokenType.BOOL_TRUE  
  155.                 ||lookAhead.getType()==TokenType.BOOL_FALSE  
  156.                 ||lookAhead.getType()==TokenType.LPAREN  
  157.                 ||lookAhead.getType()==TokenType.IDENTIFIER  
  158.                 ||lookAhead.getType()==TokenType.INTEGER_LITERAL) {  
  159.             Boolterm();  
  160.             Boolexpression_1();  
  161.         }else {  
  162.             String errorTypes =TokenType.BOOL_TRUE.toString()  
  163.                     +“,”+TokenType.BOOL_FALSE.toString()  
  164.                     +“,”+TokenType.LPAREN.toString()  
  165.                     +“,”+TokenType.IDENTIFIER.toString()  
  166.                     +“,”+TokenType.INTEGER_LITERAL.toString();  
  167.             parsingError(errorTypes, “Boolexpression”);  
  168.         }     
  169.     }  
  170.     /** 
  171.      *  
  172.      * 方法名:Boolexpression_1 
  173.      * 创建人:xrzhang  
  174.      * 时间:2018523下午7:22:10  
  175.      * 邮件:jmzhang_15_cauc@163.com void 
  176.      * @exception  
  177.      * @since  1.0.0 
  178.      */  
  179.     private void Boolexpression_1() {  
  180.         System.out.println(“In Boolexpression_1();—–jmzhang—–“);  
  181.         if (lookAhead.getType()==TokenType.LOGICAL_OR) {  
  182.             matchToken(TokenType.LOGICAL_OR, “Boolexpression_1”);  
  183.             Boolterm();  
  184.             Boolexpression_1();  
  185.         }else if (lookAhead.getType()==TokenType.RPAREN) {  
  186.             //match epslin  
  187.             //follow(E’)={‘)’,’,’}  
  188.         }else {  
  189.             String errorTypes =TokenType.LOGICAL_OR.toString()  
  190.                     +“,”+TokenType.RPAREN.toString();  
  191.             parsingError(errorTypes, “Boolexpression_1”);  
  192.         }     
  193.     }  
  194.   
     
  195.     private void Boolterm() {  
  196.         System.out.println(“In Boolterm();—–jmzhang—–“);  
  197.         if(lookAhead.getType()==TokenType.BOOL_TRUE  
  198.                 ||lookAhead.getType()==TokenType.BOOL_FALSE  
  199.                 ||lookAhead.getType()==TokenType.LPAREN  
  200.                 ||lookAhead.getType()==TokenType.IDENTIFIER  
  201.                 ||lookAhead.getType()==TokenType.INTEGER_LITERAL){  
  202.             Boolfactor();  
  203.             Boolterm_1();  
  204.         }else {  
  205.             String errorTypes =TokenType.BOOL_TRUE.toString()  
  206.                     +“,”+TokenType.BOOL_FALSE.toString()  
  207.                     +“,”+TokenType.LPAREN.toString()  
  208.                     +“,”+TokenType.IDENTIFIER.toString()  
  209.                     +“,”+TokenType.INTEGER_LITERAL.toString();  
  210.             parsingError(errorTypes, “Boolterm”);  
  211.         }  
  212.     }  
  213.     /** 
  214.      *  
  215.      * 方法名:Boolterm_1 
  216.      * 创建人:xrzhang  
  217.      * 时间:2018523下午7:12:13  
  218.      * 邮件:jmzhang_15_cauc@163.com void 
  219.      * @exception  
  220.      * @since  1.0.0 
  221.      */  
  222.     private void Boolterm_1() {  
  223.         System.out.println(“In Boolterm_1();—–jmzhang—–“);  
  224.         if (lookAhead.getType()==TokenType.LOGICAL_AND) {  
  225.             matchToken(TokenType.LOGICAL_AND, “Boolterm_1”);  
  226.             Boolfactor();  
  227.             Boolterm_1();  
  228.         }else if (lookAhead.getType()==TokenType.LOGICAL_OR  
  229.                 ||lookAhead.getType()==TokenType.RPAREN) {  
  230.             //match e[slion  
  231.             //follow(T’)={‘+’,’-‘,’)’,’,’)  
  232.         }else {  
  233.             String errorTypes =TokenType.LOGICAL_AND.toString()  
  234.                   
     
  235.                     +“,”+TokenType.LOGICAL_OR.toString()  
  236.                     +“,”+TokenType.RPAREN.toString()  
  237.                     +“,”+TokenType.SEMICOLON.toString();  
  238.             parsingError(errorTypes, “Boolterm_1”);  
  239.         }     
  240.     }  
  241.     /** 
  242.      *  
  243.      * 方法名:Boolfactor 
  244.      * 创建人:xrzhang  
  245.      * 时间:2018523下午7:09:50  
  246.      * 邮件:jmzhang_15_cauc@163.com void 
  247.      * @exception  
  248.      * @since  1.0.0 
  249.      */  
  250.     private void Boolfactor() {  
  251.         System.out.println(“In Boolfactor();—–jmzhang—–“);  
  252.         if (lookAhead.getType()==TokenType.BOOL_TRUE) {  
  253.             matchToken(TokenType.BOOL_TRUE, “Boolfactor”);  
  254.         }else if (lookAhead.getType()==TokenType.BOOL_FALSE) {  
  255.             matchToken(TokenType.BOOL_FALSE, “Boolfactor”);  
  256.         }else if (lookAhead.getType()==TokenType.LPAREN||  
  257.                 lookAhead.getType()==TokenType.IDENTIFIER||  
  258.                 lookAhead.getType()==TokenType.INTEGER_LITERAL){  
  259.             relationlExpression();  
  260.         }else {  
  261.             String errorTypes =TokenType.BOOL_TRUE.toString()  
  262.                     +“,”+TokenType.BOOL_FALSE.toString()  
  263.                     +“,”+TokenType.LPAREN.toString()  
  264.                     +“,”+TokenType.IDENTIFIER.toString()  
  265.                     +“,”+TokenType.INTEGER_LITERAL.toString();  
  266.             parsingError(errorTypes, “relationlExpressionOperator”);  
  267.         }     
  268.     }  
  269.     /** 
  270.      *  
  271.      * 方法名:relationlExpression 
  272.      * 创建人:xrzhang  
  273.      * 时间:2018523下午7:09:41  
  274.      * 邮件:jmzhang_15_cauc@163.com void 
  275.      * @exception  
  276.      * @since  1.0.0 
  277.      */  
  278.     private void relationlExpression() {  
  279.         if (lookAhead.getType()==TokenType.LPAREN||  
  280.                 lookAhead.getType()==TokenType.IDENTIFIER||  
  281.                 lookAhead.getType()==TokenType.INTEGER_LITERAL) {  
  282.             System.out.println(“In relationlExpression();—–jmzhang—–“);  
  283.             expression();  
  284.             relationlExpressionOperator();  
  285.             expression();  
  286.         }else {  
  287.             String errorTypes =TokenType.LPAREN.toString()  
  288.                     +“,”+TokenType.IDENTIFIER.toString()  
  289.                     +“,”+TokenType.INTEGER_LITERAL.toString();  
  290.             parsingError(errorTypes, “relationlExpression”);  
  291.         }  
  292.     }  
  293.     /** 
  294.      *  
  295.      * 方法名:relationlExpressionOperator 
  296.      * 创建人:xrzhang  
  297.      * 时间:2018523下午7:09:30  
  298.      * 邮件:jmzhang_15_cauc@163.com void 
  299.      * @exception  
  300.      * @since  1.0.0 
  301.      */  
  302.     private void relationlExpressionOperator() {  
  303.         System.out.println(“In relationlExpressionOperator();—–jmzhang—–“);  
  304.         if (lookAhead.getType()==TokenType.LESS) {  
  305.             matchToken(TokenType.LESS, “relationlExpressionOperator”);  
  306.         }else if (lookAhead.getType()==TokenType.GREATER) {  
  307.             matchToken(TokenType.GREATER, “relationlExpressionOperator”);  
  308.         }else if (lookAhead.getType()==TokenType.LESS_EQUAL) {  
  309.             matchToken(TokenType.LESS_EQUAL, “relationlExpressionOperator”);  
  310.         }else if (lookAhead.getType()==TokenType.GREATER_EQUAL) {  
  311.             matchToken(TokenType.GREATER_EQUAL, “relationlExpressionOperator”);  
  312.         }else if (lookAhead.getType()==TokenType.NOT_EQUAL) {  
  313.             matchToken(TokenType.NOT_EQUAL, “relationlExpressionOperator”);  
  314.         }else if(lookAhead.getType()==TokenType.EQUAL) {  
  315.             matchToken(TokenType.EQUAL, “relationlExpressionOperator”);  
  316.         }else {  
  317.             String errorTypes =TokenType.LESS.toString()  
  318.                     +“,”+TokenType.GREATER.toString()  
  319.                     +“,”+TokenType.LESS_EQUAL.toString()  
  320.                     +“,”+TokenType.GREATER_EQUAL.toString()  
  321.                     +“,”+TokenType.NOT_EQUAL.toString()  
  322.                     +“,”+TokenType.EQUAL.toString();  
  323.             parsingError(errorTypes, “relationlExpressionOperator”);  
  324.         }  
  325.           
     
  326.     }  
  327.     /** 
  328.      *  
  329.      * 方法名:ifStatement 
  330.      * 创建人:xrzhang  
  331.      * 时间:2018523下午7:29:16  
  332.      * 邮件:jmzhang_15_cauc@163.com void 
  333.      * @exception  
  334.      * @since  1.0.0 
  335.      */  
  336.     private void ifStatement() {  
  337.         System.out.println(“In ifStatement();—–jmzhang—–“);  
  338.         if(lookAhead.getType()==TokenType.KEY_IF){  
  339.             matchToken(TokenType.KEY_IF, “ifStatement”);  
  340.             matchToken(TokenType.LPAREN, “ifStatement”);  
  341.             Boolexpression();  
  342.             matchToken(TokenType.RPAREN, “ifStatement”);  
  343.             matchToken(TokenType.LBRACKET, “ifStatement”);  
  344.             Sequence();  
  345.             matchToken(TokenType.RBRACKET, “ifStatement”);  
  346.             if(lookAhead.getType()==TokenType.KEY_ELSE){  
  347.             OptionalElse();}  
  348.         }else {  
  349.             System.out.println(“In OptionalElse();—–jmzhang—–3”);  
  350.             String errorTypes =TokenType.KEY_IF.toString();  
  351.             parsingError(errorTypes, “ifStatement”);  
  352.         }  
  353.           
     
  354.     }  
  355.     /** 
  356.      * 方法名:OptionalElse 
  357.      * 创建人:xrzhang  
  358.      * 时间:2018523下午7:27:53  
  359.      * 邮件:jmzhang_15_cauc@163.com void 
  360.      * @exception  
  361.      * @since  1.0.0 
  362.      */  
  363.     private void OptionalElse() {  
  364.         System.out.println(“In OptionalElse();—–jmzhang—–“);  
  365.         if(lookAhead.getType()==TokenType.KEY_ELSE){  
  366.             System.out.println(“In OptionalElse();—–jmzhang—–1”);  
  367.             matchToken(TokenType.KEY_ELSE, “OptionalElse”);  
  368.             matchToken(TokenType.LBRACKET, “OptionalElse”);  
  369.             Sequence();  
  370.             matchToken(TokenType.RBRACKET, “OptionalElse”);  
  371.         }else if (lookAhead.getType()==TokenType.RBRACKET  
  372.                 ||lookAhead.getType()==TokenType.KEY_IF  
  373.                 ||lookAhead.getType()==TokenType.KEY_WHILE  
  374.                 ||lookAhead.getType()==TokenType.IDENTIFIER) {  
  375.             //match epslion  
  376.             System.out.println(“In OptionalElse();—–jmzhang—–2”);  
  377.         }else {  
  378.             System.out.println(“In OptionalElse();—–jmzhang—–3”);  
  379.             String errorTypes =TokenType.KEY_ELSE.toString()  
  380.                     +“,”+TokenType.RBRACKET.toString()  
  381.                     +“,”+TokenType.KEY_IF.toString()  
  382.                     +“,”+TokenType.KEY_WHILE.toString()  
  383.                     +“,”+TokenType.IDENTIFIER.toString();  
  384.             parsingError(errorTypes, “OptionalElse”);  
  385.         }  
  386.     }  
  387.   
     
  388.     /** 
  389.      *  
  390.      * assignmentStatement =IDENTIFIER ASSIGN expression SEMICOLON 
  391.      * A->id = E; 
  392.      * 方法名:assignmentStatement 
  393.      * 创建人:xrzhang  
  394.      * 时间:2018516下午8:56:26  
  395.      * 邮件:jmzhang_15_cauc@163.com void 
  396.      * @exception  
  397.      * @since  1.0.0 
  398.      */  
  399.     private void assignmentStatement() {  
  400.         System.out.println(“In assignmentStatement();—–jmzhang—–“);  
  401.         if (lookAhead.getType()==TokenType.IDENTIFIER) {  
  402.             matchToken(TokenType.IDENTIFIER, “assignmentStatement”);  
  403.             matchToken(TokenType.ASSIGN, “assignmentStatement”);  
  404.             expression();  
  405.             matchToken(TokenType.SEMICOLON, “assignmentStatement”);  
  406.         }else {  
  407.             String errorTypes=TokenType.IDENTIFIER.toString();  
  408.             parsingError(errorTypes, “assignmentStatement”);  
  409.         }  
  410.     }  
  411.     /** 
  412.      *  
  413.      * expression = term expression_1 
  414.      * E->TE’ 
  415.      * 方法名:expression 
  416.      * 创建人:xrzhang  
  417.      * 时间:2018516下午9:00:40  
  418.      * 邮件:jmzhang_15_cauc@163.com void 
  419.      * @exception  
  420.      * @since  1.0.0 
  421.      */  
  422.     private void expression() {  
  423.         System.out.println(“In expression();—–jmzhang—–“);  
  424.         if (lookAhead.getType()==TokenType.IDENTIFIER  
  425.                 ||lookAhead.getType()==TokenType.LPAREN  
  426.                 ||lookAhead.getType()==TokenType.INTEGER_LITERAL) {  
  427.             term();  
  428.             expression_1();  
  429.         }else {  
  430.             String errorTypes =TokenType.IDENTIFIER.toString()  
  431.                     +“,”+TokenType.INTEGER_LITERAL.toString()  
  432.                     +“,”+TokenType.LPAREN.toString();  
  433.             parsingError(errorTypes, “expression”);  
  434.         }  
  435.     }  
  436.     /** 
  437.      *  
  438.      * expression_1=PLUS term expression_1 | MINUS term expression_1 | epslin 
  439.      * E’->TE’ | -TE’ | ε 
  440.      * 方法名:expression_1 
  441.      * 创建人:xrzhang  
  442.      * 时间:2018516下午9:06:26  
  443.      * 邮件:jmzhang_15_cauc@163.com void 
  444.      * @exception  
  445.      * @since  1.0.0 
  446.      */  
  447.     private void expression_1() {  
  448.         System.out.println(“In expression_1();—–jmzhang—–“);  
  449.         if (lookAhead.getType()==TokenType.PLUS) {  
  450.             System.out.println(“In expression_1();—–jmzhang—–1”);  
  451.             matchToken(TokenType.PLUS, “expression_1”);  
  452.             term();  
  453.             expression_1();  
  454.         }else if (lookAhead.getType()==TokenType.MINUS) {  
  455.             System.out.println(“In expression_1();—–jmzhang—–2”);  
  456.             matchToken(TokenType.MINUS, “expression_1”);  
  457.             term();  
  458.             expression_1();  
  459.         }else if (lookAhead.getType()==TokenType.SEMICOLON  
  460.                 ||lookAhead.getType()==TokenType.LESS  
  461.                 ||lookAhead.getType()==TokenType.GREATER  
  462.                 ||lookAhead.getType()==TokenType.LESS_EQUAL  
  463.                 ||lookAhead.getType()==TokenType.GREATER_EQUAL  
  464.                 ||lookAhead.getType()==TokenType.NOT_EQUAL  
  465.                 ||lookAhead.getType()==TokenType.EQUAL  
  466.                 ||lookAhead.getType()==TokenType.LOGICAL_AND  
  467.                 ||lookAhead.getType()==TokenType.LOGICAL_OR  
  468.                 ||lookAhead.getType()==TokenType.RPAREN) {  
  469.             System.out.println(“In expression_1();—–jmzhang—–3”);  
  470.             //match epslin  
  471.             //follow(E’)={‘)’,’,’}  
  472.         }else {  
  473.             System.out.println(“In expression_1();—–jmzhang—–4”);  
  474.             String errorTypes =TokenType.PLUS.toString()  
  475.                                 +“,”+TokenType.MINUS.toString()  
  476.                                 +“,”+TokenType.SEMICOLON.toString()  
  477.                                 +“,”+TokenType.LESS.toString()  
  478.                                 +“,”+TokenType.GREATER.toString()  
  479.                                 +“,”+TokenType.LESS_EQUAL.toString()  
  480.                                 +“,”+TokenType.GREATER_EQUAL.toString()  
  481.                                 +“,”+TokenType.NOT_EQUAL.toString()  
  482.                                 +“,”+TokenType.EQUAL.toString();  
  483.             parsingError(errorTypes, “expression_1”);  
  484.         }  
  485.     }  
  486.     /** 
  487.      *  
  488.      * term=factor term_1 
  489.      * T->FT’ 
  490.      * 方法名:term 
  491.      * 创建人:xrzhang  
  492.      * 时间:2018516下午9:16:51  
  493.      * 邮件:jmzhang_15_cauc@163.com void 
  494.      * @exception  
  495.      * @since  1.0.0 
  496.      */  
  497.     private void term() {  
  498.         System.out.println(“In term();—–jmzhang—–“);  
  499.         if(lookAhead.getType()==TokenType.IDENTIFIER  
  500.                 ||lookAhead.getType()==TokenType.LPAREN  
  501.                 ||lookAhead.getType()==TokenType.INTEGER_LITERAL){  
  502.             factor();  
  503.             term_1();  
  504.         }else {  
  505.             String errorTypes =TokenType.IDENTIFIER.toString()  
  506.                     +“,”+TokenType.INTEGER_LITERAL.toString()  
  507.                     +“,”+TokenType.LPAREN.toString();  
  508.             parsingError(errorTypes, “term”);  
  509.         }  
  510.     }  
  511.     /** 
  512.      *  
  513.      * term_1=MULT factor term_1 | DIV factor term_1 | MOD factor term_1 | epslin 
  514.      * T’->*FT’ | /FT’ |%FT’|ε 
  515.      * 方法名:term_1 
  516.      * 创建人:xrzhang  
  517.      * 时间:2018516下午9:20:00  
  518.      * 邮件:jmzhang_15_cauc@163.com void 
  519.      * @exception  
  520.      * @since  1.0.0 
  521.      */  
  522.     private void term_1() {  
  523.         System.out.println(“In term_1();—–jmzhang—–“);  
  524.         if (lookAhead.getType()==TokenType.TIMES) {  
  525.             matchToken(TokenType.TIMES, “term_1”);  
  526.             factor();  
  527.             term_1();  
  528.         }else if (lookAhead.getType()==TokenType.DIVIDE) {  
  529.             matchToken(TokenType.DIVIDE, “term_1”);  
  530.             factor();  
  531.             term_1();  
  532.         }else if (lookAhead.getType()==TokenType.REMAINDER) {  
  533.             matchToken(TokenType.REMAINDER, “term_1”);  
  534.             factor();  
  535.             term_1();  
  536.         }else if (lookAhead.getType()==TokenType.PLUS  
  537.                 ||lookAhead.getType()==TokenType.MINUS  
  538.                 ||lookAhead.getType()==TokenType.SEMICOLON  
  539.                 ||lookAhead.getType()==TokenType.LESS  
  540.                 ||lookAhead.getType()==TokenType.GREATER  
  541.                 ||lookAhead.getType()==TokenType.LESS_EQUAL  
  542.                 ||lookAhead.getType()==TokenType.GREATER_EQUAL  
  543.                 ||lookAhead.getType()==TokenType.NOT_EQUAL  
  544.                 ||lookAhead.getType()==TokenType.EQUAL  
  545.                 ||lookAhead.getType()==TokenType.LOGICAL_AND  
  546.                 ||lookAhead.getType()==TokenType.LOGICAL_OR  
  547.                 ||lookAhead.getType()==TokenType.RPAREN) {  
  548.             //match e[slion  
  549.             //follow(T’)={‘+’,’-‘,’)’,’,’)  
  550.         }else {  
  551.             String errorTypes =TokenType.TIMES.toString()  
  552.                     +“,”+TokenType.DIVIDE.toString()  
  553.                     +“,”+TokenType.REMAINDER.toString()  
  554.                     +“,”+TokenType.PLUS.toString()  
  555.                     +“,”+TokenType.MINUS.toString()  
  556.                     +“,”+TokenType.SEMICOLON.toString()  
  557.                     +“,”+TokenType.LESS.toString()  
  558.                     +“,”+TokenType.GREATER.toString()  
  559.                     +“,”+TokenType.LESS_EQUAL.toString()  
  560.                     +“,”+TokenType.GREATER_EQUAL.toString()  
  561.                     +“,”+TokenType.NOT_EQUAL.toString()  
  562.                     +“,”+TokenType.EQUAL.toString()  
  563.                     +“,”+TokenType.LOGICAL_AND.toString()  
  564.                     +“,”+TokenType.LOGICAL_OR.toString();  
  565.             System.out.println(“lookAhead.getType()”+lookAhead.getType());  
  566.             parsingError(errorTypes, “term_1”);  
  567.               
     
  568.         }  
  569.           
     
  570.     }  
  571.     /** 
  572.      *  
  573.      * factor = LPAREN expression RPAREN | IDENTIFER|INTEGER_LITERAL 
  574.      * F->(E)|id| number 
  575.      * 方法名:factor 
  576.      * 创建人:xrzhang  
  577.      * 时间:2018516下午9:29:47  
  578.      * 邮件:jmzhang_15_cauc@163.com void 
  579.      * @exception  
  580.      * @since  1.0.0 
  581.      */  
  582.     private void factor() {  
  583.         System.out.println(“In factor();—–jmzhang—–“);  
  584.         if (lookAhead.getType()==TokenType.LPAREN) {  
  585.             matchToken(TokenType.LPAREN, “factor”);  
  586.             expression();  
  587.             matchToken(TokenType.RPAREN, “factor”);  
  588.         }else if (lookAhead.getType()==TokenType.IDENTIFIER) {  
  589.             matchToken(TokenType.IDENTIFIER, “factor”);  
  590.         }else if (lookAhead.getType()==TokenType.INTEGER_LITERAL) {  
  591.             matchToken(TokenType.INTEGER_LITERAL, “factor”);  
  592.         }else {  
  593.             String errorTypes =TokenType.LPAREN.toString()  
  594.                     +“,”+TokenType.IDENTIFIER.toString()  
  595.                     +“,”+TokenType.INTEGER_LITERAL.toString();  
  596.             parsingError(errorTypes, “factor”);  
  597.         }  
  598.     }  
  599. }  

发表评论

邮箱地址不会被公开。