Saturday, October 8

C Program to generate Intermediate code



#include<stdio.h>
#include<ctype.h>
#include<string.h>

main()
{
char a[20];
int i=2,n;
printf("Exp  :");
scanf("%s", a);
if(isdigit(a[0]))
printf("MVI A,%c\n",a[0]);
else printf("MOV A,%c\n",a[0]);
n=strlen(a);
while(i<n)
{
switch(a[i])
{
case '+':printf("ADD B\n");i+=3;break;
case '-':printf("SUB B\n");i+=3;break;



default:if(isdigit(a[i]))
printf("MVI B,%c\n",a[i]);
else
printf("MOV B,%c\n",a[i]);
i--;
}
}
}

LEX program to check the syntax of SCANF statement

%{
#include<stdio.h>
#include<ctype.h>
int i=0,j=0;
%}
a "%d"|"%f"|"%c"|"%s"
id [a-zA-Z][a-zA-Z0-9]*
%%
scanf\((\"({a}*|.*)*\"(,\&{id})*\))\;

{for(i=0;i<yyleng;i++)
{if(yytext[i]=='%')
j++;
if(yytext[i]==',')
j--;
}

if(j==0)
printf("Correct");
else
printf("Incorrect");
}
%%
main()
{ yyin=fopen("sample.c","r");
yylex();
}
int yywrap()
{

return 1;
}        

LEX program to check the syntax of PRINTF statement



%{
#include<stdio.h>
#include<ctype.h>
int i=0,j=0;
%}
a "%d"|"%f"|"%c"|"%s"
id [a-zA-Z][a-zA-Z0-9]*
%%

printf\((\"({a}*|.*)*\"(,{id})*\))\; {for(i=0;i<yyleng;i++) {
if(yytext[i]=='%')
j++;
if(yytext[i]==',')
j--;
}

if(j==0)
printf("Correct..!!");
else
printf("Incorrect..!!");
}
%%

main()
{

yyin=fopen("sample.c","r");
yylex();
}


int yywrap()
{

return 1;
}      




Explanation:

Well, checking syntax of printf statement is very easy.
The syntax should be "printf" followed by "(" and  . It can include a usual string or %d,%f type expression delimited by another ".

The entities specified by %f typed expression should be specified before ")" separated by commas.

The number of arguments is then checked within rule part ( i.e, no. of % is equal to no. of commas).


For more interesting LEX programs , click here...!!


                            

udp communication

Hi Viewer,

Here is a simple java program showing client-server interacion using UDP.
In tgis example, client will send a message and the sever will reply back with same message after converting it to Uppercase..!!



server.java


import java.io.*;
import java.net.*;
class UDPServer
{
public static void main(String args[])throws Exception
{
DatagramSocket serverSocket=new DatagramSocket(1249);
byte[] receiveData=new byte[1024];
byte[] sendData=new byte[1024];
while(true)
{
DatagramPacket receivePacket=new DatagramPacket(receiveData,receiveData.length);
serverSocket.receive(receivePacket);
String sentence=new String(receivePacket.getData());
InetAddress IPAddress=receivePacket.getAddress();
int port=receivePacket.getPort();
String capitalizedSentence=sentence.toUpperCase();
sendData=capitalizedSentence.getBytes();
DatagramPacket sendPacket=new DatagramPacket(sendData,sendData.length,IPAddress,port);
serverSocket.send(sendPacket);
}
}
}










client.java


import java.io.*;
import java.net.*;
class UDPClient
{
public static void main(String args[])throws Exception
{
BufferedReader inFromUser=new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientSocket=new DatagramSocket();
InetAddress IPAddress=InetAddress.getByName("localhost");
byte[] sendData=new byte[1024];
byte[] receiveData=new byte[1024];
System.out.println("Enter msg:");
String sentence=inFromUser.readLine();
sendData=sentence.getBytes();
DatagramPacket sendPacket=new DatagramPacket(sendData,sendData.length,IPAddress,1249);
clientSocket.send(sendPacket);
DatagramPacket receivePacket=new DatagramPacket(receiveData,receiveData.length);
clientSocket.receive(receivePacket);
String modifiedSentence=new String(receivePacket.getData());
System.out.println("FROM SERVER:"+modifiedSentence);
clientSocket.close();
}
}




If you have any suggestion, or doubt on it, please do comment here.!! Thank you.!! :)

Lex program to check the syntax of FOR loop



%{
#include<stdio.h>
#include<ctype.h>
int c=1;
%}
op "++"|"--"
rop "<"|">"|"<="|">="|"=="|"!="
id [a-zA-Z][a-zA-Z0-9]*
no [0-9]*
pp [\n]
%%
for\(({id}=({no}|{id}))?\;{id}{rop}({id}|{no})\;{id}{op}\){pp}+\{(.*\n)*.*\} {printf("correct");c=0;}
%%
main()
{ yyin=fopen("file11.c","r");
yylex();
if(c==1)
printf("incorrect");
}
int yywrap()
{
return 1;
}

FIRST of a Given Grammar using C program


Hello Friend,

In this post we will discuss how to find FIRST of a grammar using C.


The rules for finding FIRST of a given grammar is:
  1. If X is terminal, FIRST(X) = {X}.
  2. If X → ε is a production, then add ε to FIRST(X).
  3. If X is a non-terminal, and X → Y1 Y2 … Yk is a production, and ε is in all of FIRST(Y1), …, FIRST(Yk), then add ε to FIRST(X).
  4. If X is a non-terminal, and X → Y1 Y2 … Yk is a production, then add a to FIRST(X) if for some i, a is in FIRST(Yi), and ε is in all of FIRST(Y1), …, FIRST(Yi-1).




  1. Each Non terminal character is represented by one Uppercase letter.
  2. Each Terminal character is represented by one lowercase letter.
  3. LHS and RHS of each production will be separated by a "=" symbol.
  4. There will be no Blank space within a production string. 
  5. Assumed that Left recursion is avoided in all input productions. 



/* Author : Vipin
* mail:   vipinnarayantvm@gmail.com
 *Modified on : 23/03/2105
 */

#include<stdio.h>
#include<ctype.h>

void FIRST(char[],char );
void addToResultSet(char[],char);
int numOfProductions;
char productionSet[10][10];

main()
{
    int i;
    char choice; 
    char c;
    char result[20];
    printf("How many number of productions ? :");
    scanf(" %d",&numOfProductions);

    for(i=0;i<numOfProductions;i++)//read production string eg: E=E+T
    {
        printf("Enter productions Number %d : ",i+1);
        scanf(" %s",productionSet[i]);
    }
    do
    {

        printf("\n Find the FIRST of  :");
        scanf(" %c",&c);
        FIRST(result,c); //Compute FIRST; Get Answer in 'result' array
        printf("\n FIRST(%c)= { ",c);
        for(i=0;result[i]!='\0';i++)
        printf(" %c ",result[i]);       //Display result
        printf("}\n");

         printf("press 'y' to continue : ");
        scanf(" %c",&choice);
    }
    while(choice=='y'||choice =='Y');


}
/*
 *Function FIRST:
 *Compute the elements in FIRST(c) and write them
 *in Result Array.
 */
void FIRST(char* Result,char c)
{
    int i,j,k;
    char subResult[20];
    int foundEpsilon;
    subResult[0]='\0';
    Result[0]='\0';

    //If X is terminal, FIRST(X) = {X}.
    if(!(isupper(c)))
    {

        addToResultSet(Result,c);
               return ;
    }
    //If X is non terminal

    //Read each production
    for(i=0;i<numOfProductions;i++)
    {

//Find production with X as LHS
        if(productionSet[i][0]==c)
        {
//If X  ε is a production, then add ε to FIRST(X).
 if(productionSet[i][2]=='$') addToResultSet(Result,'$');

            //If X is a non-terminal, and X  Y1 Y2  Yk
            //is a production, then add a to FIRST(X)
            //if for some i, a is in FIRST(Yi),
            //and ε is in all of FIRST(Y1), …, FIRST(Yi-1).
      else
            {
                j=2;
                while(productionSet[i][j]!='\0')
                {
                foundEpsilon=0;
                FIRST(subResult,productionSet[i][j]);
                for(k=0;subResult[k]!='\0';k++)
                    addToResultSet(Result,subResult[k]);
                 for(k=0;subResult[k]!='\0';k++)
                     if(subResult[k]=='$')
                     {
                         foundEpsilon=1;
                         break;
                     }
                 //No ε found, no need to check next element
                 if(!foundEpsilon)
                     break;
                 j++;

                }
            }
    }
}

    return ;
}

/* addToResultSet adds the computed
 *element to result set. 
 *This code avoids multiple inclusion of elements
  */
void addToResultSet(char Result[],char val)
{
    int k;

    for(k=0 ;Result[k]!='\0';k++)
        if(Result[k]==val)
            return;
    Result[k]=val;
    Result[k+1]='\0';


}



Output







Friday, September 23

placement

If you are preparing for a campus recruitment for an IT company it is important to be clear with the basics of these languages well. There would be questions around this, especially if you have mentioned them in your Resume. The information below would be useful for you. While it is a bit in detail, at least knowing clearly a portion of this will be handy. Please do not try to over learn and create an impression to the interviewer that you are trying to be extra smart.  I have compiled this for you and hope it will be useful. Be clear with the basics of Object Orientation like Encapsulation, Polymorphism etc.

Java and C

The main difference between Java and C are speed, portability, and object-orientation. Java was created for the purpose of making a language that could be implemented on many different types of computers (cell phone, mac, PC, Linux, etc)

C on the other hand can only be run on a computer of the same type as the one that compiled the program. One of the costs of this portability in Java is speed. On numerous benchmarks Java still lags behind C in terms of speed, but that gap is narrowing.

Java is also object-oriented, whereas C is not. Java allows a user to create classes that contain data and methods. C is not capable of doing that.

Paradigm

C is geared towards procedural programming. That is, you write a number of procedures to do certain tasks and build up a program by calling those procedures as needed.

Java, on the other hand, is geared towards OOP (object oriented programming). With OOP, you define classes which represent an entity (for example, a window, a button, a string of text, a file). From one class many objects may be created, with every object of a certain class having the fields (places to store data) and methods (named blocks of code associated with the object) as defined by the class.

It is possible to write in an object oriented style in C and in a procedural style in Java, but in each case the language will somewhat get in your way. C++ is designed to support both paradigms.

Preprocessor

All C and C++ compilers implement a stage of compilation known as the preprocessor. The preprocessor basically performs an intelligent search and replace on identifiers that have been declared using the #define or #typedef directives. #define can also be used to declare macros. For example, a macro MAX(x,y) could be defined to return whichever of x or y holds the greatest value. This is not like calling a function as the substitution is done before the code is compiled. Most of the preprocessor definitions in C and C++ are stored in header files, which complement the actual source code files.

Java does not have a preprocessor. Constant data members are used in place of the #define directive and class definitions are used in lieu of the #typedef directive, however there is no substitute for macros, which can be useful. The Java approach to defining constants and naming types of data structures is probably conceptually simpler for the programmer. Additionally, Java programs don't use header files; the Java compiler builds class definitions directly from the source code files, which contain both class definitions and method implementations.

Memory Management

In C and C++, any memory that is allocated on the heap (e.g. using malloc or new) must be explicitly freed by the programmer (e.g. using free or delete). Forgetting to free memory leads to memory leaks, and in long-running programs can lead to the memory usage of the program growing very large.
Java provides garbage collection, meaning that memory is freed automatically when it is no longer reachable by any references. This prevents memory leaks, but can lead to pauses in execution while the garbage collector runs. Also, there is no promise of timely destruction in Java.

Pointers

Most developers agree that the misuse of pointers causes the majority of bugs in C and C++ programs. Put simply, when you have pointers, you have the ability to attempt to access memory that isn't yours and modify memory relating to a different data structure than the one you intended by accident. C/C++ programmers regularly use complex pointer arithmetic to create and maintain dynamic data structures. It's powerful, but can lead to a lot of time spent hunting down complex and often subtle bugs that arise as a result of having unguarded memory access.
The Java language does not support pointers. Instead, it provides similar functionality by making heavy use of references. A reference can be thought of as a "safe pointer" - the programmer can not directly manipulate the memory address. Java passes all arrays and objects by reference. This approach prevents common errors due to pointer mismanagement. It also makes programming easier in a lot of ways simply because the correct usage of pointers is easily misunderstood by inexperienced programmers.
C++ does provide references too. It considers them as aliases to another variable or object. They are safer than pointers where they can be used.

Bounds Checking

An array in C or C++ is not bounds checked, so attempts to access the sixth element of a 5-element array will appear to work - that is, no runtime error will occur. This means the programmer needs to code very carefully, especially considering the potential for buffer overflow attacks.
Java will bounds check arrays to prevent this from happening, of course with a little extra runtime cost.

Portability And Performance

C and C++ both compile to native machine code. This means that, with a good compiler, programs written in these languages will perform very well. However, it also restricts them to running on the platform they were compiled to run on.
Java generally compiles to Java bytecode, which then runs on top of a virtual machine (the JVM). The JVM has to turn instructions in the bytecode into instructions that are understood by the machine that the bytecode is running on. This gives a runtime performance penalty (although this is getting less significant as the JVM improves and computers get faster). However, now only the virtual machine (and standard library) have to be ported to different platforms, then the bytecode for many Java programs can be executed on that platform. So bytecode is portable accross different operating systems and processors.

Complex Data Types

There are two types of complex data types in C: structures and unions. C++ adds classes to this list. Java only implements one of these data types: classes.
A structure can be emulated by a class - simply write a class without any methods and make all the fields public. However, emulating a union is not always possible in Java, and the memory saving advantages unions hold in C may not carry accross. Java presents a simpler model but at the cost of not being able to save a little memory. For many applications this will be a non-issue.

Strings

C has no built-in string data type. The standard technique adopted among C programmers is that of using null-terminated arrays of characters to represent strings. This practice if often seen in C++ programs too.
Neither C++ or Java have string as a primitive type, but they do both have string objects that are a standard part of the language. In Java this type is called String, and in C++ it is called CString.

Multiple Inheritance

Multiple inheritance is a feature of some object oriented languages that allows you to derive a class from multiple parent classes. Although multiple inheritance is indeed powerful (and sometimes the logical way to define a class hierachy), it is complicated to use correctly and can create situations where it's uncertain which method will be executed. For example, if each of the parent classes provide a method X and the derived class does not, it is unclear which X should be invoked. It is also complicated to implement from the compiler perspective.

C++ supports multiple inheritance. Java provides no direct support for multiple inheritance, but you can implement functionality similar to multiple inheritance by using interfaces in Java. Java interfaces provide method descriptions but contain no implementations. Therefore implementations can only be inherited from one class, so there is no ambiguity over which method to invoke.

Operator Overloading

Operator overloading enables a class to define special behaviour for built-in operators when they are applied to objects of that class. For example, if the * (multiply) operator was to be used on two objects of type Matrix, then matrix multiplication could be implemented. This allows object types to feel much more tightly integrated into the language and can deliver much clearer code. However, sometimes it is not clear what a particular operator would sensibly do for a particular type, whereas a well-named method call would be clear.
Operator overloading is considered a prominent feature in C++. It is not supported in Java, probably in an effort to keep the language as simple as possible and help ensure it is obvious what code does, even though it may take longer to type and read.

Automatic Coercions

Automatic coercion refers to the implicit casting of data types that sometimes occurs in C and C++. For example, in C++ you can assign a float value to an int variable, which can result in a loss of information, although a compiler warning will be given about this. Java does not support C++ style automatic coercions. In Java, if coercion will result in a loss of data, you must always explicitly cast the data element to the new type.

Goto Statement

The goto statement is rarely used these days in C and C++, but it is a standard part of the language. The goto statement has historically been cited as the cause for messy, difficult to understand, and sometimes near impossible to predict code known as "spaghetti code." The primary bad usage of the goto statement has merely been as a convenience to substitute not thinking through an alternative, more structured branching technique. Very occasionally, it can lead to clearer code.
To avoid the potential for "spaghetti code", Java does not provide a goto statement. The Java language specifies goto as a keyword, but its usage is not supported. This is consistent with Java's desire to make programmers write clear, non-messy code.

Variadic Arguments

C and C++ let you declare functions, such as printf, that take a variable number of arguments. Although this is a convenient feature, it is impossible for the compiler to thoroughly type check the arguments, which means problems can arise at runtime without you knowing. Java doesn't support variable arguments at all, though if it did it would likely be able to handle subsequent runtime problems better than C or C++.

Command-line Arguments

The command-line arguments passed from the system into a Java program differ in a couple of ways from the command-line arguments passed into a C++ program. First, the number of parameters passed differs between the two languages.
In C and C++, the system passes two arguments to a program: argc and argv. argc specifies the number of arguments stored in argv. argv is a pointer to an array of characters containing the actual arguments. In Java, the system passes a single value to a program: args. ‘args’ is an array of Strings that contains the command-line arguments.

Prepare your project very well and basic C programs like factorial of number, prime number, Fibonacci series, reversing of no and string, sorting techniques, etc and you will be through in the technical round.