Correction: exercise #11

Moderator: Automated Software Engineering

Beiträge: 38
Registriert: 18. Feb 2009 10:51

Correction: exercise #11

Beitrag von monperrus »

Hi all,

Here are the important elements of exercise #11.

Question 1: -strict Enable strict mode warnings.

Question 2: number of objects = number of opening "{" in the JSON notation = 7

Question 3: Parsing "var h = g();" gives an error. On the contrary, "var g = function () {return 4;};var h = g();"is correctly parsed. What is the difference between the two expressions? Name and describe this implicit lint rule.
In "var g = function () {return 4;};var h = g();", function g is defined and it works. The cause is that topdown4js builds a scope variable and expects that a function symbol is defined before it is used.

Question 4: The error comes from the default implementation of "nud" (in original_symbol). When nud() is called, the receiver object contains the token type and value as fields. Hence the solution is to change the error message from
this.error("Undefined. Problem with token type '"+this.arity+"'. Problematic token is: "+this.value);

Question 5:

when g is defined with "var g = function ....", it triggers scope.define() (cf. 'stmt("var", '), which adds "g" in the symbol table and **replaces nud by function "itself"**. To give the cause of the error (i.e. "g is not defined"), one has to test if the variable g is in the scope, which is done with function scope.find

Code: Alles auswählen

if (scope.find("g") === symbol_table["(name)"]) { // see scope.find: if no symbol exists in this scope, it returns symbol_table["(name)"]
A complete solution is to add a diagnostic message in the default implementation of nud in original_symbol:

Code: Alles auswählen

    var original_symbol = {
        nud: function () {
            this.error("Undefined. Problem with token type '"+this.arity+"'. Problematic token is: "+this.value+" ";
// if this is the default value of scope
// which is symbol_table["(name)"]
// then the value is not defined
        if (scope.find(this.value) === symbol_table["(name)"]) {
          this.error(this.value+ " is not defined in this scope");
There are other possible solutions.

Best regards,


Zurück zu „Automated Software Engineering“