WHAT'S NEW?
Loading...

Lexical Analyzer in C

/*  
      Lexical Analyzer for 'C' Language  
      Author: Yasar Shaikh  
      Lexical Analyzer: http://en.wikipedia.org/wiki/Lexical_analysis 
*/  
#include<stdio.h>  
#include<string.h>  
#include<ctype.h>  
#define MAX 1000  
int main()  
{  
     //FILE pointer for getting input file i for general index, 
     //flag for var identification  
     FILE *fp;  
     int keyIndex,preIndex,varIndex,oprIndex,i,flag;  
     char keyStore[MAX][10],preStore[MAX][10],varStore[MAX][10];  
     char oprStore[MAX],fname[80],ch,temp[80];  
     char keyword[32][20]={  
                            "auto","break","case","continue",
                            "char","const","default","do",
                            "double","else","enum","extern",
                            "float","for","goto","if",
                            "int","long","register","return",
                            "short","signed","sizeof","static",
                            "struct","switch","typedef","union",
                            "unsigned","void","volatile","while"  
                          };  
     char preprocessor[2][20]={"include","define"};  

     printf("\nEnter file name:");  
     scanf("%s",fname);  
     //opening a file in Read mode n checking for its availabilty  
     fp=fopen(fname,"r");  
     if(NULL == fp)  
     {  
          printf("\nFile not found.");  
          return 0;  
     }  

     keyIndex=varIndex=preIndex=oprIndex=0;  

     //Do operation until EOF is reached.  
     while(!feof(fp))  
     {  
          ch=fgetc(fp);  
          if(!isalnum(ch))  
               oprStore[oprIndex++]=ch;  
          else  
          {       
               //getting string  
               i=flag=0;  
               temp[i]=ch;  
               i++;  
               temp[i]=fgetc(fp);  
               while( isalnum(temp[i]) )  
                    temp[++i]=fgetc(fp);  
               temp[i]='\0';  

               fseek(fp, SEEK_CUR-2 , SEEK_CUR);  

               //checking is it a keyword/var/preprocessor  
               for(i=0;i<32;i++)  
               {  
                    //keyword checking n storing  
                    if(strcmp(temp,keyword[i]) ==0)  
                    {  
                         strcpy(keyStore[keyIndex++],temp);  
                         flag=1;  
                         break;  
                    }  
                    
                    //preprocessor checking n storing  
                    else if(strcmp(temp,preprocessor[i])==0)  
                    {  
                         strcpy(preStore[preIndex++],temp);  
                         flag=1;  
                         break;  
                    }  
               }   

               //checking for variables  
               if(!flag)  
               {  
                    strcpy(varStore[varIndex++],temp);  
                    strcpy(temp,"");  
               }  
          }   
     }  

     printf("\n******* OUTPUT *******");  
     printf("\n\nPreprocessor's List");  
     for(i=0;i<preIndex;i++)  
          printf("\n%s",preStore[i]);  

     printf("\n\nKeyword List");  
     for(i=0;i<keyIndex;i++)  
          printf("\n%s",keyStore[i]);  

     printf("\n\nVariable List");  
     for(i=0;i<varIndex;i++)  
          printf("\n%s",varStore[i]);  

     printf("\n\nOperators List");  
     for(i=0;i<oprIndex;i++)  
          printf("\n%c",oprStore[i]);       

     return 0;  
}  

/*  
  PS; You can even seperate standard header files, 
      predefined identifiers.  
      The above program mainly focus on important tokens.   
*/  

Layout Demo program in Java

 /*  
   Title : Program for demonstrating Layouts in Java  
   Author: Yasar Shaikh  
 */  
 import java.awt.*;  
 import java.awt.event.*;  
 import java.applet.*;  
 public class LayoutDemo extends Frame implements ActionListener  
 {  
      Button b1,b2,b3,b4,b5,b6;  
      Button l1,l2,l3;  
      CheckboxGroup cbg;  
      Checkbox c1,c3,c2;  
      Panel p1,p2,main;  
      LayoutDemo l;  
      public LayoutDemo()  
      {  
           p1=new Panel();  
           p2=new Panel();  
           main=new Panel();  
           l=this;  
           p1.setLayout(new FlowLayout());  
           p2.setLayout(new GridLayout());  
           cbg=new CheckboxGroup();  
           c1=new Checkbox("Left",cbg,false);  
           c2=new Checkbox("Center",cbg,false);  
           c3=new Checkbox("Right",cbg,false);  
           l1=new Button("Flow");  
           l2=new Button("Grid");  
           l3=new Button("Border");  
           b1=new Button("Button 1");  
           b2=new Button("Button 2");  
           b3=new Button("Button 3");  
           b4=new Button("Button 4");  
           b5=new Button("Button 5");  
           b6=new Button("Button 6");  
           p1.add(b1);          p1.add(b2);  
           p1.add(b3);          p1.add(b4);  
           p1.add(b5);          p1.add(b6);  
           p1.add(c1);p1.add(c2);p1.add(c3);  
           p2.add(l1);p2.add(l2);p2.add(l3);  
           l1.addActionListener(this);  
           l2.addActionListener(this);  
           l3.addActionListener(this);  
           main.setLayout(new BorderLayout());  
           main.add(p1,BorderLayout.NORTH);  
           main.add(p2,BorderLayout.SOUTH);  
           add(main);  
      }  
      public void actionPerformed(ActionEvent ae)  
      {  
           if(ae.getSource()==l2)  
           {  
                l.setSize(700,700);  
                repaint();  
                p1.setLayout(new GridLayout(3,3));  
           }  
           else if(ae.getSource()==l3)  
           {  
                l.setSize(800,800);  
                repaint();  
                main.add(p1,BorderLayout.WEST);  
           }  
           else  
           {  
                l.setSize(600,600);  
                repaint();  
                p1.setLayout(new FlowLayout());  
           }  
      }  
      public static void main(String ar[])  
      {  
           LayoutDemo l=new LayoutDemo();  
           l.setSize(600,600);  
           l.setVisible(true);  
      }  
 }  
 /*  
 PS: Save the above program with the name LayoutDemo.java (case-sensitive)  
     and then compile it with "javac LayoutDemo.java" command and execute  
     it with "java LayoutDemo" command in command prompt/terminal.   
 */