I'm trying to parse expressions for a simple scripting language, but I'm confused. Right now, only numbers and string literals can be parsed as an expression:
int x = 5; double y = 3.4; str t = "this is a string";
However, I am confused in the analysis of more complex expressions:
int a = 5 + 9; int x = (5 + a) - (a ^ 2);
I think I would execute it like this:
do { // no clue what I would do here? if (current token is a semi colon) break; } while (true);
Any help would be great, I don't know where to start. Thanks.
EDIT: My parser is a recursive descent parser.
My expression "class" is as follows:
typedef struct s_Expression { char type; Token *value; struct s_Expression *leftHand; char operand; struct s_Expression *rightHand; } ExpressionNode;
Someone mentioned that a recursive descent parser is able to parse expressions without making infix, postfix. Preferably, I would like the expression to be as follows:
For instance:
int x = (5 + 5) - (a / b);
The following will be analyzed: Note: this is not valid C, this is just some pseudo code, to get my point is simple :)
ExpressionNode lh; lh.leftHand = new ExpressionNode(5); lh.operand = '+' lh.rightHand = new ExpressionNode(5); ExpressionNode rh; rh.leftHand = new ExpressionNode(a); rh.operand = '/'; rh.rightHand = new ExpressionNode(b); ExpressionNode res; res.leftHand = lh; res.operand = '-'; res.rightHand = rh;
I asked the question quite late, so I'm sorry if I did not understand and that I completely forgot what my original goal was.