Lexical Elements, Operators, and the C System - PowerPoint PPT Presentation

About This Presentation
Title:

Lexical Elements, Operators, and the C System

Description:

Chapter 2 3 Lexical Elements, Operators, and the C System – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 17
Provided by: hck1
Category:

less

Transcript and Presenter's Notes

Title: Lexical Elements, Operators, and the C System


1
Chapter 2
  • 3? ??
  • Lexical Elements, Operators, and the C System

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

3
Lexical Terms
  • Token? ?? (ANSI C)
  • keywords, identifiers, constants, string
    constants, operators, punctuators
  • Lexical errors

4
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
  • sumab sum a b ? s u m a b

5
Syntax Rules
  • Digit 0123 456789
  • BNF(Bacus Naur Form)
  • (rewriting), (separate),
  • (optional), (0 or more),
  • 1, 0, 1, opt
  • conditional_statement if (expression)
  • statement
  • else statementopt

6
??(identifier)
  • identifier
  • letter _1 letter digit _0
  • ?? constant(??, ??), string? ?? ??(BNF)? ???
    2.74, 36, 2.7e-04, I love you.? ??? ???.
    (constant? string?)
  • Appendix B? ???? ?? !!!!

7
Comments
  • / This is comments /
  • // This is comments
  • Self-documentation
  • ??? ?? documentation? ??? ???

8
Keywords
  • Auto, do, goto, signed (page 77)
  • Reserved word? ??? ?? word? ???
  • Keyword? reserved? ??? ??? ?? ??

9
Constants, String constants,Operators, Punctuator
  • Constants 0, 17, 3,14159, a, , \n,
    enumeration type
  • String constants I, a string, ,
    cab, \\ I \\
  • abc def ? abcdef
  • ??? / This is a string /,
  • Operators , -, , /, ??? a-b, a_b
  • Punctuators , ,, , (, )
  • ??? main(void)
  • a, ab
  • ?? ? ??? ??? ???

10
Precedence and Associativity of Operators
  • Page 332? ?? ???

Operator precedence and associativity 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
11
Increment and decrement Operators
  • val, val valval1
  • ???? ??? ???
  • ??
  • (1) a1 ba printf(d,d,a,b)
  • (2) a1 ba printf(d,d,a,b)
  • Side-effect

12
Assignment Operators
  • abc / ? ????? ?? ????? ???? ??? ?? /
  • variableright_side
  • a (b2) (c3)
  • ? b2 c3 abc / ?? ?? /
  • abc0
  • kk2 ? k2
  • , , -, , /, , gtgt, ltlt, , ,
  • / ?? gt, lt? ?? /
  • i jk ? i (jk) ? iijk ? i(i(jk))

13
The C System(1)
  • The preprocessor
  • include ltstdio.hgt
  • include ltstdio.hgt / int rand(void)? ?? /

14
The C System(2)
  • random? ??? ????
  • seed number ? srand(time(NULL))

15
?? ??? ? ??
  • 2? 5,7,8,9? ??? ??? ?????? ????
  • 10? ????.
  • 20?? ????.
  • 29? ????
  • / ?? ???? ????? ??
  • ?, ??? ??? ???? ??
  • ?? ?? ??? ??? ??? /

16
??? ? ??
  • 10, 15,17,18, 23, 25
  • 27 (? ??? ?? ?? ????
  • ??? ??)
Write a Comment
User Comments (0)
About PowerShow.com