Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Translating TINY programs into Java You will use JavaCup to translate Tiny programs to Java programs. The translated Java program will be able to run

Translating TINY programs into Java

image text in transcribed

image text in transcribed

You will use JavaCup to translate Tiny programs to Java programs. The translated Java program will be able to run and produce correct result. You need to write a JavaCUP file and a JLex file so that a translator for Tiny language can be generated. We will run the following commands to generate the translator. You need to modify the lex file and cup file that you created in assignment 3. >del *. class A4.java A4.output >java JLex.Main A4.lex >java java_cup.Main -parser A4Parser -symbols A4Symbol javac A4.lex.java A4Parser.java A4Symbol.java A4User.java >java A4User >javac 14.java >java A4 >more A4.output A4.lex and A4.cup files are what you are going to write. By running those commands, you will generate the scanner A4Scanner and the parser (or the translator) A4Parser. A4User is listed as below. It invokes the parser and generate the translated java program called A4.java. After A4.java is generated, you compile and run it. A4.class will produce a file called A4.output, which holds the correct result. import java.io.*; import java_cup.runtime. Symbol; class A4User { public static void main(String[] args) throws Exception { File input File = new File ("A4.tiny"); A4Parser parser= new A4Parser(new A4Scanner(new FileInputStream(inputFile))); String javaProgram =(String) parser.debug-parse().value; FileWriter fw=new FileWriter(new File("A4.java")); fw.write(javaProgram); fw.close(); } } Here is the last one of the nine test cases in our assignment submission site. Note that the language definition is extended by adding STRING assignment as follows: AssignStmt -> Id :- Expression ';' | Id := QString ';' Even if your program passed all the test cases in A3, it may fail our new test cases. For example, INT main f) BEGIN STRING X:="test"; @#$ END is not a valid Tiny program but YOUR parser may think that it is. To catch the error, your lexer need to return some error token when it sees any OTHER token that is not listed. If your lex and cup files are correct, all of those commands, especially A4User, will run smoothly without any error report. An A4.java program will be generated. A4.java will be compiled without error and the result of A4.java will be written into A4.output. If your programs are not correct, during the process there will be some error messages. Here are a sample A4.tiny file and the corresponding generated A4.java file. The corresponding A4.output file consists of the result f running A4.java. Note that you can generate your own A4.java as long as A4.java can produce the correct result. In this java program A4.java, just the same as in Tiny program A4.tiny, it reads integers from A41.input and A42.input. Note that file names such as A41.input, A42.input and A4.output are obtained from the Tiny program A4.input. You should not hard code those file names into your javaCup or JLex files. The main method in Tiny program will be translated into a main method in Java program. We assume that there will be only one main method in a Tiny program. Note that you don't need to write any Java programs. The parser and the scanner are generated from your cup and lex specifications. Also, we will test your program on our own data. 3 What to submit You need to turn in 2 files: the JLex file, named A4.lex, which can be used to generate the scanner; the javaCUP file, named A4.cup, which can be used to generate the translator. You will use JavaCup to translate Tiny programs to Java programs. The translated Java program will be able to run and produce correct result. You need to write a JavaCUP file and a JLex file so that a translator for Tiny language can be generated. We will run the following commands to generate the translator. You need to modify the lex file and cup file that you created in assignment 3. >del *. class A4.java A4.output >java JLex.Main A4.lex >java java_cup.Main -parser A4Parser -symbols A4Symbol javac A4.lex.java A4Parser.java A4Symbol.java A4User.java >java A4User >javac 14.java >java A4 >more A4.output A4.lex and A4.cup files are what you are going to write. By running those commands, you will generate the scanner A4Scanner and the parser (or the translator) A4Parser. A4User is listed as below. It invokes the parser and generate the translated java program called A4.java. After A4.java is generated, you compile and run it. A4.class will produce a file called A4.output, which holds the correct result. import java.io.*; import java_cup.runtime. Symbol; class A4User { public static void main(String[] args) throws Exception { File input File = new File ("A4.tiny"); A4Parser parser= new A4Parser(new A4Scanner(new FileInputStream(inputFile))); String javaProgram =(String) parser.debug-parse().value; FileWriter fw=new FileWriter(new File("A4.java")); fw.write(javaProgram); fw.close(); } } Here is the last one of the nine test cases in our assignment submission site. Note that the language definition is extended by adding STRING assignment as follows: AssignStmt -> Id :- Expression ';' | Id := QString ';' Even if your program passed all the test cases in A3, it may fail our new test cases. For example, INT main f) BEGIN STRING X:="test"; @#$ END is not a valid Tiny program but YOUR parser may think that it is. To catch the error, your lexer need to return some error token when it sees any OTHER token that is not listed. If your lex and cup files are correct, all of those commands, especially A4User, will run smoothly without any error report. An A4.java program will be generated. A4.java will be compiled without error and the result of A4.java will be written into A4.output. If your programs are not correct, during the process there will be some error messages. Here are a sample A4.tiny file and the corresponding generated A4.java file. The corresponding A4.output file consists of the result f running A4.java. Note that you can generate your own A4.java as long as A4.java can produce the correct result. In this java program A4.java, just the same as in Tiny program A4.tiny, it reads integers from A41.input and A42.input. Note that file names such as A41.input, A42.input and A4.output are obtained from the Tiny program A4.input. You should not hard code those file names into your javaCup or JLex files. The main method in Tiny program will be translated into a main method in Java program. We assume that there will be only one main method in a Tiny program. Note that you don't need to write any Java programs. The parser and the scanner are generated from your cup and lex specifications. Also, we will test your program on our own data. 3 What to submit You need to turn in 2 files: the JLex file, named A4.lex, which can be used to generate the scanner; the javaCUP file, named A4.cup, which can be used to generate the translator

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

LO2 Identify components of workflow analysis.

Answered: 1 week ago