SlideShare a Scribd company logo
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;
}
Compiler and symbol table

More Related Content

PPTX
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
PPTX
What is symbol table?
PPT
Symbol table management and error handling in compiler design
PPTX
Symbol table design (Compiler Construction)
PDF
Symbol table in compiler Design
PPTX
Symbol Table
PPTX
compiler ppt on symbol table
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
What is symbol table?
Symbol table management and error handling in compiler design
Symbol table design (Compiler Construction)
Symbol table in compiler Design
Symbol Table
compiler ppt on symbol table

What's hot (20)

PDF
Assignment2
DOCX
Yacc topic beyond syllabus
PDF
Assignment5
PDF
Handout#09
PDF
C intro
PDF
Unit iii
PPTX
6 compiler lab - Flex
PPTX
Lexical analysis-using-lex
PDF
Assignment4
PDF
PPTX
Lex & yacc
PDF
Handout#08
PPT
Symbol Table, Error Handler & Code Generation
PPTX
datatypes and variables in c language
PDF
Assignment7
PPTX
Predictive parser
PPT
PDF
C programming | Class 8 | III Term
PDF
Assignment10
PPS
Clanguage
Assignment2
Yacc topic beyond syllabus
Assignment5
Handout#09
C intro
Unit iii
6 compiler lab - Flex
Lexical analysis-using-lex
Assignment4
Lex & yacc
Handout#08
Symbol Table, Error Handler & Code Generation
datatypes and variables in c language
Assignment7
Predictive parser
C programming | Class 8 | III Term
Assignment10
Clanguage
Ad

Similar to Compiler and symbol table (20)

PPTX
Symbol Table.pptx
PPT
Linq
PPT
Chapter One
PPT
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
PPTX
First pass of assembler
PPTX
1. Introduction to C# Programming Langua
PPT
what is compiler and five phases of compiler
PDF
Lecture 2.1 - Phase of a Commmmpiler.pdf
PPT
Linq in C# 3.0: An Overview
PDF
Vizwik Coding Manual
ODP
Code Analysis and Refactoring with CDT
PPT
fdjkhdjkfhdjkjdkfhkjshfjkhdkjfhdjkhf2124C_2.ppt
PDF
C++ Pointers , Basic to advanced Concept
PPTX
outline : basicc elements of assembly language
PPT
Lecture 01 2017
DOC
Chapter 1 1
PPTX
How mysql choose the execution plan
PPT
Lexical analyzer
Symbol Table.pptx
Linq
Chapter One
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
First pass of assembler
1. Introduction to C# Programming Langua
what is compiler and five phases of compiler
Lecture 2.1 - Phase of a Commmmpiler.pdf
Linq in C# 3.0: An Overview
Vizwik Coding Manual
Code Analysis and Refactoring with CDT
fdjkhdjkfhdjkjdkfhkjshfjkhdkjfhdjkhf2124C_2.ppt
C++ Pointers , Basic to advanced Concept
outline : basicc elements of assembly language
Lecture 01 2017
Chapter 1 1
How mysql choose the execution plan
Lexical analyzer
Ad

More from Sunjid Hasan (10)

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

Recently uploaded (20)

PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Pre independence Education in Inndia.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
master seminar digital applications in india
PDF
RMMM.pdf make it easy to upload and study
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
O7-L3 Supply Chain Operations - ICLT Program
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Pre independence Education in Inndia.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Supply Chain Operations Speaking Notes -ICLT Program
master seminar digital applications in india
RMMM.pdf make it easy to upload and study
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Final Presentation General Medicine 03-08-2024.pptx
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
TR - Agricultural Crops Production NC III.pdf
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPH.pptx obstetrics and gynecology in nursing
Week 4 Term 3 Study Techniques revisited.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
human mycosis Human fungal infections are called human mycosis..pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf

Compiler and symbol table

  • 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)