1 / 22

Chapter2 Lexical Elements, Operators,and the C System

Chapter2 Lexical Elements, Operators,and the C System. Rim, Hae-Chang Department of Computer Science and Engineering Korea University. Compilers and Tokens. C is a language An alphabet Syntax: rules for putting together words and punctuations to make legal programs Compiler

abe
Download Presentation

Chapter2 Lexical Elements, Operators,and the C System

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. Chapter2 Lexical Elements, Operators,and the C System Rim, Hae-Chang Department of Computer Science and Engineering Korea University COMPUTER SCIENCE, 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

  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

  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

  5. An Example C Source Code – sum.c /* Read in two integers and print the sum. */ #include <stdio.h> 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; }

  6. An Example C Source Code – sum.c • /* Read in two integers and print their sum. */ • comments : /*부터 */까지는 공백으로 대치 • #include <stdio.h> • 전처리 지시자 : 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 • 구두점 : “{”, “,”, “;”

  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

  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 italics non-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 others terminal symbols

  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”, …

  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

  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

  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

  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.

  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”, ”\””, ”\\”

  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); }

  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

  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 0is printed  a=1, b=0, c=0, d=0 (after printf)

  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);

  19. Assignment operator(배정 연산자) • 다른 언어와는 달리 C는 =를 연산자로 다룸 a = ( b = 2 ) + ( c = 3); • 배정 연산자 =, +=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |= (주의) j *= k + 3 은 j = j * k + 3 이 아니라, j = j * (k + 3) 임

  20. 2.12 Example – Computing Powers of 2 /* Some powers of 2 are printed. */ #include <stdio.h> 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

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

  22. Practice – prn_rand.c #include <stdio.h> #include <stdlib.h> 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; }

More Related