Grammar and grammar parsing involves constructing a sentence (in
programming, you construct a legal statement). Your sentence is
constructed with a basic skeleton, in which you define what
different parts need to be put together to build your sentence. The
best way to understand this concept is to review the
examples.
Examine the practice grammars. You can access
them on the web at
http://www.cs.duke.edu/courses/cps001/spring07/class/04_Java/grammars/.
Note how they are written and how they are formatted. The poem.txt grammar in particular is a good one to emulate since it is rataher simple.
Come up with your own grammar idea and write
it out in the form of grammars found on page 78-80 in the Great
Ideas in Computer Science with Java. Your grammars must meet the
following criteria:
Your grammar should include at least 6 non-terminal tags as described in the notes from class.
Your grammar should produce at least 64 different sentences when expanded.
Your grammar should be original.
Used when something is to be done once or not at all
if ( << boolean-expression >> )
{
<<statements to repeat if
boolean-expression evaluates to true>>
}
1. Describe the behavior if it executes the following code.
Conditional Loop: while
Used when the number of repetitions depends on a condition being met.
while ( << boolean-expression >> )
{
<<statements to repeat
while boolean-expression evaluates to
true >>
}
2. Describe the behavior of karel if it executes the following code.
Suppose you are given the following problem:
Assume that your robot is positioned at (0,0) facing East. Write a method that makes the robot walk forward 5 steps. It must pick up any Things it encounters and then be labeled as "DONE."
4. All methods in Java are inside of some sort of class. What kind of class definition must the class be in? Give the class header.
5. As you can see, the algorithm is fairly simple. What particular information were we given in this particular problem that made the algorithm easy to formulate? Would the problem be harder if we had less information? Why or why not? Could you think of additional information that would lead to an even simpler algorithm?
escape that takes the robot out of the
intersection if possible.
![]() |
![]() |
| Initial Situation | Final Situation |