## ← Python Expressions Solution - Intro to Computer Science

• 2 Followers
• 80 Lines

### Get Embed Code x Embed video Use the following code to embed this video. See our usage guide for more details on embedding. Paste this in your document somewhere (closest to the closing body tag is preferable): ```<script type="text/javascript" src='https://amara.org/embedder-iframe'></script> ``` Paste this inside your HTML body, where you want to include the widget: ```<div class="amara-embed" data-url="http://www.youtube.com/watch?v=8yuXkETbdO0" data-team="udacity"></div> ``` 4 Languages

Showing Revision 6 created 05/24/2016 by Udacity Robot.

1. So, the answer is the first one. This is just
2. a number three. And we can get three from expression
3. by following this rule. Starting from expression, replacing with number,
4. and then replacing number with the number three. The second one
5. is not valid. And the way to see that is
6. if we start from expression, we only have three choices. We
7. can replace it with expression operator expression. We can replace
8. it with number, or we can replace it with a left
9. paren, expression, right paren. Since the expression we're trying
10. to match starts with a left paren, the only
11. way to get it left paren is to eventually
12. have this rule that replaces expression with left paren. When
13. we use that rule, what we get is the
14. left paren followed by expression. Now we have expression here.
15. From here, we've matched the far left paren and
16. the far right paren. What we have left is this.
17. We can replace this expression with paren expression,
18. and we have paren that's replacing this expression using
19. this rule. And then we still have the
20. second right paren that we had before. This isn't
21. going to match here because we need two right
22. parens to match. And the essence of this rule
23. is that every time we open a parenthesis we
24. have to eventually close it. So we can't product
25. expressions where the parenthesis aren't balanced. So this
26. is not a valid Python expression. We can
27. produce the third one, this has many nested
28. expressions. We have three perens that are open, but
29. we have balanced and closed perens. And the
30. way we can derive this expression, we'll start
31. with one expression that's our starting point. We
32. use this rule to replace it paren expression, paren.
33. Now, we have an expression that starts with a one,
34. so we want to replace this expression with expression operator
35. expression. We still have the outer parens that we had
36. before. Now, we're working on this part. We want to to
37. replace this expression with a number and then with the
38. number one. We're going to replace the Op, the following
39. operator goes to times rule. That's going to be replaced
40. with times And now we've got to replace this expression.
41. We're trying to produce this whole thing, so we're going to replace
42. this with, using the parentheses rule. And we can keep going,
43. we're going to replace this expression
44. with expression operator expression. Eventually get
45. the two times and then do that replacement with parens again,
46. to get the three times four. And not going to fill in
47. all the steps here because they won't fit on the screen.
48. But I hope you get the basic idea that we can
49. keep doing the replacements, making complex expressions like this one. So the
50. fourth possibility is not a valid expression. There's no way
51. that we can get An operator in front with this
52. rule. There actually are rules in Python that allow us
53. to have an expression go to operator expression. But we
54. have two expressions here. There's a three space three. If
55. it was just the number 33, this would actually be
56. a valid Python expression. Not something that we can produce
57. with this grammar though. And the first one is valid It's
58. not sensible, or necessary, to have er, nested
59. parentheses like this, but we could always keep using
60. this rule, so we can derive that expression starting
61. from expression using the last rule with the parens,
62. and we're going to end up with paren,
63. expression, paren, close paren. We can use that rule
64. again, we're going to replace the middle expression with expression
65. paren expression. Replacing this expression with the last rule.
66. That'll give us expression surrounded by parens, and we still have
67. the two outer parens on each side. So we can have
68. as many necessary parens as we want. It's not necessary, it
69. doesn't make a lot of sense. We would never actually want
70. to write code that way. But it's still a valid Python
71. expression. So recursive grammar rules like these are very powerful. We
72. only need a few simple rules to describe a big language,
73. and the whole Python language that we're going to learn in
74. this course can be described this way.
75. We're not going to describe the entire Python language
76. this way, but what I hope is that you have a good sense for how grammars
77. work, and you can see that, even as