build a scanner with stringtokenizer

169 views Asked by At

I have a problem in this code, i'm trying to build a scanner for my project in compiler course, the scanner takes any input from the user and separate it into tokens .. the output will be : print each token and it's type(like: number,identifier,keyword,plus sign...etc.) and finally print the number of tokens.

i have declared two functions, one to determine if the input is a java reserved keyword or not and the second to determine if the input is an identifier or not..

the first function that determine if the input is a java reserved keyword or not is working good, but i have a problem in the declaration of the second function that determine if the input is an identifier or not.. when i entered a reserved key word the output is: this is a reserved word and print this input is identifier many times.

Here is my code:

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);
    }
}
1

There are 1 answers

3
Darshan Mehta On BEST ANSWER

That's because the else is incorrectly placed. It cheks for all the elements and prints the statement in else everytime. Instead, it needs to print that only once after the loop is finished, try the below code:

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");
    }
}

EDIT

To handle opening and closing bracket, you can check for those before calling methods to check for identifiers and continue the loop if you find any of those chars, e.g.:

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);
}