使用 stringtokenizer 构建一个扫描器

build a scanner with stringtokenizer

我在这段代码中遇到了问题,我正在尝试为我在编译器课程中的项目构建一个扫描器,扫描器从用户那里获取任何输入并将其分成标记..输出将是:打印每个令牌及其类型(如:数字、标识符、关键字、加号...等),最后打印令牌的数量。

我已经声明了两个函数,一个用于确定输入是否为 java 保留关键字,第二个用于确定输入是否为标识符..

确定输入是否为 java 保留关键字的第一个函数运行良好,但我在确定输入是否为标识符的第二个函数的声明中遇到问题.. 当我输入保留关键字时,输出是:这是一个保留字并多次打印此输入是标识符。

这是我的代码:

package My_Scanner;
import java.util.Scanner;
import java.util.StringTokenizer;

public class MyScanner
{

    private static void  isOperator(String s)
    {
        if ( s.equals("+") )
        {
            System.out.print(s + "\t");
            System.out.println("Is Plus Sign");
        }

        else if ( s.equals("-") )
        {
            System.out.print(s + "\t");
            System.out.println("Is Minus Sign");
        }

        else if ( s.equals("*") )
        {
            System.out.print(s + "\t");
            System.out.println("Is Multiply Sign");
        }

        else if ( s.equals("/") )
        {
            System.out.print(s + "\t");
            System.out.println("Is Divide Sign");
        }

        else if ( s.equals("=") )
        {
            System.out.print(s + "\t");
            System.out.println("Is Assignment Operator");
        }

        else if ( s.equals("%") )
        {
            System.out.print(s + "\t");
            System.out.println("Is percent Operator");
        }

        else if ( s.equals(".") )
        {
            System.out.print(s + "\t");
            System.out.println("Is Full Stop");
        }
    }

    private static void  isReservedWord(String s)
    {
        for (int i = 0; i < reserved_Keywords.length; i++)
        {  
            if ( s.equals(reserved_Keywords[i]) )
            {  
                System.out.print(s + "\t");
                System.out.println("Is Reserved Keyword");
            }
        } 
    } 


    private static boolean isOpeningBracket(String s)
    {
        return (  s.equals("(") |  s.equals("{") | s.equals("[") |  s.equals("<")  ); 
    }

    private static boolean isClosingBracket(String s)
    {
        return (  s.equals(")") |  s.equals("}") | s.equals("]") |  s.equals(">")  ); 
    }

    private static void isIdentifier(String s)
    {
        boolean identifier = true;
        if ( !isOpeningBracket(s) && !isClosingBracket(s) )
        {
            for (int i = 0; i < reserved_Keywords.length; i++) {
                if ( s.equals(reserved_Keywords[i]) )
                {
                    identifier = false;
                    break;
                }
            }
        }

        if(identifier)
        {
            System.out.print(s + "\t");
            System.out.println("Is Identifier");
        }
    }

    static final String reserved_Keywords[] = { "abstract", "assert", "boolean",
            "break", "byte", "case", "catch", "char", "class", "const",
            "continue", "default", "do", "double", "else", "extends", "false",
            "final", "finally", "float", "for", "goto", "if", "implements",
            "import", "instanceof", "int", "interface", "long", "main", "native",
            "new", "null", "package", "private", "protected", "public",
            "return", "short", "static", "strictfp", "string", "super", "switch",
            "synchronized", "this", "throw", "throws", "transient", "true",
            "try", "void", "volatile", "while" };

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter Your Text: ");
        String str = sc.nextLine();
        StringTokenizer st = new StringTokenizer(str);
        int numofTokens = st.countTokens();
        while( st.hasMoreTokens() )
        {
            String TOKEN = st.nextToken();

            isOperator(TOKEN);
            isReservedWord(TOKEN);
            isIdentifier(TOKEN);
            if ( isOpeningBracket(TOKEN) )
            {
                System.out.print(TOKEN + "\t");
                System.out.println("Is Left  Bracket");
            }
            else if ( isClosingBracket(TOKEN) )
            {
                System.out.print(TOKEN + "\t");
                System.out.println("Is Right  Bracket");
            }
        } 
        sc.close(); 
        System.out.println("Number of Tokens = " + numofTokens);
    }
}

那是因为 else 放错了。它检查所有元素并每次打印 else 中的语句。相反,它只需要在循环结束后打印一次,试试下面的代码:

private static void isIdentifier(String s) {
    boolean identifier = true;
    for (int i = 0; i < reserved_Keywords.length; i++) {
        if (s.equals(reserved_Keywords[i])) {
            identifier = false;
            break;
        }
    }

    if(identifier){
        System.out.print(s + "\t");
        System.out.println("Is Identifier");
    }
}

编辑

要处理左括号和右括号,您可以在调用方法检查标识符和 continue 循环之前检查那些字符,如果您找到任何这些字符,例如:

while( st.hasMoreTokens() ){
    String TOKEN = st.nextToken();
    if ( isOpeningBracket(TOKEN) ){
        System.out.print(TOKEN + "\t");
        System.out.println("Is Left  Bracket");
        continue;
    }
    else if ( isClosingBracket(TOKEN) ){
        System.out.print(TOKEN + "\t");
        System.out.println("Is Right  Bracket");
        continue;
    }
    isOperator(TOKEN);
    isReservedWord(TOKEN);
    isIdentifier(TOKEN);
}