SP Lab File

Embed Size (px)

Citation preview

  • 7/28/2019 SP Lab File

    1/33

    AMITY SCHOOL OF ENGINEERING & TECHNOLOGY

    SYSTEM PROGRAMMING(Practical File)

    Submitted By:

    Name:SSAANN CCHH IITT MMAALLHHOOTTRR AA

    Roll No: 3081

    Branch: B.Tech - CSE (2010 2014)

    Section: 6CSE2

  • 7/28/2019 SP Lab File

    2/33

    INDEX

    S. NO Practical DATE SIGNATURE

    1. Introduction to System Programming Lab

    2.Write a program to find total number of tokens in an expression.

    3. Write a program to convert infix into prefix notation.

    4.Write a program to convert infix into postfix notation.

    5.Write a program to accept a language of type ab*a.

    6. Write a program to convert NDFA into DFA.

    7.Write a program to convert RE into NDFA.

    8.Write a program to generate symbol table for an expression.

    9.Write a program to evaluate top-down parsing technique.

    10.Write a program to evaluate top-down parsing technique.

  • 7/28/2019 SP Lab File

    3/33

    1. Introduction to System Programming

    System programming is the activity of programming system software to support operation & use of the

    computer itself rather than any particular application. The primary distinguishing characteristic of

    systems programming when compared to application programming is that application programmingaims to produce software which provides services to the user (e.g. word processor), whereas systems

    programming aims to produce software which provides services to the computer hardware (e.g. disk

    defragmenter). It requires a greater degree of hardware awareness.

    System software consists of a variety of programs that support the operation of a computer. These are

    developed to make computer better adapted to the need of the user.

    Components of system programming include the:

    Hardware it provides the basic computing resources like CPU, I/O, and memory devices etc.

    Operating system controls and coordinates the use of hardware and software among the

    various applications running on the platform. Application program it defines the way any system resource is used to solve computing

    problems of the users. Here the main focus is on application not on system.

    System program these are low level programs that comprise of mnemonics or machine level

    coding to define the basic functions that build up the OS and that help run all other higher level

    applications.

  • 7/28/2019 SP Lab File

    4/33

    2. Write a program to find total number of tokens in an expression.

    #include

    #include

    #include

    #define oper(X)(X=='+'||X=='*'||X=='/'||X=='-'||X==' ')

    void main()

    {

    inti,j,l,alpha = 0,opr = 0, con=0;

    char s[20],a,b;

    printf("\n Enter the expression");

    gets(s);

    l= strlen(s);

    for(i=0;i= 97 && s[i]=48 && s[i]=48 && s[j]

  • 7/28/2019 SP Lab File

    5/33

    OUTPUT:

  • 7/28/2019 SP Lab File

    6/33

    3. Write a program to convert infix to prefix notation.

    #include

    #include

    #define operand(x)(x>='a'&&x='A'&&x='0'&&x

  • 7/28/2019 SP Lab File

    7/33

    int y;

    switch(x)

    {

    case ')': y=0;

    break;

    case '+': y=1;

    break;

    case '-': y=1;

    break;

    case '/': y=2;

    break;

    case '*': y=2;

    break;

    case '^': y=3;

    break;

    case '(': y=6;

    break;

    default: y=-1;

    }

    return y;

    }

    voidintopre()

    {

    intj,k=0;

    charx,y;

    stack[++top]='\0';

    for(j=strlen(in)-1;j>=0;j--)

    {

    x=in[j];

    if(operand(x))

    pre[k++]=x;

    else if(x=='(')

    {

    while((y=pop())!=')')

    pre[k++]=y;

    }

    else

    {

    while(def(stack[top])>check(x))

    pre[k++]=pop();

    push(x);

    }

  • 7/28/2019 SP Lab File

    8/33

    }

    while(top>=0)

    pre[k++]=pop();

    }

    void reverse(char pre[],intlen)

    {

    chararr[len];

    for(m=len, n=0;m

  • 7/28/2019 SP Lab File

    9/33

    4. Write a program to convert infix to postfix notation.

    #include

    #include

    #define size 10

    char stack[size];

    int tos=0,ele;

    void push();

    char pop();

    void show();

    int isempty();

    int isfull();

    char infix[30],output[30];

    int prec(char);

    int main()

    {

    int i=0,j=0,k=0,length;

    char temp;

    printf("\nEnter an infix expression:");

    scanf("%s",infix);

    printf("\nThe infix expresson is %s",infix);

    length=strlen(infix);

    for(i=0;i

  • 7/28/2019 SP Lab File

    10/33

    {

    temp=pop();

    printf("\n the poped element is :%c",temp);

    output[j++]=temp;

    push(infix[i]);

    printf("\n The pushed element is :%c",infix[i]);

    show();

    }

    else

    {

    push(infix[i]);

    printf("\nThe pushed element is:%c",infix[i]);

    show();

    }

    }

    else

    {

    if(infix[i]=='(')

    {

    push(infix[i]);

    printf("\nThe pushed-- element is:%c",infix[i]);

    }

    if(infix[i]==')')

    {

    temp=pop();

    while(temp!='(')

    {

    output[j++]=temp;

    printf("\nThe element added to Q is:%c",temp);

    printf("\n the poped element is :%c",temp);

    temp=pop();

    }

    }

    }

    }

    }

    printf("\nthe infix expression is: %s",output);

    }

    while(tos!=0)

    {

    output[j++]=pop();

    }

  • 7/28/2019 SP Lab File

    11/33

    printf("the infix expression is: %s\n",output);

    }

    void push(intele)

    {

    stack[tos]=ele;

    tos++;

    }

    char pop()

    {

    tos--;

    return(stack[tos]);

    }

    void show()

    {

    int x=tos;

    printf("--The Stack elements are.....");

    while(x!=0)

    printf("%c, ",stack[--x]);

    }

    intprec(char symbol)

    {

    if(symbol== '(')

    return 0;

    if(symbol== ')')

    return 0;

    if(symbol=='+' || symbol=='-')

    return 1;

    if(symbol=='*' || symbol=='/')

    return 2;

    if(symbol=='^')

    return 3;

    return 0;

    }

  • 7/28/2019 SP Lab File

    12/33

    OUTPUT:

  • 7/28/2019 SP Lab File

    13/33

    5. Write a program to accept a language of type ab*a.

    #include

    #include

    void main()

    {

    char s[10];

    int i=0,j=0, le=0,f=0;

    printf("enter the string");

    gets(s);

    le=strlen(s);

    if(s[le-1]=='b'&&s[le-2]=='a')

    {

    for(i=0;i

  • 7/28/2019 SP Lab File

    14/33

    OUTPUT:

  • 7/28/2019 SP Lab File

    15/33

    6. Write a program to convert NDFA into DFA.

    #include

    #include

    #include

    #include

    struct table

    {

    char state[20];

    char out0[20];

    char out1[20];

    };

    void strsort(char c[])

    {

    int l=strlen(c);

    char t;

    for(int j=0;j

  • 7/28/2019 SP Lab File

    16/33

    }

    }

    }

    }

    void strred(char c[])

    {

    int l=strlen(c);

    char t;

    char s[100];

    for(int i=0,n=0;i

  • 7/28/2019 SP Lab File

    17/33

  • 7/28/2019 SP Lab File

    18/33

    int ncnt=0;

    char nst2[20][20];

    for(i=0;i

  • 7/28/2019 SP Lab File

    19/33

    break;

    }

    }

    if(f1==0)

    {

    for(int h=0;h

  • 7/28/2019 SP Lab File

    20/33

    break;

    }

    }

    if(f2==0)

    {

    for(int h=0;h

  • 7/28/2019 SP Lab File

    21/33

    for(i=0;i

  • 7/28/2019 SP Lab File

    22/33

    }

    }

    }

    strsort(str);

    strred(str);

    strcpy(d[i].out0,str);

    ////////////////////////////

    strcpy(str,"");

    //////////////////

    for( k=0;k

  • 7/28/2019 SP Lab File

    23/33

    strcpy(d[i].out1,str);

    }

    count+=ncnt;

    for(i=0;i

  • 7/28/2019 SP Lab File

    24/33

    OUTPUT:

  • 7/28/2019 SP Lab File

    25/33

    7. Write a program to convert RE into NDFA.

    #include

    #include

    #include

    #include

    struct table

    {

    char state;

    char out0;

    char out1;

    char oute[5];

    };

    void main()

    {

    clrscr();

    char re[31];

    table n[26];

    int c=0;

    cout

  • 7/28/2019 SP Lab File

    26/33

    {

    n[c].state='a'+c;

    n[c-1].oute[0]=n[c].state;

    n[c-1].oute[1]='\0';

    c++;

    n[c-1].out0='-';

    n[c-1].out1='-';

    n[c-1].oute[0]='a'+c;

    n[c-1].oute[1]='a'+c+1;

    n[c-1].oute[2]='a'+c+5;

    n[c-1].oute[3]='\0';

    n[c].state='a'+c;

    n[c].out0='a'+c+2;

    n[c].out1='-';

    strcpy(n[c].oute,"-");

    c++;

    n[c].state='a'+c;

    n[c].out1='a'+c+2;

    n[c].out0='-';

    strcpy(n[c].oute,"-");

    c++;

    n[c].state='a'+c;

    n[c].out0='-';

    n[c].out1='-';

    n[c].oute[0]='a'+c+2;

    n[c].oute[1]='\0';

    c++;

    n[c].state='a'+c;

    n[c].out0='-';

    n[c].out1='-';

    n[c].oute[0]='a'+c+2;

    n[c].oute[1]='\0';

    c++;

    n[c].state='a'+c;

    n[c].out0='-';

    n[c].out1='-';

    n[c].oute[0]='a'+c-5;

    n[c].oute[1]='a'+c+1;

    n[c].oute[2]='\0';

    c++;

  • 7/28/2019 SP Lab File

    27/33

    n[c].state='a'+c;

    n[c].out0='-';

    n[c].out1='-';

    strcpy(n[c].oute,"-");

    i=i+5;

    }

    else

    {

    n[c].state='a'+c;

    n[c-1].oute[0]=n[c].state;

    n[c-1].oute[1]='\0';

    c++;

    n[c-1].out0='-';

    n[c-1].out1='-';

    n[c-1].oute[0]='a'+c;

    n[c-1].oute[1]='a'+c+1;

    n[c-1].oute[2]='\0';

    n[c].state='a'+c;

    n[c].out0='a'+c+2;

    n[c].out1='-';

    strcpy(n[c].oute,"-");

    c++;

    n[c].state='a'+c;

    n[c].out1='a'+c+2;

    n[c].out0='-';

    strcpy(n[c].oute,"-");

    c++;

    n[c].state='a'+c;

    n[c].out0='-';

    n[c].out1='-';

    n[c].oute[0]='a'+c+2;

    n[c].oute[1]='\0';

    c++;

    n[c].state='a'+c;

    n[c].out0='-';

    n[c].out1='-';

    n[c].oute[0]='a'+c+2;

    n[c].oute[1]='\0';

    c++;

    n[c].state='a'+c;

  • 7/28/2019 SP Lab File

    28/33

    n[c].out0='-';

    n[c].out1='-';

    n[c].oute[0]='a'+c+1;

    n[c].oute[1]='\0';

    c++;

    n[c].state='a'+c;

    n[c].out0='-';

    n[c].out1='-';

    strcpy(n[c].oute,"-");

    i=i+4;

    }

    }

    }

    }

    }

    }

    else if(re[i]=='0'||re[i]=='1')

    {

    if(re[i+1]!='*'&&re[i+1]!='|')//single input char

    {

    n[c].state='a'+c;

    char tmp[2];

    tmp[0]=n[c].state;

    tmp[1]='\0';

    strcat(n[c-1].oute,tmp);

    c++;

    n[c].state='a'+c;

    n[c].out0='-';

    n[c].out1='-';

    strcpy(n[c].oute,"-");

    if(re[i]=='0')

    {

    n[c-1].out0=n[c].state;

    n[c-1].out1='-';

    strcpy(n[c-1].oute,"-");

    }

    else if(re[i]=='1')

  • 7/28/2019 SP Lab File

    29/33

    {

    n[c-1].out1=n[c].state;

    n[c-1].out0='-';

    strcpy(n[c-1].oute,"-");

    }

    c++;

    }

    else

    {

    }

    }

    }

    n[c].state='a'+c;

    n[c].out0='-';

    n[c].out1='-';

    strcpy(n[c].oute,"-");

    cout

  • 7/28/2019 SP Lab File

    30/33

    8. Wrote a program To Generate Symbol Table from an expression

    /*SYMBOL TABLE CREATION */

    #include

    #include

    #include

    void main()

    {

    char in[50],dig[50],id[50];

    int i=0,j=0,k,l=0;

    printf("Enter the Expression:\t");

    gets(in);

    printf("\nKeyword\tIdentifier\tConstants\tOperators\tSpecialCharacters\n");

    while(in[i]!='\0')

    {

    if(isalpha(in[i]))

    {

    j=0;

    while((isalpha(in[i]))||(isdigit(in[i])))

    {

    id[j]=in[i];

    i++;

    j++;

    }

    id[j]='\0';

    if(strcmp(id,"char")==0||strcmp(id,"int")==0||strcmp(id,"float")==0||strcmp(id,

    "if")==0||strcmp(id,"then")==0||strcmp(id,"while")==0||strcmp(id,"do")==0||strcmp(i

    d,"for")==0||strcmp(id,"switch")==0||strcmp(id,"case")==0)

    {

    printf("\n");

    for(l=0;l

  • 7/28/2019 SP Lab File

    31/33

    k=0;

    while(isdigit(in[i]))

    {

    dig[k]=in[i];

    i++;

    k++;

    }

    printf("\n\t\t\t");

    for(l=0;l

  • 7/28/2019 SP Lab File

    32/33

    9. Write a program for Top Down Parsing

    #include

    void main()

    {

    int a[30];

    int min=10000,temp=0,i,j,lev,n,noofc,z;

    printf("please enter how many number:");

    scanf("%d",&n);

    for(i=0;i

  • 7/28/2019 SP Lab File

    33/33

    OUTPUT: