All pastes #612547 Raw Edit

lexer

public java v1 · immutable
·published 2007-07-10 13:57 UTC
rendered paste body
package output;import java.io.* ;import java.util.* ;public class lexer extends java.lang.Object  {	public enum State {		CONTROL ,	    OPERATION,	    FLOATING_POINT,	    EXP,	    EMPTY,	    BINARY,	    UNARY,	    PARANTHESIS,	    FUNCTION,	    EXPRESSION,	    ERROR,	    TYPE,	    VALUE,		ID,		TOKEN,	    QUIT	}	class node extends lexer {		node right, left ;	    float value ;	    String token ;	    State type ;	    node( String subString, State type)	    {	    	token = subString ;	    	value = 0 ;	    	right = null ;	    	left = null ;	    	if (type == State.VALUE) {	    		value = Float.parseFloat(token);	    	}	    }	    public String toString () {	    	return type + "(" + token + ")" ;	    	}	}	public LinkedList tokenStream ;		public void Lexer(String str)  {        int e,i, begin = 0, count = 0;		tokenStream = new LinkedList<node> () ;    	e = str.length();    	i = str.indexOf(str);        State state = lexer.State.CONTROL ;        while ( i < e ) {        	char inputChar = str.charAt (i) ;        	//System.out.println(str.substring(0, 6));        	switch(state){        	case CONTROL :        		if (i <= e && is_char(inputChar)){        			begin = i ;        			//System.out.println("#1");        			state = lexer.State.ID ;        		}        		else if (i <= e && is_digit(inputChar)){        			begin = i ;        			state = lexer.State.VALUE ;        			//System.out.println("#2");        		}        		else if (i <= e && is_operation(inputChar)){        			begin = i ;        			state = lexer.State.OPERATION ;        			//System.out.println("#3");        		}        		else if ( i <=e && inputChar == ' ' ) {        			state = lexer.State.EMPTY ;        			//System.out.println("#4");        		}        		else if (i <= e && is_paranthesis(inputChar)){        			begin = i ;        			state = lexer.State.PARANTHESIS ;        			//System.out.println("#5");        		}        		else state = lexer.State.ERROR ;        		break ;        	case ID :        		if ( i <= e && (is_char(inputChar) || is_digit(inputChar)) ) {        			state = lexer.State.ID ;        			//System.out.println("#6");        			//System.out.println(str.substring(begin, i));        			++i ;        			//System.out.println("#7");        		}        		else {        			//System.out.println("#8");        			System.out.println(str.substring(begin, i));        			tokenStream.add(new node(str.substring(begin, i), lexer.State.ID));        			//System.out.println(tokenStream.size());        			state = lexer.State.CONTROL ;        		}        		break ;        	case VALUE :        		if ( i <= e && is_digit(inputChar) ){        			state = lexer.State.VALUE ;        			++i ;        			continue ;        		}        		else if (i <= e && (inputChar == ',' || inputChar == '.')) {        			state = lexer.State.FLOATING_POINT ;        			++i ;        			continue ;        		}        		else if (i <= e && (inputChar == 'E' || inputChar == 'e') ) {        			state = lexer.State.EXP ;        			++i ;        			continue ;        		}        		else if ( i <= e ) {        			//System.out.println("#9");        			System.out.println(str.substring(begin, i));        			tokenStream.add(new node(str.substring(begin, i),lexer.State.VALUE));        			count = 0 ;        			state = lexer.State.CONTROL ;        		}        		break ;        	case OPERATION :        		if ( i <= e && ("+-*/^()".indexOf(inputChar)!= -1 )) {        			++i ;        			//System.out.println("#10");        			System.out.println(str.substring(begin, i));                    tokenStream.add(new node(str.substring(begin, i),lexer.State.OPERATION)) ;                    state = lexer.State.CONTROL ;                    continue ;        		}        		break ;        	case    PARANTHESIS :                if ( i <= e && ("()".indexOf(inputChar)!= -1 )  ) {                    ++i ;                    //System.out.println("#11");                    System.out.println(str.substring(begin, i));                    tokenStream.add(new node(str.substring(begin, i),lexer.State.PARANTHESIS)) ;                    state = lexer.State.CONTROL ;                    continue ;                }                break ;        	case    FLOATING_POINT :                if ( i <= e && is_digit(inputChar) && count < 2 ) {                  state = lexer.State.VALUE ;                  ++i ;                  continue ;            }                else state = lexer.State.ERROR ;                break ;        	case    EXP :                if ( i <= e && is_digit(inputChar) || ("+-".indexOf(inputChar)!= -1 ) ) {                    state = lexer.State.VALUE ;                    ++i ;                    continue ;                }                else state = lexer.State.ERROR ;                break ;        	case    EMPTY :                if ( i <= e && inputChar == ' ' ) {                    state = lexer.State.EMPTY ;                    ++i ;                    continue ;                }                else state = lexer.State.CONTROL ;                break ;        	case    ERROR :            			default :            					System.out.println("aaa") ;        }        	}        //System.out.println("11");        if (state != lexer.State.ERROR && state != lexer.State.CONTROL && state != lexer.State.EMPTY ) {            if ( state == lexer.State.ID )                tokenStream.add(new node(str.substring(begin, i),lexer.State.ID)) ;            else            	if ( state == lexer.State.OPERATION )            	tokenStream.add(new node(str.substring(begin, i),lexer.State.OPERATION)) ;            else            	if ( state == lexer.State.VALUE )            	tokenStream.add(new node(str.substring(begin, i),lexer.State.VALUE));        }	}	public void cout () {		ListIterator<node> i = tokenStream.listIterator () ;		try {			while (i.hasNext ())				System.out.println (i.next ()) ;			}		catch (Exception ex) {}	}	static public boolean is_char (char ch){		return (ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z') ;	}	static public boolean is_digit (char ch){		return (ch>='0'&&ch<='9') ;	}	static public boolean is_symbol (char ch){		return ch=='+'||ch=='-'||ch=='/'||ch=='%'||ch=='*'||ch=='^'||ch=='<'||ch=='>'            ||ch=='='||ch==';'||ch==','||ch=='('||ch==')' ;	}	static public boolean is_operation (char ch){		return ch=='+'||ch=='-'||ch=='/'||ch=='*'||ch=='^' ;	}	static public boolean is_paranthesis (char ch){		return ch=='('||ch==')' ;	}}