0% found this document useful (0 votes)
30 views

CSC305 Chapter 2 (Part 2)

Uploaded by

ustazdanshirfan
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)
30 views

CSC305 Chapter 2 (Part 2)

Uploaded by

ustazdanshirfan
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
You are on page 1/ 27

Chapter 2:

Language
Design
Principles
(part 2)
Topics

• Describing syntax and semantics


• Lexical and syntax analysis
• Names, Data types
• Binding, type checking and scopes
• Expressions & assignment statements
• Statement-level control structures
• Subprograms
Lexical and
syntax
analysis
Compilation process
Lexical analysis

• Lexical analyzer is the first


phase of a compiler.
• Its main task is to read input
character and process as
output a sequence of tokens
that parser uses for syntax
analysis.
• It matches a given pattern of
character.
• Part of syntax analysis.
Syntax analysis

• Often called parsing.


• Syntax analyzer is a parser.
• Goal of syntax analysis:
❑ The syntax analyzer must
check the input program
to determine whether it is
syntactically correct .
❑ To produce complete
parse tree, or at least
trace the structure of the
complete parse tree, for
syntactically correct input.
Lexical Analysis vs Syntax Analysis

Lexical Analysis Syntax Analysis


Process of converting a sequence of Process of analyzing a string of
character into a sequence of tokens symbols either natural language,
Lexing and tokenization are other computer languages or data
name for lexical analysis structures conforming to the rules of
formal grammar
Read the source program one
character at a time and converts it Syntactic analysis and parsing are
into meaningful lexemes (tokens) other names for syntax analysis
First phase of compilation process Takes the tokens as input and
generates a parse tree as output
Second phase of compilation
process
Names and
Data Types
Names

• Names/identifier : String of characters used to identify some entity in


a program.
• Common : a letter followed by a string of letters, underscore.
• ‘camel’ notation : eg: mySalary
• Some PL limit the length of name(FORTRAN)some do not(JAVA,c++)
• Some programming begin with special character, eg: PHP, Ruby, Perl.
• Case sensitive issue
• Special word(reserve word, keyword) and variable.
Names
Keyword

• A keyword is a word of a PL that is special only in certain contexts.


• Example: the word “Real” in Fortran
• Real num; Declarative type
• Integer Real; Variable name
Real = 3;
Reserved word

• Reserved word is a special word of a PL, that cannot be used as a name


• Reserved words are BETTER that keywords, because the ability to define
keywords can be confusing
• Eg: Integer Real
• The appearance elsewhere in the program could be misleading to the
program readers.
• C, C++, Java: names are predefined and cannot be redefined once
imported
User Defined Names

• Can be applied to:

Variables or data storage areas

Label or points within the sequence of program statement

Subprograms/ functions/ procedures/ macros/ subroutine

Formal parameter – the names by which passed value are referred to within subroutines
Variables

• Programmers often think of variables as names for memory locations, but there is
much more to a variable than just a name.
• A variable can be characterized as a tuple of attributes:
1. Name: any names (except reserved words) that we can declare as a variable.
2. Address: a memory address, or storage location where the actual location for a particular
variable in memory
3. Value: the value that associate with the variable and stores in the particular memory
location
4. Type: determines the type of variable (eg: int, character, string, the particular range of
value, etc)
5. Lifetime: variable life time determines how long the variable life time before it is destroyed
6. Scope: defines the capacity of variables to be accessed within a program segment (eg:
local and global variables)
Data Types

• Defines the collection of data values and the set of predefined operations on those values
• Can be classified into two :
Built in data types User define data type

Primitive data type – 4 main types: int, real Enumeration Type, Subrange Type, Object
(float), Boolean, character Type

Composite data type:


• Homogenous (array) and heterogenous (struct)
• Group – fixed sized data type (array, strings,
pointers, structures, functions, list) and dynamic
sized data type (linked list, stacks, queues)

Recursive data type – commonly used in


functional and logic
Binding,
type
checking,
scopes
Binding

• A process of association of attribute variables during compilation


and execution of a program.
• Binding -> Name; Memory Address; Value; Type

A name as a
An integer data variable A value
type associated to a

int n = 5 variable

This data type has a location in a main memory that can be bound either statically or
dynamically
Binding

• A process of association of attribute variables during compilation


and execution of a program.

• Early binding

Static binding • Binds attribute at compile time & translate into


machine language.

• Binds attribute at run time

Dynamic binding • Type of variable is not specified at declaration


statement
• Bind when it is assigned to a value
Type checking

• The compiler must perform static type checking.


• Static type checking is done at the compile time.

Advantage Disadvantage
Less costly Reduced
programmer
flexibility
• Example:
Compiler will report an error if operator is applied to incompatible operand.

• Example of type error:


If an integer value was passed to a function that expected a float value
Type checking (cont.)

• In most language, types either basic or constructed.


• Basic types are the atomic types with no internal structure such as boolean,
integer, real, character, subrange and enumerated.
• Constructed types are arrays, records, sets, pointer, function

Static and dynamic checking type


• Checking done by compiler is static while if done at runtime is dynamic.
Scopes

• The scope of a variable is the range of statements over which it is


visible:
• Variable yyy is visible in statement st1 if yyy can be referenced in st1.

• The scope rules of a language determine how occurrences of names


are associated with variables:
• static scoping.
• dynamic scoping.
Scopes (cont.)

• Two types of variable:


• The range of statement in which the variable is visible.
• Scope can be declared as global or global variable.
Static scoping

• Also known as lexical scoping.


• Definition is resolved by searching it containing block or function. If
that fail, then searching the outer containing block and so on.
Static scoping:
Example
Dynamic scoping

• In dynamic scoping, the definition of the variable is resolved by


searching it containing block and if it is not found, then searching its
calling function and if it still not found then the function which called
that calling function will be such will be searched and so on.
Dynamic scoping:
Example

You might also like