• Streams & Subjects

    Streams & Subjects

    Get detailed description of each and every topic with proper examples including text, images and videos.

  • Latest Technologies

    Latest Technologies

    Know more about the latest and new emerging technologies and boost your knowledge.

  • Programming

    Programming

    Don't learn just codes and syntax, instead learn the best and efficient way of coding.

  • Technologies

    New Technologies

    Get instant and relevant updates of the latest emerging technologies of lots of streams including web, clouds, virtualization, etc.

  • Freewares

    Freewares

    Download free trials and freewares of various fields and check out their efficiency before buying.

  • Multimedia

    Multimedia

    Learn techniques to design amazing and creative multimedia designs and characters including 2D & 3D layout with rendering.

Showing posts with label Compiler Design Programs. Show all posts
Showing posts with label Compiler Design Programs. Show all posts

Lexical Analizer

/* TO IMPLEMENT LEXICAL ANALIZER IN C */
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>

void main()
{
            int i,j,lc;
            char *a[9][4]={
                                    "PRG","START" ," " ," "," " ,"USING" ,"*" ,"15"," ","L","1","FIVE",
                                    " ","A","1","FOUR"," ","ST","1","TEMP","FOUR ","DC","F","4",
                                    "FIVE","DC","F","5","TEMP","DS","1","F"," " ,"END"," "," ",
                        };
            clrscr();
            printf("\n \t\t LEXICAL ANALIZER \n");

            for(i=0;i< 9;i++)
            {
            for(j=0;j< 4;j++)
                        {
                        if(isalpha(*a[i][j]))
                                                printf("\n STRING : %s",a[i][j]);
                                    if(isdigit(*a[i][j]))
                                                printf("\n DIGIT : %s",a[i][j]);
                        }
                        printf("\n");
            }
            getch();
}


Output:
 
birwa.org


Read more

Two Pass Assembler - 2

#include<stdio.h>
#include<string.h>
#include<conio.h>

void main()
{
char *code[9][4]={
{"PRG1","START","",""},
{"","USING","*","15"},
{"","L","",""},
{"","A","",""},
{"","ST","",""},
{"FOUR","DC","F",""},
{"FIVE","DC","F",""},
{"TEMP","DS","1F",""},
{"","END","",""}
};
char av[2],avail[15]={'N','N','N','N','N','N','N','N','N','N','N','N','N','N','N'};
int i,j,k,count[3],lc[9]={0,0,0,0,0,0,0,0,0},loc=0;
clrscr();
printf("----------------------------------------------------\n");
printf("LABEL\t\tOPCODE\n");
printf("----------------------------------------------------\n\n");
for(i=0;i<=8;i++)
{
for(j=0;j<=3;j++)
{
printf("%s\t\t",code[i][j]);
}
j=0;
printf("\n");
}
getch();
printf("-----------------------------------------------------");
printf("\nVALUES FOR LC : \n\n");
for(j=0;j<=8;j++)
{
if((strcmp(code[j][1],"START")!=0)&&(strcmp(code[j][1],"USING")!=0)&&(strcmp(code[j][1],"L")!=0))
lc[j]=lc[j-1]+4;
printf("%d\t",lc[j]);
}
printf("\n\nSYMBOL TABLE:\n----------------------------------------------------\n");
printf("SYMBOL\t\tVALUE\t\tLENGTH\t\tR/A");
printf("\n----------------------------------------------------\n");
           
for(i=0;i< 9;i++)
{
if(strcmp(code[i][1],"START")==0)
{
printf("%s\t\t%d\t\t%d\t\t%c\n",code[i][0],loc,4,'R');
}
else if(strcmp(code[i][0],"")!=0)
{
printf("%s\t\t%d\t\t%d\t\t%c\n",code[i][0],loc,4,'R');
loc=4+loc;
}
else if(strcmp(code[i][1],"USING")==0) {}
else
{
loc=4+loc;
}
}
printf("----------------------------------------------------");
printf("\n\nBASE TABLE:\n-------------------------------------------------------\n");
printf("REG NO\t\tAVAILIBILITY\tCONTENTS OF BASE TABLE");
printf("\n-------------------------------------------------------\n");
for(j=0;j<=8;j++)
{
if(strcmp(code[j][1],"USING")!=0)
{ }
else
{
strcpy(av,code[j][3]);
}
}
count[0]=(int)av[0]-48;
count[1]=(int)av[1]-48;
count[2]=count[0]*10+count[1];
avail[count[2]-1]='Y';
for(k=0;k< 16;k++)
{
printf(" %d\t\t %c\n",k,avail[k-1]);
}
printf("-------------------------------------------------------\n");
printf("Continue..??");
getchar();
printf("PASS2 TABLE:\n\n");
printf("LABEL\t\tOP1\t\tLC\t\t");
printf("\n----------------------------------------------------\n");
loc=0;
for(i=0;i<=8;i++)
{
for(j=0;j<=3;j++)
{
printf("%s\t\t",code[i][j]);
}
j=0;
printf("\n");
}
printf("-----------------------------------------------------");
getch();
}


Output:
 


Read more

Two Pass Assembler


#include<stdio.h>
#include<conio.h>
#include<string.h>
#define lines_in_program 9
void print(char *p,int loc,int len,char ra)
{
            printf("%s\t%d\t%d\t%c\n",p,loc,len,ra);
}

void main()
{
            char *p[9][4] = {
                                                {"PRG1","START","",""},{"","USING","*","15"},
                                                {"","L","1","FIVE"}, {"","A","1","FOUR"},
                                                {"","ST","1","TEMP"}, {"FOUR","DC","F'4'",""},
                                                {"FIVE","DC","F'5'",""}, {"TEMP","DS","1F",""},
                                                {"","END","",""}
                                     };
            int i,j=0,location_counter=0;
            clrscr();
            for (i=0;i< 9;i++)
            {
                        for(j=0;j< 4;j++)
                        printf("%s\t",p[i][j]);
                        printf("\n");
            }
            printf("\n\n\n\n Symbol ");
            printf("Table:\nSYMBOL\tVALUE\tLENGTH\tRelocatable/Absolute\n");
            printf("---------------------------------------------\n");
           
            for(i=0;i< 9;i++)
            {
                        if(strcmp(p[i][1],"START")==0)
                        print(p[i][0],location_counter,1,'R');
                        else if(strcmp(p[i][0],"")!=0)
                        {
                                    print(p[i][0],location_counter,4,'R');
                                    location_counter=4+location_counter;
                        }
                        else if(strcmp(p[i][1],"USING")==0)
                        { }
                        else{
                                    location_counter=4+location_counter;
                        }
            }
            getch();
}


Output:



Read more