Question
Compiler Design(Dragon Book) Help me write a java code: 1)Read quadruples from txt file ; 2)Change the quadruples into algrithm expressions; 3)Compute the final result.
Compiler Design(Dragon Book)
Help me write a java code:
1)Read quadruples from txt file ;
2)Change the quadruples into algrithm expressions;
3)Compute the final result.
Don't copy and paste my own code and answer!!! Don't waste my time!
Quadruple:
1(=,1,/,x) 2(=,2,/,y) 3(+,x,y,T1) 4(=,T1,/,z)
Code I have done so far(but I didn't finish? a lot problems exist, please help me, deadline is tomorrow :-(.)
package compiler;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Compute {
private static boolean isOperator(char ch) {
boolean flag = false;
if (ch == '+'|| ch == '-'|| ch == '*'|| ch == '/')
flag = true;
return flag;
}
public static int[] Compute() throws IOException{
File file = new File("C:/Users/dell-pc/Workspaces/MyEclipse 2016 CI/C-Compiler/output/Quadruple.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String s = null;
char[] expr = new char[100];
char[] temp = new char[10];
int[] val = new int[10];
int x = 0, y = 0;
while((s = br.readLine())!= null)// (op, arg1, arg2, result)
{
//System.out.println(s);
for(int i=2;i if(s.charAt(1)=='('&&s.charAt(i)!=')'&&s.charAt(i)!='#'){//&&s.charAt(i)!=',' if(Character.isLetter(s.charAt(i))&&Character.isDigit(s.charAt(i+1))){ expr[i] = s.charAt(i); //expr[i+1] = s.charAt(i+1); } expr[i] = s.charAt(i); if(s.charAt(i)==' ') expr[i+1] = ';'; expr[i+1] = ','; } } for(int i=0;i if(expr[i]!='\0') System.out.print(expr[i]); System.out.println(); for(int i=0; i for(int j=0; j<8; j++){ int k = 0; //if(expr[i]!=',') if(expr[i]=='='){ if(Character.isDigit(expr[i])) val[k] = expr[i]; if(Character.isLetter(expr[i])) temp[k] = expr[i]; //temp[i] = val; k++; } } } for(int i=0; i System.out.print(temp[i]); for(int i=0; i System.out.print(val[i]); for(int i=0; i for(int j=0; j<8; j++){ if(isOperator(expr[i])){ if(expr[i]=='+') val[i] = expr[i+2] + expr[i+4]; if(expr[i]=='-') val[i] = expr[i+2] - expr[i+4]; if(expr[i]=='*') val[i] = expr[i+2] * expr[i+4]; if(expr[i]=='/') val[i] = expr[i+2] / expr[i+4]; } } } } br.close(); return val; } public static void main(String args[]) throws Exception { Compute(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started