Preliminary Syntax Rudra Dutta CSC 230 Spring 2007

  • Slides: 14
Download presentation
Preliminary Syntax Rudra Dutta CSC 230 - Spring 2007, Section 001 Copyright Rudra Dutta,

Preliminary Syntax Rudra Dutta CSC 230 - Spring 2007, Section 001 Copyright Rudra Dutta, NCSU, Spring, 2007

Basic Issues l Syntax - parsing Keywords, constant, string-literals, identifiers, punctuators – Tokenization –

Basic Issues l Syntax - parsing Keywords, constant, string-literals, identifiers, punctuators – Tokenization – l Variable types – Width, conversion (implicit) Precedence and Associativity l Call by value l Scope - basics l – Blocks Control flow l Array basics l Copyright Rudra Dutta, NCSU, Spring, 2007 2

Keywords l Reserved - specific meaning in language� auto break case char const continue

Keywords l Reserved - specific meaning in language� auto break case char const continue default do double else enum extern float Copyright Rudra Dutta, NCSU, Spring, 2007 for goto if inline int long register restrict return short signed sizeof static struct switch typedef union unsigned void volatile while _Bool _Complex _Imaginary 3

Literals l Literally quoted number or string constant 23 549. 44 x 4 A

Literals l Literally quoted number or string constant 23 549. 44 x 4 A 3 2 e 10 -9469 _, a-z, A-Z, 0 -9, “A” “Now is the time for all good men…” “hello, world!n” “Numbert. Square Rootnn” Copyright Rudra Dutta, NCSU, Spring, 2007 4

Escape Sequences Copyright Rudra Dutta, NCSU, Spring, 2007 5

Escape Sequences Copyright Rudra Dutta, NCSU, Spring, 2007 5

Punctuators l Tokens, other than whitespace (see later), which may separate identifiers, keywords, …

Punctuators l Tokens, other than whitespace (see later), which may separate identifiers, keywords, … [ ] ( ) { }. -> ++ -- & * + - ~ ! / % << >> < > <= >= == != ^ | && || ? : ; . . . = *= /= %= += -= <<= >>= &= ^= |= , # ## <: : > <% %> %: %: %: Copyright Rudra Dutta, NCSU, Spring, 2007 6

Tokenization l Whitespace Space, tab, vertical tab, newline, form-feed – (comments) – Preprocessor’s tokenization

Tokenization l Whitespace Space, tab, vertical tab, newline, form-feed – (comments) – Preprocessor’s tokenization is different from compiler’s l Successive token are recognized out of dissimilar elements, or elements separated by whitespace – l int main(){printf(“Hello, world!n”); } Longest possible token always formed l r Example : x+++++y – Makes sense as : x ++ + ++ y – But will be tokenized as : x ++ ++ + y – Copyright Rudra Dutta, NCSU, Spring, 2007 7

Variable Types l Really just two: int – float – l Actually some more

Variable Types l Really just two: int – float – l Actually some more (variations): double – char – short, long – l Width (length in bytes) short int long – float double – char usually 1 or 2 – Macros in limits. h such as INT_MAX, INT_MIN – Copyright Rudra Dutta, NCSU, Spring, 2007 8

Size and Conversions Implicit, assignment and explicit l Implicit - automatic conversion forced on

Size and Conversions Implicit, assignment and explicit l Implicit - automatic conversion forced on compiler due to comparison l – l Always convert to “wider” of two Assignment - assignment to a variable with literal or variable of another type “nonsense” produces error – “risky” produces warning – l Explicit - using cast expression – (float) 22, (float) i Copyright Rudra Dutta, NCSU, Spring, 2007 9

Precedence & Associativity Copyright Rudra Dutta, NCSU, Spring, 2007 10

Precedence & Associativity Copyright Rudra Dutta, NCSU, Spring, 2007 10

Call by Value Providing parameter to function call creates a copy l Copy is

Call by Value Providing parameter to function call creates a copy l Copy is passed to called function l Becomes local variable of that function – Can be changed by the called function – l At return, the copy is destroyed – l Any changes are lost Whether copy was changed or not, original variable used to call function is not affected Copyright Rudra Dutta, NCSU, Spring, 2007 11

Scope - Basics l Global variables - outside all functions – l Is accessible

Scope - Basics l Global variables - outside all functions – l Is accessible from all functions within compilation unit Local variable - within a function Just after function begins - most usual place – Accesssible only inside function – Separate variable from any global by same name – l Variables can be local to a block – BUT not inside statement (C 99 usage is exception) More specific variable always “hides” less specific l Variables only visible AFTER definition l Issue of multiple files - later (linkage) l Copyright Rudra Dutta, NCSU, Spring, 2007 12

Control Flow l Conditionals if-else and variants – switch-case – l Loops for –

Control Flow l Conditionals if-else and variants – switch-case – l Loops for – while and do-while – break and continue l goto and labels l Copyright Rudra Dutta, NCSU, Spring, 2007 13

Arrays in C Really aren’t any l Okay, there are, but only rudimentary l

Arrays in C Really aren’t any l Okay, there are, but only rudimentary l Notation: [ ] l Several of the same type of variables l – l Individual ones referred to by subscripts Variable sized arrays in general prohibited by standard int numbers[10]; numbers[0] = 4; Copyright Rudra Dutta, NCSU, Spring, 2007 14