Tuesday, March 13, 2012

Seperation of terminals,non-terminals and productions in the CFG in a compiler Design

Let us consider the productions of grammer G, that may contain any number of productions then the code is,

PROGRAM:

#include<iostream>
#include<string.h>
using namespace std;
class production
{
private:
char left;
char right[10][10];
int noa,i,j;
public:
void setproduction(char str[15])
{
j=0;noa=0;
left=str[0];
for(i=3;str[i]!='\0';i++){
if(str[i]!='/'){
   right[noa][j++]=str[i];
right[noa][j]='\0';
}
else{
   right[noa][j]='\0';noa++;j=0;
}
}
}
void printproduction()
{
cout<<left<<"->";
for(i=0;i<=noa;i++)
{
cout<<right[i];
if(i!=noa){cout<<"/";}
}
}
friend class CFG;
};

Sunday, January 1, 2012

Puzzle: Time for a change JAVA

Consider the following problem:
     Ram goes to the auto parts store to buy a spark plug that costs $1.10, but all he has in his wallet are two-dollar bills. How much change should he get if he pays for the spark plug with a two-dollar bill?

Here is a problem that attempts to solve the word problem. What does it print?

CODE:
public class Change
{
  public static void main(String args[])
  {
     System.out.println(2.00-1.10);
  }
}
Output:
0.8999999999999999

Here is a concept of BigDecimal :