SlideShare a Scribd company logo
1 of 16
Introduction of Symbol Table
What kind of work is done in Compiler LAB
Sessional?
 Lexical analysis
 Syntax analysis
 Syntax-directed translation
 Semantic analysis
 type-checking
 Run-time environments
 Intermediate code generation
 Code generation
 Code optimization.
THE STRUCTURE OF A COMPILER
Converts source program semantically into equivalent target
program.
Function of a compiler
 Record the variable names used in the source program.
 Collect information about various attributes.
Provide information on storage allocation for
 Name
 Type
 Scope
 Procedure names
 Argument types
 Argument passing method
 Type return type
Compiler structure
What is symbol table?
Data structures used by compilers for holding
information about source-program.
a int LB1 UB1 SYMBOL TABLE pointer steers the symbol table to
remotely stored information for array
Symbol tables typically need to support multiple
declarations of the same identifier within a program
Use of Symbol Table
 Generating intermediate or target code.
 For verifying used identifiers have been
defined.
 For verifying expressions & assignments are
semantically correct – type checking.
 Information is used by the analysis and
synthesis phases.
Hashing
A symbol table can be implemented
in one of the following ways:
 Linear (sorted or unsorted) list
 Binary Search Tree
 Hash table
Why use Hashing in Symbol Table?
Common data structure.
 Must be organized for quicker search.
keyword or identifier is 'hashed' to produce an array
subscript.
#include<iostream>
#include<stdlib.h>
using namespace std;
class Symbol_Info
{
public:
string Symbol_Type;
string Symbol_Name;
};
struct node
{
Symbol_Info ob;
struct node *Next;
};
typedef struct node Node;
class Link_list
{
int s ;
Node *Head[];
public:
Link_list(int n)
{
s=n;
for(int i=0; i<n; i++)
{
Head[i]=NULL;
}
}
void insert_Node()
{
int index;
Node *temp=new Node;
temp->Next=NULL;
cout<<"Token Name: ";
cin>>temp->ob.Symbol_Name;
cout<<"Token Type: ";
cin>>temp->ob.Symbol_Type;
index=Type_Match(temp-
>ob.Symbol_Type);
if(index==-1)
{
cout<<"nInvalid tokenn";
return;
}
if( Head[index] == NULL)
{
Head[index]=temp;
else
{
temp->Next=Head[index];
Head[index]=temp;
}
}
void Print_List()
{
for(int i=0; i<s; i++)
{
Node *temp=Head[i];
while(temp)
{
cout<<"<"<<temp-
>ob.Symbol_Name<<","<<temp->ob.Symbol_Type<<">-
--";
temp=temp->Next;
}
cout<<"n";
}
}
void Delete_List()
{
int index;
string N,T;
int flag=0;
cout<<"Token Name: ";
cin>>N;
cout<<"Token Type: ";
cin>>T;
index=Type_Match(T);
Node **H=&(Head[index]);
if(index==-1)
{
cout<<"Invalid Token";
return;
}
if(Head[index]==NULL)
{
cout<<"nList is Emptyn";
return;
}
if((*H)->ob.Symbol_Name==N && (*H)-
>ob.Symbol_Type==T)
{
*H=(*H)->Next;
}
else
{
Node *temp1=*H;
Node *temp2=(*H)->Next;
while(temp2)
{
if(temp2-
>ob.Symbol_Name==N&&temp2-
>ob.Symbol_Type==T)
{
flag=1;
break;
}
temp1=temp1->Next;
temp2=temp2->Next;
}
if(flag==1)
{
temp2=temp2->Next;
temp1->Next=temp2;
return;
}
cout<<"nToken not in listn";
}
}
int Type_Match(string type)
{
if(type=="INT"||type=="STRING"||type=="FLOAT"||type=="CHAR"||type=="STRUCT"||type=
="LONG")
return 0;
if(type=="NUM")
return 1;
if(type=="OP"||type=="RELOP"||type=="ARITh"||type=="SMCLN"||type=="COMMA")
return 2;
if(type=="COND"||type=="LOOP")
return 3;
if(type=="FUNC")
return 4;
return -1;
}
};
int main()
{
int n;
cout<<"Measurement : ";
cin>>n;
Link_list List(n);
int ch;
while(1)
{
cout<<endl;
cout<<"1 : Input"<<endl<<"2 : Display"<<endl<<"3 : Erase"<<endl<<"4 :
Stop"<<endl;
cout<<"Enter Your Choice : ";
cin>>ch;
if(ch==1)
{
cout<<endl;
List.insert_Node();
cout<<endl;
}
if(ch==2)
{
cout<<endl;
List.Print_List();
cout<<endl;
}
if(ch==3)
{
cout<<endl;
List.Delete_List();
cout<<endl;
}
if(ch==4)
{
exit(0);
}
}
return 0;
}
Introduction of Symbol Table in Compiler LAB Sessional

More Related Content

What's hot (20)

Assignment2
Assignment2Assignment2
Assignment2
 
Yacc topic beyond syllabus
Yacc   topic beyond syllabusYacc   topic beyond syllabus
Yacc topic beyond syllabus
 
Assignment5
Assignment5Assignment5
Assignment5
 
Handout#09
Handout#09Handout#09
Handout#09
 
C intro
C introC intro
C intro
 
Unit iii
Unit iiiUnit iii
Unit iii
 
6 compiler lab - Flex
6 compiler lab - Flex6 compiler lab - Flex
6 compiler lab - Flex
 
Lexical analysis-using-lex
Lexical analysis-using-lexLexical analysis-using-lex
Lexical analysis-using-lex
 
Assignment4
Assignment4Assignment4
Assignment4
 
Unit v
Unit vUnit v
Unit v
 
Lex & yacc
Lex & yaccLex & yacc
Lex & yacc
 
Handout#08
Handout#08Handout#08
Handout#08
 
Symbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code GenerationSymbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code Generation
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c language
 
Assignment7
Assignment7Assignment7
Assignment7
 
Predictive parser
Predictive parserPredictive parser
Predictive parser
 
C presentation
C presentationC presentation
C presentation
 
C programming | Class 8 | III Term
C programming  | Class 8  | III TermC programming  | Class 8  | III Term
C programming | Class 8 | III Term
 
Assignment10
Assignment10Assignment10
Assignment10
 
Clanguage
ClanguageClanguage
Clanguage
 

Similar to Introduction of Symbol Table in Compiler LAB Sessional

Chapter One
Chapter OneChapter One
Chapter Onebolovv
 
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...Bhavin Darji
 
what is compiler and five phases of compiler
what is compiler and five phases of compilerwhat is compiler and five phases of compiler
what is compiler and five phases of compileradilmehmood93
 
Linq in C# 3.0: An Overview
Linq in C# 3.0: An OverviewLinq in C# 3.0: An Overview
Linq in C# 3.0: An Overviewpradeepkothiyal
 
Vizwik Coding Manual
Vizwik Coding ManualVizwik Coding Manual
Vizwik Coding ManualVizwik
 
Code Analysis and Refactoring with CDT
Code Analysis and Refactoring with CDTCode Analysis and Refactoring with CDT
Code Analysis and Refactoring with CDTdschaefer
 
Chapter 1 1
Chapter 1 1Chapter 1 1
Chapter 1 1bolovv
 
How mysql choose the execution plan
How mysql choose the execution planHow mysql choose the execution plan
How mysql choose the execution plan辛鹤 李
 
1 - Introduction to Compilers.ppt
1 - Introduction to Compilers.ppt1 - Introduction to Compilers.ppt
1 - Introduction to Compilers.pptRakesh Kumar
 
Python Programming Basics for begginners
Python Programming Basics for begginnersPython Programming Basics for begginners
Python Programming Basics for begginnersAbishek Purushothaman
 
Complete Notes on Angular 2 and TypeScript
Complete Notes on Angular 2 and TypeScriptComplete Notes on Angular 2 and TypeScript
Complete Notes on Angular 2 and TypeScriptEPAM Systems
 

Similar to Introduction of Symbol Table in Compiler LAB Sessional (20)

Symbol Table.pptx
Symbol Table.pptxSymbol Table.pptx
Symbol Table.pptx
 
Linq
LinqLinq
Linq
 
Chapter One
Chapter OneChapter One
Chapter One
 
C#
C#C#
C#
 
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
 
First pass of assembler
First pass of assemblerFirst pass of assembler
First pass of assembler
 
what is compiler and five phases of compiler
what is compiler and five phases of compilerwhat is compiler and five phases of compiler
what is compiler and five phases of compiler
 
Linq in C# 3.0: An Overview
Linq in C# 3.0: An OverviewLinq in C# 3.0: An Overview
Linq in C# 3.0: An Overview
 
Vizwik Coding Manual
Vizwik Coding ManualVizwik Coding Manual
Vizwik Coding Manual
 
Code Analysis and Refactoring with CDT
Code Analysis and Refactoring with CDTCode Analysis and Refactoring with CDT
Code Analysis and Refactoring with CDT
 
C#ppt
C#pptC#ppt
C#ppt
 
Lecture 01 2017
Lecture 01 2017Lecture 01 2017
Lecture 01 2017
 
Chapter 1 1
Chapter 1 1Chapter 1 1
Chapter 1 1
 
How mysql choose the execution plan
How mysql choose the execution planHow mysql choose the execution plan
How mysql choose the execution plan
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
1 - Introduction to Compilers.ppt
1 - Introduction to Compilers.ppt1 - Introduction to Compilers.ppt
1 - Introduction to Compilers.ppt
 
Python Programming Basics for begginners
Python Programming Basics for begginnersPython Programming Basics for begginners
Python Programming Basics for begginners
 
Complete Notes on Angular 2 and TypeScript
Complete Notes on Angular 2 and TypeScriptComplete Notes on Angular 2 and TypeScript
Complete Notes on Angular 2 and TypeScript
 
Language processors
Language processorsLanguage processors
Language processors
 
C# Unit 1 notes
C# Unit 1 notesC# Unit 1 notes
C# Unit 1 notes
 

More from Sunjid Hasan

Software engineering( sms )
Software engineering( sms )Software engineering( sms )
Software engineering( sms )Sunjid Hasan
 
System analysis design of Fire Service & Civil Defence
System analysis design of Fire Service & Civil DefenceSystem analysis design of Fire Service & Civil Defence
System analysis design of Fire Service & Civil DefenceSunjid Hasan
 
Digital image processing recognition of bengali handwritten digits using co...
Digital image processing   recognition of bengali handwritten digits using co...Digital image processing   recognition of bengali handwritten digits using co...
Digital image processing recognition of bengali handwritten digits using co...Sunjid Hasan
 
Artificial intelligence - python
Artificial intelligence - pythonArtificial intelligence - python
Artificial intelligence - pythonSunjid Hasan
 
Artificial intelligence - Prolog
Artificial intelligence - Prolog Artificial intelligence - Prolog
Artificial intelligence - Prolog Sunjid Hasan
 
System analysis design of fire service and civil defence
System analysis design of fire service and civil defenceSystem analysis design of fire service and civil defence
System analysis design of fire service and civil defenceSunjid Hasan
 
Online blood sharing application
Online blood  sharing applicationOnline blood  sharing application
Online blood sharing applicationSunjid Hasan
 
Messaging application
Messaging applicationMessaging application
Messaging applicationSunjid Hasan
 
Estimation for software
Estimation for softwareEstimation for software
Estimation for softwareSunjid Hasan
 

More from Sunjid Hasan (10)

Software engineering( sms )
Software engineering( sms )Software engineering( sms )
Software engineering( sms )
 
System analysis design of Fire Service & Civil Defence
System analysis design of Fire Service & Civil DefenceSystem analysis design of Fire Service & Civil Defence
System analysis design of Fire Service & Civil Defence
 
Digital image processing recognition of bengali handwritten digits using co...
Digital image processing   recognition of bengali handwritten digits using co...Digital image processing   recognition of bengali handwritten digits using co...
Digital image processing recognition of bengali handwritten digits using co...
 
Artificial intelligence - python
Artificial intelligence - pythonArtificial intelligence - python
Artificial intelligence - python
 
Artificial intelligence - Prolog
Artificial intelligence - Prolog Artificial intelligence - Prolog
Artificial intelligence - Prolog
 
Cover page sample
Cover page sampleCover page sample
Cover page sample
 
System analysis design of fire service and civil defence
System analysis design of fire service and civil defenceSystem analysis design of fire service and civil defence
System analysis design of fire service and civil defence
 
Online blood sharing application
Online blood  sharing applicationOnline blood  sharing application
Online blood sharing application
 
Messaging application
Messaging applicationMessaging application
Messaging application
 
Estimation for software
Estimation for softwareEstimation for software
Estimation for software
 

Recently uploaded

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 

Recently uploaded (20)

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 

Introduction of Symbol Table in Compiler LAB Sessional

  • 2. What kind of work is done in Compiler LAB Sessional?  Lexical analysis  Syntax analysis  Syntax-directed translation  Semantic analysis  type-checking  Run-time environments  Intermediate code generation  Code generation  Code optimization.
  • 3. THE STRUCTURE OF A COMPILER Converts source program semantically into equivalent target program.
  • 4. Function of a compiler  Record the variable names used in the source program.  Collect information about various attributes. Provide information on storage allocation for  Name  Type  Scope  Procedure names  Argument types  Argument passing method  Type return type
  • 6. What is symbol table? Data structures used by compilers for holding information about source-program. a int LB1 UB1 SYMBOL TABLE pointer steers the symbol table to remotely stored information for array
  • 7. Symbol tables typically need to support multiple declarations of the same identifier within a program
  • 8. Use of Symbol Table  Generating intermediate or target code.  For verifying used identifiers have been defined.  For verifying expressions & assignments are semantically correct – type checking.  Information is used by the analysis and synthesis phases.
  • 9. Hashing A symbol table can be implemented in one of the following ways:  Linear (sorted or unsorted) list  Binary Search Tree  Hash table
  • 10. Why use Hashing in Symbol Table? Common data structure.  Must be organized for quicker search. keyword or identifier is 'hashed' to produce an array subscript.
  • 11. #include<iostream> #include<stdlib.h> using namespace std; class Symbol_Info { public: string Symbol_Type; string Symbol_Name; }; struct node { Symbol_Info ob; struct node *Next; }; typedef struct node Node; class Link_list { int s ; Node *Head[]; public: Link_list(int n) { s=n; for(int i=0; i<n; i++) { Head[i]=NULL; } } void insert_Node() { int index; Node *temp=new Node; temp->Next=NULL; cout<<"Token Name: "; cin>>temp->ob.Symbol_Name; cout<<"Token Type: "; cin>>temp->ob.Symbol_Type; index=Type_Match(temp- >ob.Symbol_Type); if(index==-1) { cout<<"nInvalid tokenn"; return; } if( Head[index] == NULL) { Head[index]=temp;
  • 12. else { temp->Next=Head[index]; Head[index]=temp; } } void Print_List() { for(int i=0; i<s; i++) { Node *temp=Head[i]; while(temp) { cout<<"<"<<temp- >ob.Symbol_Name<<","<<temp->ob.Symbol_Type<<">- --"; temp=temp->Next; } cout<<"n"; } } void Delete_List() { int index; string N,T; int flag=0; cout<<"Token Name: "; cin>>N; cout<<"Token Type: "; cin>>T; index=Type_Match(T); Node **H=&(Head[index]); if(index==-1) { cout<<"Invalid Token"; return; } if(Head[index]==NULL) { cout<<"nList is Emptyn"; return; } if((*H)->ob.Symbol_Name==N && (*H)- >ob.Symbol_Type==T) { *H=(*H)->Next; } else { Node *temp1=*H; Node *temp2=(*H)->Next; while(temp2) { if(temp2- >ob.Symbol_Name==N&&temp2- >ob.Symbol_Type==T)
  • 13. { flag=1; break; } temp1=temp1->Next; temp2=temp2->Next; } if(flag==1) { temp2=temp2->Next; temp1->Next=temp2; return; } cout<<"nToken not in listn"; } } int Type_Match(string type) { if(type=="INT"||type=="STRING"||type=="FLOAT"||type=="CHAR"||type=="STRUCT"||type= ="LONG") return 0; if(type=="NUM") return 1; if(type=="OP"||type=="RELOP"||type=="ARITh"||type=="SMCLN"||type=="COMMA") return 2; if(type=="COND"||type=="LOOP") return 3; if(type=="FUNC")
  • 14. return 4; return -1; } }; int main() { int n; cout<<"Measurement : "; cin>>n; Link_list List(n); int ch; while(1) { cout<<endl; cout<<"1 : Input"<<endl<<"2 : Display"<<endl<<"3 : Erase"<<endl<<"4 : Stop"<<endl; cout<<"Enter Your Choice : "; cin>>ch; if(ch==1) { cout<<endl; List.insert_Node(); cout<<endl; } if(ch==2)