YouTube

Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← Parsing Javascript Statements Solution - Programming Languages

Get Embed Code
3 Languages

Showing Revision 3 created 05/25/2016 by Udacity Robot.

  1. In this problem and the next we’re going to build
  2. a large portion of our javascript, parser. In
  3. particular, we’re first going to focus on
  4. javascript statements and then make it even
  5. easier really only go and consider six type
  6. statements. If then, so if you have an If
  7. statement basically, if then else, so here
  8. we’re going to handle a javascript statement,
  9. something like this and then also an else. Whereas
  10. this first type of statement is just this portion
  11. whereas the if and else handles all of this.
  12. We’re going to also do assignment, so variable X
  13. equals a five. Return statements, an example of
  14. that in javascript will look like this where we
  15. have the return keyword followed by an expression
  16. . Here I just have five. We have two ones left.
  17. One involving decoration namely using the
  18. var keyword, so this declares a variable X and so
  19. it’s a five versus just setting at the five later
  20. on. And the last type we’re going to handle in
  21. this portion is just the existence of an
  22. expression. You can have in your javascript
  23. an expression in the middle of your code
  24. . So instead of stuff here, we could just
  25. say five. This doesn’t do anything, but
  26. it’s a number five and its valid javascript.
  27. So this is pretty straightforward if you just follow
  28. the rules that were given at the top of the program
  29. and we’re going to go straight to the IDE and work
  30. through it one by one. So here I have my interpreter
  31. ready for us to get started with this question.
  32. So here I have the rule for a function and we’re
  33. going to write this out. Here I’m telling the
  34. parser that this is going to be a parser for the
  35. element non-terminal and I’m going to call this
  36. one the function parser. The first when to thing is
  37. going to be a string that tells a parser apply this
  38. rule. So here I say an element can go to the
  39. terminal function and identifier the
  40. some optional fantasies are right
  41. followed by a compound sequences statements.
  42. And once this has been matched, I’m going to apply
  43. it such that element is going to be the tuple
  44. with the world function. The name of the function
  45. , which is going to be identifier, the optional
  46. parameters which is number four and then six is
  47. going to be the compound statements. An
  48. can also just be a statement,
  49. which is going to be some sort of statement
  50. followed by a The two poles that
  51. it matches is going to be the word statement
  52. and the contents of that statement.
  53. Here I had my two rules for optional parameters
  54. and as the name implies the parameters can
  55. be optional, so our params can go to nothing
  56. or it can be a sequence of parameters. Now
  57. parameters can be some kind of identifier
  58. followed by a coma and more parameters or
  59. it can be one or the last parameter is just
  60. going to be an identifier. Notice how I put
  61. the identifier in a list because parameters
  62. are always a list even if there is just one thing
  63. in them. A compound statement is a sequence
  64. of statements in between a left brace and a
  65. right brace. the rules for statements,
  66. statements can be empty, so you can have nothing
  67. in your – between your left and right braces
  68. or it can be a single statement and a
  69. followed by more statements.
  70. So now we’re going to define the statements.
  71. Let’s start with F. If we do have an F statement,
  72. we’re going to put it in a tuple in our parse
  73. street where the first entry in the tuple is the
  74. string, if then it indicate that it’s an F
  75. statement. The second one is going to be
  76. the expression of the F statement and the
  77. last element of our tuple are going to be
  78. the compound statements that comprise the
  79. then. In the if then else , we have very similar
  80. structure except we’ve also added an else,
  81. which is going to be an additional entry in
  82. our tuple that is the sequence of statements
  83. that you do if the expression is not
  84. evaluated to be true.
  85. Here we have assignment. It’s going to an
  86. identifier. It’s a X equals the equal sign and
  87. then some expressions if I and then on parser
  88. you’re going to put a sign, the identifier and
  89. then expression to which the identifier is set.
  90. For return, we’re just going to do return
  91. and then the expression that we’re returning.
  92. We use the keyword bar to declare
  93. instead a variable, so we’re going to have
  94. the keywords bar followed by an identifier,
  95. the equal sign and expression we want
  96. to put bar, the identifier and expression.
  97. So make this a little more concrete,
  98. it would be as if we were doing this and the tuple
  99. that we put in our Pastry is going to be bar, X, five.
  100. And lastly we say a statement to an expression,
  101. which we haven’t defined yet and we’re saving
  102. for the next problem. Taken altogether we
  103. have much of our javascript parser. Well,
  104. at the least the part, that handles statements.