English subtitles

← Rule Order - Programming Languages

Get Embed Code
3 Languages

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

  1. As we saw in that last quiz,
  2. it's not quite clear what to do when our token definitions overlap.
  3. The 7-character sequence "hello"
  4. matches our regular expression for word
  5. but also matches our regular expression for string.
  6. This is a problem not just with computer languages
  7. but also with natural languages.
  8. As the hypothetical owner of this restaurant would notice,
  9. we don't just serve hamburgers, we serve people
  10. could be interpreted the wrong way.
  11. Presumably those hamburgers are soylent green flavored.
  12. We want to have definitive rules for figuring out
  13. which of these we prefer.
  14. In fact, we're going to use a very simple rule.
  15. The first one you list wins,
  16. the one closer to the top of the file,
  17. so this is our big winner and is going to take priority over string.
  18. If you're making a lexical analyzer for HTML or JavaScript,
  19. ordering your token definitions is of prime importance.
  20. Let's investigate this issue in the form of a quiz.
  21. Suppose we have the input string hello, "world,"
  22. and we really want that to yield word,
  23. the word hello, followed by a string.
  24. I'm going to list 3 rules for you,
  25. and I want you to tell me which one has to come last
  26. for us to get the desired effect.
  27. And here, because you've seen it all before, I'm eliding some of the details
  28. like the colon, token, blah, blah, blah.
  29. Instead what I'd like you to do is tell me
  30. which one of these functions, which one of these rules,
  31. would have to come last, bearing in mind that the one that comes first
  32. wins all ties in order for hello, "world" to break down into
  33. a word followed by a string.