Вы находитесь на странице: 1из 12

Lexical

import java.io.*;
import java.util.*;
public class lexicar {
public static void main(String... args) throws IOException,FileNotFoundException{
FileReader f1 = new FileReader("C:\\Users\\Ankur\\sample.txt");
Scanner sc =new Scanner(f1);
String t1;
int i, t=1;
String kw[]={"void", "int"};
String id[]={"printf","main"};
String ter[]={"(",")",",",";","{","}"};
String op[]={"+","=","-","/","%","*"};
String lit[]={"a","b","c"};

System.out.println("line no. \t\t"+"token \t\t"+"type");


while(sc.hasNext())
{
t1=sc.next();
for(i=0;i<kw.length;i++)
{
if(kw[i].equals(t1))
System.out.println(t+" \t\t"+t1+" \t\tKeyword");
}
for(i=0;i<id.length;i++)
{

if(id[i].equals(t1))
System.out.println(t+" \t\t"+t1+" \t\tIdentifier");
}
for(i=0;i<ter.length;i++)
{
if(ter[i].equals(t1))
System.out.println(t+" \t\t"+t1+" \t\tTerminal");
}
for(i=0;i<op.length;i++)
{
if(op[i].equals(t1))
System.out.println(t+" \t\t"+t1+" \t\tOperator");
}
for(i=0;i<lit.length;i++)
{
if(lit[i].equals(t1))
System.out.println(t+" \t\t"+t1+" \t\t Literal");
}
if(t1.equals(")")|| t1.equals("{")|| t1.equals(";"))
t++;
}
}
}

Code optimization (Sub expression elimination)


import java.util.*;

import java.io.*;

public class comsub {


public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int n,i,j;
System.out.println("Enter number of expressions you want?");
n=sc.nextInt();
String sla[]= new String[n];
String brkr[]= new String[n];
String brkl[]= new String[n];
for(i=0;i<n;i++){
sla[i]=sc.next();
}
for(i=0;i<n;i++)
{
StringTokenizer stp= new StringTokenizer(sla[i],"=");
brkr[i]=stp.nextToken();
brkl[i]=stp.nextToken();
}
for(i=0;i<n;i++){
for(j=0;j<n;j++)
{
if(i!=j){
if(brkl[j].contains(brkl[i])){
String str= brkl[j];

String searchQuery=brkl[i];
String replaceWord = brkr[i];
int startIndexVal =0;
int endIndexVal=0;
startIndexVal=str.indexOf(searchQuery);
endIndexVal=startIndexVal+searchQuery.length();
str=str.substring(0,startIndexVal) + replaceWord +
str.substring(endIndexVal);
brkl[j]=str;
}
}
}
}
System.out.println("\n After common subexpression elimination");
for(i=0;i<n;i++)
{
System.out.println(brkr[i]+"="+brkl[i]);
}
}
}

FIRST()
import java.util.*;
import java.io.*;
public class FIRST
{

static char ntermnl[],termnl[];


static int ntlen,tlen;
static String grmr[][],fst[],flw[];
public static void main(String args[]) throws IOException
{
String nt,t;
int i,j,n;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the non-terminals");
nt=br.readLine();
ntlen=nt.length();
ntermnl=new char[ntlen];
ntermnl=nt.toCharArray();
System.out.println("Enter the terminals");
t=br.readLine();
tlen=t.length();
termnl=new char[tlen];
termnl=t.toCharArray();
System.out.println("Specify the grammar(Enter 9 for epsilon production)");
grmr=new String[ntlen][];
for(i=0;i<ntlen;i++)
{
System.out.println("Enter the number of productions for "+ntermnl[i]);
n=Integer.parseInt(br.readLine());
grmr[i]=new String[n];
System.out.println("Enter the productions");
for(j=0;j<n;j++)
grmr[i][j]=br.readLine();
}
fst=new String[ntlen];
for(i=0;i<ntlen;i++)
fst[i]=first(i);
System.out.println("First Set");
for(i=0;i<ntlen;i++)
System.out.println("FIRST("+ntermnl[i]+")=\t"+removeDuplicates(fst[i]));
}
static String first(int i)
{
int j,k,l=0,found=0;
String temp="",str="";
for(j=0;j<grmr[i].length;j++) //number of productions
{
for(k=0;k<grmr[i][j].length();k++,found=0) //when nonterminal has epsilon
production
{
for(l=0;l<ntlen;l++) //finding nonterminal
{
if(grmr[i][j].charAt(k)==ntermnl[l]) //for nonterminal in first set
{
str=first(l);

if(!(str.length()==1 && str.charAt(0)=='9'))


temp=temp+str;
found=1;
break;
}
}
if(found==1)
{
if(str.contains("9"))
continue;
}
else //if first set includes terminal
temp=temp+grmr[i][j].charAt(k);
break;
}
}
return temp;
}
static String removeDuplicates(String str)
{
int i;
char ch;
boolean seen[] = new boolean[256];
StringBuilder sb = new StringBuilder(seen.length);
for(i=0;i<str.length();i++)
{
ch=str.charAt(i);
if (!seen[ch])
{
seen[ch] = true;
sb.append(ch);
}
}
return sb.toString();
}
}
OUTPUT:
Enter the non-terminals
SABC
Enter the terminals
ab
Specify the grammar(Enter 9 for epsilon production)
Enter the number of productions for S
1
Enter the productions
aABC
Enter the number of productions for A
2

Enter the productions


a
bb
Enter the number of productions for B
2
Enter the productions
a
9
Enter the number of productions for C
2
Enter the productions
b
9
First Set
FIRST(S)= a
FIRST(A)= ab
FIRST(B)= a9
FIRST(C)= b9

Left Recursion
import java.io.*;
import java.util.*;
class leftrecursion
{
public static void main (String args[])
{
char non_terminal;
char beta,alpha;
String production;
int i=0;
String[] input=new String[6];
Scanner sn=new Scanner(System.in);
System.out.println("Enter 'END' to stop");
do
{
System.out.println("Enter production");
i++;
input[i]=sn.nextLine();
}
while(input[i].equalsIgnoreCase("End")==false);
int n=i;
System.out.println("GRAMMAR AFTER REMOVING LEFT RECURSION IS");
System.out.println("WHERE 9 IS EPSILON");
System.out.println();
for(i=1;i<n;i++)
{

int index=3;
production=input[i];
non_terminal=production.charAt(0);
if(non_terminal==production.charAt(index))
{
alpha=production.charAt(index+1);
while(production.charAt(index)!=0 &&
production.charAt(index)!='|')
index++;
if(production.charAt(index)!=0)
{
beta=production.charAt(index+1);
System.out.print(non_terminal+">"+beta+non_terminal+"\'\n");
System.out.print(non_terminal+"\'>"+alpha+beta+non_terminal+"\'|9\n");
}
else
System.out.println("Grammar can't be reduced\n");
}
else
System.out.println(production);
}
}
}
OUTPUT:
Enter 'END' to stop
Enter production
E->E+T|T
Enter production
T->T*F|F
Enter production
F->(i)
Enter production
END
GRAMMAR AFTER REMOVING LEFT RECURSION IS
WHERE 9 IS EPSILON
E->TE'
E'->+TE'|9
T->FT'
T'->*FT'|9
F->(i)
Process completed

NON RECURSIVE PARSER TABLE

import java.util.*;
import java.io.*;
class NRPT
{
static DataInputStream din=new DataInputStream(System.in);
static String grammar,rule[],var[][],list,NRPPT[][],nont="";
static boolean done[];
public static void main(String args[])
{
grammar=getInput("Enter grammar");
rule=grammar.split(",");
list=getInput("Enter list");
var=new String[list.length()][3];
done=new boolean[list.length()];
System.out.println();
int i=list.length()*10;
int j,flag=0;
char x;
for(i=0;i<grammar.length();i++)
{
for(j=0;j<nont.length();j++)
if(grammar.charAt(i)==nont.charAt(j))
flag=1;
if(isTerminal(grammar.charAt(i)) && flag==0)
nont+=grammar.charAt(i);
}
nont+='$';
System.out.println("NON TERMINALS are");
System.out.println(nont);
NRPPT=new String[list.length()][nont.length()];
for(i=0;i<var.length;i++)
{
done[i]=false;
char c=list.charAt(i);
var[i][0]=c+"";
var[i][1]=getInput("First("+c+")");
var[i][2]=getInput("Follow("+c+")");
}
for(i=0;i<var.length;i++)
first(var[i][0].charAt(0));
System.out.println("\nPARSER TABLE");
System.out.print(" ");
for(i=0;i<nont.length();i++)
System.out.print(nont.charAt(i)+" ");
System.out.println();
for(i=0;i<list.length();i++)
{
char ab=list.charAt(i);
System.out.print(ab+" ");
for(j=0;j<nont.length();j++)
System.out.print(NRPPT[i][j]+" ");

System.out.println();
}
}
static String getInput(String msg)
{
try
{
System.out.print(msg+"=");
return din.readLine();
}
catch(Exception e)
{
}
return "";
}
static boolean isDone(char c)
{
for(int i=0;i<var.length;i++)
{
if(var[i][0].equals(c+""))
{
boolean flag=done[i];
done[i]=true;
return flag;
}
}
return false;
}
static String follow(char c)
{
for(int i=0;i<var.length;i++)
{
if(var[i][0].equals(c+""))
return var[i][2];
}
return "";
}
static String first(char c)
{
if(!isNonTerminal(c))
return c+"";
boolean flag=isDone(c);
String first="";
for(int i=0;i<rule.length;i++)
{
if(rule[i].charAt(0)==c)
{
String s="";
for(int j=2;j<rule[i].length();j++)
{
char ch=rule[i].charAt(j);

s=first(ch);
if(!s.equals(""))
break;
}
first+=s+" ";
if(!flag)
addToParserTable(c,s,i);
}
}
return first.trim();
}
static void addToParserTable(char nt,String t,int n)
{
int j,k;
boolean null_found=false;
for(int i=0;i<t.length();i++)
{
char ch=t.charAt(i);
if(ch!=' ' && ch!='?')
{
for(k=0;k<list.length();k++)
{
if(nt==list.charAt(k))
{
for(j=0;j<nont.length();j++)
{
if(ch==nont.charAt(j))
NRPPT[k][j]=rule[n];
}
}
}
}
if(ch=='?')
null_found=true;
}
if(null_found)
addToParserTable(nt,follow(nt),n);
}
static boolean isTerminal(char c)
{
if(!isNonTerminal(c) && c!=',' && c!='=' && c!='?')
return true;
return false;
}
static boolean isNonTerminal(char c)
{
if('A'<=c && c<='Z')
return true;
return false;
}
}

OUTPUT
Enter grammar=E=TX,X=+TX,X=?,T=FY,Y=*FY,Y=?,F=(E),F=i
Enter list=EXTYF
NON TERMINALS are
+*()i$
First(E)=(i
Follow(E)=$(
First(X)=?+
Follow(X)=$)
First(T)=(i
Follow(T)=?*
First(Y)=?*
Follow(Y)=+$)
First(F)=(i
Follow(F)=*+$)
PARSER TABLE
+*()i$
E null null E=TX null E=TX null
X X=+TX null null X=? null X=?
T null null T=FY null T=FY null
Y Y=? Y=*FY null Y=? null Y=?
F null null F=(E) null F=i null
Process completed.

Вам также может понравиться