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();
   
}

Tuesday, September 25, 2018

Write a C program to test whether a given identifier is valid or not

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
    char a[10];
    int flag,
    i=1;
    clrscr();
    printf("\n Enter an identifier:");
    gets(a);
    if(isalpha(a[0]))
    flag=1;
    else
    printf("\n Not a valid identifier");
    while(a[i]!='\0')
    {
        if(!isdigit(a[i])&&!isalpha(a[i]))
        {
            flag=0; break;
           
        } i++;
       
    }
    if(flag==1)
    printf("\n Valid identifier");
    getch();
   
}

Thursday, September 13, 2018

Program for DDA Line Drawing Algorithm in C

//   Program for DDA Line Drawing Algorithm in C
 
#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <dos.h>
 
void main( )
{
    float x,y,x1,y1,x2,y2,dx,dy,step;
    int i,gd=DETECT,gm;
 
    initgraph(&gd,&gm,"c:\\turboc3\\bgi");
 
    printf("Enter the value of x1 and y1 : ");
    scanf("%f%f",&x1,&y1);
    printf("Enter the value of x2 and y2: ");
    scanf("%f%f",&x2,&y2);
 
    dx=abs(x2-x1);
    dy=abs(y2-y1);
 
    if(dx>=dy)
        step=dx;
    else
        step=dy;
 
    dx=dx/step;
    dy=dy/step;
 
    x=x1;
    y=y1;
 
    i=1;
    while(i<=step)
    {
        putpixel(x,y,5);
        x=x+dx;
        y=y+dy;
        i=i+1;
        delay(100);
    }
 
    closegraph();
}

Tuesday, September 11, 2018

Compiler Design Program


/******************************************************************************

Write a C program to identify whether a given line is a comment or not.

*******************************************************************************/

#include<stdio.h>
#include<conio.h>
void main()
{
    char com[30];
    int i=2,a=0;
    clrscr();
    printf("\n Enter comment:");
    gets(com);
    if(com[0]=='/')
    {
        if(com[1]=='/')
    printf("\n It is a comment");
    else if(com[1]=='*')
    {
        for(i=2;i<=30;i++)
        {
            if(com[i]=='*'&&com[i+1]=='/')
            {
                printf("\n It is a comment");
                a=1;
                break;
               
            } else continue;
           
        }
        if(a==0)
        printf("\n It is not a comment");
       
    } else printf("\n It is not a comment");
       
    } else printf("\n It is not a comment");
    getch();
   
}

Wednesday, March 14, 2018

Friday, February 9, 2018

Thursday, January 11, 2018

SQL

DROP TABLE IF EXISTS emp;
CREATE TABLE emp(eid integer,ename varchar(20),sal integer,ecountry varchar(200));
INSERT INTO emp VALUES (1,'XYZ',20000,'INDIA');
INSERT INTO emp VALUES (2,'XYZ1',2000,'ENGLAND');
INSERT INTO emp VALUES (3,'XYZ2',2000,'INDIA');
INSERT INTO emp VALUES (4,'XYZ3',20000,'ENGLAND');
INSERT INTO emp VALUES (5,'XYZ4',20000 ,'RUSSIA');
INSERT INTO emp VALUES (6,'XYZ5',20000 ,'SRI LANKA');

SELECT * FROM emp;
SELECT * FROM emp WHERE eid=1;
SELECT * FROM emp WHERE ename='XYZ5';
SELECT eid,ename FROM emp;
SELECT * FROM emp WHERE sal<10000;
SELECT DISTINCT ecountry FROM emp;
SELECT DISTINCT ename FROM emp;
SELECT COUNT(DISTINCT ecountry) FROM emp;
SELECT COUNT(DISTINCT ename) FROM emp;
SELECT * FROM emp WHERE ecountry='INDIA';
DROP TABLE IF EXISTS emp;
CREATE TABLE emp(eid integer,ename varchar(20),sal integer,ecountry varchar(200));
INSERT INTO emp VALUES (1,'XYZ',20000,'INDIA');
INSERT INTO emp VALUES (2,'XYZ1',2000,'ENGLAND');
INSERT INTO emp VALUES (3,'XYZ2',2000,'INDIA');
INSERT INTO emp VALUES (4,'XYZ3',20000,'ENGLAND');
INSERT INTO emp VALUES (5,'XYZ4',20000 ,'RUSSIA');
INSERT INTO emp VALUES (6,'XYZ5',20000 ,'SRI LANKA');

SELECT * FROM emp;
SELECT * FROM emp WHERE eid=1;
SELECT * FROM emp WHERE ename='XYZ5';
SELECT eid,ename FROM emp;
SELECT * FROM emp WHERE sal<10000;
SELECT DISTINCT ecountry FROM emp;
SELECT DISTINCT ename FROM emp;
SELECT COUNT(DISTINCT ecountry) FROM emp;
SELECT COUNT(DISTINCT ename) FROM emp;
SELECT * FROM emp WHERE ecountry='INDIA';



OUTPUT:
eidenamesalecountry
1XYZ20000INDIA
2XYZ12000ENGLAND
3XYZ22000INDIA
4XYZ320000ENGLAND
5XYZ420000RUSSIA
6XYZ520000SRI LANKA
eidenamesalecountry
1XYZ20000INDIA
eidenamesalecountry
6XYZ520000SRI LANKA
eidename
1XYZ
2XYZ1
3XYZ2
4XYZ3
5XYZ4
6XYZ5
eidenamesalecountry
2XYZ12000ENGLAND
3XYZ22000INDIA
ecountry
INDIA
ENGLAND
RUSSIA
SRI LANKA
ename
XYZ
XYZ1
XYZ2
XYZ3
XYZ4
XYZ5
COUNT(DISTINCT ecountry)
4
COUNT(DISTINCT ename)
6
eidenamesalecountry
1XYZ20000INDIA
3XYZ22000INDIA