Saturday, 19 October 2013

Source-code errors found during compilation

There might be questions in CIE from chapter 3.2 asking about errors found during lexical, syntax and semantic stages of compilation. Below I describe found errors

Errors during Lexical Analysis:
There are relatively few errors which can be detected during lexical analysis.

a) Strange characters
Some programming languages do not use all possible characters, so any strange ones which appear can be reported. Note however that almost any character is allowed within a quoted string.

b) Long quoted strings (1)
Many programming languages do not allow quoted strings to extend over more than one line; in such cases a missing quote can be detected. Languages of this type often have some way of automatically joining consecutive quoted strings together to allow for really long strings.

c) Long quoted strings (2)
If quoted strings can extend over multiple lines then a missing quote can cause quite a lot of text to be 'swallowed up' before an error is detected. The error will probably then be reported as somewhere in the text of the next quoted string, which is unlikely to make sense as part of a program.

d) Invalid numbers
A number such as 123.45.67 could be detected as invalid during lexical analysis (provided the language does not allow a full stop to appear immediately after a number). Some compiler writers prefer to treat this as two consecutive numbers 123.45 and .67 as far as lexical analysis is concerned and leave it to the syntax analyser to report an error. Some languages do not allow a number to start with a full stop/decimal point, in which case the lexical analyser can easily detect this situation.

Errors during Syntax Analysis:
During syntax analysis, the compiler is usually trying to decide what to do next on the basis of expecting one of a small number of tokens. Hence in most cases it is possible to automatically generate a useful error message just by listing the tokens which would be acceptable at that point.

Source: A + * B
Error:  Found '*', expect one of: Identifier, Constant, '('
More specific hand-tailored error messages may be needed in cases of bracket mismatch.

Source: C := ( A + B * 3 ;
Error:  Missing ')' or earlier surplus '('

Errors during Semantic Analysis
One of the most common errors reported during semantic analysis is "identifier not declared"; either you have omitted a declaration or you have misspelt an identifier.
Other errors commonly detected during semantic analysis relate to incompatible use of types, e.g. attempt to assign a logical value such as true to a string of characters. Some of these errors can be quite subtle, but again it is easy to automatically generate fairly precise error messages.

Source: SomeString := true;
Error:  Can't assign logical value to character string
The extent to which such type checking is possible depends very much on the source language.

PL/1 allows an amazingly wide variety of automatic type conversions, so relatively little checking is possible.

Pascal is much more fussy; you can't even assign a real value to an integer variable without explicitly specifying whether you want the value to be rounded or truncated.

Some compiler writers have argued that type checking should be extended to cover the appropriate units as well for even more checking, e.g. it doesn't make sense to multiply a distance by a temperature.
Other possible sources of semantic errors are parameter miscount and array subscript miscount. It is generally an error to declare a subroutine as having 4 parameters and then call that routine with 5 parameters (but some languages do allow routines to have a variable number of parameters). It is also generally an error to declare an array as having 2 subscripts and then try and access an array element using 3 subscript.

O, AS & A Level Computers FREE Resources

Discussion Groups:
A Level: https://www.facebook.com/groups/computing9691/
O Level: https://www.facebook.com/groups/olevelcomputer/
Fan Pages:
A Level: https://www.facebook.com/ALevelComputer
O Level: https://www.facebook.com/OLevelComputer
Websites:
A Level: https://sites.google.com/site/computing9691/
Post a Comment