I'm attempting to implement increment operators into my parser. However I'm confused as to whether a increment operator should be treated as a statement or expression. It makes sense to implement it as a statement because one can use it as a standalone statement such as:
i++;
However it can also be used in an expression such as:
int 2 = i++;
This has implications for my AST, because I currently have two types of nodes, a Expression node and a Statement Node which other nodes can accept as their children. An expression node will always have a return value when ran through the code generation part of the interpreter.
How are increment operators classified in other languages in regards to ASTs? Are they best represented as expressions or statements?
If it's legal to write int variableName = i++;
, that means that i++
is an expression. i++;
being a legal statement means that <increment-operator> ';'
is defined as a valid statement, either directly or more commonly using a rule such as <statement> ::= <expression> ';'
, i.e. "an expression followed by a semicolon is a statement". This type of statement is known as an expression statement and is also why you can use function calls as statements in most languages (some languages restrict what type of expressions can be used in expression statements to disallow no-op statements like 42;
or x+y;
, but other languages allow those).