Posts Tagged ‘lexical analyzer program in java’

Note:

All files lexanly.java , input.txt , func.txt (function table) , op.txt (operator table) , spsy.txt (special symbol table) , ky.txt (keyword table) must be in the same folder.

Output file will be saved in the same folder on execution of the program.

Identifier Table will be printed on the console.

Solution:

import java.util.*;
import java.io.*;
class lexanly
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter file name for input”);
String filename=sc.nextLine();
String line=null;
String function_table=”func.txt”;
String linef=null;
String keyword_table=”ky.txt”;
String linek=null;
char s[]=new char[100]; //assuming line of code has maximum of 100 charcters
int i,j,k,found,id=0,match=0;
int var[]=new int[2];
String idf[]=new String[50]; //assuming 50 identifiers in the program
try
{
FileReader fr=new FileReader(filename);
BufferedReader br=new BufferedReader(fr);
File file = new File(“output.txt”);
if (!file.exists())
file.createNewFile();
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
while((line=br.readLine())!=null)
{
s=line.toCharArray();
for(i=0,j=0;i<line.length();i++)
{
found=0;
var=spopsy(s[i]);
if(var[0]!=0 || s[i]==’ ‘) //special symbol,operator and space are separators
{
FileReader frf=new FileReader(function_table); //opening all files during every traversal
BufferedReader brf=new BufferedReader(frf);
FileReader frk=new FileReader(keyword_table);
BufferedReader brk=new BufferedReader(frk);
for(k=0;(linef=brf.readLine())!=null && found==0;k++) //search for functions
if(linef.equals(line.substring(j,i)))
{
bw.write(“Func#”+(k+1)+” “);
found=1;
}
for(k=0;(linek=brk.readLine())!=null && found==0;k++) //search for keywords
if(linek.equals(line.substring(j,i)))
{
bw.write(“Keyword#”+(k+1)+” “);
found=1;
}
if(found==0 && id==0 && s[i]!=’ ‘) //space is not an identifier
{
idf[0]=line.substring(j,i); //creating identifier table
id++;
}
for(k=0;k<id && found==0 && s[i]!=’ ‘;k++) //search for identifier history
{
if(idf[k].equals(line.substring(j,i)))
{
match=1;
break;
}
else
match=0;
}
if(match==0 && found==0 && s[i]!=’ ‘ && j!=i) //j!=i inorder to avoid null substring
{
idf[id]=line.substring(j,i); //add identifier only if it does not exist
id++;
}
if(found==0 && s[i]!=’ ‘ && j!=i)
{
bw.write(“Identifier#”+(k+1)+” “); //adding identifiers in identifier table
found=1;
}
if(var[0]==1)
bw.write(“SpecialSymbol#”+var[1]+” “);
if(var[0]==2)
bw.write(“Operator#”+var[1]+” “);
j=i+1; //marks the beginning of a function,keyword or identifier
brf.close();
brk.close();
}
}
bw.newLine(); //writing on the next line of the file
}
br.close();
bw.close();
}
catch(FileNotFoundException ex)
{
System.out.println(“Unable to find file “);
}
catch(IOException e)
{
e.printStackTrace();
}
System.out.println(“Identifier table”);
for(k=0;k<id;k++)
System.out.println(idf[k]);
System.out.println(“Output printed in the file named output.txt”);
}
static int[] spopsy(char c)
{
String sp_symbol_table=”spsy.txt”;
String linessy=null;
String operator_table=”op.txt”;
String lineo=null;
int found=0,i,k;
int var[]=new int[2];
try
{
FileReader frs=new FileReader(sp_symbol_table);
BufferedReader brs=new BufferedReader(frs);
FileReader fro=new FileReader(operator_table);
BufferedReader bro=new BufferedReader(fro);
for(k=0;(linessy=brs.readLine())!=null && found==0;k++)
{
if(linessy.equals(Character.toString(c)))
{
var[0]=1;
var[1]=k+1;
found=1;
}
}
brs.close();
for(k=0;(lineo=bro.readLine())!=null && found==0;k++)
if(lineo.equals(Character.toString(c)))
{
var[0]=2;
var[1]=k+1;
found=1;
}
bro.close();
}
catch(FileNotFoundException ex)
{
System.out.println(“Unable to find file “);
}
catch(IOException ex)
{
System.out.println(“Error reading file “);
}
return var;
}
}

 

op.txt

+

/
%
=

spsy.txt

{
}
;
,

func.txt

main()
clrscr()
getch()

ky.txt

void
for
while
do
int
static
float
if
else

input.txt

void main(){
int a,b,sum,diff;
clrscr();
sum=a+b;
diff=a-b;
getch();
}

Console

lexop

output.txt

Keyword#1 Func#1 SpecialSymbol#1
Keyword#5 Identifier#1 SpecialSymbol#4 Identifier#2 SpecialSymbol#4 Identifier#3 SpecialSymbol#4 Identifier#4 SpecialSymbol#3
Func#2 SpecialSymbol#3
Identifier#3 Operator#5 Identifier#1 Operator#1 Identifier#2 SpecialSymbol#3
Identifier#4 Operator#5 Identifier#1 Operator#2 Identifier#2 SpecialSymbol#3
Func#3 SpecialSymbol#3
SpecialSymbol#2