Coursera Programming Assignment: Project 7 For the kind attn. of Mark Armbrust

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

Coursera Programming Assignment: Project 7 For the kind attn. of Mark Armbrust

Anantharamaiah Prabhakar
While I passed this Assignment with 80% marks, I lost 20% marks with the following graded output:-

" (-20) - Problems while executing the translated StaticTest.asm program - end of line expected, '/' is found"

When I ran my VMTranslator on StaticTest.asm, it appeared to run normally, and I did not see any '/'. Could you please clarify ?StaticTest.asm
StaticTest.asm
Reply | Threaded
Open this post in threaded view
|

Re: Coursera Programming Assignment: Project 7 For the kind attn. of Mark Armbrust

cadet1620
Administrator
What language did you use to write your VM translator and what OS are you running on?

Yout .asm file has mixed line endings.  Some lines end with \n (linux/mac style), and some with \r\n (windows s)

I suspect that the mixed line endings are causing the parser to get out of sync and loosing a newline before one of the comments.



--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Coursera Programming Assignment: Project 7 For the kind attn. of Mark Armbrust

Anantharamaiah Prabhakar
Hello Mark,

I wrote the translator in Java, and I am doing everything on a Windows10 laptop. I have used "\n" extensively in the file HackCodeGenerator.java. Please see the attached zip file containing the full source code. In case that causes the problem, is there any alternative ? Shall greatly appreciate your advice. However, when I ran the .asm file using the CPUEmulator in the Nand2Tetris software suite, it worked fine.

Anantharamaiah Prabhakar

On Tue, May 15, 2018 at 6:46 PM, cadet1620 [via Nand2Tetris Questions and Answers Forum] <[hidden email]> wrote:
What language did you use to write your VM translator and what OS are you running on?

Yout .asm file has mixed line endings.  Some lines end with \n (linux/mac style), and some with \r\n (windows s)

I suspect that the mixed line endings are causing the parser to get out of sync and loosing a newline before one of the comments.



--Mark



To unsubscribe from Coursera Programming Assignment: Project 7 For the kind attn. of Mark Armbrust, click here.
NAML


project7.zip (2K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Coursera Programming Assignment: Project 7 For the kind attn. of Mark Armbrust

cadet1620
Administrator
Platform independent new-line handling is often problematic, but using \n is usually OK.

I'm not fluent in Java and don't have an easy way to build your code and experiment with it.  It is odd that on Windows (my platform of choice, too) that writing strings with \n in them are not being translated to the platform's new-line, but the "bw.newLine()" is writing a platform specific new-line.

The Coursera grader is running on Linux and I would expect \n and newLine() to write identically.

There's one thing I see that you can try:
There are 4 code generating lines that do not end in \n
java:81:  myout = "//" + line + "\n@" + fname + "." + cons + "\nD=M\n@SP\nA=M\nM=D\n@SP\nM=M+1";
java:89:  myout = "//" + line + "\n@" + cons2 + "\nD=M\n@SP\nA=M\nM=D\n@SP\nM=M+1";
java:131: myout = "//" + line + "\n@SP\nA=M-1\nD=M\n@SP\nM=M-1\n@" + fname + "." + cons + "\nM=D";
java:139: myout = "//" + line + "\n@SP\nM=M-1\n@SP\nA=M\nD=M\n@" + cons2 + "\nM=D";
These lines are associated with push/pop static/pointer
Since the problem showed up in StaticTest, try adding the \n to the ends of those 4 lines so that they will be identically formatted to the code written for the earlier passing tests.

(This might be a good excuse to try to get Java SDK installed on my Linux laptop and see if I can build your code...  Something to do when I get done with work.)
Reply | Threaded
Open this post in threaded view
|

Re: Coursera Programming Assignment: Project 7 For the kind attn. of Mark Armbrust

xedover
the problem is java.io.BufferedWriter.newLine() is OS dependent. It writes \r\n on Windows, but \n on Linux/Mac

Your code writes \n everywhere except when you call bw.newLine()

if you set the system property "line.separator" to be \n, then it'll match the rest of your code.

this worked for me on Windows (technically, I'm running bash on Windows - Cygwin):

// default write \r\n
$ java BufferedWriterWriteNewLineExample
$ od -x test.txt
0a0d

// set to *nix line endings
$ java -Dline.separator=$'\n' BufferedWriterWriteNewLineExample
$ od -x test.txt
0a

of course, it'd be better to implement a PropertiesModifier, something like this:
try(PropertiesModifier pm = new PropertiesModifier("line.separator", "\n")) {
  Files.write(file, ImmutableList.of(line), Charsets.UTF_8);
}
Reply | Threaded
Open this post in threaded view
|

Re: Coursera Programming Assignment: Project 7 For the kind attn. of Mark Armbrust

Anantharamaiah Prabhakar
I will try these suggested remedies, and report back.

Thanks for your suggestions.

Anantharamaiah Prabhakar

On Wed, May 16, 2018 at 4:06 AM, xedover [via Nand2Tetris Questions and Answers Forum] <[hidden email]> wrote:
the problem is java.io.BufferedWriter.newLine() is OS dependent. It writes \r\n on Windows, but \n on Linux/Mac

Your code writes \n everywhere except when you call bw.newLine()

if you set the system property "line.separator" to be \n, then it'll match the rest of your code.

this worked for me on Windows:

// default write \r\n
$ java BufferedWriterWriteNewLineExample
$ od -x test.txt
0a0d

// set to *nix line endings
$ java -Dline.separator=$'\n' BufferedWriterWriteNewLineExample
$ od -x test.txt
0a



To unsubscribe from Coursera Programming Assignment: Project 7 For the kind attn. of Mark Armbrust, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

Re: Coursera Programming Assignment: Project 7 For the kind attn. of Mark Armbrust

xedover
I was mostly copy-pasting from a stackoverflow question (I'm sure you can Google for it yourself)

but basically the simple implementation is to just call System.setProperty()
Reply | Threaded
Open this post in threaded view
|

Re: Coursera Programming Assignment: Project 7 For the kind attn. of Mark Armbrust

cadet1620
Administrator
In reply to this post by Anantharamaiah Prabhakar
I got Java installed on my Ubuntu system and was able to build your VM translator.

The problem is that your program does not handle deep file paths. The test environment runs your translator from the projects/07 directory giving it the path to the StaticTest.vm file.

You need to handle arguments that have paths in them that include '/'. (Or '\' on Windows.)

marka@Durnik:~/nand2tetris/projects/07$ java VMTranslator MemoryAccess/StaticTest/StaticTest.vm
Translation is complete
marka@Durnik:~/nand2tetris/projects/07$
When you make the static labels for StaticTest, you need to use only the filename part of the path. You are currently using the entire argument (less its .vm extension).
marka@Durnik:~/nand2tetris/projects/07$ grep / MemoryAccess/StaticTest/StaticTest.asm
//push constant 111
//push constant 333
//push constant 888
//pop static 8
@MemoryAccess/StaticTest/StaticTest.8        Should be @StaticTest.8
//pop static 3
@MemoryAccess/StaticTest/StaticTest.3
//pop static 1
@MemoryAccess/StaticTest/StaticTest.1
//push static 3
@MemoryAccess/StaticTest/StaticTest.3
//push static 1
@MemoryAccess/StaticTest/StaticTest.1
//sub
//push static 8
@MemoryAccess/StaticTest/StaticTest.8
//add
marka@Durnik:~/nand2tetris/projects/07$
There are Java classes Paths and Path that you want to use to parse (break apart) paths in a platform independent way. Windows uses '\' and Linux uses '/' so it is rather difficult to write your own path parsing code.

--Mark

Reply | Threaded
Open this post in threaded view
|

Re: Coursera Programming Assignment: Project 7 For the kind attn. of Mark Armbrust

cadet1620
Administrator
In reply to this post by Anantharamaiah Prabhakar
When you get to Project 8, you will be translating directories with multiple .vm files in them.

In that case, your VM Translator will be run as
java VMTranslator FunctionCalls/FibonacciElement
and it will be expected to translate all the FunctionCalls/FibonacciElement/*.vm files to a single asm file
FunctionCalls/FibonacciElement/FibonacciElement.asm
I found this platform independent way to join two file path components in Java
String.join(File.pathSeparator, "path1", "path2");

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Coursera Programming Assignment: Project 7 For the kind attn. of Mark Armbrust

Anantharamaiah Prabhakar
Now the problem is solved.

Yes. I had to extract the filename from the full filepath, i.e., 
MemoryAccess/StaticTest/StaticTest.vm
I was extracting it from only StaticTest.vm. I have now added the code for extracting the filename from the full file path; see attached zip file which contains the modified source code.
I submitted this zip file, and I have been graded 100%.
Thanks for your suggestions.

Anantharamaiah Prabhakar



On Wed, May 16, 2018 at 8:50 AM, cadet1620 [via Nand2Tetris Questions and Answers Forum] <[hidden email]> wrote:
When you get to Project 8, you will be translating directories with multiple .vm files in them.

In that case, your VM Translator will be run as
java VMTranslator FunctionCalls/FibonacciElement
and it will be expected to translate all the FunctionCalls/FibonacciElement/*.vm files to a single asm file
FunctionCalls/FibonacciElement/FibonacciElement.asm
I found this platform independent way to join two file path components in Java
String.join(File.pathSeparator, "path1", "path2");

--Mark


To unsubscribe from Coursera Programming Assignment: Project 7 For the kind attn. of Mark Armbrust, click here.
NAML


project7.zip (2K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Coursera Programming Assignment: Project 7 For the kind attn. of Mark Armbrust

ivant
In reply to this post by Anantharamaiah Prabhakar
In Java, printing "\n" will only print the new line character, no matter on which platform.

With System.out.printf / String.format, and similar methods you can use "%n" to print platform-specific new line.
Reply | Threaded
Open this post in threaded view
|

Re: Coursera Programming Assignment: Project 7 For the kind attn. of Mark Armbrust

Anantharamaiah Prabhakar
Thanks ivant.

From what you have suggested, "%n" is equivalent to "\r\n"; is that correct ?

Thanks again.

Anantharamaiah Prabhakar

On Wed, May 16, 2018 at 1:00 PM, ivant [via Nand2Tetris Questions and Answers Forum] <[hidden email]> wrote:
In Java, printing "\n" will only print the new line character, no matter on which platform.

With System.out.printf / String.format, and similar methods you can use "%n" to print platform-specific new line.


To unsubscribe from Coursera Programming Assignment: Project 7 For the kind attn. of Mark Armbrust, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

Re: Coursera Programming Assignment: Project 7 For the kind attn. of Mark Armbrust

ivant
This post was updated on .
I meant to reply earlier but totally forgot. Sorry.

Anantharamaiah Prabhakar wrote
From what you have suggested, "%n" is equivalent to "\r\n"; is that correct?
Not exactly. It will print the correct new line sequence for the platform it's running on. So on Windows it will print "\r\n", while on Linux it will print "\n". I'm not sure what's the right sequence for MacOS, but it will print it correctly on a mac as well.

In this regard System.out.printf("%n") is equivalent to System.out.println(). But with the former you can do more stuff. For example System.out.printf("%s, world%n", "Hello") will print "Hello, world" and a new line sequence.