Java – the problem of split code algorithm

I have successfully implemented a split code algorithm in Java The algorithm itself is very simple, but I have trouble using tokenizer At present, the algorithm applies to everything I want, excluding one thing How to distinguish between subtraction (–) and negative numbers (–)

For example, 4-3 is subtraction, but - 4 3 is negative

I now know how to find out when it should be negative and when it should be negative, but where the algorithm should be, because if you use it like a function, it won't always work

3 4 * 2 / – (1 – 5)^ 2 ^ 3

When 1-5 becomes - 4, it becomes 4, and then becomes square and cubic

Like 3 4 * 2 / cos (1 – 5) ^ 2 ^ 3, you will take the cosine before the sum of squares and cubes

But in real math you don't use - because you really say 3 4 * 2 / – ((1 – 5) ^ 2 ^ 3) to get the right value

Solution

It sounds like you're working on a lex then parse style parser. You need to use a simple state machine in lexer to get separate tags for single and binary minus signs (in the peg parser, this is not something you need to worry about.)

In JavaCC, you will have a default status, and you can treat the – character as unary_ MINUS. When you mark the end of the main expression (the end expression or integer based on the example you give), you will switch to the infix state, where - will be treated as infix_ MINUS. Once you encounter any infix operator, you will return the default status

If you scroll by yourself, it may be simpler than this Look at this Python code. It's a smart way Basically, when you encounter - you just need to check whether the previous token is an infix operator This example uses the string "- U" to represent a unary minus token, which facilitates informal tokenization Best I can say, the python example really doesn't handle a - following open paren, or from the beginning of input Those should also be considered one dollar

In order to correctly handle the unary minus sign in the split code algorithm itself, it needs to have higher priority than any infix operator, and it needs to be marked as right association Make sure you handle right associativity. You may have missed it because the rest of the operators are left associative This is clear enough in Python code (although I will use some structure instead of two separate mappings)

When you need to evaluate, you need to change the unary operator slightly, because you only need to pop one number from the stack, not two Depending on your implementation, it may be easier to go through the list and replace each occurrence of "- U" with [- 1, "*"]

If you can focus entirely on python, you should be able to see everything I'm talking about in the example I link I found the code easier to read than the C version mentioned by others In addition, if you're curious, I did some articles on using shuning yard in Ruby some time ago, but I treated the unary operator as a separate non terminal symbol, so they didn't show up

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>