Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMPUTER SCIENCE, KOREA UNIVERSITY Chapter2 Lexical Elements, Operators,and the C System Rim, Hae-Chang Department of Computer Science and Engineering.

Similar presentations


Presentation on theme: "COMPUTER SCIENCE, KOREA UNIVERSITY Chapter2 Lexical Elements, Operators,and the C System Rim, Hae-Chang Department of Computer Science and Engineering."— Presentation transcript:

1

2 COMPUTER SCIENCE, KOREA UNIVERSITY Chapter2 Lexical Elements, Operators,and the C System Rim, Hae-Chang Department of Computer Science and Engineering Korea University

3 Natural Language Processing Lab, Korea University 2 Compilers and Tokens  C is a language  An alphabet  Syntax: rules for putting together words and punctuations to make legal programs  Compiler  Checks the legality of the source code (program)  Translate the source code into object code (object code is translated into executable code (i.e. target language, machine language)  Collects the characters of the program into tokens  Tokens in C (ANSI C)  The basic vocabulary of C  keywords, identifiers, constants, string constants, operators, punctuators

4 Natural Language Processing Lab, Korea University 3 Contents 2.1 Characters and Lexical Elements 2.2 Syntax Rule( 구문 규칙 ) 2.3 Comments( 주석 ), 2.4 keywords 2.5 Identifiers( 식별자 ), 2.6 Constants( 상수 ) 2.7 String Constants( 문자열 상수 ) 2.8 Operators and Punctuators( 연산자와 구두점 ) 2.9 Precedence and Associativity of Operators ( 연산자의 우선 순위와 결합 법칙 ) 2.10 Increment and Decrement Operators ( 증가 연산자와 감소 연산자 ) 2.11 Assignment Operators ( 배정 연산자 ) 2.12 An example: Computing Powers of 2 2.13 The C system

5 Natural Language Processing Lab, Korea University 4 2.1 Characters and Lexical Elements  A programmer construct C program as a sequence of characters  Characters that can be used in a C program  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, tab  Compiler collects characters into tokens

6 Natural Language Processing Lab, Korea University 5 An Example C Source Code – sum.c /* Read in two integers and print the sum. */ #include int main(void) { int a, b, sum; printf(”Input two integers : ”); scanf(”%d%d”, &a, &b); sum = a + b; printf(”%d + %d = %d\n”, a, b, sum); return 0; }

7 Natural Language Processing Lab, Korea University 6 An Example C Source Code – sum.c  /* Read in two integers and print their sum. */  comments : /* 부터 */ 까지는 공백으로 대치  #include  전처리 지시자 : the standard header file stdio.h is included  stdio.h contains the function prototypes for printf() and scanf()  int main(void) { int a, b, sum; => The compiler groups these characters into four kinds of tokens:  키워드 : int, void  식별자 : main, a, b, sum  연산자 : ( ) -> tells the compiler that main is a function  구두점 : “{”, “,”, “;”

8 Natural Language Processing Lab, Korea University 7 An Example C Source Code – sum.c  printf(“Input two integers: “); scanf(“%d%d”, &a, &b);  printf and scanf are identifier  () after them tells the compiler that they are functions  "Input two integers : "  String constant : 큰 따옴표로 둘러싸인 문자들  &a, &b  & is the address operator  sum = a + b;  = and + are operators

9 Natural Language Processing Lab, Korea University 8 2.2 Syntax Rules  BNF(Backus-Naur Form)  Can be used to describe a programming language  Productions or rewriting rules  Symbols used in productions italicsnon-terminals (syntactic categories) ::=LHS can be rewritten as RHS |choice { } 1 choose one item { } 0+ repeat 0 or more times { } 1+ repeat 1 or more times { } opt optional othersterminal symbols

10 Natural Language Processing Lab, Korea University 9 BNF Examples - 1  Any lowercase or uppercase alphabet or any digit digit ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 lowercase_letter ::= a | b | c |... | z uppercase_letter ::= A | B | C |... | Z letter ::= lowercase_letter | uppercase_letter letter_or_digit ::= letter | digit  e.g) “a”, “3”, …  Any sequence of letter or digits alphanumeric_string ::= {letter | digit} 1+  e.g) “3”, “ab777c”, …

11 Natural Language Processing Lab, Korea University 10 BNF Examples - 2  Any sequence of letter or digits that start with a uppercase letter  Any sequence of letter or digits that start with either a uppercase letter or a digit  Any sequence of letter or digits that may end with “.” u_alpha_string ::= uppercase_letter {letter | digit} 0+ p_name ::= {uppercase_letter | digit} 1 {letter | digit} 0+ f_word ::= {letter | digit} 1+ {.} opt

12 Natural Language Processing Lab, Korea University 11 2.3 Comments( 주석 )  C style comments  Any strings placed between “ /* ” and “ */ ”  /* here is comments */  /** this is also comments **/  /* what if ”*/” comes in the middle */  C++ style comments  // form here to the end of the line

13 Natural Language Processing Lab, Korea University 12 2.4 Keywords( 키워드 )  Reserved words in C (They can not be redefined or used in other contexts)  Additional keywords in Borland C  asm, cdecl, far, huge, interrupt, near, pascal auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while

14 Natural Language Processing Lab, Korea University 13 2.5 Identifiers( 식별자 )  Identifiers  Names of variables or functions  identifier ::= { letter | _ } 1 { letter | digit | _ } 0+  e.g.) k, _id, iamanidentifier2, so_am_i not#me, 101_south, -plus (X)  First 31 characters of identifier are discriminated (ANSI C)  Good programming style: choose meaningful names  Caution: Identifiers that begin with _ can conflict with system names.

15 Natural Language Processing Lab, Korea University 14 2.6 Constants( 상수 )  Integer  e.g.) 0, 17, 017, 0x17  Floating numbers  e.g.) 1.0, 3.141592, 3.14E+03  Character constants  e.g.) ’a’, ’b’, ’+’, ’\n’  String constants  e.g.) ”abc”, ””, ” ”, ”\n”, ”\””, ”\\”

16 Natural Language Processing Lab, Korea University 15 2.8 Operators,Punctuators( 연산자, 구두점 )  Operators  e.g.) +, -, *, /, %  Punctuators  e.g.) “ ( ”, “ ) ”, “ { ”, “ } ”, “, ”, “ ; ”  Example int main(void) { int a, b, c = 3; a = 17 * ( b + c ); printf(”%d\n”, a); }

17 Natural Language Processing Lab, Korea University 16 2.9 Precedence and Associativity ( 우선순위와 결합법칙 )  Precedence and associativity  The order in which operations are performed  e.g.) 1 + 2 * 3  1 + (2 * 3) 1 + 2 – 3  ((1 + 2) – 3)  Precedence and associativity of arithmetic operators OperatorAssociativity () ++(postfix) --(postfix) +(unary) –(unary) ++(prefix) --(prefix) * / % + - = += -= *= /= etc. left to right right to left left to right right to left

18 Natural Language Processing Lab, Korea University 17 2.10 Increment and Decrement Operators ( 증가 연산자와 감소 연산자 )  Increment and decrement operators  ++i;  i = i + 1;  i++;  --i;  i = i – 1;  i--;  Both operators can be either prefix or postfix  Exercise int a, b, c=0, d=0; a = ++c; b = d++; printf(”%d %d %d %d\n”, a, b, c--, --d);  a=1, b=0, c=1, d=1 (before printf)  1 0 1 0 is printed  a=1, b=0, c=0, d=0 (after printf)

19 Natural Language Processing Lab, Korea University 18 2.11 Assignment Operators ( 배정연산자 )  Assignment operators  Change the value of a variable  e.g.) a = 1; a = 2 + 3; a = b + c; a = ( b = 2 ) + ( c = 3 ); a = b = c = 0;  More assignment operators  +=, -=, *=, /=, %=, >=, |=, &=, ^=  e.g.) k += 2;  k = k + 2; j *= k + 3;  j = j * (k + 3);

20 Natural Language Processing Lab, Korea University 19 Assignment operator( 배정 연산자 )  다른 언어와는 달리 C 는 = 를 연산자로 다룸 a = ( b = 2 ) + ( c = 3);  배정 연산자 =, +=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |= ( 주의 ) j *= k + 3 은 j = j * k + 3 이 아니라, j = j * (k + 3) 임 선언 및 초기화 int i = 1, j = 2, k = 3, m = 4; 수식 동일한 수식 동일한 수식 결과 i += j + k i += (j + k) i = (i + (j + k)) 6 j *= k = m + 5 j *= (k = (m + 5)) j = (j * (k = (m + 5))) 18

21 Natural Language Processing Lab, Korea University 20 2.12 Example – Computing P owers of 2 /* Some powers of 2 are printed. */ #include int main(void) { int i = 0, power = 1; while (++i <= 10) printf("%-6d", power *= 2); printf("\n"); return 0; }  2 4 8 16 32 64 128 256 512 1024

22 Natural Language Processing Lab, Korea University 21 2.13 The C system Preprocessor and Standard Library  C 시스템  C 언어, 전처리기, 컴파일러, 라이브러리, 편집기 등으로 구성  전처리기  # 으로 시작하는 행을 전처리 지시자라고 함 #include #include "filename" #define PI 3.141592  표준 라이브러리  프로그램에 유용한 함수들로 C 시스템이 제공함  printf(), scanf(), 등  사용자가 알아서 해당 헤더파일을 포함시켜야함

23 Natural Language Processing Lab, Korea University 22 Practice – prn_rand.c #include int main(void) { int i, n; printf("\n%s\n%s", "Random integers will be printed.", "How many do you want to see?"); scanf("%d", &n); for (i = 0; i < n; ++i) { if (i % 10 == 0) putchar('\n'); printf("%7d", rand()); } printf("\n\n"); return 0; }


Download ppt "COMPUTER SCIENCE, KOREA UNIVERSITY Chapter2 Lexical Elements, Operators,and the C System Rim, Hae-Chang Department of Computer Science and Engineering."

Similar presentations


Ads by Google