1 / 28

Lexical Elements and Operators

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.

Download Presentation

Lexical Elements and Operators

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 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. 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) Senem Kumova Metin

  5. 6 kinds of Tokens in ANSI C • keywords • identifiers • constants • string constants • punctuators • 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! 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 meaningfulto the human reader Senem Kumova Metin

  8. 2. Identifier (2/2) • 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    */ Senem Kumova Metin

  9. 3. Constants • integer 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 constantswritten 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, ...) 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” 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+b printf("\"a+b\"");  "a+b" printf("a\\b");  a\b } 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) 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 */ 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*/ • =      +=     -=   *=   /=   %=   >>=  <<=  &=  ^=  Senem Kumova Metin

  15. Precedence and Associativity of Operators Senem Kumova Metin

  16. Incrementand 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 */ Senem Kumova Metin

  17. Incrementand 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= 2 a2= 2 b2= 1 Senem Kumova Metin

  18. Incrementand 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+1 printf(“ a1= %d b1=%d\n”, a1,b1); } Senem Kumova Metin

  19. Incrementand 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); } 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?? Senem Kumova Metin

  21. Assignment Operators(2/2) #include<stdio.h> main() { int a = 1; int b= 3; a= b ; // now a is 3 and b is 3 a+=2; // means a= a+2 a+=b; // means a= a+b a*=3; // means a= a*3 a*=b; // means a= a*b } Senem Kumova Metin

  22. Assignment Compability int x= 3;// a decimal integer double 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 OK x = 10/4; // x is 2 y = 10/4; // y is 2.0 y = 10/4.0; // y is 2.5 /* CHECK IT IN YOUR PROGRAM */ Senem Kumova Metin

  23. Integer Modulo Operator #include<stdio.h> main() { int y= 0; int x; x= 35; y= x%5; // y = mod5(x) = 0 y= x%3 // y = mod3(x) = 2 y= x%-3 // y = mod-3(x) = 2 x= -35; y= x%5; // y = mod5(x) = 0 y= x%3 // y = mod3(x) = -2 y= x%-3 // y = mod-3(x) = -2 } Senem Kumova Metin

  24. Relational Operators (1\2) #include<stdio.h> main() { int y; int x; y=0; x= 35 if( 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 ”); } Senem Kumova Metin

  25. Relational Operators (2\2) #include<stdio.h> main() { int y; int x; y=35; x= 35 if( 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 ”); } 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 ”); } 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 Senem Kumova Metin

  28. Conditional Operator ?: CAN BE CONVERTED TO int x, y; if( x>0) y=3; else y=5; int x, y; y=x>0 ? 3 : 5; • expression1 ? expression2 : expression3 • Convert first operand to boolean ( FALSE / TRUE) • If first operand = True (1), Evaluate the second operand • If first operand = False (0), Evaluate the third operand Senem Kumova Metin

More Related