Sie sind auf Seite 1von 12

/*

Program to implement Comment or not in Java


*/
import java.util.*;
class comment
{
public static void main(String arg[])
{
Scanner sc=new Scanner(System.in);
String s;
s=sc.nextLine();
int l=s.length();
char c1=s.charAt(0);
char c2=s.charAt(1);
char c3=s.charAt(l-1);
char c4=s.charAt(l-2);
if((c1=='/' && c2=='/')||(c1=='/' && c2=='*' && c4=='*' && c3=='/'))
System.out.println("A comment");
else
System.out.println("Not a comment");
}
}

Sample Output
Input : Hgasfhgfhgs
OUT PUT:Not a comment

/*
Program to implement Recursive Descent Parser in Java

Grammar:
E -> x + T
T -> (E)
T -> x
*/

import java.util.*;

class RecursiveDescentParser {
static int ptr;
static char[] input;

public static void main(String args[]) {


System.out.println("Enter the input string:");
String s = new Scanner(System.in).nextLine();
input = s.toCharArray();
if(input.length < 2) {
System.out.println("The input string is invalid.");
System.exit(0);
}
ptr = 0;
boolean isValid = E();
if((isValid) & (ptr == input.length)) {
System.out.println("The input string is valid.");
} else {
System.out.println("The input string is invalid.");
}
}

static boolean E() {


// Check if 'ptr' to 'ptr+2' is 'x + T'
int fallback = ptr;
if(input[ptr++] != 'x') {
ptr = fallback;
return false;
}
if(input[ptr++] != '+') {
ptr = fallback;
return false;
}
if(T() == false) {
ptr = fallback;
return false;
}
return true;
}

static boolean T() {


// Check if 'ptr' to 'ptr+2' is '(E)' or if 'ptr' is 'x'
int fallback = ptr;
if(input[ptr] == 'x') {
ptr++;
return true;
}
else {
if(input[ptr++] != '(') {
ptr = fallback;
return false;
}
if(E() == false) {
ptr = fallback;
return false;
}
if(input[ptr++] != ')') {
ptr = fallback;
return false;
}
return true;
}
}
}

Sample Output
/*
Sample Inputs:
- xxx: invalid
- x+x+x: invalid
- x+(x+x): valid
- x+(x+(x+x)): valid
- xxx: invalid
- x+(x+x)+x: invalid
*/

/*
Program to implement Shift Reduce Parser in Java

*/
import java.io.*;

class ShiftReduceParser
{
public static void main(String[] args) throws Exception
{
int no, loc=0, i, j;
String str, stack="", temp;
String[][] productions;
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter no. of productions: ");
no = Integer.parseInt(in.readLine());
productions = new String[no][2];
System.out.println("Enter the productions:");
for (i=0; i < no; i++)
{
System.out.print("LHS for production "+(i+1)+": ");
productions[i][0] = in.readLine();
System.out.print("RHS for production "+(i+1)+": ");
productions[i][1] = in.readLine();
}
System.out.println("The productions are:");
for (i=0; i < no; i++)
{
System.out.println(productions[i][0]+" ->
"+productions[i][1]);
}
System.out.print("Enter a string: ");
str = in.readLine();
while (loc < str.length()-1)
{
temp = str.substring(loc, str.indexOf(' ', loc));
loc = str.indexOf(' ', loc)+1;
for (i=0; i < no; i++)
{
if (temp.equals(productions[i][1]))
{
temp = productions[i][0];
break;
}
}
stack = stack+temp;
System.out.println("Stack contents: "+stack);
for (i=0; i < no; i++)
{
if (stack.equals(productions[i][1]))
{
stack = productions[i][0];
break;
}
}
}
System.out.println("Stack contents: "+stack);
if (stack.equals(productions[0][0]))
System.out.println("Accepted.");
else
System.out.println("Rejected.");
}
}

Sample Output
> java ShiftReduceParser
Enter no. of productions: 2
Enter the productions:
LHS for production 1: E
RHS for production 1: E+E
LHS for production 2: E
RHS for production 2: id
The productions are:
E -> E+E
E -> id
Enter a string: id + id //id(space)+(space)id(space) - pgm will generate
error otherwise
Stack contents: E
Stack contents: E+
Stack contents: E+E
Stack contents: E
Accepted.

> java ShiftReduceParser


Enter no. of productions: 2
Enter the productions:
LHS for production 1: E
RHS for production 1: E+E
LHS for production 2: E
RHS for production 2: id
The productions are:
E -> E+E
E -> id
Enter a string: id * id //id(space)+(space)id(space) - pgm will generate
error otherwise
Stack contents: E
Stack contents: E*
Stack contents: E*E
Stack contents: E*E
Rejected.

/*
Program to Implement Common SubExpression Elimination in Java

*/
import java.io.*;
import java.util.*;

class Common_SubExpression
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(new
FileInputStream("input.txt"))); // Read Input file
PrintWriter pw=new PrintWriter(new FileOutputStream(new
File("output.txt")),true); // Prepare Output file

Vector L = new Vector();

String s;
Boolean flag=false;

while((s=br.readLine())!=null)
{
flag=false;
String r=s.substring(s.indexOf("=")+1); //Evaluate Right-
Hand Side of Expression

for(int i=0;i < L.size();i++)


{
if((L.elementAt(i)).equals(r))
flag=true; //If Expression already present
in Vector do nothing

}
if(!flag)
{
L.addElement(r); // If Expression not present in
Vector, Add it inside Vector and Print it to output file
pw.println(s);
}
}
}
}

Sample Input (input.txt)


t1 = -c
t2 = a + b
t3 = d + 5
t4 = a + b
t5 = -c

Sample Output (output.txt)


t1 = -c
t2 = a + b
t3 = d + 5

/*
Program to implement Three Address Code in Java

*/
import java.io.*;

class ThreeAddressCode
{
private static final char[][] precedence = {
{'/', '1'},
{'*', '1'},
{'+', '2'},
{'-', '2'}
};

private static int precedenceOf(String t)


{
char token = t.charAt(0);
for (int i=0; i < precedence.length; i++)
{
if (token == precedence[i][0])
{
return Integer.parseInt(precedence[i][1]+"");
}
}
return -1;
}

public static void main(String[] args) throws Exception


{
int i, j, opc=0;
char token;
boolean processed[];
String[][] operators = new String[10][2];
String expr="", temp;
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("\nEnter an expression: ");
expr = in.readLine();
processed = new boolean[expr.length()];
for (i=0; i < processed.length; i++)
{
processed[i] = false;
}
for (i=0; i < expr.length(); i++)
{
token = expr.charAt(i);
for (j=0; j < precedence.length; j++)
{
if (token==precedence[j][0])
{
operators[opc][0] = token+"";
operators[opc][1] = i+"";
opc++;
break;
}
}
}
System.out.println("\nOperators:\nOperator\tLocation");
for (i=0; i < opc; i++)
{
System.out.println(operators[i][0] + "\t\t" +
operators[i][1]);
}
//sort
for (i=opc-1; i >= 0; i--)
{
for (j=0; j < i; j++)
{
if (precedenceOf(operators[j][0]) >
precedenceOf(operators[j+1][0]))
{
temp = operators[j][0];
operators[j][0] = operators[j+1][0];
operators[j+1][0] = temp;
temp = operators[j][1];
operators[j][1] = operators[j+1][1];
operators[j+1][1] = temp;
}
}
}
System.out.println("\nOperators sorted in their
precedence:\nOperator\tLocation");
for (i=0; i < opc; i++)
{
System.out.println(operators[i][0] + "\t\t" +
operators[i][1]);
}
System.out.println();
for (i=0; i < opc; i++)
{
j = Integer.parseInt(operators[i][1]+"");
String op1="", op2="";
if (processed[j-1]==true)
{
if (precedenceOf(operators[i-1][0]) ==
precedenceOf(operators[i][0]))
{
op1 = "t"+i;
}
else
{
for (int x=0; x < opc; x++)
{
if ((j-2) ==
Integer.parseInt(operators[x][1]))
{
op1 = "t"+(x+1)+"";
}
}
}
}
else
{
op1 = expr.charAt(j-1)+"";
}
if (processed[j+1]==true)
{
for (int x=0; x < opc; x++)
{
if ((j+2) ==
Integer.parseInt(operators[x][1]))
{
op2 = "t"+(x+1)+"";
}
}
}
else
{
op2 = expr.charAt(j+1)+"";
}
System.out.println("t"+(i+1)+" = "+op1+operators[i][0]+op2);
processed[j] = processed[j-1] = processed[j+1] = true;
}
}
}

DOWNLOAD SOURCE CODE PROGRAM LIST

Sample Output

Das könnte Ihnen auch gefallen