import java.util.*;
import java.io.*;
class LexicalAnalyzer
{
public static void main(String args[]) throws Exception
{
int kc=0,oc=0,sc=0,dc=0;
File f=new File("input.txt");
FileReader fi=new FileReader(f);
BufferedReader br=new BufferedReader(fi);
String temp;
String words[];
while((temp=br.readLine())!=null)
{
//temp=br.readLine();
words=temp.split("[\\s ,;\\(\\)\\{\\}]"); //delimiters are whitspace characters , ; ( ) { }
//For reference on regular expression for split method see
// http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html#sum
dc+=words.length-1;
for(int i=0;i<words.length;i++)
if(isKeyword(words[i])==true)
kc++;
else if(isOperator(words[i])==true)
oc++;
else
sc++;
}
System.out.println("Keywords:"+kc);
System.out.println("Operators:"+oc);
System.out.println("Symbols:"+sc);
System.out.println("Delimiters:"+dc);
}
//implement boolean returning methods like these below to detect anything
public static boolean isKeyword(String x)
{
String keywords[]={"auto","break","case","char","const","continue","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"};
for(int i=0;i<keywords.length;i++)
if(x.equals(keywords[i])==true)
return true;
return false;
}
public static boolean isOperator(String x)
{
String operator[]={"--","++","~","+","-","*","/","^","&","%","!","<",">","|","="};
for(int i=0;i<operator.length;i++)
if(x.equals(operator[i])==true)
return true;
return false;
}
}
------------------------------------------------------------------
input.txt
-----------------
int main ()
{
int a,b,c;
float x,y;
a = a + b;
}
----------------
Output:
----------------
Keywords:3
Operators:2
Symbols:9
Delimiters:8
--------------

No comments:
Post a Comment