compiler: programming language= assignments and statements

47
COMPILERS: PROGRAMMING LANGUAGES ASSIGNMENTS - STATEMENTS. 1 Presentation By: Geo S. Mariyan (Master in Computer Science)

Upload: geo-marian

Post on 21-Mar-2017

29 views

Category:

Presentations & Public Speaking


2 download

TRANSCRIPT

Page 1: Compiler: Programming Language= Assignments and statements

COMPILERS: PROGRAMMING

LANGUAGESASSIGNMENTS -

STATEMENTS.

1

Presentation By: Geo S. Mariyan (Master in

Computer Science)

Page 2: Compiler: Programming Language= Assignments and statements

2Assignment - Introduction

•Assignment is to "assign a pre declared variable with some value or by the value of another variable of the same data type. This is done anywhere in body of the program.

For E.g. : n=9 or m=5 then, n=5 ! assigns value 5 to n

• Initialization is telling compiler to allocate a memory to the initialized variable so that compiler can know that this variable is going to store some value.

Page 3: Compiler: Programming Language= Assignments and statements

3ASSIGNMENT – Symbolic

Representation• ASSIGNMENT: The Basic Operation for changing the

binding of the value to the data object.• Assignment also returns the value, which is a data object

containing the copy of a value assigned.

Page 4: Compiler: Programming Language= Assignments and statements

4Ex : int a; Initializing 'a' as of type int

Assignment means proving a particular value to any variable.

Ex: int a=2; a in assigned a value to 2.

This is purely assignment and assignment can be done n no. of times in a program. There is no such restriction on it.

Assignment throws away the old value of a variable and replacing it with a new one. It can be done anywhere in the whole program.

Page 5: Compiler: Programming Language= Assignments and statements

5l- and r- values•  An lvalue can appear on the left side of an assignment operator, whereas an rvalue can appear on the right side.• "lvalue" and "rvalue" are so named because of where each of them can appear in an assignment operation.

• ex: int a, b;• a = 4;• b = a;

• In the third line of that example, "b" is the lvalue, and "a" is the rvalue, whereas it was the lvalue in line. • This illustrates an important point: An lvalue can also be an rvalue.

Page 6: Compiler: Programming Language= Assignments and statements

6Assignment Operators

• An assignment operator is the operator used to assign a new value to a variable, property, event or indexer element.

• Assignment operators can also be used for logical operations such as bitwise logical operations or operations on integral operands and Boolean operands.

• Unlike in C++, assignment operators in C# cannot be overloaded directly, but the user-defined types can overload the operators like +, -, /, etc. This allows the assignment operator to be used with those types.

Page 7: Compiler: Programming Language= Assignments and statements

7Characteristics of Assignment Operators:

• The different assignment operators are based on the type of operation performed between two operands such as addition (+=), subtraction, (-=), etc. The meaning of the operator symbol used depends on the type of the operands.

• Assignment operators are right-associative, which means they are grouped from right to left.

• Although assignment using assignment operator (a += b) achieves the same result as that without ( =a +b), the difference between the two ways is that unlike in the latter example, "a" is evaluated only once.

Page 8: Compiler: Programming Language= Assignments and statements

8Practice with Assignment Operators

The assignment operator expects the type of both the left- and right-hand side to be the same for successful assignment.

Example:

i += 24

i *= x

Page 9: Compiler: Programming Language= Assignments and statements

9Variant forms of Assignment

•Augmented Assignment

•Chained Assignment

•Parallel Assignment

Page 10: Compiler: Programming Language= Assignments and statements

10• Augmented assignment: The case where the assigned value depends on a previous one is so common that many imperative languages, most notably C and the majority of its descendants, provide special operators called Augmented assignment

ex: *=, so a = 2*a can instead be written as a *= 2

• Chained assignment: A statement like w = x = y = z is called a chained assignment in which the value of z is assigned to multiple variables w, x, and y. Chained assignments are often used to initialize multiple variables, as in

• example: a = b = c = d = f = 0

Page 11: Compiler: Programming Language= Assignments and statements

11Parallel Assignment:

Some programming languages, such as Go, JavaScript (since, Maple, Perl, Python, REBOL, Ruby, and Windows Power Shell allow several variables to be assigned in parallel, with syntax like:

ex: a, b := 0, 1

which simultaneously assigns 0 to a and 1 to b. This is most often known as parallel assignment.

Page 12: Compiler: Programming Language= Assignments and statements

Assignment statements

•Once a variable is declared, it can be assigned values multiple times in a program; for example:

int num; // num declared,

uninitializednum = 3; // num assigned 3num = num + 1; // num assigned 3 + 1 (4)num = 15 / num; // num assigned 15 / 4

12

Page 13: Compiler: Programming Language= Assignments and statements

13Implementation of Assignment:

• Compiler can implement the assignment A := B • The Compiler generates code to compute the r-value

and B into some register r-value of B into some register r

• If the data types of A and B are incompatible, the compiler generates code to convert the r-value of B to a type appropriate for A.

• If necessary, the compiler generates the code to determine the l-value of A.

(if A is in primitive type , no code is needed)• Finally, the compiler generates code to store

the contents of register r into the l-values of A.

Page 14: Compiler: Programming Language= Assignments and statements

14Assignment compatibility

•When a variable is declared, the data type in the declaration indicates the nature of the values the variable is capable of storing.

• For example, an int variable can store a whole number, a char variable can store a character, and a double variable can store a real number.

• The value assigned to the variable must be compatible with its data type.

Page 15: Compiler: Programming Language= Assignments and statements

15• Java is a strongly-typed language.

•This means that, for the most part, you can only assign a value of a particular data type to a variable of the same type

•Some examples:int x = -2; // this is fine; -2 is an integerchar c = ‘2’; // this is OK alsox = 2.5; // syntax error: 2.5 is a double value, not an int

c = 2; // also an error; 2 is an int value, not a char

Page 16: Compiler: Programming Language= Assignments and statements

16Assignment Statement Variety:

= FORTRAN, C, C++, Java

= Can be bad if it is overloaded for the relational operator for equality.

•ExampleC: a=b=c

• In the C statement A = B = C, the value of C is assigned to B, and the value of B is assigned to A.

Page 17: Compiler: Programming Language= Assignments and statements

17Statements:

Definition : A statement in programming

context is a single complete programming instruction that is written as code.

The Elementary action of evaluation, assignment and control of evaluation orders are specified by the statements of the programming language .

Statements can have diverse forms and languages.

Page 18: Compiler: Programming Language= Assignments and statements

18Function Call Statement

•Certain function calls, such as the MESSAGE function, can be a statement on their own. They are known as function call statements

Page 19: Compiler: Programming Language= Assignments and statements

19The Statement Separator

A single programming statement may span several code lines; one code line may consist of multiple statements.

Therefore, the C compiler must be able to detect when one statement ends and another statement begins.

[ <statement> { ; <statement> } ]

Example : Integer Num:=10;

Page 20: Compiler: Programming Language= Assignments and statements

20Assignment Statement

In computer programming, an assignment statement sets and/or re-sets the value stored in the storage location(s) denoted by a variable name; in other words, it copies a value into the variable. In most imperative programming languages, the assignment statement (or expression) is a fundamental construct.

The syntax of an assignment statement is almost as easy as the syntax of the function call statement. The syntax is as follows:

<variable> := <expression>

Page 21: Compiler: Programming Language= Assignments and statements

21Assignment Statement

• Assignment statements can be in the following two forms

1) x := op y

2) x := y op z

• First statement op is a unary operation. Essential unary operations are unary logical negation, shift operators and conversion operators.

• Second statement op is a binary arithmetic or logical operator.

Page 22: Compiler: Programming Language= Assignments and statements

22Assignment Operator

• An assignment operator is the operator used to assign a new value to a variable, property, event or indexer element in C# programming language.

• Assignment operators can also be used for logical operations such as bitwise logical operations or operations on integral operands and Boolean operands.

• The “colon equals” (:=) is known as the assignment operator. You use it to assign a value or an expression that evaluates to a value to a variable.

• :=

Page 23: Compiler: Programming Language= Assignments and statements

23Jump Statements• The jump statements are the goto statement, the continue statement, the break statement, and the return statement. • The source statement like if and while-do cause jump in the control flow through three address code so any statement in three address code can be given label to make it the target of a jump.

Page 24: Compiler: Programming Language= Assignments and statements

24Indexed Assignment

• Indexed assignment of the form A:=B[I] and A[I]:=B. the first statement sets A to the value in the location I memory units beyond location B.

• In the later statement A [I]:=B, sets the location I units beyond A to the value of B.

• In Both instructions A, B, and I are assumed to refer data objects and will represented by pointers to the symbol table.

Page 25: Compiler: Programming Language= Assignments and statements

25Address and Pointer Assignment

• Address and pointer assignmentx := &yx := *y*x := y

• First statement, sets the value of x to be the location of y. • In x := *y, here y is a pointer or temporary whose r-value is a location. The r-value of x is made equal to the contents of that location. • *x := y sets the r-value of the object pointed to by a to the r-value of y.

Page 26: Compiler: Programming Language= Assignments and statements

26Assignment Operations

•Most basic statements for initializing variables•General syntax:• variable = expression;

•Example:• length = 25;

•Expression•Any combination of constants and variables that can be evaluated to yield a result

Page 27: Compiler: Programming Language= Assignments and statements

27

•All variables used in an expression must previously have been given valid values.

•Must be a variable listed immediately to the left of the equal sign.

•Has lower precedence than any other arithmetic operator.

Page 28: Compiler: Programming Language= Assignments and statements

28Assignment Statements: Compound Operators

•A shorthand method of specifying a commonly needed form of assignment.

•Introduced in ALGOL; adopted by CExample:

a = a + b is written as a += b

•Unary assignment operators in C-based languages combine increment and decrement operations with assignment.

Page 29: Compiler: Programming Language= Assignments and statements

29Examples:

• sum = ++count (count incremented, then assigned to sum)

• sum = count++ (count assigned to sum, then incremented)

• count++ (count incremented)• -count++ (count incremented then negated)

Page 30: Compiler: Programming Language= Assignments and statements

30Assignment 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())!= EndOfFile){…}

ch = getchar() is carried out; the result (assigned to ch) is used as a conditional value for the while statement

Page 31: Compiler: Programming Language= Assignments and statements

31Simple and Compound Statements

• Simple Statement: A Simple statement is one which does not contain any augmented statement.

• Read, Assignment and goto are the three examples of simple statements.• In certain languages, a program may be

regarded as a sequence of simple statements. (e.g., BASIC, SNOBOL)

• Compound Statement: A Compound Statement is one that may contain one or more embedded statements.• The logical IF in FORTRAN • IF (condition) statement

Page 32: Compiler: Programming Language= Assignments and statements

32Compound statements: The Compound Statement enable computation that logically belong together and be grouped together syntactically.

• This ability of grouping together helps make program readable.

Example of Compound statement:

1. If condition then statement

2. If condition then statement else statement

3. While condition do statement

Page 33: Compiler: Programming Language= Assignments and statements

33

Explanation:

• The if-then and if-then-else statements allows all actions that apply for the value of condition to be grouped together.

• The compound statements permits the sequence of the statements to be used wherever one may appear.

• The while statements is to establish the loops with arbitrary loop control conditions.

• In compound statement in other languages the syntax may be widely different.

• Note*: The test for the condition is performed before the statements allowing the loops to be executed zero times if necessary.

Page 34: Compiler: Programming Language= Assignments and statements

34Types of Statements

1.Conditional Statements.

2.Sequential Statements.

3.Control Statements.

4.Structural Statements.

5.Declaration Statements.

6.Input Statements.

7.Output Statements.

Page 35: Compiler: Programming Language= Assignments and statements

35Conditional Statements:

• In Conditional statements are features of a programming language, which perform different computations or actions depending on whether a programmer specified boolean condition evaluates to true or false.

Page 36: Compiler: Programming Language= Assignments and statements

36

Conditional statement is a set of rules performed if a certain condition is met. Below are some examples of conditional statements in programming.

this last example will print if it is greater than 10, less than 10 or equal to 10.

Page 37: Compiler: Programming Language= Assignments and statements

37Sequential Statement:

• In most languages , control automatically flows from one statement to the next.• Certain statements such as goto, break, call and return deliberately alter the flow of control.• There has been considerable debate as to what statements are best suited for creating easy to understand flow-of-control patterns in programs.• The Sequential statement has been the focus and it permits the arbitrary flow-of-control patterns, including those that are difficult to comprehend.

Page 38: Compiler: Programming Language= Assignments and statements

38Example:

Page 39: Compiler: Programming Language= Assignments and statements

39Control Structure:

• A control statement is a statement that determines whether other statements will be executed.

• An if statement decides whether to execute another statement, or decides which of two statements to execute.

• A loop decides how many times to execute another statement.

• while loops test whether a condition is true before executing the controlled statement.• do-while loops test whether a condition is true after

executing controlled statement.

Page 40: Compiler: Programming Language= Assignments and statements

40

• The for loops are (typically) used to execute the controlled statement a given number of times.

• A switch statement decides which of several statements to execute.

Page 41: Compiler: Programming Language= Assignments and statements

41Structural Statements:

•Certain Statements such as END serve to group Simple statements into structures.• Following the structured program theorem:

• "Sequence: Ordered statements or subroutines executed in sequence.

• "Selection: One or a number of statements is executed depending on the state of the program. This is usually expressed with keywords such as if..then..else.

Page 42: Compiler: Programming Language= Assignments and statements

42

Iteration: a statement or block is executed until the program reaches a certain state, or operations have been applied to every element of a collection. This is usually expressed with keywords such as while, repeat, for or do..until

Recursion: a statement is executed by repeatedly calling itself until termination conditions are met.

Page 43: Compiler: Programming Language= Assignments and statements

43Declaration Statements:

• These statements generally produce no executable code.

• Their semantic effect is to uniform the compiler about the attributes of names encountered in the program .

• The compiler implements’ a declaration statement by storing the attribute information contained therein in the symbol table and then consulting this information whenever declared names are used in program.

Page 44: Compiler: Programming Language= Assignments and statements

44•Declaration of a variable serves two purposes: It associates a type and an identifier (or name) with the variable. The type allows the compiler to interpret statements correctly.

•Declarations are most commonly used for functions, variables, constants, and classes, but can also be used for other entities such as enumerations and type definitions.

Page 45: Compiler: Programming Language= Assignments and statements

45Input & Output Statements:

• These statements serve the role as their names .

• In many languages they are implemented by library subroutines made available by operating system to control input/output devices.

• It convert data to human readable form.

• In variety of languages we see formats, which are strings of information about how data is to be interpreted or Expressed (Read or Write)

Page 46: Compiler: Programming Language= Assignments and statements

46

Example:

Page 47: Compiler: Programming Language= Assignments and statements

47