Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Notes  >  Compiler Design  >  Type Checking & Type Expressions: Intermediate Code Generation

Type Checking & Type Expressions: Intermediate Code Generation | Compiler Design - Computer Science Engineering (CSE) PDF Download

Type Expression:
•The type of a language construct is denoted by a type expression.

•A type expression can be:

–A basic type

•a primitive data type such as integer, real, char, Boolean, …

•type-error to signal a type error

•void: no type

–A type name

•a name can be used to denote a type expression.
 

–A type constructor applies to other type expressions.

•arrays: If T is a type expression, then array (I,T)is a type expression where I denotes index range. Ex: array (0..99,int)

•products: If T1and T2 are type expressions, then their Cartesian product T1 x T2 is a type expression. Ex: int x int

•pointers: If T is a type expression, then pointer (T) is a type expression. Ex: pointer (int)

•functions: We may treat functions in a programming language as mapping from a domain type D to a range type R. So, the type of a function can be denoted by the type expression D→R where D are R type expressions. Ex: int→int represents the type of a function which takes an int value as parameter, and its return type is also int.

Type Checking of Statements: S ->d= E

                                                                     { if (id.type=E.type then S.type=void
                                                                     else S.type=type-error }
 

S ->if E then S1
                                                                      { if (E.type=boolean then S.type=S1.type
                                                                       else S.type=type-error }
S->while E do S1
                                                                      { if (E.type=boolean then S.type=S1.type
                                                                       else S.type=type-error }
 

Type Checking of Functions: 
E->E1( E2)
                                                                                                 Type Checking & Type Expressions: Intermediate Code Generation | Compiler Design - Computer Science Engineering (CSE)
 

Ex: int f(double x, char y) { ... }
f:                    double x char->int argume types return type
 

 

Structural Equivalence of Type Expressions: 

•How do we know that two type expressions are equal?

•As long as type expressions are built from basic types (no type names), we may use structural equivalence between two type expressions

Structural Equivalence Algorithm (sequin):

if (s and t are same basic types) then return true

else if (s=array(s1,s2) and t=array(t1,t2)) then return (sequiv(s1,t1) and sequiv(s2,t2)) else if (s = s1 x

s2and t = t1 x t2) then return (sequiv(s1,t1) and sequiv(s2,t2))

else if (s=pointer(s1) and t=pointer(t1)) then return (sequiv(s1,t1))
Type Checking & Type Expressions: Intermediate Code Generation | Compiler Design - Computer Science Engineering (CSE)
 

Names for Type Expressions:

•In some programming languages, we give a name to a type expression, and we use that name as a type expression afterwards.

type link = ↑cell; ? p,q,r,s have same types ? var p,q : link;
var r,s : ↑cell

•How do we treat type names?

–Get equivalent type expression for a type name (then use structural equivalence), or

–Treat a type name as a basic type
 

 

The document Type Checking & Type Expressions: Intermediate Code Generation | Compiler Design - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Compiler Design.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
26 videos|66 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Type Checking & Type Expressions: Intermediate Code Generation - Compiler Design - Computer Science Engineering (CSE)

1. What is type checking in intermediate code generation?
Ans. Type checking is a process in intermediate code generation that ensures that the types of operands and operators are compatible. It is performed after the syntax analysis phase and before the code generation phase. The type checking phase ensures that the program is semantically correct and that the types of expressions are consistent.
2. What are type expressions in intermediate code generation?
Ans. Type expressions are used in intermediate code generation to represent the types of variables, expressions, and functions. A type expression is a symbolic representation of a type, such as int, float, or char. It is used by the compiler to determine the size and layout of data structures, and to perform type checking.
3. Why is type checking important in intermediate code generation?
Ans. Type checking is important in intermediate code generation because it helps to ensure that the program is semantically correct and that the types of expressions are consistent. Type checking also helps to catch errors early in the development process, which can save time and effort in debugging. Furthermore, type checking is essential for optimizing code, as it enables the compiler to generate code that is more efficient and uses less memory.
4. What are the benefits of using type expressions in intermediate code generation?
Ans. Using type expressions in intermediate code generation has several benefits. Firstly, it enables the compiler to determine the size and layout of data structures, which is essential for efficient memory management. Secondly, it enables the compiler to perform type checking, which helps to catch errors early in the development process. Thirdly, it enables the compiler to generate code that is more efficient and uses less memory, as it can optimize the use of data types.
5. How does intermediate code generation differ from machine code generation?
Ans. Intermediate code generation and machine code generation are two phases of the compiler process. Intermediate code generation produces an intermediate representation of the source code that is independent of the target architecture. This intermediate code is then optimized and translated into machine code during the machine code generation phase. The main difference between the two phases is that intermediate code generation is platform-independent, while machine code generation is platform-specific. Intermediate code can be used to generate machine code for multiple platforms, while machine code is specific to a particular platform.
26 videos|66 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

mock tests for examination

,

pdf

,

Extra Questions

,

Type Checking & Type Expressions: Intermediate Code Generation | Compiler Design - Computer Science Engineering (CSE)

,

Important questions

,

video lectures

,

study material

,

Free

,

Previous Year Questions with Solutions

,

shortcuts and tricks

,

Type Checking & Type Expressions: Intermediate Code Generation | Compiler Design - Computer Science Engineering (CSE)

,

Semester Notes

,

Summary

,

past year papers

,

Objective type Questions

,

MCQs

,

practice quizzes

,

Viva Questions

,

Exam

,

Sample Paper

,

ppt

,

Type Checking & Type Expressions: Intermediate Code Generation | Compiler Design - Computer Science Engineering (CSE)

;