0% found this document useful (0 votes)
32 views4 pages

Compiler Type Checking Explained

Static type checking is performed at compile time by examining the program text to check that variables are used according to their declared types. This catches many errors but reduces flexibility. Dynamic type checking is performed at runtime by associating type tags with values. It is more flexible but does not catch as many errors until runtime. Both approaches have advantages and disadvantages.

Uploaded by

yaredteshager01
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views4 pages

Compiler Type Checking Explained

Static type checking is performed at compile time by examining the program text to check that variables are used according to their declared types. This catches many errors but reduces flexibility. Dynamic type checking is performed at runtime by associating type tags with values. It is more flexible but does not catch as many errors until runtime. Both approaches have advantages and disadvantages.

Uploaded by

yaredteshager01
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Rules of Type Checking

– Type Checker is a module of a compiler devoted to type checking tasks.


Compiler Design – Type checking is the process of verifying and enforcing constraints of types in
values.
Chapter 5: Type Checking – A compiler must check that the source program should follow the syntactic and
semantic conventions of the source language and it should also check the type
rules of the language.
– It allows the programmer to limit what types may be used in certain circumstances
Instructor: Fikru T. (MSc.)
Email: fikrutafesse08@[Link] and assigns types to values.
– The type-checker determines whether these values are used appropriately or not.

1 2

Cont'd ...
– It checks the type of objects and reports a type error in the case of a violation,
Type Conversions
– Conversion from one type to another type is known as implicit if it is to be done
and incorrect types are corrected.
automatically by the compiler.
– Whatever the compiler we use, while it is compiling the program, it has to follow
– Implicit type conversions are also called Coercion and coercion is limited in many
the type rules of the language.
languages.
– Every language has its own set of type rules for the language.
– Example: An integer may be converted to a real but real is not converted to an
– We know that the information about data types is maintained and computed by
integer. float a =20;
the compiler.
– The information about data types like INTEGER, FLOAT, CHARACTER, and all int b = a; // implicit conversion
– Conversion is said to be Explicit if the programmer writes something to do the
the other data types is maintained and computed by the compiler.
Conversion.
– The compiler contains modules, where the type checker is a module of a compiler int a=10
– Example
and its task is type checking. 3 c=float(a) 4

Page 1
Types of Type Checking Types of Type Checking Cont.….
• There are two kinds of type checking: – Examples of Static checks include:
1. Static Type Checking i. Type-checks
2. Dynamic Type Checking • A compiler should report an error if an operator is applied to an incompatible
1. Static Type Checking operand.
• Is type checking performed at compile time. • For example, if an array variable and function variable are added together.
• It checks the type variables at compile time, which means the type of the variable is ii. The flow of control checks
known at the compile time. • Statements that cause the flow of control to leave a construct must have
• It generally examines the program text during the translation of the program. someplace to which to transfer the flow of control.
• Using the type rules of a system, a compiler can infer from the source text that a • For example, a break statement in C causes control to leave the smallest
function(fun) will be applied to an operand (a) of the right type each time the expression enclosing while, for, or switch statement, an error occurs if such an enclosing
fun(a) is evaluated. 5 statement does not exist. 6

Cont'd ...
Types of Type Checking Cont.….
The Benefits of Static Type Checking
i. Uniqueness checks
• There are situations in which an object must be defined only once.
• Runtime Error Protection.

• There are situations in which an object must be defined only once. • It catches syntactic errors like spurious words or extra punctuation.

• For example, in Pascal an identifier must be declared uniquely, labels in a case • It catches wrong names like Math and Predefined Naming.
statement must be distinct, and else a statement in a scalar type may not be • Detects incorrect argument types.
represented. • It catches the wrong number of arguments.
iv. Name-related checks: • It catches wrong return types, like return "70", from a function that’s declared
• Sometimes the same name may appear two or more times. to return an int.
• For example in Ada, a loop may have a name that appears at the beginning and
end of the construct.
• The compiler must check that the same name is used at both places. 7 8

Page 2
Cont'd ... Cont'd ...
Disadvantages of Static Type Checking 2. Dynamic Type Checking:
• There are the following disadvantages of static type checking which are as follows: • The type checking being done at run time.
 It can reduce programmer flexibility. • In Dynamic Type Checking, types are associated with values, not variables.
 Many languages like APL and SNOBOL4, because of their dynamic type binding, • Implementations of dynamically type-checked languages runtime objects are
enable only dynamic type checking. generally associated with each other through a type tag,
 In languages without declarations, there is no static type checking is possible. • which is a reference to a type containing its type information.
 Static type checking likely to influence declarations, data control structures, and • Dynamic typing is more flexible.
supplies for independent compilation of subprograms.
• A static type system always restricts what can be conveniently expressed.
 Static type checking is complex when a language enables a memory cell to save
• Dynamic typing results in more compact programs since it is more flexible
values of multiple types at various times during execution.
and does not require types to be spelled out.
9 10

Cont'd ... Cont'd ...


Advantage of Dynamic Type Checking Disadvantage of Dynamic Type Checking
• There are the following advantages of dynamic type checking which are as follows • Disadvantages of dynamic type checking which are as follows:
There is no flexibility in program design.  The dynamic type checking takes up more space as the dynamic type checking
There is no requirement for declarations. involves inserting extra code into the program to detect impending errors.
The type of data object associated with a variable name can be changed as  Programs are difficult to debug as all possible execution paths are not explored
per need during the program execution. during program testing.
Dynamic type checking can find many errors that cannot be identified by  The dynamic type checking takes more time which reduces the speed of
static type checking. executing the operation.
In most languages, static type checking is not possible for some language  It is less efficient than static type checking.
constructs in certain cases but the same purpose can be achieved by dynamic  It is more expensive.
type checking. 11  Errors can lurk in a program until they are reached during execution. 12

Page 3
Cont'd ... Cont'd ...
• Programming with a static type system often requires more design and
Large programs tend to have portions that are rarely executed, so a program
implementation effort.
can be in use for a long time before dynamic type checking detects a type
• Languages like Pascal and C have static type checking.
error.
• Type checking is used to check the correctness of the program before its
 Properties that depend on values computed at runtime are rarely checked.
execution.
• For example, imperative languages rarely check that an array index is
• The main purpose of type-checking is to check the correctness and data type
within bounds.
assignments and type-casting of the data types is syntactically correct or not
 The fundamental hardware does not implement for dynamic type checking.
before its Execution.
 It can manage the tags and increases the complexity.
• Whether it is syntactically correct or not before its execution.
• Static Type-Checking is also used to determine the amount of memory needed to
13 14
store the variable.

Cont'd ... Cont'd ...


The design of the type-checker depends on: – The token streams from the lexical analyzer are passed to the PARSER.
– Syntactic Structure of language constructs. – The PARSER will generate a syntax tree.
– The Expressions of languages. – When a program (source code) is converted into a syntax tree, the type-checker
– The rules for assigning types to constructs (semantic rules). plays a Crucial Role.
– So, by seeing the syntax tree, you can tell whether each data type is handling the
correct variable or not.
– The Type-Checker will check and if any modifications are present, then it will
modify.

Figure: The Position of the Type checker – It produces a syntax tree, and after that, Intermediate Code Generation is done.

15
End!!! 16

Page 4

Common questions

Powered by AI

Static type checking is performed at compile time, examining program text to infer correct application of functions to operands. It provides runtime error protection by catching syntactic errors, wrong names, incorrect argument types, and more, but it can reduce programmer flexibility . Dynamic type checking occurs at runtime, with types associated with values, allowing more flexibility as types can be changed during execution without declarations. However, it tends to be less efficient, slower during execution, and more expensive compared to static typing .

Static type checking challenges arise in languages that allow runtime type variability because the static system requires all variable types to be fixed and verifiable at compile time. In such languages, variables can hold different types at various execution times, complicating static type verification. This rigidity can limit expression convenience, complicate program structure, and interfere with the language's intended dynamism, which can be better supported by dynamic type checking .

In the type checking process, the parser generates a syntax tree from the token streams received from the lexical analyzer. The type-checker uses this syntax tree to verify whether each data type is managing the correct variables. It is crucial in determining the correctness of type assignments, and any discrepancies found are modified. The syntax tree facilitates further processes such as Intermediate Code Generation .

Implicit type conversions, or coercions, occur automatically by the compiler, such as converting an integer to a real. This is limited in many languages. Explicit conversions require the programmer to manually enforce the conversion through explicit commands, such as casting an integer to a float in code. This distinction helps prevent type-related errors and enforces type safety during programming .

The design of a type checker is heavily influenced by a language's syntactic structure and semantic rules, which determine how expressions and constructs are analyzed for type correctness. Syntactic rules guide the type checker in parsing and structural validation, while semantic rules dictate the constraints and permissible operations on types. These rules together ensure that type checking aligns with the language's specifications, thereby maintaining program correctness and preventing type errors before execution .

Static type checking improves programming safety by providing runtime error protection, detecting syntactic errors, and ensuring correct argument types and numbers. It prevents errors related to incompatible operations and incorrect data types before execution . However, it may reduce programmer flexibility and is complex when dealing with languages that allow memory cells to hold multiple types over time. Static typing also is not feasible in languages without explicit type declarations .

Uniqueness checks ensure that objects must be defined only once within a program, such as unique identifiers or labels. Name-related checks ensure that occurrences of names in certain contexts, such as loop names in Ada, are consistent. These checks prevent errors related to confusing or misusing names and identifiers, ensuring that the program semantics align with its syntax rules .

Dynamic type checking enhances program design flexibility by allowing the type of a data object associated with a variable to be changed as needed during execution, eliminating the need for explicit declarations. This flexibility can lead to more compact programs. However, it introduces complexity as errors might not surface until execution. Dynamic checking uses more space and time, which can decrease program efficiency. Errors could remain hidden until they are encountered during runtime, especially in rarely executed code segments, complicating debugging .

During compilation, the type-checker module works alongside the lexical analyzer and the parser. The lexical analyzer provides token streams, which the parser uses to create a syntax tree. The type-checker examines this syntax tree to validate type correctness and adherence to type rules. It modifies the tree if necessary and prepares it for Intermediate Code Generation. This sequence ensures the source program's syntactic and semantic integrity before converting it into machine code .

A type checker in a compiler is responsible for verifying and enforcing constraints of types in values. It ensures that the source program adheres to the syntactic and semantic conventions of the source language, and checks the type rules of the language. It also determines whether values are used appropriately and reports a type error in cases of violations. Additionally, the type checker manages data type information such as INTEGER, FLOAT, and CHARACTER .

You might also like