Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

how to make a java lex analyzer

import java.util.List;
import java.util.ArrayList;

/*
 * Lexical analyzer for Scheme-like minilanguage:
 * (define (foo x) (bar (baz x)))
 */
public class Lexer {
    public static enum Type {
        // This Scheme-like language has three token types:
        // open parens, close parens, and an "atom" type
        LPAREN, RPAREN, ATOM;
    }
    public static class Token {
        public final Type t;
        public final String c; // contents mainly for atom tokens
        // could have column and line number fields too, for reporting errors later
        public Token(Type t, String c) {
            this.t = t;
            this.c = c;
        }
        public String toString() {
            if(t == Type.ATOM) {
                return "ATOM<" + c + ">";
            }
            return t.toString();
        }
    }

    /*
     * Given a String, and an index, get the atom starting at that index
     */
    public static String getAtom(String s, int i) {
        int j = i;
        for( ; j < s.length(); ) {
            if(Character.isLetter(s.charAt(j))) {
                j++;
            } else {
                return s.substring(i, j);
            }
        }
        return s.substring(i, j);
    }

    public static List<Token> lex(String input) {
        List<Token> result = new ArrayList<Token>();
        for(int i = 0; i < input.length(); ) {
            switch(input.charAt(i)) {
            case '(':
                result.add(new Token(Type.LPAREN, "("));
                i++;
                break;
            case ')':
                result.add(new Token(Type.RPAREN, ")"));
                i++;
                break;
            default:
                if(Character.isWhitespace(input.charAt(i))) {
                    i++;
                } else {
                    String atom = getAtom(input, i);
                    i += atom.length();
                    result.add(new Token(Type.ATOM, atom));
                }
                break;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        if(args.length < 1) {
            System.out.println("Usage: java Lexer "((some Scheme) (code to) lex)".");
            return;
        }
        List<Token> tokens = lex(args[0]);
        for(Token t : tokens) {
            System.out.println(t);
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: how to apply validation on aiphanumeric series starting with 4 characters with 4 letters in java 
Java :: jdbc outside of ide 
Java :: sha 1 key throguh java is not working 
Java :: java print data and check in android studio 
Java :: how to assign one value to 2 variable in one line in java 
Java :: a Java-8 stream of batches, 
Java :: Error: Unable to export or encrypt the private key java.lang.NullPointerException 
Java :: jdned 
Java :: java oop design patterns 
Java :: java file reader construct input 
Java :: java switch expression produce result 
Java :: actionevent java swing 
Java :: governmental transparency 
Java :: comvertir a java 
Java :: data input stream using while loop 
Java :: java scanner use all symbols as delimiter 
Java :: gif to blob java 
Java :: java random number generator 6 
Java :: x/=y is the same as x=x/y 
Java :: In similar fashion we will use (RIGHT – SHIFT) to retrieve bits from val 6 at a time 
Java :: custom validator arrays 
Java :: android studio enum usage 
Java :: conditional statement problems in java 
Java :: reset android studio settings windows 
Java :: javafx change application title 
Java :: ways to add properties to Bean Spring 
Java :: does java recognize on a txt file 
Java :: node constructor 
Java :: Java Single element Annotations 
Java :: minecraft bukkit coding player sharing variable 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =