English subtitles

← Parsing Javascript Expressions Solution - Programming Languages

Get Embed Code
3 Languages

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

  1. In this problem we’re going to build the part of
  2. our javascript parser that handles javascript
  3. expressions. This is pretty straightforward, just
  4. like the last problem where we’re essentially
  5. numerating all the rules that we’ve predefined
  6. for our javascript language. So let’s go right
  7. through the I, the E.
  8. So here we have the supply code for the
  9. problem and if we look closely, we see that
  10. almost all the parse float are numerated to
  11. exactly what they should be. We have Identifier,
  12. numbers, strings, true and false, how to handle a
  13. not keyword do the opposite and then for
  14. expressions. We are also given an enumeration
  15. of the precedence and the associativity for each
  16. of these operations.
  17. So really we have everything we need in the
  18. problem description to do this problem. And as
  19. you can see here, I’ve already filled in the
  20. precedence ordering for the operations. And this
  21. has simply taken almost exactly right from the
  22. comment given in the problem where or is listed
  23. at the lowest precedence and we go all the way
  24. up to division, right here. I’ve also added not
  25. just to make it work.
  26. So let’s start filling in the rules. My first rule is
  27. going to handle. If we add a matching left and
  28. right parenthesis, the expression that is equal to
  29. is simply the contents of the parenthesis, pretty
  30. straightforward. And now I have four rules for
  31. some of our literal values. We have a number,
  32. we’re going to say number and then the contents
  33. of that string, just simply say the word string,
  34. we’ve matched true or false, we’re going to
  35. return the specified tuples. If we see a notch,
  36. then we simply have in our Pastry that we’re not
  37. and then the contents that are being.
  38. Afterwards, we have about a dozen or so binary
  39. rules. Addition, subtraction, times, modulus
  40. division etcetera, etcetera and to save some time
  41. I could enumerate each function, but I want to
  42. take a short cut. So here I’ve said if we match
  43. any of these things, I’m calling it a binary
  44. operation. For the first element is new left,
  45. upper end of the binary operation. The next
  46. entry in our tuple is going to be the operation
  47. being used and the last one its going to be the
  48. right upper hand of the binary operation.
  49. And now I have the expressions for function
  50. calls, not just decorations, which we had in the
  51. last problem. A function call is going to be
  52. identifier, with optional argument in between
  53. parenthesis. And the code for handling optional
  54. arguments is almost exactly the same as the
  55. code we use to handle, optional arguments in
  56. the function decoration. In fact, I think it is
  57. exactly the same. And with all that, we’ve done
  58. it. It’s about 50 lines a code and we are happy.