'this' keyword

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

'this' keyword

Krozu
This post was updated on .
After not touching the book for a long time I decided to get back to it and actually finish it.
I remember completing chapter 11, but went back a few chapters to optimize things. (Compiling the OS gave me 37k lines at the very start, which made me remember why I had a mental breakdown in the first place)

Now that I changed it to be a whole lot better, I ran chapter 10 through my syntax analyzer and saw the following error in ExpressionlessSquare\Square.xml on line 113.

n2t file: <keyword> this </keyword>
my file: <keywordConstant> this </keywordConstant>

jack file: do Memory.deAlloc(this);

In the book it states that 'this' in the above case is a term inside an expression inside an expressionlist inside a subroutineCall inside a doStatement. Now I know that there is the 'this' keyword, but why <keyword> and not <keywordConstant>? Is it even possible to have <keywordConstant> with 'this', ever? If so, where would this be?

Or is it as simple as being wrong in the book? For me it says that 'true', 'false' 'null' and 'this' are keywordConstants, but only 'this' is also a keyword. Should it be removed from keywordConstants?
Reply | Threaded
Open this post in threaded view
|

Re: 'this' keyword

cadet1620
Administrator
Welcome back!

This is a good question and it points out the difference between between lexical analysis, syntax, and semantics.

Lexically, 'this' is a keyword. It is returned by the tokenizer as a token with type KEYWORD.
Syntactically, 'this' appears in the grammar rule keywordConstant.
Semantically, 'this' is the VM variable pointer 0.

In the SquareT.xml file, which is the output of the tokenizer, this should clearly be "<keyword> this <keyword>".

In the Square.xml file, I can see this either way. I read the keywordConstant grammar rule as a non-structural rule. It just shortens the term rule, sort of like breaking up an overly long function by moving some of the code into a separate function.

My compiler naturally writes this as <keyword> because I have a writeXML(currentToken) that is called in compileTerm().

If I had structured my code to call compileKeywordConst() from compileTerm() then I might have written <keywordConst> into the XML and seen the same issue you do.

--Mark

Reply | Threaded
Open this post in threaded view
|

Re: 'this' keyword

Krozu
Just went through chapter 10 again looking for answers and I think I found it.

In '10.2.4 The Syntax Analyzer's Output' it has a paragraph about terminals. There it says "Where xxx is one of the five lexical elements of the Jack language: namely, keyword, symbol, integerConstant, stringConstant, or identifier". And I was wrong about 'true', 'false' and 'null' not being keywords, they are. I was just too tired.

The same with figure 10.5 (The whole list of of Lexical elements, Program structure, etc), stating: "The Jack language includes five types of terminal elements (tokens)". Then listing them(see above paragraph).

That's what I get for going through code without rereading the book.

Still, I don't see a problem with adding a keywordConstant token, as long as the rest of the compiler supports it. Sticking with the book for now though.
Reply | Threaded
Open this post in threaded view
|

Re: 'this' keyword

Krozu
tldr;

Don't skim looking for answers, read the whole thing and pay attention, and don't do it while falling asleep.