Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » ParsingRSS Feeds

Develop a Toy Compiler, which takes a series of statements as input, and creates a symbol table from it

Posted By: Henry Evans     Category: C++ Programming     Views: 2910

Develop a Toy Compiler, which takes a series of statements as input, and creates a symbol table from it.

Code for Develop a Toy Compiler, which takes a series of statements as input, and creates a symbol table from it in C++ Programming

#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<conio.h>

struct symbol_table
{
    char sym[10];
    char type[10];
    symbol_table *next;
};

int line=0;

class ToyCmp
{
    private:
        symbol_table *head;
        char stack[10][10];
        int top;
    public:
        ToyCmp()
        {
            head=NULL;
            top=0;
        }
        void creat(char [][10],char *,int );
        void display();
        void checkexp(char *,char *);
        void push(char *);
        void validateexp();
        int checktype(char *,char *);
};

void ToyCmp :: creat(char id[10][10],char *kw,int n)
{
    int i;
    symbol_table *temp,*temp1;

    for(i=0;i<n;i++)
    {
        temp=new symbol_table;
        strcpy(temp->sym,id[i]);
        strcpy(temp->type,kw);
        temp->next=NULL;
        if(head==NULL)
            head=temp;
        else
        {
            temp1=head;
            while(temp1->next!=NULL)
                temp1=temp1->next;
            temp1->next=temp;
        }
    }
}

void ToyCmp :: display()
{
    symbol_table *temp;

    temp=head;
    cout<<"\nId\tKeyword\n";
    while(temp!=NULL)
    {
        cout<<temp->sym<<"\t"<<temp->type<<endl;
        temp=temp->next;
    }
}

void ToyCmp :: checkexp(char *exp,char *ansid)
{
    int i,n,k,j,x;
    char id[10][10],data[10],anstype[10],idtype[10],op;
    symbol_table *temp,*temp1;
    fstream f4;

    f4.open(" operator.txt",ios::in);
    top=0;
    n=strlen(exp);
    k=0;
    j=0;
    for(i=0;i<n;i++)
    {
        f4.clear();
        f4.seekg(0);
        int chk=0;
        while(!f4.eof())
        {
            f4>>op;
            if(exp[i]==op)
            {
                data[k]='\0';
                strcpy(id[j],data);
                k=0;
                j++;
                push(id[j-1]);
                data[0]=exp[i];
                data[1]='\0';
                push(data);
                chk=1;
                break;
            }
            else
                data[k]=exp[i];
        }
        if(chk==0)
            k++;
    }
    data[k]='\0';
    strcpy(id[j],data);
    k=0;
    j++;
    push(id[j-1]);
    f4.close();
    validateexp();
    temp=head;
    while(strcmp(temp->sym,ansid)!=0 && temp!=NULL)
        temp=temp->next;
    if(temp==NULL)
    {
        cout<<"\nError on Line : "<<line<<" : Identifier Not found";
        exit(1);
    }
    strcpy(anstype,temp->type);
    int chk=0;
    for(i=0;i<top;i++)
    {
        if(chk==1)
            chk=0;
        else
        {
            temp=head;
            while(strcmp(temp->sym,stack[i])!=0 && temp!=NULL)
                temp=temp->next;
            if(temp==NULL)
            {
                 cout<<"\nError on Line : "<<line<<" : Identifier Not found";
                exit(1);
            }
            strcpy(idtype,temp->type);
            x=checktype(idtype,anstype);
            if(x==-1)
            {
                temp=new symbol_table;
                strcpy(id[0],stack[i]);
                x=strlen(stack[i]);
                id[0][x]='*';
                id[0][x+1]='\0';
                strcpy(temp->sym,id[0]);
                strcpy(temp->type,anstype);
                temp->next=NULL;
cout<<endl<<stack[i]<<" is converted to "<<id[0]<<" form "<<idtype<<" to "<<anstype;
                temp1=head;
                while(temp1->next!=NULL)
                    temp1=temp1->next;
                temp1->next=temp;
            }
            elseif(x==1)
            {
                cout<<"\nError on line : "<<line<<" : Invalid type conversion";
                exit(1);
            }
            chk=1;
        }
    }
    f4.clear();
    f4.seekg(0);
    chk=0;
    for(i=0;i<top-1;i++)
    {
        if(chk==0)
            chk=1;
        else
        {
            temp=new symbol_table;
            strcpy(temp->sym,"temp");
            strcpy(temp->type,anstype);
            temp->next=NULL;
            temp1=head;
            while(temp1->next!=NULL)
                temp1=temp1->next;
            temp1->next=temp;
cout<<"\n"<<stack[i-1]<<" "<<stack[i]<<" "<<stack[i+1]<<" is stored in temp variable";
            cout<<"\nTemp is given to "<<ansid<<endl;
        }
    }
}

void ToyCmp :: push(char *data)
{
    top=top+1;
    strcpy(stack[top-1],data);
}

void ToyCmp :: validateexp()
{
    int chk=0,i,chkop;
    char op[2];
    fstream f4;

    op[1]='\0';
    f4.open(" operator.txt",ios::in);
    for(i=0;i<top;i++)
    {
        if(chk==0)
        {
            chkop=0;
            f4.clear();
            f4.seekg(0);
            while(!f4.eof())
            {
                f4>>op[0];
                if(strcmp(stack[i],op)==0)
                {
                    chkop=1;
                    break;
                }
            }
            if(chkop==1)
            {
                cout<<"Erro on Line : "<<line<<" : Invalid Expression";
                exit(1);
            }
            chk=1;
        }
        elseif(chk==1)
        {
            chkop=0;
            f4.clear();
            f4.seekg(0);
            while(!f4.eof())
            {
                f4>>op[0];
                if(strcmp(stack[i],op)==0)
                {
                    chkop=1;
                    break;
                }
            }
            if(chkop==0)
            {
                cout<<"Erro on Line : "<<line<<" : Invalid Expression";
                exit(1);
            }
            chk=0;
        }
    }
    f4.close();
}

 
int ToyCmp :: checktype(char *idtype,char *anstype)
{
    fstream f2;
    int chk=0;
    char kw[10];
    if(strcmp(idtype,anstype)==0)
        return(0);
    f2.open(" kw.txt",ios::in);
    while(!f2.eof())
    {
        f2>>kw;
        if(strcmp(idtype,kw)==0)
            chk=1;
        if(strcmp(anstype,kw)==0 && chk==1)
        {
            f2.close();
            return(-1);
        }
        if(strcmp(anstype,kw)==0 && chk==0)
        {
            f2.close();
            return(1);
        }
    }
}

void main()
{
    fstream f1,f2,f3;
    ToyCmp s1;
    char c,id[5][10],kw[10],d,data[10],tmpchar='\0';
    char l1[80],exp[10],tempid[10];
    int i=-1,chkid=0,n,j=0,expchk=0;
    clrscr();

    f1.open(" toyc.txt",ios::in);
    f2.open(" kw.txt",ios::in);
    f3.open(" delimit.txt",ios::in);

    while(!f1.eof())
    {
        f1>>l1;
        line++;
        n=strlen(l1);
        if(l1[n-1]!=';')
        {
            cout<<"Error on Line : "<<line<<" : Statement missing ;";
            exit(1);
        }
    }

 
    line=0;
    f1.clear();
    f1.seekg(0);
    while(!f1.eof())
    {
        f1>>c;
        i++;
        f3.clear();
        f3.seekg(0);
        while(!f3.eof())
        {
            f3>>d;
            if(c==d)
            {
                data[i]='\0';
                i=-1;
                chkid=1;
                break;
            }
            else
                data[i]=c;
            if(isdigit(data[0]))
            {
                cout<<"\nError on line "<<line<<" : Invalid Id name.";
                exit(1);
            }
            d='\0';
        }
        if(chkid==1 && (d==',' || d==':'))
        {
            strcpy(id[j],data);
            j++;
            if(d==':')
            {
                tmpchar=':';
                n=j;
                j=0;
            }
        }
        if(chkid==1 && (tmpchar==':' && d==';'))
        {
            line++;
            f2.clear();
            f2.seekg(0);
            int chkkw=0;
            while(!f2.eof())
            {    f2>>kw;
                if(strcmp(kw,data)==0)
                {    s1.creat(id,kw,n);
                    chkkw=1;
                }
            }
            if(chkkw==0)
            {    strcpy(exp,data);
                expchk=1;
            }}
        chkid=0;
        if(expchk==1)
            break;
    }
    int x=n;
    i=-1;
    n=strlen(exp);
    for(j=0;j<x;j++)
        n=n+strlen(id[j]);
    n=n+1;
    f1.seekg(-n,ios::cur);
    i=-1;
    j=0;
    while(!f1.eof())
    {    f1>>c;
        f3.clear();
        f3.seekg(0);
        chkid=0;
        i++;
        while(!f3.eof())
        {    f3>>d;
            if(c==d)
            {    chkid=1;
                break;
            }
            else
                data[i]=c;
        }
        if(f1.eof())
            break;
        if(chkid==1 && d=='=')
        {    data[i]='\0';
            strcpy(id[j],data);
            i=-1;
            j++;
            strcpy(tempid,data);
        }
        elseif(chkid==1 && d==';')
        {    data[i]='\0';
            strcpy(exp,data);
            i=-1;
            j=0;
            s1.checkexp(exp,tempid);
        }
        line++;
    }    s1.display();
    getch();
}
Input Files:


Keyword file.

integer
real


Compiler file.

i:integer;
a,b:real;
a=b*i;


/*******************************************
Output:
********************************************


i is converted to i* form integer to real
b * i is stored in temp variable
Temp is given to a

Id Keyword
i integer
a real
b real
i* real
temp real

*********************************************/
  
Share: 



Henry Evans
Henry Evans author of Develop a Toy Compiler, which takes a series of statements as input, and creates a symbol table from it is from London, United Kingdom.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!