SlideShare a Scribd company logo
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
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("%ds%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.

More Related Content

What's hot

Software project management
Software project managementSoftware project management
Software project management
R A Akerkar
 
Software Quality Management
Software Quality ManagementSoftware Quality Management
Software Quality Management
Krishna Sujeer
 
Formal Methods lecture 01
Formal Methods lecture 01Formal Methods lecture 01
Formal Methods lecture 01
Sidra Ashraf
 
Capability Maturity Model (CMM)
Capability Maturity Model (CMM)Capability Maturity Model (CMM)
Capability Maturity Model (CMM)
Ali Sadhik Shaik
 
Requirement engineering evaluation
Requirement engineering evaluationRequirement engineering evaluation
Requirement engineering evaluation
Ishraq Al Fataftah
 

What's hot (20)

Software project management
Software project managementSoftware project management
Software project management
 
Uml in software engineering
Uml in software engineeringUml in software engineering
Uml in software engineering
 
How Do You Build Software? Software Engineering Practices of an Agile Developer
How Do You Build Software? Software Engineering Practices of an Agile DeveloperHow Do You Build Software? Software Engineering Practices of an Agile Developer
How Do You Build Software? Software Engineering Practices of an Agile Developer
 
Control Flow Graphs
Control Flow GraphsControl Flow Graphs
Control Flow Graphs
 
1.2. introduction to automata theory
1.2. introduction to automata theory1.2. introduction to automata theory
1.2. introduction to automata theory
 
Software Quality Management
Software Quality ManagementSoftware Quality Management
Software Quality Management
 
Formal Methods lecture 01
Formal Methods lecture 01Formal Methods lecture 01
Formal Methods lecture 01
 
finding Min and max element from given array using divide & conquer
finding Min and max element from given array using  divide & conquer finding Min and max element from given array using  divide & conquer
finding Min and max element from given array using divide & conquer
 
Pumping lemma Theory Of Automata
Pumping lemma Theory Of AutomataPumping lemma Theory Of Automata
Pumping lemma Theory Of Automata
 
Chapter 01 software engineering pressman
Chapter 01  software engineering pressmanChapter 01  software engineering pressman
Chapter 01 software engineering pressman
 
Software Engineering
Software EngineeringSoftware Engineering
Software Engineering
 
Capability Maturity Model (CMM)
Capability Maturity Model (CMM)Capability Maturity Model (CMM)
Capability Maturity Model (CMM)
 
COCOMO model
COCOMO modelCOCOMO model
COCOMO model
 
Requirement engineering evaluation
Requirement engineering evaluationRequirement engineering evaluation
Requirement engineering evaluation
 
Assemblers
AssemblersAssemblers
Assemblers
 
Evolution of programming language
Evolution of programming languageEvolution of programming language
Evolution of programming language
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
 
1.8. equivalence of finite automaton and regular expressions
1.8. equivalence of finite automaton and regular expressions1.8. equivalence of finite automaton and regular expressions
1.8. equivalence of finite automaton and regular expressions
 
Agile software development
Agile software developmentAgile software development
Agile software development
 
Spiral model presentation
Spiral model presentationSpiral model presentation
Spiral model presentation
 

Viewers also liked (8)

Semantics analysis ppt
Semantics analysis pptSemantics analysis ppt
Semantics analysis ppt
 
Fundamentals of Computing and C Programming - Part 1
Fundamentals of Computing and C Programming - Part 1Fundamentals of Computing and C Programming - Part 1
Fundamentals of Computing and C Programming - Part 1
 
Poset
PosetPoset
Poset
 
Lattices AND Hasse Diagrams
Lattices AND Hasse DiagramsLattices AND Hasse Diagrams
Lattices AND Hasse Diagrams
 
Theory of computation Lec1
Theory of computation Lec1Theory of computation Lec1
Theory of computation Lec1
 
Turing machines
Turing machinesTuring machines
Turing machines
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
 
Lecture: Context-Free Grammars
Lecture: Context-Free GrammarsLecture: Context-Free Grammars
Lecture: Context-Free Grammars
 

Similar to Semantic Analysis of a C Program

Datastructurenotes 100627004340-phpapp01
Datastructurenotes 100627004340-phpapp01Datastructurenotes 100627004340-phpapp01
Datastructurenotes 100627004340-phpapp01
Getachew Ganfur
 
Compiler Design lab manual for Computer Engineering .pdf
Compiler Design lab manual for Computer Engineering .pdfCompiler Design lab manual for Computer Engineering .pdf
Compiler Design lab manual for Computer Engineering .pdf
kalpana Manudhane
 
Datastructure notes
Datastructure notesDatastructure notes
Datastructure notes
Srikanth
 
Frequently asked questions in c
Frequently asked questions in cFrequently asked questions in c
Frequently asked questions in c
David Livingston J
 

Similar to Semantic Analysis of a C Program (20)

complete data structure
complete data structurecomplete data structure
complete data structure
 
Datastructurenotes 100627004340-phpapp01
Datastructurenotes 100627004340-phpapp01Datastructurenotes 100627004340-phpapp01
Datastructurenotes 100627004340-phpapp01
 
Learn C
Learn CLearn C
Learn C
 
Compiler Design lab manual for Computer Engineering .pdf
Compiler Design lab manual for Computer Engineering .pdfCompiler Design lab manual for Computer Engineering .pdf
Compiler Design lab manual for Computer Engineering .pdf
 
C programming
C programmingC programming
C programming
 
Datastructure notes
Datastructure notesDatastructure notes
Datastructure notes
 
Book management system
Book management systemBook management system
Book management system
 
C programming
C programmingC programming
C programming
 
over all view programming to computer
over all view programming to computer over all view programming to computer
over all view programming to computer
 
Introduction to C Programming - I
Introduction to C Programming - I Introduction to C Programming - I
Introduction to C Programming - I
 
C programming course material
C programming course materialC programming course material
C programming course material
 
Presentation on C language By Kirtika thakur
Presentation on C language By Kirtika thakurPresentation on C language By Kirtika thakur
Presentation on C language By Kirtika thakur
 
Oop lec 1
Oop lec 1Oop lec 1
Oop lec 1
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
 
C++
C++C++
C++
 
Function
FunctionFunction
Function
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1
 
U19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).pptU19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).ppt
 
Frequently asked questions in c
Frequently asked questions in cFrequently asked questions in c
Frequently asked questions in c
 
Introduction%20C.pptx
Introduction%20C.pptxIntroduction%20C.pptx
Introduction%20C.pptx
 

Recently uploaded

Online blood donation management system project.pdf
Online blood donation management system project.pdfOnline blood donation management system project.pdf
Online blood donation management system project.pdf
Kamal Acharya
 
Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdf
Kamal Acharya
 
Hall booking system project report .pdf
Hall booking system project report  .pdfHall booking system project report  .pdf
Hall booking system project report .pdf
Kamal Acharya
 
Antenna efficency lecture course chapter 3.pdf
Antenna  efficency lecture course chapter 3.pdfAntenna  efficency lecture course chapter 3.pdf
Antenna efficency lecture course chapter 3.pdf
AbrahamGadissa
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdf
AbrahamGadissa
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
Kamal Acharya
 

Recently uploaded (20)

Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
Online blood donation management system project.pdf
Online blood donation management system project.pdfOnline blood donation management system project.pdf
Online blood donation management system project.pdf
 
Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdf
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
Hall booking system project report .pdf
Hall booking system project report  .pdfHall booking system project report  .pdf
Hall booking system project report .pdf
 
Online resume builder management system project report.pdf
Online resume builder management system project report.pdfOnline resume builder management system project report.pdf
Online resume builder management system project report.pdf
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
shape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptxshape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptx
 
Antenna efficency lecture course chapter 3.pdf
Antenna  efficency lecture course chapter 3.pdfAntenna  efficency lecture course chapter 3.pdf
Antenna efficency lecture course chapter 3.pdf
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdf
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES  INTRODUCTION UNIT-IENERGY STORAGE DEVICES  INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
 
Explosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdfExplosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdf
 
Toll tax management system project report..pdf
Toll tax management system project report..pdfToll tax management system project report..pdf
Toll tax management system project report..pdf
 
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 

Semantic Analysis of a C Program

  • 1. 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
  • 2. Stages in Compiler Design We Work on this stage !
  • 3. 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.
  • 4. Questions Pertaining to Semantic Analysis
  • 5. The Questions We Answer : Semantic Analysis Function always returns some value Unreachable Statements Function Return Type is compatible with Header Expression Compatibility with Datatypes
  • 6. 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.
  • 7. Design of the System LEX YACC PYTHON Program Input C Program Semantic Errors
  • 8. 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. }
  • 9. 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;
  • 10. Example int func1(int x) { if(x==1) { return x; } else { if(x==2) { return x; } } }
  • 11.
  • 12. 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("%ds%d",x,y); }
  • 13. Need For Semantic Analysis 1. Code becomes more meaningful 2. Code has more potential for Optimisation 3. Code becomes flexible and ERROR free.