semantic analysis of a c program

13
Semantic Analysis of a C program Done under the guidance of Prof. Kiran P Department of Computer Science PES Institute of Technology Done by Aakar Tripathi - 01 Chandramouli S Sastry - 46 Darshan SJ - 51 Debarati Das - 52

Upload: debarati-das

Post on 16-Feb-2017

587 views

Category:

Engineering


2 download

TRANSCRIPT

Page 1: Semantic Analysis of a C Program

Semantic Analysis of a C program

Done under the guidance of Prof. Kiran PDepartment of Computer Science

PES Institute of Technology

Done by

Aakar Tripathi - 01

Chandramouli S Sastry - 46

Darshan SJ - 51

Debarati Das - 52

Page 2: Semantic Analysis of a C Program

Stages in Compiler Design

We Work on this stage !

Page 3: Semantic Analysis of a C Program

Semantic Analysis Phase

Semantics of a language provide meaning to its constructs, like tokens and syntax structure and help in interpreting symbols, their types, and their relations with each other.

Semantic analysis is that phase in Compiler Design where we delve deep to check whether the code we have written forms a sensible set of instructions in the programming language.

This project aims at finding semantic errors when the given input is a C program.

Page 4: Semantic Analysis of a C Program

Questions Pertaining to Semantic Analysis

Page 5: Semantic Analysis of a C Program

The Questions We Answer :

Semantic Analysis

Function always

returns some value

Unreachable Statements

Function Return Type is compatible with Header

Expression Compatibility

with Datatypes

Page 6: Semantic Analysis of a C Program

Assumptions made in Design

1.  The given code is syntactically valid (parenthesis matched, function call statements are correct and so on.)

2. We assume that the only type of control structures used is the if condition.

3. The function name is of the form func[a-z0-9]*

4. Declarations can only be of the form Type ID; That is, multiple declarations on a single line is not allowed.

5. Return statements can be of the form return ID; It shouldn’t have any expressions.

Page 7: Semantic Analysis of a C Program

Design of the System

LEX YACC PYTHON Program

Input C Program

Semantic Errors

Page 8: Semantic Analysis of a C Program

Compatibility of Returned Value with Return Type

int func1(int c){ int x; float y; if(c==1)

return x; else

return y;//y is of type float. But return type should be int.}

Page 9: Semantic Analysis of a C Program

Handling No Unreachable Statements and Function should always return some value. int func1(int a,int b,int c)

{if(c==1)

return a+b;else if(c==2)

return b-a;else if(c==3)

return a-b;//semantic error->nothing

may be returned}

**************************************************int func2(int a,int b,int c){ if(c==1)

return a+b; else

return b-a;//following statements are unreachable printf(“here”); if(c==3) return a-b;}

Page 10: Semantic Analysis of a C Program

Example

int func1(int x){ if(x==1) {

return x; } else {

if(x==2){ return x;}

}}

Page 11: Semantic Analysis of a C Program
Page 12: Semantic Analysis of a C Program

4. Expression Type Compatibility

int funcabc(int a,float b,double c){ int x; x=10.5;//error int y; y=a+b+c+10.03f+100.505;//error printf("%d\s%d",x,y);}

Page 13: Semantic Analysis of a C Program

Need For Semantic Analysis

1. Code becomes more meaningful

2. Code has more potential for Optimisation

3. Code becomes flexible and ERROR free.