CSE 111 Karel the Robot

advertisement
CSE 111
Karel the Robot
Karel’s Programs
Components of a Program
Reserved Words
Used to structure & organize program
Delimiters
 BEGIN
 END
Examples
 BEGINNING-OF-PROGRAM
 END-OF-PROGRAM
 BEGINNING-OF-EXECUTION
 END-OF-EXECUTION
Instructions
Punctuation
Semicolons
 Separates instructions
Karel’s Programs
Readability
Indentation
Instructions
Primitive & User-defined
Lowercase
Reserved Words
Uppercase
Errors
Lexical Errors
Word exists in program that is not in Karel’s
vocabulary
Syntactic Errors
Incorrect grammar
Incorrect punctuation
Execution Errors
Karel cannot execute instruction
Error-shutoff
Intent Errors
Program successfully completes, but task is not
completed successfully
Sample Program #1
The following program has one syntax error,
and one lexical error.
Program
BEGINNING-OF-PROGRAM
BEGINNING-OF-EXECUTION
putbeeper;
move;
putbeeper
move;
turnright;
move;
turnoff;
END-OF-EXECUTION
END-OF-PROGRAM
What are they?
Sample Program #1
Program
BEGINNING-OF-PROGRAM
BEGINNING-OF-EXECUTION
putbeeper;
move;
putbeeper
move;
turnright;
move;
turnoff;
END-OF-EXECUTION
END-OF-PROGRAM
What are they?
Lexical
turnright
Syntax
Second putbeeper is missing semicolon
Sample Program #2
Task
Karel will place 4 beepers in the shape of the letter
L.
Karel starts from avenue 5,
street 2, facing north.
There must be at least
four beepers in Karel’s bag.
The following program
has an intent error.
Streets
Initial World
12
11
10
9
8
7
6
5
4
3
2
1
N
W
Karel initially has
at least four beepers
in beeper bag
E
S
Karel’s Initial
Position
(facing north)
1 2 3 4 5 6 7 8 9 10 11 12
Avenues
Sample Program #2
Program
BEGINNING-OF-PROGRAM
BEGINNING-OF-EXECUTION
putbeeper;
turnleft;
move;
putbeeper;
turnleft;
turnleft;
turnleft;
turnleft;
move;
putbeeper;
move;
putbeeper;
turnoff;
END-OF-EXECUTION
END-OF-PROGRAM
Sample Program #3
Repeat the previous example but have Karel
start with two beepers in the beeper bag
Execution Error
Bugs
Bug
Term used to describe an error in a program
Where does the term bug originate from?
Debugging
The process of locating and fixing bugs in a
program
References
Richard E. Pattis (revised by Jim Roberts &
mark Stehlik), Karel the Robot, John Wiley &
Sons, Inc., 2nd edition, 1995, pp 11-18
Download