Infix to postfix with real (postive and negative) numbers

前端 未结 1 1470
别跟我提以往
别跟我提以往 2021-01-07 14:21

I\'ve written a program that allows translation from infix to postfix expression but it works only for one digit [A-Z][a-z][0-9]. How can I do to make it possib

相关标签:
1条回答
  • 2021-01-07 15:11

    You have to do a Lexical analysis. You have to convert your input stream to tokens and classify them. At the moment your program already does, but it is very very basic then your tokens consist only of one digit. Though tokens can be more complex, starting from simple integer number literals greater than 9, floating point number literal, string literals, simple operators and so on.

    What you might have now is something that gives you on subsequent calls the next token to analyze process it and step to the next. something like:

    String input = "1 + 2";
    int actPos = 0 ;
    
    ....
    
    char getNextToken() {
        return input.charAt(actPos++);
    }
    

    What you need to do is to rewrite the getNextToken() so that it gives you a "complex" token back (consisting of more than one digit/char, thus a string) witch you have to classify in the next step.

    String getNextToken() {
        String StringBuilder token = new StirngBuilde();
    
        // extract the next token from the input stream
        // using a state automata witch comes to a final
        // state when a token was recognized or an erroneous input
    
        return token.toString();
    }
    

    EIDT

    You need to write finite state machine to analyze the input stream and produce tokens.

    Just to give you a very simple example (assuming you have read the definition in the link above), let's say you have the alphabet {'0','1','2','3','4','5','6','7','8','9'} and want to build tokens that are integers without a sign. You define integer as [0-9]+

    Your state machine will need at least three states START witch is the start state, INTEGER witch is a final and accept state, and ERROR witch denotes that there is an error in the input.

    Now You need a transition function and a state transition table, this is what takes the actual input and produces the next state according the actual state.

    The state transition table could look something like that (very basic and cannot handle when given no input)

                  i n p u t
                  +----------+----------+
                  | [0-9]    | else     |
    s  +----------+----------+----------+
    t  | START    | INTEGER  | ERROR    |
    a  +----------+----------+----------+
    t  | INTEGER  | INTEGER  | ERROR    |
    e  +----------+----------+----------+
       | ERROR    | ERROR    | ERROR    |
       +----------+----------+----------+
    

    So let's say you have the following input: 27

    • At the start the state machine has the state START (actual state)
    • You read the first char 2 and pass it along with the actual state to the transition function
    • Since 2 is an integer your function brings you to the state INTEGER and since this is not an ERROR you put it in the buffer where you build the token char by char.
    • Now you read the next char 7 and pass it along with the actual state INTEGER to the transition function.
    • 7 is also an integer, the same thing happens as with 2.
    • Now the token buffer has the content 27 and your state machine still has the state INTEGER witch is an accept state and there is no input left. At this moment you can return the content of the token buffer as being the next token.

    Now suppose that you have the input: 2e7

    Your state machine proceeds like above with the first char 2 and the state INTEGER, then comes e witch does not meet the rule for integer numbers defined above ([0-9]+). Passing e with the actual state INTEGER to the transition function result in the transition the state ERROR. At this point you can notify that there is an error in the input at position/index of e.

    My advise is that you read more about lexical analysis and how you can code a finite state machine the achieve it.

    This been said, you could always use some tool like JLex that generates this analysis code for you, but you still have to define some rules for it to have generate code that does what you want it to do, witch leads you back to reading more about lexical analysis :)

    Good luck!

    0 讨论(0)
提交回复
热议问题