kathleen55
kathleen55

Reputation: 341

Java Lexical Analyzer

i'm creating a lexical analyzer. in my code i can output the symbols, but when it comes to letters and numbers i cant design what will be my code in that..

please help me in my project.. thanks

package lab7;

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

public class Lab7 {
public static void main(String[] args) {
                String word;
        char[] wordArray;

        Scanner sc = new Scanner(System.in);

        wordArray = word.toCharArray();

        for(int x = 0; x < wordArray.length; x++) {

            if(wordArray[x] == '+') {
                System.out.print("ADD ");
            }
            else if(wordArray[x] == '-') {
                System.out.print("SUBTRACT ");
            }
            else if(wordArray[x] == '*') {
                System.out.print("MULTIPLY ");
            }
            else if(wordArray[x] == '/') {
                System.out.print("DIVIDE ");
            }
            else if(wordArray[x] == '(') {
                System.out.print("MATH ");
            }
            else if(wordArray[x] == ')') {
                System.out.print("MATH ");
            }
            else if(wordArray[x] == '=') {
                System.out.print("ASSIGN ");
            }

        }
    }
}

Upvotes: 1

Views: 3254

Answers (1)

Max Vasileuski
Max Vasileuski

Reputation: 2651

Character methods Character.isLetter and Character.isDigit may help you here.

However you can't write real lexical analyzer by only scanning string letter by letter. Lexical analyzer produces multi-character tokens and usually implemented as a state machine, which is defined by regular expressions.

Automatic splitting isn't useful here. For example, if you'll split input string by whitespaces, 12+34 will be a single token. So your should define some rules, for example, read digits, while there are digits, and so on.. This leads to the idea of ​​a finite automaton (or a state machine).

Upvotes: 1

Related Questions