Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 < A4 . cup > javac A4 . lex . java A4Parser . java A4Symbol . java A4User . java > java A4User > javac A4 . 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 inputFile = 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. 1 4 MARKING SCHEME 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 dont 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. 4 Marking scheme yourMark =0; if ( A4 . lex and A4 . cup files not submitted or file names are incorrect ) return ; if ( A4 . lex . java && A4Parser . java && A4Symbol . java are generated and compiled correctly ) { for ( each of the 10 A4 . tiny files ) { if ( A4 . tiny is valid and it is a correct program && A4 . java is generated && A4 . output contains correct answer ) // suppose A4. tiny has a write statement that write the result to the file A4. output . yourMark +=0.8; if ( A4 . tiny is valid program wrt the grammar but not a correct program ( such as missing variable declaration ) && A4 . java is generated && A4 . java has compilation error ) yourMark +=0.8; if ( A4 . tiny is not a valid Tiny program && A4 . java is not generated ) yourMark +=0.8; } } if ( your submission is late within 24 hours ) yourMark = yourMark -2; if ( your submission is late more than 24 hours ) yourMark =0; if ( your code length is among the top 6) // code length is the number of tokens , not the number of words that were used in previous assignments , yourMark +=1;

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

Moving Objects Databases

Authors: Ralf Hartmut Güting, Markus Schneider

1st Edition

0120887991, 978-0120887996

More Books

Students also viewed these Databases questions