Tuesday, October 30, 2018

Program to evaluate simple expressions

<?php
// PHP program to evaluate a
// given expression

// A utility function to check if
// a given character is operand
function isOperand($c)
{
    return ($c >= '0' && $c <= '9');
}

// utility function to find
// value of and operand
function value($c)
{
    return ($c - '0');
}

// This function evaluates simple
// expressions. It returns -1 if the
// given expression is invalid.
function evaluate($exp)
{
    $len = strlen($exp);
   
    // Base Case: Given expression is empty
    if ($len == 0) return -1;

    // The first character must be
    // an operand, find its value
    $res = (int)(value($exp[0]));

    // Traverse the remaining
    // characters in pairs
    for ($i = 1; $i < $len; $i += 2)
    {
        // The next character must be
        // an operator, and next to
        // next an operand
        $opr = $exp[$i];
        $opd = $exp[$i + 1];

        // If next to next character
        // is not an operand
        if (!isOperand($opd))
        return -1;

        // Update result according
        // to the operator
        if ($opr == '+')   
        $res += value($opd);
        else if ($opr == '-')
            $res -= (int)(value($opd));
        else if ($opr == '*')
            $res *= (int)(value($opd));
        else if ($opr == '/')
            $res /= (int)(value($opd));

        // If not a valid operator
        else               
        return -1;
    }
    return $res;
}

// Driver Code
$expr1 = "1+2*5+3";
$res = evaluate($expr1);
($res == -1) ? print($expr1." is Invalid\n"):
            print("Value of " . $expr1 .
                    " is " . $res . "\n");
           
$expr2 = "1+2*3";
$res = evaluate($expr2);
($res == -1) ? print($expr2." is Invalid\n"):
            print("Value of " . $expr2 .
                    " is " . $res . "\n");
           
$expr3 = "4-2+6*3";
$res = evaluate($expr3);
($res == -1) ? print($expr3." is Invalid\n"):
            print("Value of " . $expr3 .
                    " is " . $res . "\n");
           
$expr4 = "1++2";
$res = evaluate($expr4);
($res == -1) ? print($expr4." is Invalid\n"):
            print("Value of " . $expr4 .
                    " is " . $res . "\n");

// This code is contributed by mits
?>

Tuesday, October 16, 2018

C Program for Predictive Parsing


The following is the predictive parsing table for the following grammar:                                                    
S->A                                                                                                                        
A->Bb                                                                                                                       
A->Cd                                                                                                                       
B->aB 

_______________________________________________________________________________________
#include<stdio.h>
#include<conio.h>
#include<string.h>
char prol[7][10]={"S","A","A","B","B","C","C"};
char pror[7][10]={"A","Bb","Cd","aB","@","Cc","@"};
char prod[7][10]={"S->A","A->Bb","A->Cd","B->aB","B->@","C->Cc","C->@"};
char first[7][10]={"abcd","ab","cd","a@","@","c@","@"};
char follow[7][10]={"$","$","$","a$","b$","c$","d$"};
char table[5][6][10];
numr(char c)
{
    switch(c)
    {
        case 'S': return 0;
        case 'A': return 1;
        case 'B': return 2;
        case 'C': return 3;
        case 'a': return 0;
        case 'b': return 1;
        case 'c': return 2;
        case 'd': return 3;
        case '$': return 4;
       
    }
    return(2);
   
}
void main()
{
    int i,j,k;
    clrscr();
    for(i=0;i<5;i++)
    for(j=0;j<6;j++)
    strcpy(table[i][j]," ");
    printf("\nThe following is the predictive parsing table for the following grammar:\n");
    for(i=0;i<7;i++)
    printf("%s\n",prod[i]);
    printf("\nPredictive parsing table is\n");
    fflush(stdin); for(i=0;i<7;i++)
    {
        k=strlen(first[i]);
        for(j=0;j<10;j++)
        if(first[i][j]!='@')
        strcpy(table[numr(prol[i][0])+1][numr(first[i][j])+1],prod[i]);
       
    }
    for(i=0;i<7;i++)
    {
        if(strlen(pror[i])==1)
        {
            if(pror[i][0]=='@')
            {
                k=strlen(follow[i]);
                for(j=0;j<k;j++)
                strcpy(table[numr(prol[i][0])+1][numr(follow[i][j])+1],prod[i]);
            }
           
        }
       
    }
    strcpy(table[0][0]," ");
    strcpy(table[0][1],"a");
    strcpy(table[0][2],"b");
    strcpy(table[0][3],"c");
    strcpy(table[0][4],"d");
    strcpy(table[0][5],"$");
    strcpy(table[1][0],"S");
    strcpy(table[2][0],"A");
    strcpy(table[3][0],"B");
    strcpy(table[4][0],"C");
    printf("\n--------------------------------------------------------\n");
    for(i=0;i<5;i++)
    for(j=0;j<6;j++)
    {
        printf("%-10s",table[i][j]);
        if(j==5)
        printf("\n--------------------------------------------------------\n");
       
    }
    getch();
   
}

Thursday, October 4, 2018

DDA line drawing algorithm

    #include<stdio.h>
    #include<conio.h>
    #include<graphics.h>
    void main()
    {
    int x1,y1,x2,y2,dx,dy,length,i;
    float x,y,xinc,yinc;
    int gd=DETECT,gm;
    //clrscr();
    //gd=DETECT;
    initgraph(&gd,&gm,"c:\\tc\\bgi");
     printf("Enter the starting coordinates");
    scanf("%d%d",&x1,&y1);
    printf("Enter the ending coordinates");
    scanf("%d%d",&x2,&y2);
    dx=x2-x1;
    dy=y2-y1;
    if(abs(dx)>abs(dy))
    length=abs(dx);
    else
    length=abs(dy);
    xinc=dx/(float)length;
    yinc=dy/(float)length;
    x=x1
    y=y1
    putpixel(x,y,10);
    for(i=0;i<length;i++)
    {
    putpixel(x,y,10);
    x=x+xinc;
    y=y+yinc;
    delay(10);
    }
    getch();
    closegraph();
    }

Tuesday, October 2, 2018

Write a C program for constructing of LL (1) parsing.

Q. Perform LL(1) Parsing for the given grammar
 b->tb
t->cf
f->i
i->@
c->f|@
b->+tb
f->id
t->*ft

Click here for demo

#include<stdio.h>
#include<conio.h>
#include<string.h>
char s[20],stack[20];
void main()
{
    char m[5][6][3]={"tb"," "," ","tb"," "," "," ","+tb"," "," ","n","n","fc"," "," ","fc"," "," "," ","n","*fc"," a ","n","n","i"," "," ","(e)"," "," "};
    int size[5][6]={2,0,0,2,0,0,0,3,0,0,1,1,2,0,0,2,0,0,0,1,3,0,1,1,1,0,0,3,0,0};
    int i,j,k,n,str1,str2;
    clrscr();
    printf("\n Enter the input string: ");
    scanf("%s",s);
    strcat(s,"$");
    n=strlen(s);
    stack[0]='$';
    stack[1]='e';
    i=1;
    j=0;
    printf("\nStack Input\n");
    printf("__________________\n");
    while((stack[i]!='$')&&(s[j]!='$'))
    {
        if(stack[i]==s[j])
        {
            i--;
            j++;
        }
        switch(stack[i])
        {
            case 'e':
            str1=0;
            break;
            case 'b':
            str1=1;
            break;
            case 't':
            str1=2;
            break;
            case 'c':
            str1=3;
            break;
            case 'f':
            str1=4;
            break;
           
        }
        switch(s[j])
        { case 'i':
        str2=0;
        break;
        case '+':
        str2=1;
        break;
        case '*': str2=2;
        break;
        case '(': str2=3;
        break;
        case ')': str2=4;
        break;
        case '$': str2=5;
        break;
           
        }
        if(m[str1][str2][0]=='\0')
        {
            printf("\nERROR");
            exit(0);
           
        }
        else if(m[str1][str2][0]=='n')
        i--;
        else if(m[str1][str2][0]=='i')
        stack[i]='i';
        else
        {
            for(k=size[str1][str2]-1;k>=0;k--)
            {
                stack[i]=m[str1][str2][k];
                i++;
               
            }
            i--;
           
        }
        for(k=0;k<=i;k++)
        printf(" %c",stack[k]);
        printf(" ");
        for(k=j;k<=n;k++)
        printf("%c",s[k]);
        printf(" \n ");
       
    }
    printf("\n SUCCESS");
    getch();
   
}