Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Types of Errors during Compilation and at Runtime

I have this question in a homework assignment for my Computer Languages class. I'm trying to figure out what each one means, but I'm getting stuck.

Errors in a computer program can be classified according to when they are detected and, if they are detected at compile time, what part of the compiler detects them. Using your favorite programming language, give an example of:

(a) A lexical error, detected by the scanner.

(b) A syntax error, detected by the parser.

(c) A static semantic error, detected (at compile-time) by semantic analysis.

(d) A dynamic semantic error, detected (at run-time) by code generated by the compiler.

For (a), I think this is would be correct: int char foo;

For (b), int foo (no semicolon)

For (c) and (d), I'm not sure what is being asked.

Thanks for the help.

like image 341
Reti Avatar asked Sep 09 '10 03:09

Reti


People also ask

What are compilation and runtime errors?

A compile-time error generally refers to the errors that correspond to the semantics or syntax. A runtime error refers to the error that we encounter during the code execution during runtime. Fixation. We can easily fix a compile-time error during the development of code. A compiler cannot identify a runtime error.

What type of error occurs during runtime?

A runtime error occurs when a program is syntactically correct but contains an issue that is only detected during program execution. These issues cannot be caught at compile-time by the Java compiler and are only detected by the Java Virtual Machine (JVM) when the application is running.

What are the 3 types of programming errors?

When developing programs there are three types of error that can occur: syntax errors. logic errors. runtime errors.

What errors are detected at compile-time?

Compile-Time Errors: Errors that occur when you violate the rules of writing syntax are known as Compile-Time errors. This compiler error indicates something that must be fixed before the code can be compiled. All these errors are detected by the compiler and thus are known as compile-time errors.


2 Answers

I think it's important to understand what a scanner is, what a parser is and how they are involved in the compilation process.

(I'll try my best at a high-level explanation)

The scanner takes a sequence of characters (a source file) and converts it to a sequence of tokens. e.g., sees the text if 234 ) and converts to the tokens, IF INTEGER RPAREN (there's more to it but should be enough for the example).

Another way you can think of how the scanner works is that it takes the text and makes sure you use the correct keywords and not makes them up. It has to be able to convert the entire source file to the associated language's recognized tokens and this varies from language to language. In other words, "Does every piece of text correspond to a construct a language understands". Or better put with an example, "Do all these words found in a book, belong to the English language?"


The parser takes a sequence of tokens (usually from the scanner) and (among other things) sees if it is well formed. e.g., a C variable declaration is in the form Type Identifier SEMICOLON.

The parser checks "Does this sequence of tokens in this order make sense to me?" And similarly the analogy, "Does this sequence of English words (with punctuation) form complete sentences?"


C asks for errors that can be found when compiling the program. D asks for errors that you see when running the program after it compiled successfully. You should be able to distinguish these two by now hopefully.

I hope this helps you get a better understanding and make answering these easier.

like image 73
Jeff Mercado Avatar answered Oct 05 '22 18:10

Jeff Mercado


I'll give it a shot. Here's what I think:

a. int foo+; (foo+ is an invalid identifier because + is not a valid char in identifiers)

b. foo int; (Syntax error is any error where the syntax is invalid - either due to misplacement of words, bad spelling, missing semicolons etc.)

c. Static semantic error are logical errors. for e.g passing float as index of an array - arr[1.5] should be a SSE.

d. I think exceptions like NullReferenceException might be an example of DME. Not completely sure but in covariant returns that raise an exception at compile time (in some languages) might also come in this category. Also, passing the wrong type of object in another object (like passing a Cat in a Person object at runtime might qualify for DME.) Simplest example would be trying to access an index that is out of bounds of the array.

Hope this helps.

like image 20
Sidharth Panwar Avatar answered Oct 05 '22 17:10

Sidharth Panwar