Tuesday, 3 March 2015

shift reduce parser in c

SHIFT REDUCE PARSER

        
        To write a c program for implementing the shift reduce parser. The grammar used in this program is 
E->E+E
E->E*E
E->(E)
E->id
        This program works for all possible input strings. Lets take the input string (id*id)+id

Program:


#include<stdio.h>
#include<conio.h>
#include<string.h>
int k=0,z=0,i=0,j=0,c=0;
char a[16],ac[20],stk[15],act[10];
void check();
void main()
   {
      clrscr();
      puts("GRAMMAR is E->E+E \n E->E*E \n E->(E) \n E->id");
      puts("enter input string ");
      gets(a);
      c=strlen(a);
      strcpy(act,"SHIFT->");
      puts("stack \t input \t action");
      for(k=0,i=0; j<c; k++,i++,j++)
       {
         if(a[j]=='i' && a[j+1]=='d')
           {
              stk[i]=a[j];
              stk[i+1]=a[j+1];
              stk[i+2]='\0';
              a[j]=' ';
              a[j+1]=' ';
              printf("\n$%s\t%s$\t%sid",stk,a,act);
              check();
           }
         else
           {
              stk[i]=a[j];
              stk[i+1]='\0';
              a[j]=' ';
              printf("\n$%s\t%s$\t%ssymbols",stk,a,act);
              check();
           }
       }
       getch();
   }
void check()
   {
     strcpy(ac,"REDUCE TO E");
     for(z=0; z<c; z++)
       if(stk[z]=='i' && stk[z+1]=='d')
         {
           stk[z]='E';
           stk[z+1]='\0';
           printf("\n$%s\t%s$\t%s",stk,a,ac);
           j++;
         }
     for(z=0; z<c; z++)
       if(stk[z]=='E' && stk[z+1]=='+' && stk[z+2]=='E')
         {
           stk[z]='E';
           stk[z+1]='\0';
           stk[z+2]='\0';
           printf("\n$%s\t%s$\t%s",stk,a,ac);
           i=i-2;
         }
     for(z=0; z<c; z++)
       if(stk[z]=='E' && stk[z+1]=='*' && stk[z+2]=='E')
         {
           stk[z]='E';
           stk[z+1]='\0';
           stk[z+1]='\0';
           printf("\n$%s\t%s$\t%s",stk,a,ac);
           i=i-2;
         }
     for(z=0; z<c; z++)
       if(stk[z]=='(' && stk[z+1]=='E' && stk[z+2]==')')
         {
           stk[z]='E';
           stk[z+1]='\0';
           stk[z+1]='\0';
           printf("\n$%s\t%s$\t%s",stk,a,ac);
           i=i-2; 
         }
   }

Output:



jewellery loan management program in c#


JEWELLERY LOAN MANAGEMENT 

Aim:


    To write a c# program for jewellery loan management system using basic c# concepts.


Algorithm:


Step 1: Include the necessary namespace and create the user defined classes.

Step 2: Get the user’s choice and perform computations.

Step 3: If choice is 1, get jewellery details and debtor details and display the amount of loan sanctioned for the jewellery.

Step 4: If choice is 2, display the monthly interest for the loan amount.

Step 5: If choice is 3, display the details of pending amount if any for the debtor who wish to get back the jewellery.

Step 6: If choice is 4, terminate the program.

Program:


using System;                                                         //library declaration
namespace jew_loan                                              //namespace
{
    interface month                                                  //interface declaration
    {
        void newloan();                                              //method
    }
    interface amount
    {
        void amt();
    }
    class met1 : month, amount         //multiple inheritance using interface
    {
        public met1()                                          //constructor
        {
            p = 0;
        }
        protected decimal gram;                         //protected acess specifier
        public int loan_no;                                  //public acess specifier
        protected decimal p;
        #region month Members
        public void newloan()                              //declaring methods of interface
        {
            try                                                     //try block    
            {
                string name, jew_type, address;
                Console.WriteLine("enter the details:");
                Console.WriteLine("name:");
                name = Console.ReadLine();
                Console.WriteLine("address:");
                address = Console.ReadLine();
                Console.WriteLine("loan number:");
                loan_no = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("jewel type");
                jew_type = Console.ReadLine();
                Console.WriteLine("gram:");
                gram = Convert.ToDecimal(Console.ReadLine());  
            }                                                         
            catch (StackOverflowException) { }                //catch block
        }
        #endregion
        #region amount Members
        public void amt()
        {
        p = gram * 2000;
        Console.WriteLine("loan sanctioned is {0}", p); //container
        }
        #endregion
        public decimal mon_i;
        public int interest;
        public void mon()
        {
            try
            {
                Console.WriteLine("enter loan amt:");
                p = Convert.ToDecimal(Console.ReadLine());
                if (p < 1000)                                        //if-else selection statement
                {
                    interest = 3;
                }
                else
                    interest = 2;
                mon_i = (p * interest) / 100;
                Console.WriteLine("monthly interest is" + mon_i);
                Console.WriteLine("your jewel will go to auction if interest is not paid for 3 months");
            }
            catch (StackOverflowException) { }
        }       
     }
    class met2 : met1                                              //hierarchical inheritance
    {
        public void check()                        
        {
            int mon_no;
            Decimal total_pay=0;
            try
            {
                Console.WriteLine("enter loan num:");
                loan_no = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("enter loan amt:");
                p = Convert.ToDecimal(Console.ReadLine());
                Console.WriteLine("enter no of monthly interest not paid:");
                mon_no = Convert.ToInt32(Console.ReadLine());
                if (mon_no <= 3)
                 {
                   if (p < 1000)                             
                {
                    interest = 3;
                }
                else
                    interest = 2;
                mon_i = (p * interest) / 100;
                Console.WriteLine("monthly interest is" + mon_i);
                total_pay =(mon_no * mon_i)+ p;
                Console.WriteLine("you have to pay {0} to get your jewel back", total_pay);
                }
                else
                {
                    Console.WriteLine("you didnt pay interest for {0} months",mon_no);
                    Console.WriteLine("your jewel is taken for auction");
                }
            }
            catch (StackOverflowException) { }
        }
    }
    class jewel
    {
        static void Main(string[] args)
        {
            met1 n1 = new met1();
            met2 n2 = new met2();
            int c;
            string ch;
            Console.WriteLine("\t***welcome to vasantham jewellery***");
            do                                                     //do-while iterative statement
            {
                Console.WriteLine("1.new loan 2.monthly payment 3.close loan 4.exit \nenter your choice:");
                ch = Console.ReadLine();
                c= Int32.Parse(ch);
            switch (c)                                          //switch control statement
            {
                case 1: n1.newloan();
                            n1.amt();
                            break;
                case 2: n1.mon();
                            break;
                case 3: n2.check();
                            break;
                case 4: break;
                default: Console.WriteLine("enter valid choice");
                             break;
            }
            } while (c != 4);
        }
    }

}

Output:


Choice 1:


choice 2:




choice 3:



Result:
       Thus the program to perform jewellery loan management system is executed successfully in c# program.