+ 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!

24th May 2019, 11:03 AM
Filippo
Filippo - avatar
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.
24th May 2019, 5:32 PM
Da2
Da2 - avatar
+ 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/
26th May 2019, 8:59 AM
Da2
Da2 - avatar
+ 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?)?
26th May 2019, 8:44 AM
Filippo
Filippo - avatar
+ 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 :)
26th May 2019, 1:33 PM
Filippo
Filippo - avatar