Lexical and Syntax Analysis

Size: px
Start display at page:

Download "Lexical and Syntax Analysis"

Transcription

1 Lexical and Syntax Analysis (of Programming Languages) Abstract Syntax

2 Lexical and Syntax Analysis (of Programming Languages) Abstract Syntax

3 What is Parsing? Parser String of characters Data structure Easy for humans to write Easy for programs to process A parser also checks that the input string is well-formed, and if not, rejects it.

4 What is Parsing? Parser String of characters Data structure Easy for humans to write Easy for programs to process A parser also checks that the input string is well-formed, and if not, rejects it.

5 Example 1 Parser Charlton, 49 Lineker, 48 Beckham, Charlton 17 Beckham 48 Lineker CSV (Comma Separated Value) Array of pairs

6 Example 1 Parser Charlton, 49 Lineker, 48 Beckham, Charlton 17 Beckham 48 Lineker CSV (Comma Separated Value) Array of pairs

7 Concrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is a set of rules that describe valid outputs from the parser. The data structure produced by a parser is commonly termed the abstract syntax tree.

8 Concrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is a set of rules that describe valid outputs from the parser. The data structure produced by a parser is commonly termed the abstract syntax tree.

9 Concrete and Abstract Syntax Parser String of characters Data structure Conforms to the Concrete Syntax of the language Conforms to the Abstract Syntax of the language

10 Concrete and Abstract Syntax Parser String of characters Data structure Conforms to the Concrete Syntax of the language Conforms to the Abstract Syntax of the language

11 Abstract syntax The abstract syntax is usually specified as a data type in the programming language being used, in our case C. Example: typedef struct { char* name; int goals; } Player; typedef struct { Player* players; int size; } Squad; An abstract syntax tree is a value of this type.

12 Abstract syntax The abstract syntax is usually specified as a data type in the programming language being used, in our case C. Example: typedef struct { char* name; int goals; } Player; typedef struct { Player* players; int size; } Squad; An abstract syntax tree is a value of this type.

13 This Chapter How: to define the abstract syntax to construct abstract syntax trees in the programming language C. Also revisits some important C programming techniques. If you need a C tutorial then the following books are recommended.

14 This Chapter How: to define the abstract syntax to construct abstract syntax trees in the programming language C. Also revisits some important C programming techniques. If you need a C tutorial then the following books are recommended.

15 POINTERS Pointer: a variable that holds the address of a core storage location. [The Free Dictionary]

16 POINTERS Pointer: a variable that holds the address of a core storage location. [The Free Dictionary]

17 Pointers Declare a variable x of type int and initialise it to the value 10. x: int x = 10; 10 Declare a variable p of type int* (read: int pointer). int* p; Make p point to x (or assign the address of x to p). p = &x; p: p: x: 10

18 Pointers Declare a variable x of type int and initialise it to the value 10. x: int x = 10; 10 Declare a variable p of type int* (read: int pointer). int* p; Make p point to x (or assign the address of x to p). p = &x; p: p: x: 10

19 Pointers Print p (here, the address of x). printf("%i\n", p ); p: x: Print the value pointed to by p (here, the value of x). printf("%i\n", *p ); p: 10 x: 10 Assign 20 to the location pointed to by p. *p = 20; p: x: 20

20 Pointers Print p (here, the address of x). printf("%i\n", p ); p: x: Print the value pointed to by p (here, the value of x). printf("%i\n", *p ); p: 10 x: 10 Assign 20 to the location pointed to by p. *p = 20; p: x: 20

21 Exercise 1 What is printed by the following program? void swap(int* x, int* y) { int tmp; tmp = *x; *x = *y; *y = tmp; } void main() { int a = 1; int b = 2; swap(&a, &b); printf("a=%i, b=%i\n", a, b); }

22 Exercise 1 What is printed by the following program? void swap(int* x, int* y) { int tmp; tmp = *x; *x = *y; *y = tmp; } void main() { int a = 1; int b = 2; swap(&a, &b); printf("a=%i, b=%i\n", a, b); }

23 DYNAMIC ALLOCATION Dynamic Allocation: the allocation of memory storage for use in a computer program. [The Free Dictionary]

24 DYNAMIC ALLOCATION Dynamic Allocation: the allocation of memory storage for use in a computer program. [The Free Dictionary]

25 Array allocation Declare a variable p of type int*. int* p; p: Allocate memory for an array of 4 int values and let point p to it. p = malloc(4 * sizeof(int)); p:

26 Array allocation Declare a variable p of type int*. int* p; p: Allocate memory for an array of 4 int values and let point p to it. p = malloc(4 * sizeof(int)); p:

27 Array indexing Assign 10 to the location pointed to by p. *p = 10; Assign 20 to the first element of the array pointed to by p. p[0] = 20; Copy the first element of the array to the third element. p[2] = p[0]; p: p: p:

28 Array indexing Assign 10 to the location pointed to by p. *p = 10; Assign 20 to the first element of the array pointed to by p. p[0] = 20; Copy the first element of the array to the third element. p[2] = p[0]; p: p: p:

29 Array deallocation When finished with an array allocated by malloc, call free to release the space, otherwise your program may run out of memory. free(p); p: Space released, so it can be reused by future calls to malloc.

30 Array deallocation When finished with an array allocated by malloc, call free to release the space, otherwise your program may run out of memory. free(p); p: Space released, so it can be reused by future calls to malloc.

31 STRINGS String: a series of consecutive characters. [The Free Dictionary]

32 STRINGS String: a series of consecutive characters. [The Free Dictionary]

33 Strings Declare a variable s, initialised to point to the string hi. char* s = hi ; s: h i \0 Let s point to the next character. s = s + 1; s: h i \0 And let s point to the previous character again. s = s - 1; s: h i \0

34 Strings Declare a variable s, initialised to point to the string hi. char* s = hi ; s: h i \0 Let s point to the next character. s = s + 1; s: h i \0 And let s point to the previous character again. s = s - 1; s: h i \0

35 Exercise 2 What is printed by the following program? int f(char* s) { int i = 0; while (s[i]!= '\0') i++; return i; } void main() { char* x = Hello ; printf( %i\n, f(x)); }

36 Exercise 2 What is printed by the following program? int f(char* s) { int i = 0; while (s[i]!= '\0') i++; return i; } void main() { char* x = Hello ; printf( %i\n, f(x)); }

37 USER-DEFINED TYPES Type: the general character or structure held in common by a number of things. [The Free Dictionary]

38 USER-DEFINED TYPES Type: the general character or structure held in common by a number of things. [The Free Dictionary]

39 Type definitions A typedef declaration allows a new name to be given to a type. typedef int Integer; typedef char* String; Existing type A new name Example use: String s; /* Declare a string s */ Integer i; /* and an integer i */ i = 0; s = hello ;

40 Type definitions A typedef declaration allows a new name to be given to a type. typedef int Integer; typedef char* String; Existing type A new name Example use: String s; /* Declare a string s */ Integer i; /* and an integer i */ i = 0; s = hello ;

41 Enumerations An enum declaration introduces a new type whose values are members of a given set. enum colour {RED, GREEN, BLUE}; New type Possible values Example use: enum colour c; c = RED; if (c == RED) printf( Red\n ); Give it a shorter name: typedef enum colour Colour;

42 Enumerations An enum declaration introduces a new type whose values are members of a given set. enum colour {RED, GREEN, BLUE}; New type Possible values Example use: enum colour c; c = RED; if (c == RED) printf( Red\n ); Give it a shorter name: typedef enum colour Colour;

43 Structures An struct declaration introduces a new type that is a conjunction of one or more existing types. New type struct rectangle { float width; float height; }; Example use: struct rectangle r; r.width = 10; r.height = 20; A width and a height A circle: struct circle { float radius; }

44 Structures An struct declaration introduces a new type that is a conjunction of one or more existing types. New type struct rectangle { float width; float height; }; Example use: struct rectangle r; r.width = 10; r.height = 20; A width and a height A circle: struct circle { float radius; }

45 Unions An union declaration introduces a new type that is a disjunction of one or more existing types. New type union shape { struct circle circ; struct rectangle rect; }; A circle or a rectangle Example use: struct shape s; s.circ.radius = 10;

46 Unions An union declaration introduces a new type that is a disjunction of one or more existing types. New type union shape { struct circle circ; struct rectangle rect; }; A circle or a rectangle Example use: struct shape s; s.circ.radius = 10;

47 Tagged unions Often a tag is used to denote the active disjunct of a union. Another definition of shape: struct shape { enum { CIRCLE, RECTANGLE } tag; union { struct circle circ; struct rectangle rect; }; };

48 Tagged unions Often a tag is used to denote the active disjunct of a union. Another definition of shape: struct shape { enum { CIRCLE, RECTANGLE } tag; union { struct circle circ; struct rectangle rect; }; };

49 Tagged unions Example: s is a circle and t is a rectangle, and both are of type struct shape. struct shape s, t; s.tag = CIRCLE; s.circ.radius = 10; t.tag = RECTANGLE; t.rect.width = 5; t.rect.height = 15;

50 Tagged unions Example: s is a circle and t is a rectangle, and both are of type struct shape. struct shape s, t; s.tag = CIRCLE; s.circ.radius = 10; t.tag = RECTANGLE; t.rect.width = 5; t.rect.height = 15;

51 Tagged unions Example: compute the area of any given shape s. float area(struct shape s) { if (s.tag == CIRCLE) { float r = s.circ.radius; return (3.14 * r * r); } if (s.tag == RECTANGLE) { return (s.rect.width * s.rect.height); } }

52 Tagged unions Example: compute the area of any given shape s. float area(struct shape s) { if (s.tag == CIRCLE) { float r = s.circ.radius; return (3.14 * r * r); } if (s.tag == RECTANGLE) { return (s.rect.width * s.rect.height); } }

53 Recursive structures A value of type struct t may contain a value of type struct t*. struct list { int head; struct list* tail; }; typedef struct list List; Suppose x is a value of type List*. (*xs).head xs->head (*(*xs).tail).head xs->tail->head

54 Recursive structures A value of type struct t may contain a value of type struct t*. struct list { int head; struct list* tail; }; typedef struct list List; Suppose x is a value of type List*. (*xs).head xs->head (*(*xs).tail).head xs->tail->head

55 Recursive structures Example: inserting an item onto the front of a linked list. List* insert(list* xs, int x) { List* ys = malloc(sizeof(list)); ys->head = x; ys->tail = xs; return ys; }

56 Recursive structures Example: inserting an item onto the front of a linked list. List* insert(list* xs, int x) { List* ys = malloc(sizeof(list)); ys->head = x; ys->tail = xs; return ys; }

57 CASE STUDY A simplifier for arithmetic expressions.

58 CASE STUDY A simplifier for arithmetic expressions.

59 Concrete syntax Consider the following concrete syntax for arithmetic expressions, where v ranges over variable names and n over integers. e v n e + e e * e ( e ) Example expression: x * y + (z * 10)

60 Concrete syntax Consider the following concrete syntax for arithmetic expressions, where v ranges over variable names and n over integers. e v n e + e e * e ( e ) Example expression: x * y + (z * 10)

61 Simplification Consider the algebraic law: x. x * 1 = x This law can be used to simplify expressions by using it as a rewrite rule from left to right. Example simplification: x * (y * 1) x * y

62 Simplification Consider the algebraic law: x. x * 1 = x This law can be used to simplify expressions by using it as a rewrite rule from left to right. Example simplification: x * (y * 1) x * y

63 Problem 1. Define an abstract syntax, in C, for arithmetic expressions. 2. Show how to construct abstract syntax trees that represent arithmetic expressions. 3. Implement the simplification as a C function that takes and returns an abstract syntax tree.

64 Problem 1. Define an abstract syntax, in C, for arithmetic expressions. 2. Show how to construct abstract syntax trees that represent arithmetic expressions. 3. Implement the simplification as a C function that takes and returns an abstract syntax tree.

65 Abstract syntax typedef enum { ADD, MUL } Op; struct expr { enum { VAR, NUM, APP } tag; union { char* var; }; }; int num; struct { struct expr* e1; Op op; struct expr* e2; } app; typedef struct expr Expr; A variable or a number or an op and two sub-expressions

66 Abstract syntax typedef enum { ADD, MUL } Op; struct expr { enum { VAR, NUM, APP } tag; union { char* var; }; }; int num; struct { struct expr* e1; Op op; struct expr* e2; } app; typedef struct expr Expr; A variable or a number or an op and two sub-expressions

67 Constructors Expr* mkvar(char* v) { Expr* e = malloc(sizeof(expr)); e->tag = VAR; e->var = v; return e; } Expr* mknum(int n) { Expr* e = malloc(sizeof(expr)); e->tag = NUM; e->num = n; return e; } Expr* mkapp(expr* e1, Op op, Expr* e2) { Expr* e = malloc(sizeof(expr)); e->tag = APP; e->app.op = op; e->app.e1 = e1; e->app.e2 = e2; return e; }

68 Constructors Expr* mkvar(char* v) { Expr* e = malloc(sizeof(expr)); e->tag = VAR; e->var = v; return e; } Expr* mknum(int n) { Expr* e = malloc(sizeof(expr)); e->tag = NUM; e->num = n; return e; } Expr* mkapp(expr* e1, Op op, Expr* e2) { Expr* e = malloc(sizeof(expr)); e->tag = APP; e->app.op = op; e->app.e1 = e1; e->app.e2 = e2; return e; }

69 Abstract syntax trees An abstract syntax tree that represents the expression x + y * 2 can be constructed by the following C expression mkapp( mkvar("x"), ADD, mkapp( mkvar("y"), MUL, mknum(2)))

70 Abstract syntax trees An abstract syntax tree that represents the expression x + y * 2 can be constructed by the following C expression mkapp( mkvar("x"), ADD, mkapp( mkvar("y"), MUL, mknum(2)))

71 Simplification x. x * 1 = x is implemented by void simplify(expr* e) { if (e->tag == APP && e->app.op == MUL && e->app.e2->tag == NUM && e->app.e2->num == 1) { *e = *(e->app.e1); } if (e->tag == APP) { simplify(e->app.e1); simplify(e->app.e2); } }

72 Simplification x. x * 1 = x is implemented by void simplify(expr* e) { if (e->tag == APP && e->app.op == MUL && e->app.e2->tag == NUM && e->app.e2->num == 1) { *e = *(e->app.e1); } if (e->tag == APP) { simplify(e->app.e1); simplify(e->app.e2); } }

73 Homework exercises Implement a pretty printer that prints an abstract syntax tree in a concrete form. void print(expr* e) {... } Extend the simplifier to exploit the following algebraic law. x. x * 0 = 0

74 Homework exercises Implement a pretty printer that prints an abstract syntax tree in a concrete form. void print(expr* e) {... } Extend the simplifier to exploit the following algebraic law. x. x * 0 = 0

75 Motivation for LSA In LSA, we are interested in how to implement the following kind of function Expr* parse(char* string) {... } It takes a string conforming to the concrete syntax and returns an abstract syntax tree.

Lexical and Syntax Analysis. Abstract Syntax

Lexical and Syntax Analysis. Abstract Syntax Lexical and Syntax Analysis Abstract Syntax What is Parsing? Parser String of characters Data structure Easy for humans to write Easy for programs to process A parser also checks that the input string

More information

Lexical and Syntax Analysis

Lexical and Syntax Analysis Lexical and Syntax Analysis (of Programming Languages) Bison, a Parser Generator Lexical and Syntax Analysis (of Programming Languages) Bison, a Parser Generator Bison: a parser generator Bison Specification

More information

Lecture 19: Functions, Types and Data Structures in Haskell

Lecture 19: Functions, Types and Data Structures in Haskell The University of North Carolina at Chapel Hill Spring 2002 Lecture 19: Functions, Types and Data Structures in Haskell Feb 25 1 Functions Functions are the most important kind of value in functional programming

More information

Prof. Carl Schultheiss MS, PE. CLASS NOTES Lecture 12

Prof. Carl Schultheiss MS, PE. CLASS NOTES Lecture 12 Prof. Carl Schultheiss MS, PE CLASS NOTES Lecture 12 In addition to the basic data types C supports user-defined data types. Userdefined data types allow the development of programs using data types that

More information

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9 Fundamental of C Programming Unit I: Q1. What will be the value of the following expression? (2017) A + 9 Q2. Write down the C statement to calculate percentage where three subjects English, hindi, maths

More information

Lectures 5-6: Introduction to C

Lectures 5-6: Introduction to C Lectures 5-6: Introduction to C Motivation: C is both a high and a low-level language Very useful for systems programming Faster than Java This intro assumes knowledge of Java Focus is on differences Most

More information

Topic 7: Algebraic Data Types

Topic 7: Algebraic Data Types Topic 7: Algebraic Data Types 1 Recommended Exercises and Readings From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 5.5, 5.7, 5.8, 5.10, 5.11, 5.12, 5.14 14.4, 14.5, 14.6 14.9, 14.11,

More information

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information Laboratory 2: Programming Basics and Variables Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information 3. Comment: a. name your program with extension.c b. use o option to specify

More information

Pointers, Dynamic Data, and Reference Types

Pointers, Dynamic Data, and Reference Types Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation The new operator The delete operator Dynamic Memory Allocation for Arrays 1 C++ Data Types simple

More information

Lectures 5-6: Introduction to C

Lectures 5-6: Introduction to C Lectures 5-6: Introduction to C Motivation: C is both a high and a low-level language Very useful for systems programming Faster than Java This intro assumes knowledge of Java Focus is on differences Most

More information

CSC324 Principles of Programming Languages

CSC324 Principles of Programming Languages CSC324 Principles of Programming Languages http://mcs.utm.utoronto.ca/~324 November 14, 2018 Today Final chapter of the course! Types and type systems Haskell s type system Types Terminology Type: set

More information

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017 Pointers (part 1) EECS 2031 25 September 2017 1 What are pointers? We have seen pointers before. scanf( %f, &inches );! 2 1 Example char c; c = getchar(); printf( %c, c); char c; char *p; c = getchar();

More information

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park Variables in C CMSC 104, Fall 2012 John Y. Park 1 Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement 2 What Are Variables in C? Variables in C have the

More information

Custom Types. Outline. COMP105 Lecture 19. Today Creating our own types The type keyword The data keyword Records

Custom Types. Outline. COMP105 Lecture 19. Today Creating our own types The type keyword The data keyword Records Outline COMP105 Lecture 19 Custom Types Today Creating our own types The type keyword The data keyword Records Relevant book chapters Programming In Haskell Chapter 8 Learn You a Haskell Chapter 8 The

More information

Structures and Pointers

Structures and Pointers Structures and Pointers Comp-206 : Introduction to Software Systems Lecture 11 Alexandre Denault Computer Science McGill University Fall 2006 Note on Assignment 1 Please note that handin does not allow

More information

Homework #3: CMPT-379 Distributed on Oct 23; due on Nov 6 Anoop Sarkar

Homework #3: CMPT-379 Distributed on Oct 23; due on Nov 6 Anoop Sarkar Homework #3: CMPT-379 Distributed on Oct 23; due on Nov 6 Anoop Sarkar anoop@cs.sfu.ca Only submit answers for questions marked with. Provide a makefile such that make compiles all your programs, and make

More information

Character Strings. String-copy Example

Character Strings. String-copy Example Character Strings No operations for string as a unit A string is just an array of char terminated by the null character \0 The null character makes it easy for programs to detect the end char s[] = "0123456789";

More information

Pointers. Pointers. Pointers (cont) CS 217

Pointers. Pointers. Pointers (cont) CS 217 Pointers CS 217 Pointers Variables whose values are the addresses of variables Operations address of (reference) & indirection (dereference) * arithmetic +, - Declaration mimics use char *p; *p is a char,

More information

Week 3 Lecture 2. Types Constants and Variables

Week 3 Lecture 2. Types Constants and Variables Lecture 2 Types Constants and Variables Types Computers store bits: strings of 0s and 1s Types define how bits are interpreted They can be integers (whole numbers): 1, 2, 3 They can be characters 'a',

More information

Arrays and Pointers (part 1)

Arrays and Pointers (part 1) Arrays and Pointers (part 1) CSE 2031 Fall 2012 Arrays Grouping of data of the same type. Loops commonly used for manipulation. Programmers set array sizes explicitly. Arrays: Example Syntax type name[size];

More information

Dynamic Memory Allocation and Command-line Arguments

Dynamic Memory Allocation and Command-line Arguments Dynamic Memory Allocation and Command-line Arguments CSC209: Software Tools and Systems Programming Furkan Alaca & Paul Vrbik University of Toronto Mississauga https://mcs.utm.utoronto.ca/~209/ Week 3

More information

CSE2301. Dynamic memory Allocation. malloc() Dynamic Memory Allocation and Structs

CSE2301. Dynamic memory Allocation. malloc() Dynamic Memory Allocation and Structs Warning: These notes are not complete, it is a Skelton that will be modified/add-to in the class. If you want to us them for studying, either attend the class or get the completed notes from someone who

More information

Arrays and Pointers (part 1)

Arrays and Pointers (part 1) Arrays and Pointers (part 1) CSE 2031 Fall 2010 17 October 2010 1 Arrays Grouping of data of the same type. Loops commonly used for manipulation. Programmers set array sizes explicitly. 2 1 Arrays: Example

More information

Data Representation and Storage. Some definitions (in C)

Data Representation and Storage. Some definitions (in C) Data Representation and Storage Learning Objectives Define the following terms (with respect to C): Object Declaration Definition Alias Fundamental type Derived type Use pointer arithmetic correctly Explain

More information

Programming. Structures, enums and unions

Programming. Structures, enums and unions Programming Structures, enums and unions Summary } Structures } Declaration } Member access } Function arguments } Memory layout } Array of structures } Typedef } Enums } Unions 2 Idea! } I want to describe

More information

Lecture 2. Xiaoguang Wang. January 16th, 2014 STAT 598W. (STAT 598W) Lecture 2 1 / 41

Lecture 2. Xiaoguang Wang. January 16th, 2014 STAT 598W. (STAT 598W) Lecture 2 1 / 41 Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41 Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures 4 Compilation Process 5 Exercises (STAT 598W) Lecture

More information

Computer Organization & Systems Exam I Example Questions

Computer Organization & Systems Exam I Example Questions Computer Organization & Systems Exam I Example Questions 1. Pointer Question. Write a function char *circle(char *str) that receives a character pointer (which points to an array that is in standard C

More information

Compiling and Running a C Program in Unix

Compiling and Running a C Program in Unix CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 95 ] Compiling and Running a C Program in Unix Simple scenario in which your program is in a single file: Suppose you want to name

More information

C library = Header files + Reserved words + main method

C library = Header files + Reserved words + main method DAY 1: What are Libraries and Header files in C. Suppose you need to see an Atlas of a country in your college. What do you need to do? You will first go to the Library of your college and then to the

More information

PART ONE Fundamentals of Compilation

PART ONE Fundamentals of Compilation PART ONE Fundamentals of Compilation 1 Introduction A compiler was originally a program that compiled subroutines [a link-loader]. When in 1954the combination algebraic compiler came into use, or rather

More information

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms SE352b Software Engineering Design Tools W3: Programming Paradigms Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa SE352b: Roadmap CASE Tools: Introduction System Programming Tools Programming Paradigms

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages CMSC330 Fall 2017 OCaml Data Types CMSC330 Fall 2017 1 OCaml Data So far, we ve seen the following kinds of data Basic types (int, float, char, string) Lists

More information

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14 Variables in C CMSC 104, Spring 2014 Christopher S. Marron (thanks to John Park for slides) 1 Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement 2 What

More information

CSE 230 Intermediate Programming in C and C++

CSE 230 Intermediate Programming in C and C++ CSE 230 Intermediate Programming in C and C++ Unions and Bit Fields Fall 2017 Stony Brook University Instructor: Shebuti Rayana http://www3.cs.stonybrook.edu/~cse230/ Union Like structures, unions are

More information

C-types: basic & constructed. C basic types: int, char, float, C constructed types: pointer, array, struct

C-types: basic & constructed. C basic types: int, char, float, C constructed types: pointer, array, struct C-types: basic & constructed C basic types: int, char, float, C constructed types: pointer, array, struct Memory Management Code Global variables in file (module) Local static variables in functions Dynamic

More information

Motivation was to facilitate development of systems software, especially OS development.

Motivation was to facilitate development of systems software, especially OS development. A History Lesson C Basics 1 Development of language by Dennis Ritchie at Bell Labs culminated in the C language in 1972. Motivation was to facilitate development of systems software, especially OS development.

More information

Input And Output of C++

Input And Output of C++ Input And Output of C++ Input And Output of C++ Seperating Lines of Output New lines in output Recall: "\n" "newline" A second method: object endl Examples: cout

More information

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1 300580 Programming Fundamentals 3 With C++ Variable Declaration, Evaluation and Assignment 1 Today s Topics Variable declaration Assignment to variables Typecasting Counting Mathematical functions Keyboard

More information

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables. When a program is run, memory space is immediately reserved for the variables defined in the program. This memory space is kept by the variables until the program terminates. These variables are called

More information

We do not teach programming

We do not teach programming We do not teach programming We do not teach C Take a course Read a book The C Programming Language, Kernighan, Richie Georgios Georgiadis Negin F.Nejad This is a brief tutorial on C s traps and pitfalls

More information

Chapter-8 DATA TYPES. Introduction. Variable:

Chapter-8 DATA TYPES. Introduction. Variable: Chapter-8 DATA TYPES Introduction To understand any programming languages we need to first understand the elementary concepts which form the building block of that program. The basic building blocks include

More information

This is CS50. Harvard University Fall Quiz 0 Answer Key

This is CS50. Harvard University Fall Quiz 0 Answer Key Quiz 0 Answer Key Answers other than the below may be possible. Binary Bulbs. 0. Bit- Sized Questions. 1. Because 0 is non- negative, we need to set aside one pattern of bits (000) for it, which leaves

More information

CS349/SE382 A1 C Programming Tutorial

CS349/SE382 A1 C Programming Tutorial CS349/SE382 A1 C Programming Tutorial Erin Lester January 2005 Outline Comments Variable Declarations Objects Dynamic Memory Boolean Type structs, enums and unions Other Differences The Event Loop Comments

More information

High Performance Computing in C and C++

High Performance Computing in C and C++ High Performance Computing in C and C++ Rita Borgo Computer Science Department, Swansea University Summary Introduction to C Writing a simple C program Compiling a simple C program Running a simple C program

More information

BLM2031 Structured Programming. Zeyneb KURT

BLM2031 Structured Programming. Zeyneb KURT BLM2031 Structured Programming Zeyneb KURT 1 Contact Contact info office : D-219 e-mail zeynebkurt@gmail.com, zeyneb@ce.yildiz.edu.tr When to contact e-mail first, take an appointment What to expect help

More information

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS Manish Dronacharya College Of Engineering, Maharishi Dayanand University, Gurgaon, Haryana, India III. Abstract- C Language History: The C programming language

More information

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings Lecture 4: Outline I. Pointers A. Accessing data objects using pointers B. Type casting with pointers C. Difference with Java references D. Pointer pitfalls E. Use case II. Arrays A. Representation in

More information

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 05 I/O statements Printf, Scanf Simple

More information

12 CREATING NEW TYPES

12 CREATING NEW TYPES Lecture 12 CREATING NEW TYPES of DATA Typedef declaration Enumeration Structure Bit fields Uninon Creating New Types Is difficult to solve complex problems by using programs written with only fundamental

More information

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

COP 3223 Introduction to Programming with C - Study Union - Fall 2017 COP 3223 Introduction to Programming with C - Study Union - Fall 2017 Chris Marsh and Matthew Villegas Contents 1 Code Tracing 2 2 Pass by Value Functions 4 3 Statically Allocated Arrays 5 3.1 One Dimensional.................................

More information

Principles of C and Memory Management

Principles of C and Memory Management COMP281 Lecture 8 Principles of C and Memory Management Dr Lei Shi Last Lecture Pointer Basics Previous Lectures Arrays, Arithmetic, Functions Last Lecture Pointer Basics Previous Lectures Arrays, Arithmetic,

More information

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef today advanced data types (1) typedef. mon 23 sep 2002 homework #1 due today homework #2 out today quiz #1 next class 30-45 minutes long one page of notes topics: C advanced data types dynamic memory allocation

More information

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW IMPORTANT QUESTIONS IN C FOR THE INTERVIEW 1. What is a header file? Header file is a simple text file which contains prototypes of all in-built functions, predefined variables and symbolic constants.

More information

Types. C Types. Floating Point. Derived. fractional part. no fractional part. Boolean Character Integer Real Imaginary Complex

Types. C Types. Floating Point. Derived. fractional part. no fractional part. Boolean Character Integer Real Imaginary Complex Types C Types Void Integral Floating Point Derived Boolean Character Integer Real Imaginary Complex no fractional part fractional part 2 tj Types C Types Derived Function Array Pointer Structure Union

More information

Motivation was to facilitate development of systems software, especially OS development.

Motivation was to facilitate development of systems software, especially OS development. A History Lesson C Basics 1 Development of language by Dennis Ritchie at Bell Labs culminated in the C language in 1972. Motivation was to facilitate development of systems software, especially OS development.

More information

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona The List Datatype CSc 372 Comparative Programming Languages 6 : Haskell Lists Department of Computer Science University of Arizona collberg@gmail.com All functional programming languages have the ConsList

More information

Chapter 10 C Structures, Unions, Bit Manipulations

Chapter 10 C Structures, Unions, Bit Manipulations Chapter 10 C Structures, Unions, Bit Manipulations Skipped! Skipped! and Enumerations Skipped! Page 416 In programming languages, Arrays (Chapter 6) allows programmers to group elements of the same type

More information

Programming in C - Part 2

Programming in C - Part 2 Programming in C - Part 2 CPSC 457 Mohammad Reza Zakerinasab May 11, 2016 These slides are forked from slides created by Mike Clark Where to find these slides and related source code? http://goo.gl/k1qixb

More information

Lecture 3: C Programm

Lecture 3: C Programm 0 3 E CS 1 Lecture 3: C Programm ing Reading Quiz Note the intimidating red border! 2 A variable is: A. an area in memory that is reserved at run time to hold a value of particular type B. an area in memory

More information

PRINCIPLES OF OPERATING SYSTEMS

PRINCIPLES OF OPERATING SYSTEMS PRINCIPLES OF OPERATING SYSTEMS Tutorial-1&2: C Review CPSC 457, Spring 2015 May 20-21, 2015 Department of Computer Science, University of Calgary Connecting to your VM Open a terminal (in your linux machine)

More information

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking Agenda COMP 181 Type checking October 21, 2009 Next week OOPSLA: Object-oriented Programming Systems Languages and Applications One of the top PL conferences Monday (Oct 26 th ) In-class midterm Review

More information

CS24 Week 2 Lecture 1

CS24 Week 2 Lecture 1 CS24 Week 2 Lecture 1 Kyle Dewey Overview C Review Void pointers Allocation structs void* (Void Pointers) void* Like any other pointer, it refers to some memory address However, it has no associated type,

More information

Data Representation and Storage

Data Representation and Storage Data Representation and Storage Learning Objectives Define the following terms (with respect to C): Object Declaration Definition Alias Fundamental type Derived type Use size_t, ssize_t appropriately Use

More information

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Pointers and Structure Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island 1 Pointer Variables Each variable in a C program occupies space in

More information

CS 314 Principles of Programming Languages. Lecture 11

CS 314 Principles of Programming Languages. Lecture 11 CS 314 Principles of Programming Languages Lecture 11 Zheng Zhang Department of Computer Science Rutgers University Wednesday 12 th October, 2016 Zheng Zhang 1 eddy.zhengzhang@cs.rutgers.edu Class Information

More information

XSEDE Scholars Program Introduction to C Programming. John Lockman III June 7 th, 2012

XSEDE Scholars Program Introduction to C Programming. John Lockman III June 7 th, 2012 XSEDE Scholars Program Introduction to C Programming John Lockman III June 7 th, 2012 Homework 1 Problem 1 Find the error in the following code #include int main(){ } printf(find the error!\n");

More information

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1 Announcements assign due tonight No late submissions Labs start this week Very helpful for assign1 Goals for Today Pointer operators Allocating memory in the heap malloc and free Arrays and pointer arithmetic

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

Advanced Pointer Topics

Advanced Pointer Topics Advanced Pointer Topics Pointers to Pointers A pointer variable is a variable that takes some memory address as its value. Therefore, you can have another pointer pointing to it. int x; int * px; int **

More information

Informatics 1 Functional Programming Lecture 9. Algebraic Data Types. Don Sannella University of Edinburgh

Informatics 1 Functional Programming Lecture 9. Algebraic Data Types. Don Sannella University of Edinburgh Informatics 1 Functional Programming Lecture 9 Algebraic Data Types Don Sannella University of Edinburgh Part I Algebraic types Everything is an algebraic type data Bool = False True data Season = Winter

More information

Subject: Fundamental of Computer Programming 2068

Subject: Fundamental of Computer Programming 2068 Subject: Fundamental of Computer Programming 2068 1 Write an algorithm and flowchart to determine whether a given integer is odd or even and explain it. Algorithm Step 1: Start Step 2: Read a Step 3: Find

More information

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE NOTE: 1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains FIVE questions. 2. PART ONE is to be

More information

Tokens, Expressions and Control Structures

Tokens, Expressions and Control Structures 3 Tokens, Expressions and Control Structures Tokens Keywords Identifiers Data types User-defined types Derived types Symbolic constants Declaration of variables Initialization Reference variables Type

More information

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam Multimedia Programming 2004 Lecture 2 Erwin M. Bakker Joachim Rijsdam Recap Learning C++ by example No groups: everybody should experience developing and programming in C++! Assignments will determine

More information

SYSC 2006 C Winter 2012

SYSC 2006 C Winter 2012 SYSC 2006 C Winter 2012 Pointers and Arrays Copyright D. Bailey, Systems and Computer Engineering, Carleton University updated Sept. 21, 2011, Oct.18, 2011,Oct. 28, 2011, Feb. 25, 2011 Memory Organization

More information

Type checking of statements We change the start rule from P D ; E to P D ; S and add the following rules for statements: S id := E

Type checking of statements We change the start rule from P D ; E to P D ; S and add the following rules for statements: S id := E Type checking of statements We change the start rule from P D ; E to P D ; S and add the following rules for statements: S id := E if E then S while E do S S ; S Type checking of statements The purpose

More information

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19 Data Storage Geoffrey Brown Bryce Himebaugh Indiana University August 9, 2016 Geoffrey Brown, Bryce Himebaugh 2015 August 9, 2016 1 / 19 Outline Bits, Bytes, Words Word Size Byte Addressable Memory Byte

More information

IV Unit Second Part STRUCTURES

IV Unit Second Part STRUCTURES STRUCTURES IV Unit Second Part Structure is a very useful derived data type supported in c that allows grouping one or more variables of different data types with a single name. The general syntax of structure

More information

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary GATE- 2016-17 Postal Correspondence 1 C-Programming Computer Science & Information Technology (CS) 20 Rank under AIR 100 Postal Correspondence Examination Oriented Theory, Practice Set Key concepts, Analysis

More information

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5 1 What is structure? How to declare a Structure? Explain with Example Structure is a collection of logically related data items of different data types grouped together under a single name. Structure is

More information

Do not start the test until instructed to do so!

Do not start the test until instructed to do so! Instructions: Print your name in the space provided below. This examination is closed book and closed notes, aside from the permitted one-page formula sheet. No calculators or other electronic devices

More information

Structures, Operators

Structures, Operators Structures Typedef Operators Type conversion Structures, Operators Basics of Programming 1 G. Horváth, A.B. Nagy, Z. Zsóka, P. Fiala, A. Vitéz 10 October, 2018 c based on slides by Zsóka, Fiala, Vitéz

More information

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010 CSE 374 Programming Concepts & Tools Hal Perkins Spring 2010 Lecture 12 C: structs, t linked lists, and casts Where we are We ve seen most of the basic stuff about C, but we still need to look at structs

More information

Review of the C Programming Language

Review of the C Programming Language Review of the C Programming Language Prof. James L. Frankel Harvard University Version of 11:55 AM 22-Apr-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights reserved. Reference Manual for the

More information

Computer Systems Principles. C Pointers

Computer Systems Principles. C Pointers Computer Systems Principles C Pointers 1 Learning Objectives Learn about floating point number Learn about typedef, enum, and union Learn and understand pointers 2 FLOATING POINT NUMBER 3 IEEE Floating

More information

Parsing and Pattern Recognition

Parsing and Pattern Recognition Topics in IT 1 Parsing and Pattern Recognition Week 10 Lexical analysis College of Information Science and Engineering Ritsumeikan University 1 this week mid-term evaluation review lexical analysis its

More information

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body; CLASSROOM SESSION Loops in C Loops are used to repeat the execution of statement or blocks There are two types of loops 1.Entry Controlled For and While 2. Exit Controlled Do while FOR Loop FOR Loop has

More information

Low-Level C Programming. Memory map Pointers Arrays Structures

Low-Level C Programming. Memory map Pointers Arrays Structures Low-Level C Programming Memory map Pointers Arrays Structures Memory Map 0x7FFF_FFFF Binaries load at 0x20000 by default Stack start set by binary when started Stack grows downwards You will need one stack

More information

CMSC 330: Organization of Programming Languages. OCaml Data Types

CMSC 330: Organization of Programming Languages. OCaml Data Types CMSC 330: Organization of Programming Languages OCaml Data Types CMSC330 Spring 2018 1 OCaml Data So far, we ve seen the following kinds of data Basic types (int, float, char, string) Lists Ø One kind

More information

MODULE 5: Pointers, Preprocessor Directives and Data Structures

MODULE 5: Pointers, Preprocessor Directives and Data Structures MODULE 5: Pointers, Preprocessor Directives and Data Structures 1. What is pointer? Explain with an example program. Solution: Pointer is a variable which contains the address of another variable. Two

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Fall 2016 Lecture 3a Andrew Tolmach Portland State University 1994-2016 Formal Semantics Goal: rigorous and unambiguous definition in terms of a wellunderstood formalism (e.g.

More information

Linked List. April 2, 2007 Programming and Data Structure 1

Linked List. April 2, 2007 Programming and Data Structure 1 Linked List April 2, 2007 Programming and Data Structure 1 Introduction head A linked list is a data structure which can change during execution. Successive elements are connected by pointers. Last element

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Fall 2017 Lecture 3a Andrew Tolmach Portland State University 1994-2017 Binding, Scope, Storage Part of being a high-level language is letting the programmer name things: variables

More information

Review of the C Programming Language for Principles of Operating Systems

Review of the C Programming Language for Principles of Operating Systems Review of the C Programming Language for Principles of Operating Systems Prof. James L. Frankel Harvard University Version of 7:26 PM 4-Sep-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights

More information

The New C Standard (Excerpted material)

The New C Standard (Excerpted material) The New C Standard (Excerpted material) An Economic and Cultural Derek M. Jones derek@knosof.co.uk Copyright 2002-2008 Derek M. Jones. All rights reserved. 1378 type specifier type-specifier: void char

More information

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers CSE 30: Computer Organization and Systems Programming Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers Diba Mirza University of California, San Diego 1 Q: Which of the assignment

More information

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18 Assignment Lecture 9 Logical Operations Formatted Print Printf Increment and decrement Read through 3.9, 3.10 Read 4.1. 4.2, 4.3 Go through checkpoint exercise 4.1 Logical Operations - Motivation Logical

More information

Procedural programming with C

Procedural programming with C Procedural programming with C Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 77 Functions Similarly to its mathematical

More information

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

Chapter 2 (Dynamic variable (i.e. pointer), Static variable) Chapter 2 (Dynamic variable (i.e. pointer), Static variable) August_04 A2. Identify and explain the error in the program below. [4] #include int *pptr; void fun1() { int num; num=25; pptr= #

More information

Programming in Haskell Aug-Nov 2015

Programming in Haskell Aug-Nov 2015 Programming in Haskell Aug-Nov 2015 LECTURE 14 OCTOBER 1, 2015 S P SURESH CHENNAI MATHEMATICAL INSTITUTE Enumerated data types The data keyword is used to define new types data Bool = False True data Day

More information