cdlabprograms1!10!150221003628 conversion gate02

Upload: sivakumar-soubraylu

Post on 27-Feb-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    1/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    Course: Bachelor of Technology

    Branch: Computer Science & Engineering

    Semester: 7th

    GURU JAMBHESHWAR UNIVERSITY OF

    SCIENCE & TECHNOLOGY, HISAR

    Submitted To: - Submitted By:-

    Er. Isha Nagpal Mukesh Kumar

    Asstt. Professor in CSE Deptt. 1110751908

    Department of Computer Science & Engineering

    Prannath Parnami Institute of Management & Technology, Hisar

    Prannath Parnami Universe, Website: ppu.edu.in

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    2/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    INDEX

    Sr.

    No.

    Name of Experiment Date Sign.

    1. Write a program to design lexical analyzer.

    2. Write a program to generate three address codesfor assignment, arithmetic and relationalexpressions.

    3. Write a program to check whether a string to thegrammar or not.

    4. Write a program to find the number ofwhitespaces & newline characters.

    5. Write a program to find the leading terminals.

    6. Write a program to find the trailing terminals.

    7. Write a program for computation of first.

    8. Write a program to show the operations of stack.

    9. Write a program to perform the operations onstack by using linked list.

    10. Write a program to perform the operations on file.

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    3/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    Program-01

    Write a program to design lexical analyzer.

    #include

    #include

    #include

    void keyword(char str[10])

    {

    if(strcmp("for",str)==0||strcmp("while",str)==0||

    strcmp("do",str)==0||strcmp("int",str)==0||strcmp

    ("float",str)==0||strcmp("char",str)==0||strcmp

    ("double",str)==0||strcmp("static",str)==0||strcmp("switch",str)==0||strcmp("case",str)==0)

    printf("\n%s is a keyword", str);

    else

    printf("\n%s is an identifier", str);

    }

    void main()

    {

    FILE *f1,*f2,*f3;

    char c,str[10],st1[10];

    int num[100],lineno=0,tokenvalue=0,i=0,j=0,k=0;

    printf("\n enter the c program");

    gets(st1);

    f1=fopen("input.txt","w");

    while((c=getchar())!=EOF)

    putc(c,f1);

    fclose(f1);

    f1=fopen ("input.txt","r");

    f2=fopen ("identifier.txt","w");

    f3=fopen ("specialchar.txt","w");

    while((c=getc(f1))!=EOF)

    {

    if(isdigit(c))

    {

    tokenvalue=c-'0';

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    4/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    c=getc(f1);

    while(isdigit(c))

    {

    tokenvalue*=10+c-'0';

    c=getc(f1);

    }

    num[i++]=tokenvalue;

    ungetc(c,f1);

    }

    else

    if(isalpha(c))

    {

    putc(c,f2);

    c=getc(f1);while(isdigit(c)||isalpha(c)||c=='_'||c=='$')

    {

    putc(c,f2);

    c=getc(f1);

    }

    putc(' ',f2);

    ungetc(c,f1);//to get unsigned char to the spcified stream

    }

    else

    if(c==' '||c=='\t')

    printf("");

    else

    if(c=='\n')

    lineno++;

    else

    putc(c,f3);

    }

    fclose(f2);

    fclose(f3);

    fclose(f1);

    printf("\n the no's in the program are");

    for(j=0;j

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    5/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    k=0;

    printf("the keyword and identifiers are:");

    while((c=getc(f2))!=EOF)

    {

    if(c!=' ')

    str[k++]=c;

    else

    {

    str[k]='\0';

    keyword(str);

    k=0;

    }

    }fclose(f2);

    f3=fopen ("\n Specialchar.txt","r");

    printf("\n special characters are");

    while((c=getc(f3))!=EOF)

    printf("%c",c);

    printf("\n");

    fclose(f3);

    printf("Total no. os lines are: %d", lineno);

    getch();

    }

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    6/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    OUTPUT

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    7/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    Program-02

    Write a program to generate three address code for assignment, arithmetic and

    relational expressions.#include

    #include

    int i,ch,j,l,addr=100;

    char ex[10],exp[10],exp1[10],exp2[10],id1[5],op[5],id2[5];

    void main()

    {

    clrscr();

    while(1){

    printf("\n 1. Assignment Expression or Arithmetic Expression \n 2. Relational or Expression \n

    3. Exit \n Enter the choice:");

    scanf("%d", &ch);

    switch(ch)

    {

    case 1:

    printf("\n Enter the expression with Assignment Expression Operator or Arithmetic Operator:");

    scanf("%s",exp);

    l=strlen(exp);exp2[0]='\0';

    i=0;

    while(exp[i]!='=')

    {

    i++;

    }

    strncat(exp2,exp,i);

    strrev(exp);

    exp1[0]='\0';

    strncat(exp1,exp,l-(i+1));

    strrev(exp1);

    printf("three address code:\ntemp=%s\n%s=temp\n",exp1,exp2);

    break;

    case 2:

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    8/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    printf("Enter the expression with relational operator");

    scanf("%s%s%s",&id1,&op,&id2);

    if(((strcmp(op,"")==0)||(strcmp(op,"=")==0)||(strcmp(op,"==")==0)||(strcmp(op,"!=")==0))==0)

    printf("Expression is error");

    else

    {

    printf("\n %d\tif%s%s%s goto %d",addr,id1,op,id2,addr+3);

    addr++;

    printf("\n %d\t T:=0",addr);

    addr++;

    printf("\n %d\t goto %d",addr,addr+2);addr++;

    printf("\n %d\t T:=1",addr);

    }

    break;

    case 3:

    exit(0);

    }

    }

    }

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    9/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    OUTPUT

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    10/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    Program-03

    Write a program to check whether a string to the grammar or not.

    #include

    #include

    #include

    void main()

    {

    char a1[100],b1[100],c1[100],p1[10],s1;

    int v=0,i,j,k,m,n=0,x,a=0,l1,l2,l3,loc,lx,lx1,loc1,s;

    clrscr();

    printf("enter the start symbol");

    scanf("%c", & s1);printf("enter the production");

    scanf("%s",&a1);

    printf("enter the string to be searching");

    scanf("%s",&p1);

    strcpy(b1,a1);

    printf("b1=%s" ,b1);

    l1=strlen(a1);

    l2=strlen(b1);

    l3=strlen(p1);

    lx=l1;lx1=l1;

    printf("a1=%s", a1);

    printf("l1=%d", l1);

    while(((2*(13+2))>11))

    {

    for(i=0;i

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    11/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    }

    else

    {

    l1=l1+l1;

    for(j=loc;j

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    12/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    OUTPUT

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    13/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    Program-04

    Write a program to find the number of whitespaces & newline characters.

    #include

    #include

    #include

    void main ()

    {

    char str[200],ch;

    int a=0, space=0,newline=0;

    clrscr();

    printf("enter a string ( press escape to quit entering)");

    ch=getche();while((ch!=27)&&(a

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    14/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    OUTPUT

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    15/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    Program-05

    Write a program to find the leading terminals.

    #include

    #include

    #include

    int nt,t,top=0;

    char s[50],NT[10],T[10],st[50],l[10][10],tr[50][50];

    int searchnt(char a)

    {

    int count=-1,i;

    for(i=0;i

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    16/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    {

    if(l[a][b]=='f')

    {

    l[a][b]='t';

    push(T[b]);

    push(NT[a]);

    }}

    void main()

    {

    int i,s,k,j,n;

    char pr[30][30],b,c;

    clrscr();

    printf("Enter the no of production :");

    scanf("%d",&n);printf("Enter the productions one by one\n");

    for(i=0;i

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    17/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    for(i=0;i

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    18/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    OUTPUT

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    19/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    Program-06

    Write a program to find the trailing terminals.

    #include

    #include

    #include

    int nt,t,top=0;

    char s[50],NT[10],T[10],st[50],l[10][10],tr[50][50];

    int searchnt(char a)

    {

    int count=-1,i;

    for(i=0;i

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    20/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    if(tr[a][b]=='f'){

    tr[a][b]='t';

    push(T[b]);

    push(NT[a]);

    }}

    void main()

    {

    int i,s,k,j,n;

    char pr[30][30],b,c;

    clrscr();

    printf("Enter the no of productions:");

    scanf("%d",&n);

    printf("Enter the productions one by one\n");

    for(i=0;i

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    21/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    for(j=0;j

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    22/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    OUTPUT

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    23/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    Program-07

    Write a program for computation of first.#include

    #include

    #include

    void main()

    {

    char t[5],nt[10],p[5][5],first[5][5],temp;

    int i,j,not,nont,k=0,f=0;

    clrscr();

    printf("Enter the no. of Non-terminals in the grammer:");scanf("%d",&nont);

    printf("\nEnter the Non-terminals in the grammer:");

    for(i=0;i

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    24/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    j+=1;

    scanf("%c",&p[i][j]);

    }}

    for(i=0;i

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    25/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    OUTPUT

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    26/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    Program-08

    Write a program to show the operations of stack.

    #include

    #include

    #define MAXSIZE 10

    void push();

    int pop();

    void traverse();

    int stack[MAXSIZE];

    int Top=-1;

    void main()

    {int choice;

    char ch;

    do

    {

    clrscr();

    printf("\nENTER 1 FOR PUSH: ");

    printf("\nENTER 2 TO POP THE ELEMENT: ");

    printf("\nENTER 3 TO TRAVERSE THE ELEMENTS: ");

    printf("\n ENTER YOUR CHOICE: ");

    scanf("%d",&choice);switch(choice)

    {

    case 1:push();

    break;

    case 2:printf("\nTHE DELETED ELEMENT IS %d",pop());

    break;

    case 3: traverse();

    break;

    default:printf("\n SORRY!!!!!YOU HAVE ENTERED A WRONG CHOICE");

    }

    printf("\n DO YOU WISH TO CONTINUE???(Y/N): ");

    fflush(stdin);

    scanf("%c",&ch);

    }

    while(ch=='y'|| ch=='Y');

    }

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    27/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    void push()

    {

    int item;

    if(Top==MAXSIZE-1)

    {

    printf("\n SORRY STACK IS FULL");

    getch();

    exit();

    }

    else

    {

    printf("ENTER THE ELEMENT TO BE INSERTED: ");

    scanf("%d",&item);

    Top=Top+1;stack[Top]=item;

    }

    }

    int pop()

    {

    int item;

    if(Top==-1)

    {

    printf("SORRY!! STACK IS EMPTY");

    getch();

    exit();

    }

    else

    {

    item=stack[Top];

    Top=Top-1;

    }

    return(item);

    }

    void traverse()

    {

    int i;

    if(Top==-1)

    {

    printf("OHH!! STACK IS EMPTY");

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    28/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    getch();

    exit();

    }

    else

    {

    for(i=Top;i>=0;i--)

    {

    printf("\nTRAVERSED ELEMENT(S) IS/ARE:");

    printf(" %d",stack[i]);

    }

    }

    }

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    29/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    OUTPUT

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    30/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    Program-09

    Write a program to perform the operations on stack by using linked list.

    #include

    #include

    struct stack

    {

    int no;

    struct stack *next;

    }

    *start=NULL;

    typedef struct stack st;

    void push();int pop();

    void display();

    void main()

    {

    int choice, item;

    char ch;

    do

    {

    clrscr();

    printf("\nENTER 1 FOR PUSH ITEM: ");printf("\nENTER 2 TO POP THE ITEM FROM STACK: ");

    printf("\nENTER 3 TO DISPLAY THE ITEMS OF STACK: ");

    printf("\n ENTER YOUR CHOICE: ");

    scanf("%d",&choice);

    switch(choice)

    {

    case 1:push();

    break;

    case 2: item=pop();

    printf("THE DELETED ITEM IS -->%d", item);

    break;

    case 3: display();

    break;

    default:printf("\n SORRY!!!!!YOU HAVE ENTERED A WRONG CHOICE");

    }

    printf("\n DO YOU WISH TO CONTINUE???(Y/N): ");

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    31/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    fflush(stdin);

    scanf("%c",&ch);

    }

    while(ch=='y'|| ch=='Y');

    }

    void push()

    {

    st *node ;

    node=(st *)malloc(sizeof(st));

    printf("\n ENTER THE NUMBER TO BE INSERTED");

    scanf("%d",&node->no);

    node->next =start;

    start=node;

    }int pop()

    {

    st *temp;

    temp=start;

    if(start==NULL){

    printf("STACK IS ALREADY EMPTY: ");

    getch();

    exit();

    }

    else

    {

    start=start->next;

    free(temp);

    }

    return(temp->no);

    }

    void display(){

    st *temp;

    temp=start;

    while(temp->next!=NULL){

    printf("\n no=%d", temp->no);

    temp=temp->next;

    }

    printf("\n no=%d",temp->no);

    }

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    32/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    OUTPUT

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    33/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    Program-10

    Write a program to perform the operations on file.

    #include

    #include

    int main()

    {

    char ch,source_file[20],target_file[20];

    FILE *source, *target;

    printf("Enter the name of file to copy\n ");

    gets(source_file);

    source=fopen(source_file,"r");

    if(source==NULL){

    printf("Press any key to exit..\n ");

    exit(EXIT_FAILURE);

    }

    printf("Enter the name of target file\n");

    gets(target_file);

    target=fopen(target_file,"w");

    if(target==NULL)

    {

    fclose(source);printf("Press any key to exit..\n");

    exit(EXIT_FAILURE);

    }

    while((ch=fgetc(source))!=EOF)

    fputc(ch,target);

    printf("File copied successfully\n");

    fclose(source);

    fclose(target);

    return 0;

    }

  • 7/25/2019 Cdlabprograms1!10!150221003628 Conversion Gate02

    34/34

    Mukesh Kumar

    1110751908

    CSE 7th Sem

    OUTPUT