Lexical Elements and Operators

28
Senem Kumova Metin 1 Lexical Elements and Operators

description

Lexical Elements and Operators. C like other languages, has an alphabet and rules for putting together words and punctuation to make correct, or legal programs These rules are the syntax of the language and the compiler checks on the program syntax correctness. C Alphabet. - PowerPoint PPT Presentation

Transcript of Lexical Elements and Operators

Page 1: Lexical Elements and Operators

Senem Kumova Metin 1

Lexical Elements and Operators

Page 2: Lexical Elements and Operators

Senem Kumova Metin 2

C like other languages, has an alphabet and rules for putting together words and punctuation to make correct, or legal programs

These rules are the syntax of the language and the compiler checks on the program syntax correctness

Page 3: Lexical Elements and Operators

Senem Kumova Metin 3

C Alphabet

lowercase letters : a   b   c  ...  z

uppercase letters : A  B  C  ...  Z

digits : 0  1  2  3  4  5  6  7  8  9

other characters : +  *  /  =  (  )  {  }  [  ]  <  >  '  "  !  #  %  &  _  |  ^  ~  \  .  ,  ;  :  ?

white space characters : blank, newline \n, tab \t, etc.

Page 4: Lexical Elements and Operators

Senem Kumova Metin 4

The compiler firstly collects the characters of the program into tokens, which can be thought of as the basic vocabulary of the language (tokens are generated by lexical analyzer)

Next compiler checks syntax rules using

syntax analyzer according to Backus-Naur (BNF) Form (a kind of context-free grammar describing all valid constructions of the language)

Page 5: Lexical Elements and Operators

Senem Kumova Metin 5

6 kinds of Tokens in ANSI C

1. keywords 2. identifiers 3. constants 4. string constants 5. punctuators6. operators

Page 6: Lexical Elements and Operators

Senem Kumova Metin 6

1. Keywords

A keyword, also called a reserved word, has a strict meaning. There are 32 keywords in C They cannot be redefined!

Page 7: Lexical Elements and Operators

Senem Kumova Metin 7

2. Identifier (1/2)

A token that is composed of a sequence of letters, digits and the special character “_”

The programmer uses them chiefly to name variables and functions

They begin with a letter or underscore and are chosen to be meaningful to the human reader

Page 8: Lexical Elements and Operators

Senem Kumova Metin 8

C is case sensitive !!! int CS115; int Cs115; int cS115 are all

different !

      float x, _id; char so_am_I; int cs115/* These are all valid */

     int not#me; char c&&d; /* special characters not allowed */

char 123th;   /* identifiers can not start with a digit   

*/

2. Identifier (2/2)

Page 9: Lexical Elements and Operators

Senem Kumova Metin 9

3. Constantsinteger constants (e.g., 12, 790, however -32, +14 are considered constant integer expressions, and not integer constants)

int myint= 7; int yourint= 6;

floating constants (e.g., 1.0, 3.14, 2.72) float pi= 2.14 ; double c= 10.00002;

character constants written between single quotas (e.g., 'a', '+', '\n' (backslash n - newline character))

char mychar = ‘A’;

enumeration constants (e.g., enum day {su, mo, tu, we, th, fr, sa} – they are constants of type int, with values 0, 1, 2, ...)

Page 10: Lexical Elements and Operators

Senem Kumova Metin 10

4. String Constants(1/2)

A sequence of characters enclosed in a pair of double-quote marks

"abc" "123" "" (null string) "   " (a string of blank characters)

NOTE: ‘a’ is not same with “a”

Page 11: Lexical Elements and Operators

Senem Kumova Metin 11

4. String Constants(2/2)

If the character " (double-quote) or \ (backslash) are to occur in a string constant, they must be preceded by a backslash character \

EXAMPLE:

#include<stdio.h>

main(){

printf( "a+b"); a+bprintf("\"a+b\""); "a+b" printf("a\\b"); a\b

}

Page 12: Lexical Elements and Operators

Senem Kumova Metin 12

5. Punctuators

Curly Brackets {} EXAMPLE: main() { /* …… */ }

Comma , EXAMPLE: int y, x,z;

Semicolon ; EXAMPLE: printf(“hi”);

Brackets (Paranthesis) ()   EXAMPLE: Group some expressions 

2*(7+6)

Page 13: Lexical Elements and Operators

Senem Kumova Metin 13

6. Operators (1\2)Operators can be (arithmetic, logical, assignment, relational...)

()     /* immediately following the name of function */

[] /* Used for arrays*/ ->  .    /* Access members of structures */

+ /* addition operator*/ - /*subtraction operator */ * /*multiplication operator */ / /*division operator */ %       /* integer modulo division:  7 % 3 = 1 */

&&    ||    !   /* logic AND , OR and NOT  */ ^        /* bitwise XOR */ ~        /* bitwise negation */ &   |    /* bitwise AND and OR */

Page 14: Lexical Elements and Operators

Senem Kumova Metin 14

6. Operators (2\2)

sizeof   /* size of an object  */

?:       /* e.g., (n > 0) ? f : n       */

*   &    /* indirection through a pointer and address of an object */

>>     <<   /* shift right and shift left */

<   >   <=  >=  ==  !=    /* relational operators */

++     --     /* increment and decrement operator*/

=      +=     -=   *=   /=   %=   >>=  <<=  &=  ^= 

Page 15: Lexical Elements and Operators

Senem Kumova Metin 15

Precedence and Associativity of Operators

Category  Operator  Associativity 

Postfix  () [] -> . ++ - -   Left to right 

Unary + - ! ~ ++ - - (type) * & sizeof   Right to left 

Multiplicative   * / %  Left to right Additive   + -  Left to right Shift   << >>  Left to right Relational   < <= > >=  Left to right Equality   == !=  Left to right Bitwise AND  &  Left to right Bitwise XOR  ^  Left to right Bitwise OR  |  Left to right Logical AND  &&  Left to right Logical OR  ||  Left to right Conditional  ?:  Right to left 

Assignment = += -= *= /= %= >>= <<= &= ^= |=  Right to left 

Comma  ,  Left to right 

Page 16: Lexical Elements and Operators

Senem Kumova Metin 16

Increment and Decrement Operators (1/4)

cnt++ or ++cnt cnt-- or --cnt

/* cnt ++ or cnt -- ( postfix) ++cnt or -- cnt (prefix) */

Side effect : These operators not only yield a value they also changes the stored value of a variable in memory !!

++45 /* constants cannot be incremented */

++(a*b+1) /* ordinary expressions cannot be incremented */

Page 17: Lexical Elements and Operators

Senem Kumova Metin 17

Increment and Decrement Operators (2/4)#include<stdio.h>main() { int a1 = 1, a2 = 1;

int b1 = 0, b2 = 0;

b1= ++a1; printf(“ a1 = %d b1 =%d\n”, a1, b1);

b2 =a2++;printf(“ a2= %d b2=%d\n”, a2,b2); }

OUTPUT

a1= 2 b1= 2a2= 2 b2= 1

Page 18: Lexical Elements and Operators

Senem Kumova Metin 18

Increment and Decrement Operators (3/4)

#include<stdio.h>main() { int a1 = 1, a2 = 1;

int b1 = 0, b2 = 0;

b1= ++a1; // a1=a1+1 then b1=a1 printf(“ a1= %d b1=%d\n”, a1, b1);

b2 =a2++; // b2=a2 then a2=a2+1printf(“ a1= %d b1=%d\n”, a1,b1);

}

Page 19: Lexical Elements and Operators

Senem Kumova Metin 19

Increment and Decrement Operators (4/4)

#include<stdio.h>main() {

int a = 0, b = 0, c=0;

a= ++c;b=c++;

printf(“ %d %d %d \n”,a,c,++c);}

Page 20: Lexical Elements and Operators

Senem Kumova Metin 20

Assignment Operators (1/2)

Changes the value of a variable !! Unlike other languages C treats = as an operator!!

Variable = right_side Examples :

int a =0, b= 4; a = b // Values of a,b??

int a=0, b=0, c=0;

a=(b=2)+(c=3); // Values of a,b,c??

Assigment operators= +

=-= *= /= %= >>= <<= &= ^= \=

Page 21: Lexical Elements and Operators

Senem Kumova Metin 21

#include<stdio.h>main() { int a = 1; int b= 3;

a= b ; // now a is 3 and b is 3a+=2; // means a= a+2a+=b; // means a= a+ba*=3; // means a= a*3a*=b;// means a= a*b

}

= +=

-= *= /= %= >>= <<= &= ^= \=

Assignment Operators (2/2)

Page 22: Lexical Elements and Operators

Senem Kumova Metin 22

Assignment Compability

int x= 3; // a decimal integerdouble y= 5.234; // a floating number x = y; // truncates y! x is 5 y = x; // it is OK.x = 5 + 3.2; // truncates the result!y = 5 + 3.2; // it is OKx = 10/4; // x is 2y = 10/4; // y is 2.0y = 10/4.0; // y is 2.5

/* CHECK IT IN YOUR PROGRAM */

Page 23: Lexical Elements and Operators

Senem Kumova Metin 23

Integer Modulo Operator

#include<stdio.h>main(){ int y= 0; int x;

x= 35; y= x%5; // y = mod5(x) = 0y= x%3 // y = mod3(x) = 2 y= x%-3 // y = mod-3(x) = 2

x= -35;y= x%5; // y = mod5(x) = 0y= x%3 // y = mod3(x) = -2 y= x%-3 // y = mod-3(x) = -2

}

Page 24: Lexical Elements and Operators

Senem Kumova Metin 24

Relational Operators (1\2)

#include<stdio.h>main(){ int y; int x;

y=0; x= 35if( x == y) // If x is equal to y ?

printf(“ x equals to y \n”);else

printf(“ x does not equal to y\n ”);

x=y;if( x != y) // If x is not equal to y ?

printf(“ x does not equal to y \n”);else

printf(“ x equals to y\n ”); }

Page 25: Lexical Elements and Operators

Senem Kumova Metin 25

Relational Operators (2\2)

#include<stdio.h>main(){ int y; int x;

y=35; x= 35if( x <= y) // If x is less than or equal to y ?

printf(“ x is less than or equal to y \n”);else

printf(“ x is greater than y\n ”);

x=34;if( !(x <= y)) // NOT + If x is less than or equal to

y ?// If x is NOT less than or equal to y?// If x is greater than y?

printf(“ x is greater than y \n”);else

printf(“ x is not greater than y\n ”); }

Page 26: Lexical Elements and Operators

Senem Kumova Metin 26

Logical Operators (AND + OR ) (1\2)

#include<stdio.h>main(){ int x=0; int y= 1;

if( x< 1 && y<0) printf(“ x is less than 1 AND y is less than 1

“); else printf(“ x >= 1 OR y>=0 \n OR both of

them”);

if( x ==1 || y==1)) printf(“ x equals to 1 OR y equals to 1”);

else printf(“ Neither of x or y is not equla to 1 \n

”);}

Page 27: Lexical Elements and Operators

Senem Kumova Metin 27

Logical Operators: && and || (2\2)

AND OPERATOR : &&

First Second Statement && Statement RESULT True && True = True True && False = False False && True = False False && False = False

SOME EXAMPLES :

int X=-1, Y=-1;

• X>0 && Y< 0 F && T FALSE • X!=0 && Y!=10 T && T TRUE• !(X>0 && Y<0) !( F&&T )

!( F )TRUE

OR OPERATOR : ||

First Second Statement || Statement RESULT True || True = True True || False = True False || True = True False || False = False

SOME EXAMPLES :

int X=-1, Y=-1;

• X>0 || Y< 0 F || T TRUE • X!=0 || Y!=10 T || T TRUE• !(X>0 || Y<0) !( F||T )

!( T )FALSE

Page 28: Lexical Elements and Operators

Senem Kumova Metin 28

Conditional Operator ?:

int x, y;

if( x>0) y=3;else y=5;

int x, y;y=x>0 ? 3 : 5;

CAN BE CONVERTED TO

expression1 ? expression2 : expression3

1. Convert first operand to boolean ( FALSE / TRUE)2. If first operand = True (1), Evaluate the second operand3. If first operand = False (0), Evaluate the third operand