chapter 2

15
Chapter 2 3 주 주주 Lexical Elements, Operators, and the C System

Upload: ashtyn

Post on 05-Jan-2016

26 views

Category:

Documents


0 download

DESCRIPTION

Chapter 2. 3 주 강의 Lexical Elements, Operators, and the C System. Compiler. Grammar ::: lexical, syntax, semantics, pragmatics Compiler checks the legality of C code Preprocessor  compiler Error recovery. Lexical Terms. Token 의 종류 (ANSI C) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 2

Chapter 2

3 주 강의Lexical Elements, Operators,

and the C System

Page 2: Chapter 2

Compiler

• Grammar ::: lexical, syntax, semantics, pragmatics • Compiler checks the legality of C code• Preprocessor compiler• Error recovery

Page 3: Chapter 2

Lexical Terms

• Token 의 종류 (ANSI C) : keywords, identifiers, constants,

string constants, operators, punctuators

• Lexical errors

Page 4: Chapter 2

Characters and Lexical Elements

• Lowercase letters, uppercase letters, digits, other characters (+, -, …), white space characters (blank, newline, tab)

• Lexical errors int a, b, c; inta, b, c &a, &b == & a , & b• sum=a+b; == sum = a +b; s u m = a +b;

Page 5: Chapter 2

Syntax Rules

• Digit ::= 0|1|2|3 |4|5|6|7|8|9• BNF(Bacus Naur Form) ::= (rewriting), | (separate), * [] (optional), {} (0 or more), * {}1, {}0+, {}1+, {}opt• conditional_statement :: if (expression) statement {else statement}opt

Page 6: Chapter 2

명칭 (identifier)

• identifier ::= {letter | _}1 {letter | digit | _}0+

• 숙제 ::: constant( 실수 , 정수 ), string을 위한 문법 (BNF) 을 참고로 2.74, 36, 2.7e-04, “I love you.” 가 바름을 보여라 . (constant 나 string 임 )

• Appendix B 를 참고하여 설명 !!!!

Page 7: Chapter 2

Comments

• /* This is comments */ // This is comments• Self-documentation

??? 바른 documentation 의 중요성 ???

Page 8: Chapter 2

Keywords

• Auto, do, goto, signed … (page 77)• Reserved word 와 그렇지 않은 word 의

차이점• Keyword 가 reserved 된 언어와 그렇지

않은 언어

Page 9: Chapter 2

Constants, String constants,Operators, Punctuator

• Constants : 0, 17, 3,14159, ‘a’, ‘+’, “\n”, enumeration type• String constants : “I”, “a string”, “ “, “c=a+b”, “\\ I \\” “abc” “def” “abcdef” ??? /* “ This is a string” */, “ ”• Operators : +, -, *, /, % ??? a-b, a_b• Punctuators : “{“, “,”, “;”, “(“, “)” ??? main(void)• ++a, a+=b• 질문 :::: ‘=‘ 은 어디에 속하나 ???

Page 10: Chapter 2

Precedence and Associativity of Operators

*** Page 332 에 전체 테이블 Operator precedence and associativity Operator Associativity () ++(postfix) --(postfix) left to right

+(unary), -(unary), ++(prefix), --(prefix)

right to left

* / % left to right + - left to right = += -= *= /= right to left

Page 11: Chapter 2

Increment and decrement Operators

• ++val, val++ :: val=val+1• 속도에서 빠르다 … 이유 ?• 차이 :: (1) a=1; b=++a; printf(“%d,%d”,a,b) (2) a=1; b=a++; printf(“%d,%d”,a,b)• Side-effect

Page 12: Chapter 2

Assignment Operators

• a=b+c; /* ‘=‘ 의 우선순위는 어떤 연산자보다 낮으면서 오른쪽 우선 */

• variable=right_side • a= (b=2) + (c=3); b=2; c=3; a=b+c; /* 속도 차이 */• a=b=c=0;• k=k+2 k+=2;• =, +=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |= /* 조심 … ‘ >=‘, ‘<=‘ 와 구별 */• i += j+k i += (j+k) i=i+j+k i=(i+(j+k))

Page 13: Chapter 2

The C System

• The preprocessor #include <stdio.h> #include <stdio.h> /* int rand(void) 가

있음 */• random 을 이용한 프로그램 seed number srand(time(NULL))

Page 14: Chapter 2

강의 시간에 풀 문제• 2 장 5,7,8,9 를 합하여 하나의 프로그램으로

작성한다• 10 을 검증한다 .• 20 번을 해결한다 .• 29 를 해결한다/* 각자 어디까지 해결하는지 검증…단 , 아직은 성적에 반영하지 않고 해결 못한 사람은 집에서 해온다 */

Page 15: Chapter 2

집에서 풀 문제• 10, 15,17,18, 23, 25• 27 ( 이 문제는 여러 명이 상의해서 풀어도 된다 )