Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Applications of Types and Declarations
3.
Type Expressions
4.
Type Equivalence
5.
Declarations
6.
Frequently Asked Questions
6.1.
What are the types and declarations in compiler design?
6.2.
What is type declaration?
6.3.
What is declaration compiler design?
6.4.
What is a type checker?
6.5.
What is the purpose of a declaration?
7.
Conclusion
Last Updated: Mar 27, 2024

Types and Declarations

Author Abhay Trivedi
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

In this article, we will discuss Types and Declarations in compiler design. Typical basic types and declarations for a language contain boolean, char, integer, float, and void; here, void denotes "the absence of a value." A type name is a type expression. We can form a type expression by applying the array type constructor to a number and a type expression.

Declaration involves allocating space in memory and entering type and name in the symbol table. Memory allocation is consecutive, and it assigns names to memory in the sequence they are declared in the program.

Compiler Design

Also read About, Specifications of Tokens in Compiler Design

Applications of Types and Declarations

• Type checking: It uses logical rules to reason regarding the behavior of a program at run time. It ensures that the operands match the type that an operation expects. For example, the && operator in Java expects two booleans as operands and returns a boolean result.

• Translation Applications: A compiler can determine the storage that we will need for that name(declared within a procedure or a class) at run time from the type of name. Type information is also required to calculate the address denoted by an array reference, to insert explicit type conversions, and to choose the correct version of an arithmetic operator, among other things.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Type Expressions

A primary type is a type of expression. Typical basic types for a language contain boolean, char, integer, float, and void; the void implies "the absence of a value."

We can form a type name or type expression by applying the array type constructor to a number and a type expression.

Types have structure, which we can represent using type expressions: a type expression is either a primary type or is formed by applying a type constructor operator to a type expression. The basic types and constructors depend on the language to be checked.

For Example,

 Type Expressions

The array type int [2][3][2] can be read as an "array of 2 arrays each of 3 arrays of 2 integers each" and written as a type expression array(2, array(3, array(2, integer))).

Basic types

          char, int, double, float are type expressions.

Type names

          It is convenient to consider that names of types are type expressions.

Arrays

If E is a type expression and n is an int, then

array(nE)

is a type expression indicating the type of an array with elements in T and indices in the range 0 ... n - 1.

Products

If E1 and E2 are type expressions then

E1 × E2

is a type expression indicating the type of an element of the Cartesian product of E1 and E2. The products of more than two types are defined similarly, and this product operation is left-associative. Hence

(E1 × E2) × E3 and E1 × E2 × E3

are equivalent.

Records

The only difference between a record and a product is that the fields of a record have names.

If abc and xyz are two type names, if E1 and E2 are type expressions then

record(abcE1, xyzE2)

is a type expression indicating the type of an element of the Cartesian product of T1 and T2.

Pointers

Let E is a type expression, then

pointer(E)

is a type expression denoting the type pointer to an object of type T.

Function types

If E1 and E2 are type expressions then

E1 -> E2

is a type expression denoting the type of functions associating an element from E1 with an element from E2.

Also See, Top Down Parsing

Type Equivalence

When graphs represent type expressions, two types are structurally equivalent if and only if one of the following conditions is true:

  • They are of the same basic type.
  • They are formed by applying the identical constructor to structurally equivalent types.
  • One is a type name that refers to the other.

 

The first two conditions in the above definition lead to the name equivalence of type expressions if type names are interpreted as though they stand for themselves.

"If two type expressions are equal, return a specified type else error," says a common type-checking rule. Ambiguities might develop when type expressions are given names and then utilized in later type expressions. The critical question is whether a type expression's name refers to itself or is an abbreviation for another type of expression.

Type names can create implicit cycles by denoting type expressions. The resulting graph can have cycles owing to recursive types if edges to type names are redirected to the type expressions given by the names.

Example of a non Cyclic graph:

Example of Non-Cyclic Graph

Declarations

When we encounter declarations, we need to layout storage for the declared variables.

For every local name in a procedure, we create a Symbol Table(ST) entry including:

  1. The type of the name
  2. How much storage the name requires

Grammar:

D -> real, id
D -> integer, id
D -> D1, id

We use ENTER to enter the symbol table and use ATTR to trace the data type.

Production Rule (PR) Semantic Action

-> real, id

ENTER (id.PLACE, real)

D.ATTR = real

-> integer, id

ENTER (id.PLACE, integer)

D.ATTR = integer

-> D1, id

ENTER (id.PLACE, D1.ATTR)

D.ATTR = D1.ATTR

 

A succession of declarations is generated by nonterminal D: Basic, array, and record types are developed by nonterminal T. One of the basic types int and float is caused by nonterminal B. Nonterminal C, which stands for "component," creates strings of zero or more integers, each encircled by brackets. A primary kind given by B is followed by array components specified by nonterminal C in an array type. A record type (T's second product) is a set of declarations for the record fields, all of which are enclosed in curly braces.

Frequently Asked Questions

What are the types and declarations in compiler design?

Standard basic types for a language include boolean, char, integer, float, and void; the latter denotes "the absence of a value." A type name is a type expression. We can form a type expression by applying the array type constructor to a number and a type expression.

What is type declaration?

A type declaration statement specifies objects and functions' type, length, and attributes. You can assign initial values to objects. A declaration type specification (declaration_type_spec) is used in a nonexecutable statement.

What is declaration compiler design?

Declaration involves allocating space in memory and entering type and name in the symbol table. It does memory allocation consecutively and assigns names to memory in the sequence they are declared in the program.

What is a type checker?

Type checking is the process of testing and enforcing type constraints at compile time (statically) or runtime (dynamically). The goal of type checking is to ensure that the program is type-safe, which means that type mistakes are minimal.

What is the purpose of a declaration?

Declarations are essential because they inform the compiler or interpreter what the identifying word means and how to use it. A declaration may be optional or required, depending on the programming language.

Conclusion

This article teaches about types and declarations in compiler design. We also discussed the characteristics, benefits, and applications of Types and Declarations.

Recommended Reading:

 

Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Do upvote our blog to help other ninjas grow.

Happy Learning!

Previous article
Three address code in Compiler
Next article
Translation Of Expressions
Live masterclass