hardwarehdlnand2tetris

Syntax error when implementing a Mux gate in Nand2Tetris


I am working on the first project of the Nand2Tetris course, where I have to implement a Mux gate using only Nand, Not, And and Or gates.

I have written the following code in the Mux.hdl file:

CHIP Mux {
    IN a, b, sel;
    OUT out;

    PARTS:
    // Put your code here:
    Not(in=sel, out=not_sel);
    And(a=a, b=not_sel, out=and_a);
    And(a=b, b=sel, out=and_b);
    Or(a=and_a, b=and_b, out=out);
}

However, when I try to load this code in the Hardware Simulator, I get the following error message: In HDL file C:\Users\BG\Nand2Tetris\projects\01\Mux.hdl Line 18 '' or ')' are expected

I have checked my code for any typos or syntax errors, but I can’t find any. I have also made sure that I have all the files required for the project in the correct directory. I am using the latest version of the Hardware Simulator from the course website.

What am I doing wrong? How can I fix this error?


Solution

  • First off, when posting your code, you should post the whole file, so we know what line the error message is referring to. In your case, line 18 is the first line of your code, the Not gate.

    Rather than give you the answer immediately, I am going to step you through the solution process, as it will help you in the future to have this in your toolkit.

    1. The fact that you errored out in the very first line is a clue you have a systemic problem.

    2. The error says ',' or ')' are expected (not '' or ')' as you posted, btw). That that at some point along the way, the parser ran into a situation where it expected one of those separator tokens.

    3. Your Not gate appears to be correctly formed; it has wires listed inside (), it has the correct number of wires, in= and out= are the correct names, they are separated by a comma.

    4. So step along the line one character at a time, as a parser would, and look for the character that would make it say "hey, I am expecting a , or a ) here, but there's something else instead.

    5. Can't see it? Don't feel bad. I missed it at first glance too. We both made an assumption that is true for most programming languages, but is not true for NAND2Tetris HDL.

    6. Think to yourself, what's the most unusual character in the line? It's the "_". You've trained yourself from other programming languages that _ is a valid character in a name. Is this also true in this case?

    7. Appendix 2 of the book describes the HDL syntax. Page 285 tells you what a valid name is -- "any sequence of letters and digits not starting with a digit (some hardware simulators disallow using hyphens)". This could admittedly be worded a bit better to make it explicit that hyphens are not allowed because some real-life hardware simulators disallow using hyphens.

    8. So that's your problem. Just change your wire names to be valid HDL names (eg: not_sel -> notSel) and your code will run.