A2Z-F15 icon indicating copy to clipboard operation
A2Z-F15 copied to clipboard

JSON grammar doesn't match rules object in ContextFree

Open shiffman opened this issue 10 years ago • 0 comments

In week 8 cf.js examples with JSON, it would be nice to be able to avoid this extra step to parse the JSON file into the rules object.

  // Look at the JSON object
  for (var rule in grammar) {
    // Get the expansions and split them
    var expansions = grammar[rule];
    for (var j = 0; j < expansions.length; j++) {
      // Now split up each expansion into its own array
      // Internally, the CF object is using arrays of arrays
      // It's awkward to look at it that in the JSON file so split by whitespace
      var tokens = expansions[j].split(/\s+/);
      // Add the rule
      cfree.addRule(rule, tokens);
    }
  }

and just say:

  cfree.rules = grammar;

shiffman avatar Nov 02 '15 20:11 shiffman