I’m trying to implement prefix to infix in PHP. The input should be for example something like this:
prefix * + 3 2 4
infix ((3+2)*4)
I want to convert prefix expression like (* + 3 2 4) into infix expression using PHP or JavaScript (((3+2)*4)).
An exemplary object oriented AST and parser:
The parser:
The object model for the AST
I’m not very confident with the wording, because the nodes aren’t exactly fully related to infix, large parts of them could be used for prefix or postfix as well, only the
__toString()functions are infix related actually.(old version) Some PHP code, using some recursive parse function and an object model for the nodes. Usage:
The node classes: