Algorithm for parsing arithmetic expressions