English subtitles

← 06-08 Calling The Interpreter

Get Embed Code
3 Languages

Showing Revision 1 created 06/10/2012 by Amara Bot.

  1. So let's work together to extend our HTML Interpreter
  2. to handle these new JavaScript elements--
  3. just like we saw a few seconds ago.
  4. Just like we saw in the last example,
  5. typically, the parse tree for HTML
  6. will be a list of elements.
  7. So we'll just run down and process each one, in turn.
  8. How we process it depends on its type.
  9. Is it a word, a tag, JavaScript?
  10. One of the easiest cases for us is
  11. if it's a word_element, we just call our graphics library
  12. and add that word to the final Web page.
  13. We've already seen what to do with _ elements in a previous quiz,
  14. so let's just do JavaScript now.
  15. We can get the text of the embedded JavaScript fragment
  16. out of the Abstract Syntax Tree.
  17. That's going to be something like: "document.write(55);"--
  18. could be more complicated, calling Fibonacci functions--
  19. doing arbitrary computation, in fact.
  20. Here "js" stands for JavaScript,
  21. and this is the text of the program.
  22. This next bit is a bit arbitrary.
  23. I'm going to walk through all the steps, but you should
  24. not worry about having to guess these.
  25. These come out of nowhere or come from library integration.
  26. Let's say that we've written down our
  27. JavaScript token definitions
  28. in some other file called: jstokens.
  29. I'm going to need to build a lexer
  30. to break this string down into tokens.
  31. Similarly, let's say I've put all of my
  32. JavaScript grammar rules somewhere else.
  33. I'm not showing them right here, but they're saved
  34. somewhere else on the disc,
  35. in a file called: jsgrammar.
  36. Once I break this text down into tokens,
  37. I'm going to need to check and see if it's valid
  38. JavaScript and build a parse tree.
  39. "lex" is the name of our library module for doing lexing.
  40. "yacc" is the name for our library module for doing parsing.
  41. "yacc" does not sound like it should be doing parsing.
  42. It stands for: Yet Another Compiler-Compiler,
  43. a tool for making compilers or interpreters.
  44. In fact, just like the JavaScript interpreter that we're making now.
  45. For now, this name is just a historical accident.
  46. Here's the real exciting part: I'm going to call our JavaScript parser
  47. and ask it to parse the text
  48. from the embedded JavaScript fragment,
  49. using the lexer--the token definitions--from jslexer.
  50. So jstree now holds a parse tree for JavaScript.
  51. And now the quiz for you
  52. is just to fill in these two blanks.
  53. I have this parse tree for JavaScript,
  54. and now I want to do--(something).
  55. I want to call our JavaScript interpreter
  56. and ask it to interpret (something--you tell me)
  57. and then, eventually, I want to call the graphics library
  58. and draw some word in the Web page.
  59. Which word should it be? Let's fill in the blanks.