G
G
gleendo2017-07-04 03:54:00
Java
gleendo, 2017-07-04 03:54:00

How to make a parser checkmate. expressions in which arguments can have length greater than 1?

In general, I figured out an example of how to calculate a mathematical expression, but at the moment the program calculates examples in which the arguments are in the range from 0 to 9.
How can I make the arguments be, for example, from 0 to 10k?
It would be possible to compare the range with the string "10000" in the range check, but xs how it will first pull out what is needed from the input string, it is not clear, for example, from the string 954 + *, what to take, 9 or 95 for example.
Stack for converting infix notation to postfix
Code for converting infix notation to postfix
Postfix notation parser and expression evaluation

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arseniy Efremov, 2017-07-04
@arusef

First, describe the minimal language of your expressions:

<выражение> ::= <литерал> <оператор> <литерал>
<литерал> ::= <число> | (<выражение>)
<число> ::= [<число> | "-"]<0-9> // или всё, что парсится parseInt.
<оператор> ::= < + | - | * | / >

Now you can see the structure of your expression, and apply some kind of parsing algorithm:
The expression starts with a literal, which means we read characters from the input string until we meet a space or \0: if these are numbers, then our literal is a number, if "(" - an expression for which you want to repeat the entire procedure, waiting for the closing ")". Next, we read the operator. And then again a literal. If something according to the rules does not fit, then we end with an error.
Well, each literal can be evaluated - the number returns a number, and the expression returns the result of applying the operator to two literals. The overall result is the result of evaluating the root expression.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question