+ 2
Expression or statement?
In general, without taking as an example a particular programming language, how can I recognized when a particular instruction is a expression or a command? In general, a expression return a value, while the commands not, but modify the program state (memory used for the program). Sometimes this two constructs can be mixed and I have some difficulties to recognize one and the other. Can someone help me with any tips and / or tricks? Moreover, given a generic compiled programming language, how does the compiler recognize an instruction as an expression or a command and how does it behave when it has to compile it (in both cases)? Thanks!
4 Answers
+ 3
Interesting question! I may be wrong but your question may be a matter of semantics. So it is a matter of how it is used (context).
Even in natural languages, this can apply for example
I am confused. It is both a statment and an expression, depending on it's context.
+ 2
Compilers have different ways of compiling high level source code to executable object code. Check out the link do you get a better idea.
https://www.calleerlandsson.com/the-four-stages-of-compiling-a-c-program/
+ 1
Interesting answer. Surely it is a matter of semantics and probably everything depends on the context.
At this point I think, if everything depends on the context, how does a compiler understand if an instruction is a command or an assignment?
What is the context for a compiler and how is it used (is it a data structure that is used as a context? If so, this "structure" what should contain?)?
+ 1
I think that this process is more simple in C because the compiler compiled C code to native machine code. There is no just in time compilation involved.
For example, in Java the compiler compiles JAVA code into Java bytecode and then the interpreter interpretes the bycode into native machine code.
During the semantics analysis, how does the compiler understand the current context and discriminate a command from an expression?
I just need to understand the idea, there is no need to think of a real programming language and a real compiler/interpreter :)