Semantic errors

Can semantic errors be detected by the compiler or not? If not, when are errors detected?

As far as I know, semantic errors are those errors that arise from expressions associated with operators with the wrong number / type of operands.

For instance:

n3=n1*n2;//n1 is integer, n2 is a string, n3 is an integer 

The above statement is semantically incorrect.

But while reading Stephen Prata's C Primer Plus, I found the following statement

The compiler does not detect semantic errors, because they do not violate the rules of C. The compiler does not have the ability to guess your true intentions. This leaves you the opportunity to find such errors. One way is to compare what the program does with what you expected from it.

If not a compiler that detects these errors?

Did I miss something?

+4
source share
11 answers

The word semantics is ambiguous, and you come across two slightly different meanings in these different contexts.

The first value (your code) is related to how the compiler interprets the code you entered. But there are different degrees of interpretation for this. The syntax is one level where the interpretation simply determines that n1*n2 means that you want to do the multiplication. But there is also a higher level of interpretation - if n1 is an integer and n2 is a floating point, what is the result? What if I drop it, if it is rounded, truncated, etc.? These are “semantic” questions, not syntactic ones, but someone decided that yes, the compiler can answer most of them.

They also decided that the compiler had limitations on what it could (and should!) Interpret. For example, he may decide that casting to int is truncation rather than rounding, but he cannot decide what you really need when you are trying to multiply the array by a number.

(Sometimes people decide that they CAN, though. In Python, [1] * 3 == [1,1,1] .)

The second meaning means much wider coverage. If it is assumed that the result of this operation will be sent to a peripheral device, which can take values ​​from 0x000 to 0xFFF, and you multiply 0x7FF by 0x010, it is obvious that you made a semantic error. Peripheral device designers must decide whether to handle this or how. You, as a programmer, can also take steps to test sanity. But the compiler has no idea about these external semantic restrictions or about how to enforce them (filter user input? Return error? Truncate? Wrap?), Which is what the second quote says.

+5
source

"Semantic error" is another term for "logical error", where you literally write the wrong code. For example, writing n3=n1*n2 when you really want to split - the compiler cannot say that your algorithm should divide instead of multiplying; you told him to multiply, the way it is.

The error described in your example is a type safety error, and compilers can catch it during the type checking phase (if the language is strongly typed)

+5
source

There are basically three types of errors.

1) Syntax errors. This is invalid code that the compiler does not understand, for example. your example of multiplying a string with an integer in C. The compiler will detect them because it cannot compile them.

2) Semantic errors. This is valid code that the compiler understands, but they are not what you, the programmer, are destined for. They may use the wrong variable, the wrong operation, or the operations in the wrong order. The compiler cannot detect them.

There is a third class, which may be the most expensive:

3) Design errors. The code is correct and error-free and does exactly what you intended. But your intentions are wrong, for example. based on incorrect assumptions, wrong models, or you used the wrong forms, misunderstood the client or such.

+2
source

Semantic errors are all those where your code does what you did not intend.

These errors can be detected by testing or analysis.

Analysis means that you or the tool are reviewing your code and trying to figure out the problems. This includes the use of code reviews and static analyzers.

Testing is when you provide your program with some inputs that are expected to produce the given result if the program is semantically correct. So, if the actual output does not match the expected result, the program is semantically incorrect.

It’s so simple that you are a developer or tester who needs to catch semantic errors.

+1
source

I think that the writer who wrote the book had a different definition of "semantic." For most compilers, there is a step that includes semantic checks .

Semantic analysis is the stage in which the compiler adds semantic information to the parse tree and builds a symbol table. This phase performs semantic checks, such as type checking (checking type errors) or object binding (linking variable and function references with their definitions) or a specific purpose (requiring initialization of all local variables before use), rejection of incorrect programs, or warning. Semantic analysis usually requires a complete parsing tree, which means that this phase logically follows the parsing phase and logically precedes the code generation phase, although it is often possible to reset several phases in a single pass through the code in the compiler implementation.

+1
source

In fact, multiplication of a string and an integer is a syntax error, since the multiplication of incompatible types (such as string and integer) is not defined in C.

A semantic error is an error that occurs when your program compiles but does not do what you want.

0
source

This quote talks about things like x <= 1 , where you really should have done x < 1 .

But for the semantics of the language (not allowing you to add a string and an integer), yes, this is a compiler that handles this.

0
source

This is a syntax error that compilers can actually detect and report.

A semantic error is more like compiling fine (down to the types itself), but that’s not what you want. Semantic errors are part of your algorithm more than your actual syntax.

0
source

If not a compiler that detects these errors?

Sometimes, no one: the compiler does not have to insert any checks at runtime that can help notice an error when this happens, and the execution just continues.

Sometimes a runtime environment: the program accesses an invalid address due to an error and is located outside the address space available for the process.

You can use an additional compiler with a static analyzer to detect some or all errors in the program, but they can also have false positives: they can give a warning for a piece of code that works without errors.

0
source

String literals and strings are represented in memory as numbers (bytes / m of a word or high-level ones - shorts, integers). C is a low-level programming level in which all things approach the level of a machine / assembler. Therefore, multiplying a number by a string literal (if it is an array, it will be incorrect) is correct because this string literal will actually (after compilation) be a number.

0
source

Actually (since there is no type of string in C, but only char* ), you can very well propagate n1 with n2 . The operation is legal and clearly defined, so the compiler will not throw an error.

Logically (semantically) a statement makes very little sense, therefore, most likely, this is an encoding error. To answer your question: you are responsible for detecting and correcting such errors.

-1
source

Source: https://habr.com/ru/post/1309514/


All Articles