无法完全使词法分析器在我的Java程序中工作

问题描述

我正在尝试获取这段Java代码,以词法分析短语“(和+ 47)/总数”,并将其吐出为:

    Next token is: 25 Next lexeme is (
    Next token is: 11 Next lexeme is sum 
    Next token is: 21 Next lexeme is +
    Next token is: 10 Next lexeme is 47
    Next token is: 26 Next lexeme is )
    Next token is: 24 Next lexeme is /
    Next token is: 11 Next lexeme is total
    Next token is: -1 Next lexeme is EOF

但是,结果却是这样的:

    Next token is: 25 Next lexeme is (
    Next token is: 11 Next lexeme is um 
    Next token is: 21 Next lexeme is +
    Next token is: 10 Next lexeme is 47
    Next token is: 24 Next lexeme is /
    Next token is: 11 Next lexeme is total

我知道我正在为EOF弄乱某些内容,但我无法弄清楚为什么它会截断47之后的“ s”和“)”。这是我的代码供参考。请让我知道我是否需要对此帖子做任何事情,因为这是我的第一个帖子。

    import java.io.*;
    import java.util.*;

    public class Main
    {
       private static final int LETTER=0;
       private static final int DIGIT=1;
       private static final int UNKNOWN=99;
       private static final int EOF=-1;
       private static final int INT_LIT=10;
       private static final int IDENT=11;
       private static final int ASSIGN_OP=20;
       private static final int ADD_OP=21;
       private static final int SUB_OP=22;
       private static final int MULT_OP=23;
       private static final int DIV_OP=24;
       private static final int LEFT_PAREN=25;
       private static final int RIGHT_PAREN=26;


       private static int charClass;
       private static char lexeme[];
       private static char nextChar;
       private static int lexLen;
       private static int token;
       private static int nextToken;
       private static File file;
       private static FileInputStream fis;
       public static int lookup(char ch)
       {
           switch (ch)
           {
            case '(':
                addChar();
                nextToken = LEFT_PAREN;
                break;
            case ')':
                addChar();
                nextToken = RIGHT_PAREN;
                break;
            case '+':
                addChar();
                nextToken = ADD_OP;
                break;
            case '-':
                addChar();
                nextToken = SUB_OP;
                break;
            case '*':
                addChar();
                nextToken = MULT_OP;
                break;
            case '/':
                addChar();
                nextToken = DIV_OP;
                break;
            default:
                addChar();
                nextToken = EOF;
                break;
           }
           return nextToken;
       }
       public static void addChar()
       {
            if (lexLen <= 98)
            {
                lexeme[lexLen++] = nextChar;
                lexeme[lexLen] = 0;
            }
            else
               System.out.println("Error -lexeme is too long\n");
       }
       public static void getChar()
       {
           try
           {
           if(fis.available()>0)
           {
               nextChar=(char)fis.read();
               if(Character.isLetter(nextChar))
                   charClass=LETTER;
               else if(Character.isDigit(nextChar))
                   charClass=DIGIT;
               else
                   charClass=UNKNOWN;
           }
           else
               charClass=EOF;
      
           }
           catch(IOException e)
           {
               e.printStackTrace();
           }
       }
       public static void getNonBlank()
       {
           while(Character.isSpaceChar(nextChar))
               getChar();
  
       }
       public static int lex()
       {
           lexLen = 0;
            getNonBlank();
            switch (charClass)
            {
            /* parse identifiers */
                case LETTER:
                    addChar();
                    getChar();
                    while (charClass == LETTER || charClass == DIGIT)
                    {
                       addChar();
                       getChar();
                   }
                    nextToken = IDENT;
                    break;
                    /* parse integer literals and integers */
                case DIGIT:
                    addChar();
                    getChar();
                   while(charClass == DIGIT)
                   {
                        addChar();
                        getChar();
                   }
                   nextToken = INT_LIT;
                   break;
                /* parentheses and operators */
                case UNKNOWN:
                    lookup(nextChar);
                    getChar();
                   break;
       /* EOF */
        case EOF:
            nextToken = EOF;
            break;

           } /* end of switch */
           System.out.print("Next token is :"+nextToken+" Next lexeme is :");
           for(int i=0;i<lexLen;i++)
               System.out.print(lexeme[i]);
           System.out.println();
           return nextToken;
       }
       public static void main(String args[])
       {
                   lexLen=0;
           lexeme=new char[100];
           for(int i=0;i<100;i++)
               lexeme[i]='0';
           file = new File("input1.txt");
           if (!file.exists())
           {
                 System.out.println( "input1.txt does not exist.");
                 return;
           }
           if (!(file.isFile() && file.canRead()))
           {
                 System.out.println(file.getName() + " cannot be read.");
                 return;
                   }
           try
           {
                 fis = new FileInputStream(file);
                 char current;
                 while (fis.available() > 0)
                 {
                     getChar();
                 //   System.out.println(nextChar+" "+charClass);
                   lex();
               }
           }
           catch (IOException e)
           {
                 e.printStackTrace();
           }
       }
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)