Java - data structure for infix calculator

I want to build an infix calculator. The approach that I choose analyzes the input in a tree with operators in nodes and operands in sheets, then crosses the tree from bottom to top (from high priority operators to lower ones).

Tree example:

                         (12 + 8) / 2 - 5

                                
                              /   \
                            '/'    5
                            / \
                           +   2    
                          / \  
                         12  8

First: I'm new to Java, what data structure should I choose, or maybe I need to create a new class with my own tree implementation?

And secondly: what is best for parenthesis priorities?

+4
source share
1 answer

Operand, Operation Literal. Operation (: ) Operand.

Operation Literal Operation.

, ( Q) : , , , .

, antlr .

BNF :

<expression> ::= <term> | <term> <addop> <expression>
<term>       ::= <factor> | <term> <mulop> <factor>
<factor>     ::= <constant> | (" <expression> ")"
<constant>   ::= <digit> | <digit> <constant>
<addop>      ::= "+" | "-"
<mulop>      ::= "*" | "/"
<digit>      ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"

( , , ) . Literal Operation, , .

+5

All Articles