All pastes #613291 Raw Edit

parser

public java v1 · immutable
·published 2007-07-10 22:08 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) ;        	        	switch(state){        	case CONTROL :        		if (i != e && is_char(inputChar)){        			begin = i ;        			state = lexer.State.ID ;        		}        		else if (i != e && is_digit(inputChar)){        			begin = i ;        			state = lexer.State.VALUE ;        		}        		else if (i != e && is_operation(inputChar)){        			begin = i ;        			state = lexer.State.OPERATION ;        		}        		else if ( i !=e && inputChar == ' ' ) {        			state = lexer.State.EMPTY ;        		}        		else if (i != e && is_paranthesis(inputChar)){        			begin = i ;        			state = lexer.State.PARANTHESIS ;        		}        		else state = lexer.State.ERROR ;        		break ;        	case ID :        		if ( i != e && (is_char(inputChar) || is_digit(inputChar)) ) {        			state = lexer.State.ID ;        			++i ;        		}        		else {        			tokenStream.add(new node(str.substring(begin, i), lexer.State.ID));        			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 ) {        			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 ;                    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 ;                    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") ;        }        	}        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==')' ;	}}