SlideShare a Scribd company logo
1 of 76
UNIT-III
Game Theory
Principle of Programming Language is a set of rules and norms
governed to communicate instructions (high level instruction or
assemble level instruction) to a machine or particularly a
computer.”
Types of Programming Language that are classified are:
• Procedural Programming Language.
• Functional Programming Language.
• Scripting Programming Language.
• Logic Programming Language.
• Object-Oriented Programming Language.
Why Study Programming Languages?
Increased capacity to express ideas
Improved background for choosing appropriate language
Increased ability to learn new languages
Better understanding of the significance of implementation
Better use of languages that are already known
Overall advancement of computing
PROGRAMMING PARADIGMS
A Programming Paradigm is a pattern of problem-solving
thoughts that underlies a particular genre of programs and
languages.
Types:-
Imperative Programming
Object-Oriented Programming
 Functional Programming
Logic Programming
Syntax
Syntax is the study of sentence structure and the rules of
grammar
Syntax helps common users of a language understand how
to organize words so that they make the most sense.
e.g "The through pasture the chased a dog rabbit."
Using normal rules of syntax, this sentence means nothing.
But rearrange those exact words in a new order and they
make perfect syntactical sense:
"The dog chased a rabbit through the pasture."
Semantics
Semantics, on the other hand, is the study of the meaning of
sentences.
For instance, there's a world of difference in these two sentences:
"I robbed a bank;" and, "A bank robbed me." In the former, I
took money from a bank against their will. In the latter, they took
money from me.
e.g"The squirrel sang bumper cars."
On a pure syntax level, this sentence "makes sense" with a noun-
verb-noun structure, right?
 It's only semantics that you think, how the heck does a squirrel
sing bumper cars?
Pragmatics
It is the study of the meaning of sentences within a certain
context.
Semantics vs. Syntax vs. Pragmatics
 1. Syntax is what we use to do our best to communicate on the
most basic level.
2. Syntax is the study of sentences and phrases, and the rules of
grammar that sentences obey.
 1. Semantics helps us determine if there's any meaning to be found
2. Semantics is the study of sentence meaning;
 1. Pragmatics enables us to apply the correct meaning to the correct
situation.
2.pragmatics is the study of sentence meaning in context.
Translation Models
Grammars
– Terminal and non terminal symbols
– Grammar rules
– BNF notation
– Derivations, Parse trees and Ambiguity
Grammars for programming languages
– Types of grammars
– Regular expressions and Context-free grammars
Grammars
Syntax is concerned with the structure of programs. The
formal description of the syntax of a language is called
grammar
Grammars consist of rewriting rules and may be used for
both recognition and generation of sentences (statements).
-Grammars are independent of the syntactic analysis.
More about grammars
Word categories ,Constituents
- The boy is reading a book.
- The boy is reading an interesting book.
- The boy is reading a book by Mark Twain.
Terminal and non-terminal symbols, grammar rules
-Terminal to represent the words
- Non-terminal to represent categories of words and
constituents.
Starting symbol S represents "sentence"
These symbols are used in grammar rules
Example
Rule Meaning
N ->boy N is the non-terminal symbol
for "noun", "boy" is a terminal
"symbol“
D ->the | a | an D is the non-terminal symbol
for definite or indefinite articles.
NP ->D N This rule says that a noun phrase
NP may consist of an article
followed by a noun
BNF notation
Grammars for programming languages use a special
notation called BNF (Backus-Naur form)
 The non-terminal symbols are enclosed in < > Instead of
->the symbol ::= is used
The vertical bar is used in the same way - meaning choice.
 [] are used to represent optional constituents.
BNF notation is equivalent to the first notation in the
examples above.
BNF Example
The rule
<assignment statement>::=
<variable>=<arithmetic expression>
says that an assignment statement has
-a variable name on its left-hand side
-followed by the symbol "=",
-followed by an arithmetic expression
Derivations, Parse trees and Ambiguity
Using a grammar, we can generate sentences. The process
is called derivation
Example : S ->SS | (S) | ( )
generates all sequences of paired parentheses.
Derivation example
The rules of the grammar can be written separately:
Rule1: S ->SS
Rule2: S ->(S)
Rule3: S -> ( )
Derivation of (( )( ))
S (S) by Rule2
(SS) by Rule1
 (( )S) by Rule3
- (( )( )) by Rule3
Parsing
Sentential forms - strings obtained at each derivation step.
May contain both terminal and non-terminal symbols.
 Sentence - the last string obtained in the derivation, contains
only terminal symbols.
 Parsing - determines whether a string is a correct sentence or
not. It can be displayed in the form of a parse tree
Ambiguity
Grammar rules can generate two possible parse trees for one
and the same sequence of terminal symbols.
Example
Rule1: If_statement  if Exp then S else S
Rule2: If_statement  if Exp then S
if a < b then if c < y then write(yes) else write(no);
If a < b then if c < y then write(yes) else write(no); Rule 2
If a < b then if c < y then write(yes)
else write(no); Rule 1
Grammars for programming languages
Four types of grammars
 Regular grammars: (Type 3 )
Rule format:
A a
A aB
Context-free grammars (Type 2)
Rule format:
A any string consisting of terminals and non-terminals
Types of grammars (cont)
Context-sensitive grammars (Type 1)
Rule format:
String1  String2
|String1| |String2|, terminals and nonterminals
General (unrestricted) grammars (Type 0)
Rule format:
String1  String2, no restrictions
Regular grammars and Regular expressions
Operations on strings of symbols:
concatenation - appending two strings
 Kleene star operation - any repetition of the string. e.g. a* can be
a, or aa, or aaaaaaa, etc
Regular expressions: a form of representing regular grammars
Regular expressions on alphabet
 ∑ string concatenations combined with the symbols U and *,
possibly using '(' and ')’.
the empty expression: Ø
Examples
Let ∑ = {0,1}.
 Examples of regular expressions are: 0,1, 010101, any
combination of 0s and 1s

generated strings:
0 U 1 0, 1
(0 U 1)1* 0, 01, 011, 0111,…, 1, 11, 111..
(0 U 1)*01 01, 001, 0001,… 1101, 1001,
(any strings that end in 01)
Regular languages
Context-free languages
Regular languages are languages whose sentences can be
described by a regular expression.
Regular expressions are used to describe identifiers in
programming languages and arithmetic expressions.
Context-free grammars generate context-free languages.
They are used to describe programming languages.
Variables
• A variable is an abstraction of a memory cell
• Variables can be characterized as a 6-tuple of
attributes:
Name: identifier
Address: memory location(s)
Value: particular value at a moment
Type: range of possible values
Lifetime: when the variable accessible
Scope: where in the program it can be accessed
Variables
Name - not all variables have them (examples?)
Address - the memory address with which it is
associated
A variable may have different addresses at different
times during execution
A variable may have different addresses at different
places in a program
If two (or more) variable names can be used to
access the same memory location, they are called
aliases
Aliases are harmful to readability, but they are useful
under certain circumstances
Variables Type and Value
Type - determines the range of values of variables and the
set of operations that are defined for values of that type;
in the case of floating point, type also determines the
precision
Value - the contents of the location with which the variable
is associated
Abstract memory cell - the physical cell or collection of
cells associated with a variable
lvalue and rvalue
Are the two occurrences of “a” in this expression the same?
a := a + 1;
In a sense,
• The one on the left of the assignment refers to the location
of the variable whose name is a;
• The one on the right of the assignment refers to the value
of the variable whose name is a;
• The lvalue of a variable is its address
• The rvalue of a variable is its value
Expressions and Assignment statement
Expressions are the fundamental means of specifying
computations in a programming language
To understand expression evaluation, need to be familiar
with the orders of operator and operand evaluation
Essence of imperative languages is dominant role of
assignment statements
Arithmetic Expressions
Arithmetic evaluation was one of the motivations for the
development of the first programming languages
Arithmetic expressions consist of operators, operands,
parentheses, and function calls
 Design issues for arithmetic expressions
 Operator precedence rules?
 Operator associativity rules?
 Order of operand evaluation?
 Operand evaluation side effects?
 Operator overloading?
 Type mixing in expressions?
Arithmetic Expressions: Operators
A unary operator has one operand
A binary operator has two operands
A ternary operator has three operands
Arithmetic Expressions: Operator
Precedence Rules
The operator precedence rules for expression evaluation
define the order in which “adjacent” operators of different
precedence levels are evaluated
Typical precedence levels
 parentheses
 unary operators
 ** (if the language supports it)
 *, /
 +, -
Arithmetic Expressions: Operator Associativity
Rule
The operator associativity rules for expression evaluation
define the order in which adjacent operators with the same
precedence level are evaluated
Typical associativity rules
Left to right, except **, which is right to left
Sometimes unary operators associate right to left (e.g.,
in FORTRAN)
APL is different; all operators have equal precedence and
all operators associate right to left
Precedence and associativity rules can be overriden with
parentheses
1-35
Ruby Expressions
All arithmetic, relational, and assignment operators, as well as
array indexing, shifts, and bit-wise logic operators, are
implemented as methods
- One result of this is that these operators can all
be overriden by application programs
Arithmetic Expressions: Conditional
Expressions
 Conditional Expressions
C-based languages (e.g., C, C++)
An example:
average = (count == 0)? 0 : sum / count
Evaluates as if written like
if (count == 0)
average = 0
else
average = sum /count
Arithmetic Expressions: Operand Evaluation
Order
 Operand evaluation order
1. Variables: fetch the value from memory
2. Constants: sometimes a fetch from memory;
sometimes the constant is in the machine language
instruction
3. Parenthesized expressions: evaluate all operands and
operators first
4. The most interesting case is when an operand is a
function call
Arithmetic Expressions: Potentials for
Side Effects
Functional side effects: when a function changes a two-
way parameter or a non-local variable
Problem with functional side effects:
When a function referenced in an expression alters
another operand of the expression; e.g., for a parameter
change:
a = 10;
/* assume that fun changes its parameter */
b = a + fun(&a);
Functional Side Effects
 Two possible solutions to the problem
1. Write the language definition to disallow functional side effects
 No two-way parameters in functions
 No non-local references in functions
 Advantage: it works!
 Disadvantage: inflexibility of one-way parameters and lack of
non-local references
2. Write the language definition to demand that operand evaluation
order be fixed
 Disadvantage: limits some compiler optimizations
 Java requires that operands appear to be evaluated in left-to-
right order
Overloaded Operators
Use of an operator for more than one purpose is called
operator overloading
Some are common (e.g., + for int and float)
Some are potential trouble (e.g., * in C and C++)
Loss of compiler error detection (omission of an
operand should be a detectable error)
Some loss of readability
Overloaded Operators (continued)
C++ and C# allow user-defined overloaded operators
Potential problems:
Users can define nonsense operations
Readability may suffer, even when the operators make
sense
Type Conversions
A narrowing conversion is one that converts an object to a
type that cannot include all of the values of the original
type e.g., float to int
A widening conversion is one in which an object is
converted to a type that can include at least
approximations to all of the values of the original type
e.g., int to float
Type Conversions: Mixed Mode
A mixed-mode expression is one that has operands of
different types
A coercion is an implicit type conversion
Disadvantage of coercions:
They decrease in the type error detection ability of the
compiler
In most languages, all numeric types are coerced in
expressions, using widening conversions
In Ada, there are virtually no coercions in expressions
Explicit Type Conversions
Called casting in C-based languages
Examples
C: (int)angle
Ada: Float (Sum)
Note that Ada’s syntax is similar to that of function
calls
Type Conversions: Errors in Expressions
Causes
Inherent limitations of arithmetic e.g.,
division by zero
Limitations of computer arithmetic e.g.
overflow
 Often ignored by the run-time system
Relational and Boolean Expressions
Relational Expressions
Use relational operators and operands of various types
Evaluate to some Boolean representation
Operator symbols used vary somewhat among
languages (!=, /=, ~=, .NE., <>, #)
JavaScript and PHP have two additional relational
operator, === and !==
- Similar to their cousins, == and !=, except that they do
not coerce their operands
Relational and Boolean Expressions
Boolean Expressions
Operands are Boolean and the result is Boolean
Example operators
FORTRAN 77 FORTRAN 90 C Ada
.AND. and && and
.OR. or || or
.NOT. not ! not
xor
Relational and Boolean Expressions: No
Boolean Type in C
C89 has no Boolean type--it uses int type with 0 for false
and nonzero for true
One odd characteristic of C’s expressions: a < b < c
is a legal expression, but the result is not what you might
expect:
Left operator is evaluated, producing 0 or 1
The evaluation result is then compared with the third
operand (i.e., c)
Short Circuit Evaluation
An expression in which the result is determined without
evaluating all of the operands and/or operators
Example: (13*a) * (b/13–1)
If a is zero, there is no need to evaluate (b/13-1)
Problem with non-short-circuit evaluation
index = 1;
while (index <= length) && (LIST[index] != value)
index++;
When index=length, LIST [index] will cause an
indexing problem (assuming LIST has length -1
elements)
Short Circuit Evaluation (continued)
C, C++, and Java: use short-circuit evaluation for the usual
Boolean operators (&& and ||), but also provide bitwise
Boolean operators that are not short circuit (& and |)
Ada: programmer can specify either (short-circuit is specified
with and then and or else)
Short-circuit evaluation exposes the potential problem of side
effects in expressions
e.g. (a > b) || (b++ / 3)
Assignment Statements
The general syntax
<target_var> <assign_operator> <expression>
The assignment operator
= FORTRAN, BASIC, the C-based languages
:= ALGOLs, Pascal, Ada
= can be bad when it is overloaded for the relational
operator for equality (that’s why the C-based languages
use == as the relational operator)
1-52
Assignment Statements: Conditional
Targets
Conditional targets (Perl)
($flag ? $total : $subtotal) = 0
Which is equivalent to
if ($flag){
$total = 0
} else {
$subtotal = 0
}
Assignment Statements: Compound
Operators
A shorthand method of specifying a commonly needed form of
assignment
Introduced in ALGOL; adopted by C
Example
a = a + b
is written as
a += b
Assignment Statements: Unary
Assignment Operators
Unary assignment operators in C-based languages
combine increment and decrement operations with
assignment
Examples
sum = ++count (count incremented, added to sum)
sum = count++ (count incremented, added to sum)
count++ (count incremented)
-count++ (count incremented then negated)
Assignment as an Expression
In C, C++, and Java, the assignment statement
produces a result and can be used as operands
An example:
while ((ch = getchar())!= EOF){…}
ch = getchar() is carried out; the result (assigned to
ch) is used as a conditional value for the while
statement
Mixed-Mode Assignment
Assignment statements can also be mixed-mode
In Fortran, C, and C++, any numeric type value can be assigned to
any numeric type variable
In Java, only widening assignment coercions are done
In Ada, there is no assignment coercion
57
Binding
A binding is an association, such as between an attribute and an
entity, or between an operation and a symbol
Binding time is the time at which a binding takes place.
Possible binding times:
Language design time -- e.g., bind operator symbols to
operations
Language implementation time -- e.g., bind floating point type
to a representation
Compile time -- e.g., bind a variable to a type in C or Java
Link time
Load time--e.g., bind a FORTRAN 77 variable to memory
cell (or a C static variable)
Runtime -- e.g., bind a nonstatic local variable to a memory
cell
Type Bindings
• A binding is static if it occurs before run time and remains
unchanged throughout program execution.
• A binding is dynamic if it occurs during execution or can
change during execution of the program.
• Type binding issues
• How is a type specified?
• When does the binding take place?
• If static, type may be specified by either an explicit or an
implicit declaration
Variable Declarations
An explicit declaration is a program statement used for declaring
the types of variables
Def: An implicit declaration is a default mechanism for
specifying types of variables (the first appearance of the
variable in the program)
E.g.: in Perl, variables of type scalar, array and hash begin
with a $, @ or %, respectively.
E.g.: In Fortran, variables beginning with I-N are assumed
to be of type integer.
E.g.: ML (and other languages) use sophisticated type
inference mechanisms
Advantages: writability, convenience
Disadvantages: reliability
Dynamic Type Binding
• The type of a variable can chance during the course of the
program and, in general, is re-determined on every
assignment.
• Usually associated with languages first implemented via
an interpreter rather than a compiler.
• Specified through an assignment statement, e.g. APL
LIST <- 2 4 6 8
LIST <- 17.3 23.5
• Advantages:
• Flexibility
• Obviates the need for “polymorphic” types
• Development of generic functions (e.g. sort)
• Disadvantages:
• High cost (dynamic type checking and interpretation)
• Type error detection by the compiler is difficult
Type Inferencing
 Type Inferencing is used in some programming languages,
including ML, Miranda, and Haskell.
 Types are determined from the context of the reference,
rather than just by assignment statement.
 Legal:
fun circumf(r) = 3.14159 * r * r; // infer r is real
fun time10(x) = 10 * x; // infer x is integer
 Illegal:
fun square(x) = x * x; // can’t deduce anything
 Fixed
fun square(x) : int = x * x; // use explicit declaration
62
Storage Bindings and Lifetime
• Storage Bindings
• Allocation - getting a cell from some pool of
available cells
• Deallocation - putting a cell back into the pool
• Def: The lifetime of a variable is the time during
which it is bound to a particular memory cell
• Categories of variables by lifetimes
• Static
• Stack dynamic
• Explicit heap dynamic
• Implicit heap dynamic
Static Variables
• Static variables are bound to memory cells before
execution begins and remains bound to the same
memory cell throughout execution.
• Examples:
• all FORTRAN 77 variables
• C static variables
Advantage: efficiency (direct addressing),
history-sensitive subprogram support
Disadvantage: lack of flexibility, no recursion!
64
Static Dynamic Variables
Stack-dynamic variables -- Storage bindings are created for
variables when their declaration statements are elaborated.
If scalar, all attributes except address are statically bound
e.g. local variables in Pascal and C subprograms
Advantages:
allows recursion
conserves storage
Disadvantages:
Overhead of allocation and deallocation
Subprograms cannot be history sensitive
Inefficient references (indirect addressing)
Constant
Constant is a value that cannot be altered by the program
during normal execution, i.e., the value is constant.
When associated with an identifier, a constant is said to be
“named,” although the terms “constant” and “named constant”
are often used interchangeably.
 This is contrasted with a variable, which is an identifier with
a value that can be changed during normal execution, i.e., the
value is variable
A constant is a data item whose value cannot change during
the program’s execution. Thus, as its name implies – the value
is constant.
A variable is a data item whose value can change during the
program’s execution. Thus, as its name implies – the value can
vary.
Constants are used in two ways. They are:
1. literal constant
2. defined constant
literal constant is a value you type into your program
wherever it is needed.
Examples include the constants used for initializing a
variable and constants used in lines of code:
21
12.34
'A'
"Hello world!"
false
null
Named Constants:
A named constant is a variable that is bound to a value only
when it is bound to storage
Advantages: readability and modifiability
Used to parameterize programs
The binding of values to named constants can be either static
(called manifest constants) or dynamic
Languages: – Pascal: literals only
– FORTRAN 90: constant-valued expressions
– Ada, C++, and Java: expressions of any kind
Statement-Level Control Structures
A control structure is a control statement and the
statements whose execution it controls.
•Selection Statements
• Iterative Statements
• Unconditional Branching
Selection Statements
Selection statement provides the means of choosing between two
or more paths of execution.
 Two general categories:
o Two-way selectors
o Multiple-way selectors
Two-Way Selection Statements
The general form of a two-way selector is as follows:
if control_expression then clause else clause
Two-Way Selection Statements
General form:
if control_expression
then clause
else clause
Design Issues:
What is the form and type of the control expression?
How are the then and else clauses specified?
How should the meaning of nested selectors be specified?
The Control Expression
If the then reserved word or some other syntactic marker is
not used to introduce the then clause, the control
expression is placed in parentheses
In C89, C99, Python, and C++, the control expression can
be arithmetic.
In languages such as Ada, Java, Ruby, and C#, the control
expression must be Boolean
Multiple-Way Selection Statements
Allow the selection of one of any number of statements or
statement groups
Design Issues:
•What is the form and type of the control expression?
• How are the selectable segments specified?
• Is execution flow through the structure restricted to include
just a single selectable segment?
• How are case values specified?
• What is done about unrepresented expression values?
•
Iterative Statements
The repeated execution of a statement or compound
statement is accomplished either by iteration or recursion
General design issues for iteration control statements
1. How is iteration controlled?
2. Where is the control mechanism in the loop?
Counter-Controlled Loops
A counting iterative statement has a loop variable, and a
means of specifying the initial and terminal, and stepsize
values.
Design Issues:
1. What are the type and scope of the loop variable?
2. What is the value of the loop variable at loop termination?
3. Should it be legal for the loop variable or loop parameters to
be changed in the loop body, and if so, does the change
affect loop control?
4. Should the loop parameters be evaluated only once, or once
for every iteration?
Unit-1 PPL PPTvvhvmmmmmmmmmmmmmmmmmmmmmm

More Related Content

Similar to Unit-1 PPL PPTvvhvmmmmmmmmmmmmmmmmmmmmmm

Shallow parser for hindi language with an input from a transliterator
Shallow parser for hindi language with an input from a transliteratorShallow parser for hindi language with an input from a transliterator
Shallow parser for hindi language with an input from a transliteratorShashank Shisodia
 
Syntax analyzer
Syntax analyzerSyntax analyzer
Syntax analyzerahmed51236
 
Pptphrase tagset mapping for french and english treebanks and its application...
Pptphrase tagset mapping for french and english treebanks and its application...Pptphrase tagset mapping for french and english treebanks and its application...
Pptphrase tagset mapping for french and english treebanks and its application...Lifeng (Aaron) Han
 
pptphrase-tagset-mapping-for-french-and-english-treebanks-and-its-application...
pptphrase-tagset-mapping-for-french-and-english-treebanks-and-its-application...pptphrase-tagset-mapping-for-french-and-english-treebanks-and-its-application...
pptphrase-tagset-mapping-for-french-and-english-treebanks-and-its-application...Lifeng (Aaron) Han
 
Basic techniques in nlp
Basic techniques in nlpBasic techniques in nlp
Basic techniques in nlpSumit Sony
 
Implementation Of Syntax Parser For English Language Using Grammar Rules
Implementation Of Syntax Parser For English Language Using Grammar RulesImplementation Of Syntax Parser For English Language Using Grammar Rules
Implementation Of Syntax Parser For English Language Using Grammar RulesIJERA Editor
 
Modern Programming Languages classification Poster
Modern Programming Languages classification PosterModern Programming Languages classification Poster
Modern Programming Languages classification PosterSaulo Aguiar
 
An Intuitive Natural Language Understanding System
An Intuitive Natural Language Understanding SystemAn Intuitive Natural Language Understanding System
An Intuitive Natural Language Understanding Systeminscit2006
 
PL Lecture 01 - preliminaries
PL Lecture 01 - preliminariesPL Lecture 01 - preliminaries
PL Lecture 01 - preliminariesSchwannden Kuo
 

Similar to Unit-1 PPL PPTvvhvmmmmmmmmmmmmmmmmmmmmmm (20)

INTERPRETER.ppt
INTERPRETER.pptINTERPRETER.ppt
INTERPRETER.ppt
 
Shallow parser for hindi language with an input from a transliterator
Shallow parser for hindi language with an input from a transliteratorShallow parser for hindi language with an input from a transliterator
Shallow parser for hindi language with an input from a transliterator
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
8074448.ppt
8074448.ppt8074448.ppt
8074448.ppt
 
FIRE2014_IIT-P
FIRE2014_IIT-PFIRE2014_IIT-P
FIRE2014_IIT-P
 
Syntax analyzer
Syntax analyzerSyntax analyzer
Syntax analyzer
 
7. name binding and scopes
7. name binding and scopes7. name binding and scopes
7. name binding and scopes
 
AINL 2016: Eyecioglu
AINL 2016: EyeciogluAINL 2016: Eyecioglu
AINL 2016: Eyecioglu
 
Pptphrase tagset mapping for french and english treebanks and its application...
Pptphrase tagset mapping for french and english treebanks and its application...Pptphrase tagset mapping for french and english treebanks and its application...
Pptphrase tagset mapping for french and english treebanks and its application...
 
pptphrase-tagset-mapping-for-french-and-english-treebanks-and-its-application...
pptphrase-tagset-mapping-for-french-and-english-treebanks-and-its-application...pptphrase-tagset-mapping-for-french-and-english-treebanks-and-its-application...
pptphrase-tagset-mapping-for-french-and-english-treebanks-and-its-application...
 
chapter4 end.pptx
chapter4 end.pptxchapter4 end.pptx
chapter4 end.pptx
 
P-6
P-6P-6
P-6
 
P-6
P-6P-6
P-6
 
Basic techniques in nlp
Basic techniques in nlpBasic techniques in nlp
Basic techniques in nlp
 
Implementation Of Syntax Parser For English Language Using Grammar Rules
Implementation Of Syntax Parser For English Language Using Grammar RulesImplementation Of Syntax Parser For English Language Using Grammar Rules
Implementation Of Syntax Parser For English Language Using Grammar Rules
 
Modern Programming Languages classification Poster
Modern Programming Languages classification PosterModern Programming Languages classification Poster
Modern Programming Languages classification Poster
 
Syntax
SyntaxSyntax
Syntax
 
Pcd question bank
Pcd question bank Pcd question bank
Pcd question bank
 
An Intuitive Natural Language Understanding System
An Intuitive Natural Language Understanding SystemAn Intuitive Natural Language Understanding System
An Intuitive Natural Language Understanding System
 
PL Lecture 01 - preliminaries
PL Lecture 01 - preliminariesPL Lecture 01 - preliminaries
PL Lecture 01 - preliminaries
 

Recently uploaded

Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.MateoGardella
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...KokoStevan
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 

Recently uploaded (20)

Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 

Unit-1 PPL PPTvvhvmmmmmmmmmmmmmmmmmmmmmm

  • 2.
  • 3.
  • 4. Principle of Programming Language is a set of rules and norms governed to communicate instructions (high level instruction or assemble level instruction) to a machine or particularly a computer.” Types of Programming Language that are classified are: • Procedural Programming Language. • Functional Programming Language. • Scripting Programming Language. • Logic Programming Language. • Object-Oriented Programming Language.
  • 5. Why Study Programming Languages? Increased capacity to express ideas Improved background for choosing appropriate language Increased ability to learn new languages Better understanding of the significance of implementation Better use of languages that are already known Overall advancement of computing
  • 6. PROGRAMMING PARADIGMS A Programming Paradigm is a pattern of problem-solving thoughts that underlies a particular genre of programs and languages. Types:- Imperative Programming Object-Oriented Programming  Functional Programming Logic Programming
  • 7. Syntax Syntax is the study of sentence structure and the rules of grammar Syntax helps common users of a language understand how to organize words so that they make the most sense. e.g "The through pasture the chased a dog rabbit." Using normal rules of syntax, this sentence means nothing. But rearrange those exact words in a new order and they make perfect syntactical sense: "The dog chased a rabbit through the pasture."
  • 8. Semantics Semantics, on the other hand, is the study of the meaning of sentences. For instance, there's a world of difference in these two sentences: "I robbed a bank;" and, "A bank robbed me." In the former, I took money from a bank against their will. In the latter, they took money from me. e.g"The squirrel sang bumper cars." On a pure syntax level, this sentence "makes sense" with a noun- verb-noun structure, right?  It's only semantics that you think, how the heck does a squirrel sing bumper cars?
  • 9. Pragmatics It is the study of the meaning of sentences within a certain context.
  • 10. Semantics vs. Syntax vs. Pragmatics  1. Syntax is what we use to do our best to communicate on the most basic level. 2. Syntax is the study of sentences and phrases, and the rules of grammar that sentences obey.  1. Semantics helps us determine if there's any meaning to be found 2. Semantics is the study of sentence meaning;  1. Pragmatics enables us to apply the correct meaning to the correct situation. 2.pragmatics is the study of sentence meaning in context.
  • 11. Translation Models Grammars – Terminal and non terminal symbols – Grammar rules – BNF notation – Derivations, Parse trees and Ambiguity Grammars for programming languages – Types of grammars – Regular expressions and Context-free grammars
  • 12. Grammars Syntax is concerned with the structure of programs. The formal description of the syntax of a language is called grammar Grammars consist of rewriting rules and may be used for both recognition and generation of sentences (statements). -Grammars are independent of the syntactic analysis.
  • 13. More about grammars Word categories ,Constituents - The boy is reading a book. - The boy is reading an interesting book. - The boy is reading a book by Mark Twain. Terminal and non-terminal symbols, grammar rules -Terminal to represent the words - Non-terminal to represent categories of words and constituents. Starting symbol S represents "sentence" These symbols are used in grammar rules
  • 14. Example Rule Meaning N ->boy N is the non-terminal symbol for "noun", "boy" is a terminal "symbol“ D ->the | a | an D is the non-terminal symbol for definite or indefinite articles. NP ->D N This rule says that a noun phrase NP may consist of an article followed by a noun
  • 15. BNF notation Grammars for programming languages use a special notation called BNF (Backus-Naur form)  The non-terminal symbols are enclosed in < > Instead of ->the symbol ::= is used The vertical bar is used in the same way - meaning choice.  [] are used to represent optional constituents. BNF notation is equivalent to the first notation in the examples above.
  • 16. BNF Example The rule <assignment statement>::= <variable>=<arithmetic expression> says that an assignment statement has -a variable name on its left-hand side -followed by the symbol "=", -followed by an arithmetic expression
  • 17. Derivations, Parse trees and Ambiguity Using a grammar, we can generate sentences. The process is called derivation Example : S ->SS | (S) | ( ) generates all sequences of paired parentheses.
  • 18. Derivation example The rules of the grammar can be written separately: Rule1: S ->SS Rule2: S ->(S) Rule3: S -> ( ) Derivation of (( )( )) S (S) by Rule2 (SS) by Rule1  (( )S) by Rule3 - (( )( )) by Rule3
  • 19. Parsing Sentential forms - strings obtained at each derivation step. May contain both terminal and non-terminal symbols.  Sentence - the last string obtained in the derivation, contains only terminal symbols.  Parsing - determines whether a string is a correct sentence or not. It can be displayed in the form of a parse tree
  • 20. Ambiguity Grammar rules can generate two possible parse trees for one and the same sequence of terminal symbols. Example Rule1: If_statement  if Exp then S else S Rule2: If_statement  if Exp then S if a < b then if c < y then write(yes) else write(no); If a < b then if c < y then write(yes) else write(no); Rule 2 If a < b then if c < y then write(yes) else write(no); Rule 1
  • 21. Grammars for programming languages Four types of grammars  Regular grammars: (Type 3 ) Rule format: A a A aB Context-free grammars (Type 2) Rule format: A any string consisting of terminals and non-terminals
  • 22. Types of grammars (cont) Context-sensitive grammars (Type 1) Rule format: String1  String2 |String1| |String2|, terminals and nonterminals General (unrestricted) grammars (Type 0) Rule format: String1  String2, no restrictions
  • 23. Regular grammars and Regular expressions Operations on strings of symbols: concatenation - appending two strings  Kleene star operation - any repetition of the string. e.g. a* can be a, or aa, or aaaaaaa, etc Regular expressions: a form of representing regular grammars Regular expressions on alphabet  ∑ string concatenations combined with the symbols U and *, possibly using '(' and ')’. the empty expression: Ø
  • 24. Examples Let ∑ = {0,1}.  Examples of regular expressions are: 0,1, 010101, any combination of 0s and 1s  generated strings: 0 U 1 0, 1 (0 U 1)1* 0, 01, 011, 0111,…, 1, 11, 111.. (0 U 1)*01 01, 001, 0001,… 1101, 1001, (any strings that end in 01)
  • 25. Regular languages Context-free languages Regular languages are languages whose sentences can be described by a regular expression. Regular expressions are used to describe identifiers in programming languages and arithmetic expressions. Context-free grammars generate context-free languages. They are used to describe programming languages.
  • 26. Variables • A variable is an abstraction of a memory cell • Variables can be characterized as a 6-tuple of attributes: Name: identifier Address: memory location(s) Value: particular value at a moment Type: range of possible values Lifetime: when the variable accessible Scope: where in the program it can be accessed
  • 27. Variables Name - not all variables have them (examples?) Address - the memory address with which it is associated A variable may have different addresses at different times during execution A variable may have different addresses at different places in a program If two (or more) variable names can be used to access the same memory location, they are called aliases Aliases are harmful to readability, but they are useful under certain circumstances
  • 28. Variables Type and Value Type - determines the range of values of variables and the set of operations that are defined for values of that type; in the case of floating point, type also determines the precision Value - the contents of the location with which the variable is associated Abstract memory cell - the physical cell or collection of cells associated with a variable
  • 29. lvalue and rvalue Are the two occurrences of “a” in this expression the same? a := a + 1; In a sense, • The one on the left of the assignment refers to the location of the variable whose name is a; • The one on the right of the assignment refers to the value of the variable whose name is a; • The lvalue of a variable is its address • The rvalue of a variable is its value
  • 30. Expressions and Assignment statement Expressions are the fundamental means of specifying computations in a programming language To understand expression evaluation, need to be familiar with the orders of operator and operand evaluation Essence of imperative languages is dominant role of assignment statements
  • 31. Arithmetic Expressions Arithmetic evaluation was one of the motivations for the development of the first programming languages Arithmetic expressions consist of operators, operands, parentheses, and function calls  Design issues for arithmetic expressions  Operator precedence rules?  Operator associativity rules?  Order of operand evaluation?  Operand evaluation side effects?  Operator overloading?  Type mixing in expressions?
  • 32. Arithmetic Expressions: Operators A unary operator has one operand A binary operator has two operands A ternary operator has three operands
  • 33. Arithmetic Expressions: Operator Precedence Rules The operator precedence rules for expression evaluation define the order in which “adjacent” operators of different precedence levels are evaluated Typical precedence levels  parentheses  unary operators  ** (if the language supports it)  *, /  +, -
  • 34. Arithmetic Expressions: Operator Associativity Rule The operator associativity rules for expression evaluation define the order in which adjacent operators with the same precedence level are evaluated Typical associativity rules Left to right, except **, which is right to left Sometimes unary operators associate right to left (e.g., in FORTRAN) APL is different; all operators have equal precedence and all operators associate right to left Precedence and associativity rules can be overriden with parentheses
  • 35. 1-35 Ruby Expressions All arithmetic, relational, and assignment operators, as well as array indexing, shifts, and bit-wise logic operators, are implemented as methods - One result of this is that these operators can all be overriden by application programs
  • 36. Arithmetic Expressions: Conditional Expressions  Conditional Expressions C-based languages (e.g., C, C++) An example: average = (count == 0)? 0 : sum / count Evaluates as if written like if (count == 0) average = 0 else average = sum /count
  • 37. Arithmetic Expressions: Operand Evaluation Order  Operand evaluation order 1. Variables: fetch the value from memory 2. Constants: sometimes a fetch from memory; sometimes the constant is in the machine language instruction 3. Parenthesized expressions: evaluate all operands and operators first 4. The most interesting case is when an operand is a function call
  • 38. Arithmetic Expressions: Potentials for Side Effects Functional side effects: when a function changes a two- way parameter or a non-local variable Problem with functional side effects: When a function referenced in an expression alters another operand of the expression; e.g., for a parameter change: a = 10; /* assume that fun changes its parameter */ b = a + fun(&a);
  • 39. Functional Side Effects  Two possible solutions to the problem 1. Write the language definition to disallow functional side effects  No two-way parameters in functions  No non-local references in functions  Advantage: it works!  Disadvantage: inflexibility of one-way parameters and lack of non-local references 2. Write the language definition to demand that operand evaluation order be fixed  Disadvantage: limits some compiler optimizations  Java requires that operands appear to be evaluated in left-to- right order
  • 40. Overloaded Operators Use of an operator for more than one purpose is called operator overloading Some are common (e.g., + for int and float) Some are potential trouble (e.g., * in C and C++) Loss of compiler error detection (omission of an operand should be a detectable error) Some loss of readability
  • 41. Overloaded Operators (continued) C++ and C# allow user-defined overloaded operators Potential problems: Users can define nonsense operations Readability may suffer, even when the operators make sense
  • 42. Type Conversions A narrowing conversion is one that converts an object to a type that cannot include all of the values of the original type e.g., float to int A widening conversion is one in which an object is converted to a type that can include at least approximations to all of the values of the original type e.g., int to float
  • 43. Type Conversions: Mixed Mode A mixed-mode expression is one that has operands of different types A coercion is an implicit type conversion Disadvantage of coercions: They decrease in the type error detection ability of the compiler In most languages, all numeric types are coerced in expressions, using widening conversions In Ada, there are virtually no coercions in expressions
  • 44. Explicit Type Conversions Called casting in C-based languages Examples C: (int)angle Ada: Float (Sum) Note that Ada’s syntax is similar to that of function calls
  • 45. Type Conversions: Errors in Expressions Causes Inherent limitations of arithmetic e.g., division by zero Limitations of computer arithmetic e.g. overflow  Often ignored by the run-time system
  • 46. Relational and Boolean Expressions Relational Expressions Use relational operators and operands of various types Evaluate to some Boolean representation Operator symbols used vary somewhat among languages (!=, /=, ~=, .NE., <>, #) JavaScript and PHP have two additional relational operator, === and !== - Similar to their cousins, == and !=, except that they do not coerce their operands
  • 47. Relational and Boolean Expressions Boolean Expressions Operands are Boolean and the result is Boolean Example operators FORTRAN 77 FORTRAN 90 C Ada .AND. and && and .OR. or || or .NOT. not ! not xor
  • 48. Relational and Boolean Expressions: No Boolean Type in C C89 has no Boolean type--it uses int type with 0 for false and nonzero for true One odd characteristic of C’s expressions: a < b < c is a legal expression, but the result is not what you might expect: Left operator is evaluated, producing 0 or 1 The evaluation result is then compared with the third operand (i.e., c)
  • 49. Short Circuit Evaluation An expression in which the result is determined without evaluating all of the operands and/or operators Example: (13*a) * (b/13–1) If a is zero, there is no need to evaluate (b/13-1) Problem with non-short-circuit evaluation index = 1; while (index <= length) && (LIST[index] != value) index++; When index=length, LIST [index] will cause an indexing problem (assuming LIST has length -1 elements)
  • 50. Short Circuit Evaluation (continued) C, C++, and Java: use short-circuit evaluation for the usual Boolean operators (&& and ||), but also provide bitwise Boolean operators that are not short circuit (& and |) Ada: programmer can specify either (short-circuit is specified with and then and or else) Short-circuit evaluation exposes the potential problem of side effects in expressions e.g. (a > b) || (b++ / 3)
  • 51. Assignment Statements The general syntax <target_var> <assign_operator> <expression> The assignment operator = FORTRAN, BASIC, the C-based languages := ALGOLs, Pascal, Ada = can be bad when it is overloaded for the relational operator for equality (that’s why the C-based languages use == as the relational operator)
  • 52. 1-52 Assignment Statements: Conditional Targets Conditional targets (Perl) ($flag ? $total : $subtotal) = 0 Which is equivalent to if ($flag){ $total = 0 } else { $subtotal = 0 }
  • 53. Assignment Statements: Compound Operators A shorthand method of specifying a commonly needed form of assignment Introduced in ALGOL; adopted by C Example a = a + b is written as a += b
  • 54. Assignment Statements: Unary Assignment Operators Unary assignment operators in C-based languages combine increment and decrement operations with assignment Examples sum = ++count (count incremented, added to sum) sum = count++ (count incremented, added to sum) count++ (count incremented) -count++ (count incremented then negated)
  • 55. Assignment as an Expression In C, C++, and Java, the assignment statement produces a result and can be used as operands An example: while ((ch = getchar())!= EOF){…} ch = getchar() is carried out; the result (assigned to ch) is used as a conditional value for the while statement
  • 56. Mixed-Mode Assignment Assignment statements can also be mixed-mode In Fortran, C, and C++, any numeric type value can be assigned to any numeric type variable In Java, only widening assignment coercions are done In Ada, there is no assignment coercion
  • 57. 57 Binding A binding is an association, such as between an attribute and an entity, or between an operation and a symbol Binding time is the time at which a binding takes place. Possible binding times: Language design time -- e.g., bind operator symbols to operations Language implementation time -- e.g., bind floating point type to a representation Compile time -- e.g., bind a variable to a type in C or Java Link time Load time--e.g., bind a FORTRAN 77 variable to memory cell (or a C static variable) Runtime -- e.g., bind a nonstatic local variable to a memory cell
  • 58. Type Bindings • A binding is static if it occurs before run time and remains unchanged throughout program execution. • A binding is dynamic if it occurs during execution or can change during execution of the program. • Type binding issues • How is a type specified? • When does the binding take place? • If static, type may be specified by either an explicit or an implicit declaration
  • 59. Variable Declarations An explicit declaration is a program statement used for declaring the types of variables Def: An implicit declaration is a default mechanism for specifying types of variables (the first appearance of the variable in the program) E.g.: in Perl, variables of type scalar, array and hash begin with a $, @ or %, respectively. E.g.: In Fortran, variables beginning with I-N are assumed to be of type integer. E.g.: ML (and other languages) use sophisticated type inference mechanisms Advantages: writability, convenience Disadvantages: reliability
  • 60. Dynamic Type Binding • The type of a variable can chance during the course of the program and, in general, is re-determined on every assignment. • Usually associated with languages first implemented via an interpreter rather than a compiler. • Specified through an assignment statement, e.g. APL LIST <- 2 4 6 8 LIST <- 17.3 23.5 • Advantages: • Flexibility • Obviates the need for “polymorphic” types • Development of generic functions (e.g. sort) • Disadvantages: • High cost (dynamic type checking and interpretation) • Type error detection by the compiler is difficult
  • 61. Type Inferencing  Type Inferencing is used in some programming languages, including ML, Miranda, and Haskell.  Types are determined from the context of the reference, rather than just by assignment statement.  Legal: fun circumf(r) = 3.14159 * r * r; // infer r is real fun time10(x) = 10 * x; // infer x is integer  Illegal: fun square(x) = x * x; // can’t deduce anything  Fixed fun square(x) : int = x * x; // use explicit declaration
  • 62. 62 Storage Bindings and Lifetime • Storage Bindings • Allocation - getting a cell from some pool of available cells • Deallocation - putting a cell back into the pool • Def: The lifetime of a variable is the time during which it is bound to a particular memory cell • Categories of variables by lifetimes • Static • Stack dynamic • Explicit heap dynamic • Implicit heap dynamic
  • 63. Static Variables • Static variables are bound to memory cells before execution begins and remains bound to the same memory cell throughout execution. • Examples: • all FORTRAN 77 variables • C static variables Advantage: efficiency (direct addressing), history-sensitive subprogram support Disadvantage: lack of flexibility, no recursion!
  • 64. 64 Static Dynamic Variables Stack-dynamic variables -- Storage bindings are created for variables when their declaration statements are elaborated. If scalar, all attributes except address are statically bound e.g. local variables in Pascal and C subprograms Advantages: allows recursion conserves storage Disadvantages: Overhead of allocation and deallocation Subprograms cannot be history sensitive Inefficient references (indirect addressing)
  • 65. Constant Constant is a value that cannot be altered by the program during normal execution, i.e., the value is constant. When associated with an identifier, a constant is said to be “named,” although the terms “constant” and “named constant” are often used interchangeably.  This is contrasted with a variable, which is an identifier with a value that can be changed during normal execution, i.e., the value is variable
  • 66. A constant is a data item whose value cannot change during the program’s execution. Thus, as its name implies – the value is constant. A variable is a data item whose value can change during the program’s execution. Thus, as its name implies – the value can vary. Constants are used in two ways. They are: 1. literal constant 2. defined constant
  • 67. literal constant is a value you type into your program wherever it is needed. Examples include the constants used for initializing a variable and constants used in lines of code: 21 12.34 'A' "Hello world!" false null
  • 68. Named Constants: A named constant is a variable that is bound to a value only when it is bound to storage Advantages: readability and modifiability Used to parameterize programs The binding of values to named constants can be either static (called manifest constants) or dynamic Languages: – Pascal: literals only – FORTRAN 90: constant-valued expressions – Ada, C++, and Java: expressions of any kind
  • 69. Statement-Level Control Structures A control structure is a control statement and the statements whose execution it controls. •Selection Statements • Iterative Statements • Unconditional Branching
  • 70. Selection Statements Selection statement provides the means of choosing between two or more paths of execution.  Two general categories: o Two-way selectors o Multiple-way selectors Two-Way Selection Statements The general form of a two-way selector is as follows: if control_expression then clause else clause
  • 71. Two-Way Selection Statements General form: if control_expression then clause else clause Design Issues: What is the form and type of the control expression? How are the then and else clauses specified? How should the meaning of nested selectors be specified?
  • 72. The Control Expression If the then reserved word or some other syntactic marker is not used to introduce the then clause, the control expression is placed in parentheses In C89, C99, Python, and C++, the control expression can be arithmetic. In languages such as Ada, Java, Ruby, and C#, the control expression must be Boolean
  • 73. Multiple-Way Selection Statements Allow the selection of one of any number of statements or statement groups Design Issues: •What is the form and type of the control expression? • How are the selectable segments specified? • Is execution flow through the structure restricted to include just a single selectable segment? • How are case values specified? • What is done about unrepresented expression values? •
  • 74. Iterative Statements The repeated execution of a statement or compound statement is accomplished either by iteration or recursion General design issues for iteration control statements 1. How is iteration controlled? 2. Where is the control mechanism in the loop?
  • 75. Counter-Controlled Loops A counting iterative statement has a loop variable, and a means of specifying the initial and terminal, and stepsize values. Design Issues: 1. What are the type and scope of the loop variable? 2. What is the value of the loop variable at loop termination? 3. Should it be legal for the loop variable or loop parameters to be changed in the loop body, and if so, does the change affect loop control? 4. Should the loop parameters be evaluated only once, or once for every iteration?