14.4 Detecting programming errors by linguistic formalism

14.4 Detecting programming errors by linguistic formalism

The front end of a compiler performs lexical, syntactic, and semantic analysis. Not all compilers find the same defects. The more information a compiler has, the more defects it can find. Some compilers operate in a “forgiving” mode but have a “strict” or “pedantic” mode, if you request it.

14.4.1 Regular grammars

Lexical analyzers can find the following errors:

Characters in the source that aren’t in the alphabet of the language

Words in the source that aren’t in the vocabulary of the language

14.4.2 Context-free grammars

Syntactic analyzers for fixed-form languages such as Fortran and COBOL can find the following errors:

Required fields that haven’t been used

Incorrect values in a field

Invalid continuations of statements across multiple lines

Keywords that have been misspelled

Required punctuation that is missing

Delimiters such as parentheses that are missing

Blank or tab characters that are missing

Blank or tab characters that shouldn’t occur where they’re found

Syntactic analyzers for free-form languages such as C++ and Java can find the following errors:

Comment delimiters that have been put in the wrong place or omitted

Literal delimiters that have been put in the wrong place or omitted

Keywords that have been misspelled

Required punctuation that is missing

Construct delimiters such as parentheses or braces that have been misplaced

Blank or tab characters that are missing

Blank or tab characters that shouldn’t occur where they’re found

14.4.3 Semantic analysis

If a semantic analyzer has a symbol table for each separate procedure, it can find semantic errors that occur because of the following mistakes:

Names that aren’t declared

Operands of the wrong type for the operator they’re used with

Values that have the wrong type for the name to which they’re assigned

If a semantic analyzer has a symbol table for the program as a whole, it can find semantic errors that occur because of the following mistakes:

Procedures that are invoked with the wrong number of arguments

Procedures that are invoked with the wrong type of arguments

Function return values that are the wrong type for the context in which they’re used

If a semantic analyzer has control-flow and data-flow information for each separate procedure, it can find semantic errors that occur because of the following mistakes:

Code blocks that are unreachable

Code blocks that have no effect

Local variables that are used before being initialized or assigned

Local variables that are initialized or assigned but not used

If a semantic analyzer has control-flow and data-flow information for the program as a whole, it can find semantic errors that occur because of the following mistakes: