0% found this document useful (0 votes)
41 views25 pages

Compiler p3

The program parses context-free grammar productions to generate parsing tables for a Simple Precedence Grammar (SPG). It accepts grammar productions as input, generates various parsing tables like FIRST, FOLLOW, <=, => relations, and checks if the grammar qualifies as an SPG by verifying the relations in the generated parsing tables adhere to SPG properties. If not an SPG, the program terminates after reporting the grammar is not simple precedence.

Uploaded by

Priya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views25 pages

Compiler p3

The program parses context-free grammar productions to generate parsing tables for a Simple Precedence Grammar (SPG). It accepts grammar productions as input, generates various parsing tables like FIRST, FOLLOW, <=, => relations, and checks if the grammar qualifies as an SPG by verifying the relations in the generated parsing tables adhere to SPG properties. If not an SPG, the program terminates after reporting the grammar is not simple precedence.

Uploaded by

Priya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

PRACTICAL 3

Program:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

public class SPMParse


{
String Buffersb;
String m[] = new String[10];
String s = "", str = "", str1 = "";
int gt = 0, lt = 0;
boolean bool = false;
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
int n, count = 0, ct = 0, flag = 0;
String prod[] = new String[20];
String rhs[] = new String[20];
String lhs[] = new String[20];
String tmp[] = new String[20];
String tmpnew[] = new String[20];
int first[][] = new int[20][20];
int last[][] = new int[20][20];
int equal[][] = new int[20][20];
int first_plus[][] = new int[20][20];
int first_star[][] = new int[20][20];
int last_plus[][] = new int[20][20];
int last_star[][] = new int[20][20];
int gtr[][] = new int[20][20];
int t_gtr[][] = new int[20][20];
int less[][] = new int[20][20];
int le[][] = new int[20][20];
int ge[][] = new int[20][20];
int spm[][] = new int[20][20];
String mat[] = new String[20];
String spm1[][] = new String[20][20];

public SPMParse()
{
try
{
System.out.println("Enter number of productions:");
n = Integer.parseInt(br.readLine());
for (int i = 0; i < n ; i++)
{
System.out.print("Enter" + (i + 1));
System.out.println(" Production (L.H.S = R.H.S): ");
prod[i] = br.readLine();
}
System.out.println("\nProductions entered are:");
for (int i = 0; i < n; i++)
{
System.out.println(prod[i]);
}
for (int i = 0; i < n; i++)
{
lhs[i] = "" + prod[i].charAt(0);
rhs[i] = prod[i].substring(2, prod[i].length());
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < prod[i].length(); j++)
{
tmp[ct] = "" + prod[i].charAt(j);
ct++;
}
}

tmpnew[0] = tmp[0];
for (int i = 0; i <ct; i++)
{
for (int j = 0; j < count; j++)
{
if (tmpnew[j].equals(tmp[i]))
{
flag = 1;
}
if (tmp[i].equals("="))
{
flag = 1;
}
}
if (flag == 0)
{
tmpnew[count] = tmp[i];
System.out.println(tmpnew[count]+count+" -------- count i------- "+tmp[i]+i);
count++;
}
else
{
flag = 0;
}
}
System.out.println();
System.out.println("Count : "+count);
for (int i = 0; i < count; i++)
{
for (int j = 0; j < count; j++)
{
first[i][j] = 0;
first_plus[i][j] = 0;
first_star[i][j] = 0;
last[i][j] = 0;
last_plus[i][j] = 0;
last_star[i][j] = 0;
equal[i][j] = 0;
gtr[i][j] = 0;
less[i][j] = 0;
spm[i][j] = 0;
spm1[i][j] = "0";
t_gtr[i][j] = 0;
le[i][j] = 0;
mat[i] = "0";
ge[i][j] = 0;
}
}
for (int i = 0; i < n; i++)
{
String p = rhs[i].charAt(0) + "";
String q = lhs[i].charAt(0) + "";
int row = 0, col = 0;
for (int j = 0; j < count; j++)
{
if (tmpnew[j].equals(p))
{
row = j;
}
if (tmpnew[j].equals(q))
{
col = j;
}
}
first[col][row] = 1;
first_plus[col][row] = 1;
first_star[col][row] = 1;
}
System.out.println("First Matrix:");
// display(first);
System.out.print(" \t");
for (int i = 0; i < count; i++)
{
System.out.print(tmpnew[i] + "\t");
}
for (int i = 0; i < count; i++)
{
System.out.print("\n " + tmpnew[i]);
for (int j = 0; j < count; j++)
{
System.out.print("\t" + first[i][j]);
}
}
System.out.println();
//lastmatrix
for (int i = 0; i < n; i++)
{
String p = rhs[i].substring(rhs[i].length() - 1);
String q = lhs[i].substring(lhs[i].length() - 1);
int row = 0, col = 0;
for (int j = 0; j < count; j++)
{
if (tmpnew[j].equals(p))
{
row = j;
}
if (tmpnew[j].equals(q))
{
col = j;
}
}
last[col][row] = 1;
last_plus[col][row] = 1;
last_star[col][row] = 1;
}

System.out.println("Last Matrix:");
//display(last);

System.out.print(" \t");
for (int i = 0; i < count; i++)
{
System.out.print(tmpnew[i] + "\t");
}
for (int i = 0; i < count; i++)
{
System.out.print("\n " + tmpnew[i]);
for (int j = 0; j < count; j++)
{
System.out.print("\t" + last[i][j]);
}
}
System.out.println();
//first plus
for (int i = 1; i < count; i++)
{
for (int j = 0; j < count; j++)
{
if (first_plus[j][i] == 1)
{
for (int k = 0; k < count; k++)
{
first_plus[j][k] = first_plus[j][k] | first_plus[i][k];
first_star[j][k] = first_plus[j][k] | first_plus[i][k];
}
}
}
}
System.out.println("First Plus:");
System.out.print(" \t");
for (int i = 0; i < count; i++)
{
System.out.print(tmpnew[i] + "\t");
}
for (int i = 0; i < count; i++)
{
System.out.print("\n " + tmpnew[i]);
for (int j = 0; j < count; j++)
{
System.out.print("\t" + first_plus[i][j]);
}
}
System.out.println();
//last plus
for (int i = 1; i < count; i++)
{
for (int j = 0; j < count; j++)
{
if (last_plus[j][i] == 1)
{
for (int k = 0; k < count; k++)
{
last_plus[j][k] = last_plus[j][k] | last_plus[i][k];
last_star[j][k] = last_plus[j][k] | last_plus[i][k];
}
}
}
}
System.out.println("Last Plus:");
System.out.print(" \t");
for (int i = 0; i < count; i++)
{
System.out.print(tmpnew[i] + "\t");
}
for (int i = 0; i < count; i++)
{
System.out.print("\n " + tmpnew[i]);
for (int j = 0; j < count; j++)
{
System.out.print("\t" + last_plus[i][j]);
}
}
System.out.println();
//first star
for (int i = 0; i < count; i++)
{
first_star[i][i] = 1;
}
System.out.println("First Star:");
System.out.print(" \t");
for (int i = 0; i < count; i++)
{
System.out.print(tmpnew[i] + "\t");
}
for (int i = 0; i < count; i++)
{
System.out.print("\n " + tmpnew[i]);
for (int j = 0; j < count; j++)
{
System.out.print("\t" + first_star[i][j]);
}
}
System.out.println();
//last star
for (int i = 0; i < count; i++)
{
last_star[i][i] = 1;
}
System.out.println("Last Star:");
System.out.print(" \t");
for (int i = 0; i < count; i++)
{
System.out.print(tmpnew[i] + "\t");
}
for (int i = 0; i < count; i++)
{
System.out.print("\n " + tmpnew[i]);
for (int j = 0; j < count; j++)
{
System.out.print("\t" + last_star[i][j]);
}
}
System.out.println();
//equal
for (int i = 0; i < n; i++)
{
for (int j = 0; j <rhs[i].length() - 1; j++)
{
int row = 0, col = 0;
String p = "" + rhs[i].charAt(j);
String q = "" + rhs[i].charAt(j + 1);
for (int k = 0; k < count; k++)
{
if (tmpnew[k].equals(p))
{
row = k;
}
if (tmpnew[k].equals(q))
{
col = k;
}
}
equal[row][col] = 1;
}
}
System.out.println("Equal:");
System.out.print(" \t");
for (int i = 0; i < count; i++)
{
System.out.print(tmpnew[i] + "\t");
}
for (int i = 0; i < count; i++)
{
System.out.print("\n " + tmpnew[i]);
for (int j = 0; j < count; j++)
{
System.out.print("\t" + equal[i][j]);
}
}
System.out.println();
//less than
int cnt = 0;
for (int i = 0; i < count; i++)
{
for (int j = 0; j < count; j++)
{
int sum = 0;
for (int k = 0; k < count; k++)
{
cnt = equal[i][k] * first_plus[k][j];
sum = sum + cnt;
}
less[i][j] = sum;
}
}
//display less than
System.out.println("Less than:"); System.out.print(" \t");
for (int i = 0; i < count; i++)
{
System.out.print(tmpnew[i] + "\t");
}
for (int i = 0; i < count; i++)
{
System.out.print("\n " + tmpnew[i]);
for (int j = 0; j < count; j++)
{
System.out.print("\t" + less[i][j]);
}
}
System.out.println();
//greater than
int sum = 0;
for (int i = 0; i < count; i++)
{
for (int j = 0; j < count; j++)
{
sum = 0;
for (int k = 0; k < count; k++)
{
cnt = last_plus[k][i] * equal[k][j];
sum = sum + cnt;
}
t_gtr[i][j] = sum;
}
}
for (int i = 0; i < count; i++)
{
for (int j = 0; j < count; j++)
{
sum = 0;
for (int k = 0; k < count; k++)
{
cnt = t_gtr[i][k] * first_star[k][j];
sum = sum + cnt;
}
gtr[i][j] = sum;
if (sum >= 1)
{
gtr[i][j] = 1;
}
}
}
System.out.println("Greater than:");
System.out.print(" \t");
for (int i = 0; i < count; i++)
{
System.out.print(tmpnew[i] + "\t");
}
for (int i = 0; i < count; i++)
{
System.out.print("\n " + tmpnew[i]);
for (int j = 0; j < count; j++)
{
System.out.print("\t" + gtr[i][j]);
}
}
System.out.println();
//spm
for (int i = 0; i < count; i++)
{
for (int j = 0; j < count; j++)
{
flag = 0;
if (less[i][j] == 1)
{
if (flag == 0)
{
spm1[i][j] = "<";
flag = 1;
}
else
{
System.out.println("Given grammar is not Simple Precedence Grammar.");
System.exit(0);
}
}
if (gtr[i][j] == 1)
{
if (flag == 0)
{
spm1[i][j] = ">";
flag = 1;
}
else
{
System.out.println("\n\n\nGiven grammar is not Simple Precedence Grammar.");
System.exit(0);
}
}
if (equal[i][j] == 1)
{
if (flag == 0)
{
spm1[i][j] = "=";
flag = 1;
}
else
{
System.out.println("Given grammar is not Simple Precedence Grammar.");
System.exit(0);
}
}
}
}
System.out.println("SPM:");
System.out.print(" \t");
for (int i = 0; i < count; i++)
{
System.out.print(tmpnew[i] + "\t");
}
for (int i = 0; i < count; i++)
{
System.out.print("\n " + tmpnew[i]);
for (int j = 0; j < count; j++)
{
System.out.print("\t" + spm1[i][j]);
}
}
System.out.println();
}
catch(Exception ex)
{
ex.printStackTrace();
}

s = "" + prod[0].charAt(0);System.out.println("Enter string to be parse");


try
{
str1 = br.readLine();
}
catch (Exception ex)
{
ex.printStackTrace();
}
str = "#" + str1 + "#";
StringBuffer sb = new StringBuffer(str);
for (int i = 0; i < count; i++)
{
for (int j = 0; j < count; j++)
{
mat[i] = spm1[i][j];
}
}
do
{
parsing();
if (bool == false)
{
System.out.println("\nThe Handle is not found");
System.out.println("\nConclusion : The given string" + " " + str1 + " " + "is not parsable");
break;
}
if (bool == true)
{
parsing();
}
}
while (!(str.equals("#" + s + "#")));
if (str.substring(1, 2).equals(s))
{
System.out.println("\nConclusion :" + "'" + s + "=start symbol' therefore, the given string" + " " +
str1 + " " + " is parsable");
}
}
public void parsing()
{
String temp = "";
StringBuffer sb = new StringBuffer(str);
int c = 3; //to count the position where we want to insert the <,>,=,symbol.
if (str.length() != 3)
{
sb.insert(1, "<");
}
String str2 = "";
int row[] = new int[10];
int col[] = new int[10]; //String all the symbols i.e<,>,=.
for (int i = 2; i <sb.length() - 1; i++)
{
for (int j = 0; j < count; j++)
{
if (sb.substring(i, i + 1).equals(tmpnew[j]))
{
row[i] = j;
}
}
}
for (int i = 2; i <sb.length() - 1; i++)
{
for (int j = 0; j < count; j++)
{
if (sb.substring(i + 1, i + 2).equals(tmpnew[j]))
{
col[i] = j;
}
}
}
for (int i = 2; i <sb.length() - 1; i++)
{
if (spm1[row[i]][col[i]] != "0")
{
temp += spm1[row[i]][col[i]];
}
}
if (temp.indexOf(">") != -1)
{
for (int r = 0; r <= temp.indexOf(">"); r++)
{
sb.insert(c, temp.charAt(r));
c = c + 2;
}
gt = c - 2;//indicates the position of greater than symbol.
}
else
{
gt = sb.length();
}
for (int i = c - 2; i > 0; i--)
{
if (sb.substring(i - 1, i).equals("<"))
{
lt = i; //position of less than symbol.
break;
}
else
lt = 2;
}
//}
//inserting > at last position.
if (gt == sb.length() &str.length() != 3)
{
for (int r = 0; r <temp.length(); r++)
{
sb.insert(c, temp.charAt(r));
c = c + 2;
}
gt = c;
sb.insert(sb.length() - 1, ">");
}
System.out.println("Relations:" + sb);
System.out.println("lt= " + lt + "and gt= " + gt);
//To remove the '=' sign from handle
if (sb.substring(lt, gt).length() != 1)
{
for (int j = lt; j <gt; j++)
{
if (!sb.substring(j, j + 1).equals("="))
{
str2 += sb.substring(j, j + 1);
}
}
}
System.out.println("Relations:" + str2);
for (int i = 0; i <prod.length; i++)
{
if (sb.substring(lt, gt).length() == 1)
{
if (sb.substring(lt, gt).equals(rhs[i]))
{
bool = true;
System.out.println("\nHandle : " + sb.substring(lt, gt));
str = str.replaceFirst(sb.substring(lt, gt), lhs[i]);
System.out.println("Production : " + prod[i]);
System.out.println("New String : " + str);
break;
}
}
else
{
try
{
if (str2.equals(prod[i].substring(2, prod[i].length())))
{
bool = true;
System.out.println("\nHandle : " + str2);
str = str.replace(str2, prod[i].substring(0, 1));
System.out.println("Production : " + prod[i]);
System.out.println("New String : " + str);
break;
}
}
catch (Exception e)
{

}
}
bool = false;
}
}
public static void main(String args[])
{
SPMParse sp = new SPMParse();
}
}
Output:

You might also like