-1
$\begingroup$

Does checking for correctness belong predominantly to "semantic analysis"?

Or is it reasonable to be done in earlier phases (in a compiler like construction) as well? Mainly in syntax analysis. Or is syntax analysis often/always "formal", rather than semantical?

enter image description here

http://courses.cs.vt.edu/cs1104/Compilers/Compilers.020.html

$\endgroup$
  • 3
    $\begingroup$ Checking for correctness of what? "Correctness" of a program usually means that it meets whatever specification it was designed to meet. That's not something the compiler is interested in. $\endgroup$ – David Richerby Dec 30 '15 at 17:23
  • $\begingroup$ @DavidRicherby I believe the semantic analysis phase is to verify that the computations done are "rational". Whereas syntax analysis merely verified that the program contains syntactically valid sentences. $\endgroup$ – mavavilj Dec 30 '15 at 17:38
  • $\begingroup$ So "correctness" of whatever is deemed correct semantics. $\endgroup$ – mavavilj Dec 30 '15 at 17:38
  • 3
    $\begingroup$ "Correctness" of what? The program being compiled vs some specification? Completely outside the scope of a compiler. $\endgroup$ – vonbrand Dec 30 '15 at 19:01
2
$\begingroup$

Does checking for correctness belong predominantly to "semantic analysis"?

It depends on what you mean with 'correctness'.

  1. Correctness of syntax. This is checked during lexical analysis, and incorrect syntactical structures are rejected.

  2. Correctness of semantics. During the semantical analysis the compiler rejects statements that are syntactically correct, but are nonsensical semantically. E.g. 10 / "blue".

  3. Correctness of the effects of code. This has no place in a traditional compiler. A traditional compiler merely takes the code and transforms it into a different form (more) readily acceptable for execution. What the code is supposed to implement it does not care about.

    However, it is not that black and white, as compilers will often warn about lexically and semantically perfectly legal constructs that are likely to be misinterpreted, e.g. in C++ an assignment inside a conditional:if (x = 5) ....

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.