views:

42

answers:

2

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.

+2  A: 

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.

Sidharth Panwar
+2  A: 

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.

Jeff M