Date post: | 16-Feb-2017 |
Category: |
Engineering |
Upload: | debarati-das |
View: | 587 times |
Download: | 2 times |
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
Stages in Compiler Design
We Work on this stage !
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.
Questions Pertaining to Semantic Analysis
The Questions We Answer :
Semantic Analysis
Function always
returns some value
Unreachable Statements
Function Return Type is compatible with Header
Expression Compatibility
with Datatypes
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.
Design of the System
LEX YACC PYTHON Program
Input C Program
Semantic Errors
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.}
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;}
Example
int func1(int x){ if(x==1) {
return x; } else {
if(x==2){ return x;}
}}
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);}
Need For Semantic Analysis
1. Code becomes more meaningful
2. Code has more potential for Optimisation
3. Code becomes flexible and ERROR free.