java method for parsing nested expressions

后端 未结 5 1209
Happy的楠姐
Happy的楠姐 2020-12-16 04:37

lets say I\'ve written a function to evaluate a simple mathematical operation, and I have some user input in a string such as: \"1 + [2 + [3+4]]\" How can I parse these squa

5条回答
  •  情书的邮戳
    2020-12-16 05:05

    For Java, you could use JavaCC for parser/lexer. I have used this one in numerous projects. It's pretty easy to use. One of the examples, I think, includes an arithmetic parsing. JavaCC will build the syntax tree where you could walk through.

    Trying out arithmetic using JavaCC would give good intro to Context Free Grammar and the concept of Abstract Syntax Tree. If you are learning, then it is a good step to take after trying out what @emboss suggested

提交回复
热议问题