如何使用stringtokenizer构建扫描程序

时间:2016-12-16 12:39:28

标签: java eclipse compiler-construction stringtokenizer

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

我尝试了更多输入,每次输出都是标识符,当我尝试输入数字或关键字或+或 - 输出是标识符..

这是我的代码:

import java.util.Scanner;
import java.util.StringTokenizer;

public class MyScanner
{
    public static void main(String[] args)
    {
        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", "native",
                "new", "null", "package", "private", "protected", "public",
                "return", "short", "static", "strictfp", "super", "switch",
                "synchronized", "this", "throw", "throws", "transient", "true",
                "try", "void", "volatile", "while" };
        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.hasMoreElements() )
        {
            for (int i = 0; i < reserved_Keywords.length; i++)
            {  
                if ( st.equals(reserved_Keywords[i]) )
                {  
                    System.out.print(st.nextElement() + "\t");
                    System.out.println("Is Reserved Keyword");
                }
            }  

            if ( st.equals("+") )
            {
                System.out.print(st.nextElement() + "\t");
                System.out.println("Is Plus Sign");
            }

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

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

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

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

            else
            {
                System.out.print(st.nextElement() + "\t");
                System.out.println("Is Identifier");
            }
        }
        sc.close(); 
        System.out.println("Number of Tokens = " + numofTokens);
    }
}

1 个答案:

答案 0 :(得分:1)

总是比较(调用equals(..)(使用StringTokenizer,而不是使用StringTokenizer返回的标记。

要解除此问题,请添加while循环的第一行

UIView

然后用ST替换所有比较(调用equals())而不是TOKEN。

(你应该将变量命名为不是大写字母,我这样做只是为了便于阅读)

然后您的代码将如下所示:

 String TOKEN = st.nextToken();

...