This document describes Karel, a simple programming language intended for basic programming education. For further info about the language, see the book “Karel the Robot”, by Richard E. Pattis. Every Karel program specifies what a simple robot, called Karel (named after Karel Čapek, a Czech writer, who has invented the word “robot”) should do in a city, consisting of regular square grid. Each grid cell can be either a wall or a free field. Karel has a backpack, in which he can keep any number of beepers that can also be placed on free (non-wall) city cells.
BEGINNING-OF-PROGRAM
DEFINE turnright AS
BEGIN
turnleft
turnleft
turnleft
END
BEGINNING-OF-EXECUTION
ITERATE 3 TIMES
turnright
turnoff
END-OF-EXECUTION
END-OF-PROGRAM
A Karel program contains a main executable part and a list of new instructions.
It has the following form:
Where new instructions is a list of definitions of the following form:
BEGINNING-OF-PROGRAM
new instructions
BEGINNING-OF-EXECUTION
main executable code
END-OF-EXECUTION
END-OF-PROGRAM
DEFINE-NEW-INSTRUCTION instruction-name AS
BEGIN
instruction code
END
The programming language is case-insensitive, i.e. BEGIN
,
Begin
, and begin
are all the same to the compiler.
Four control statements, built-in instructions, and user-defined instructions may occur in the instruction code and in the main executable code. The control statements are:
The statement ITERATE number TIMES statement
means to repeat the execution of the statement, number times.
The statement WHILE condition DO statement
means to
check if the condition is true, and if it is, to execute the
statement. Then again to check the condition, and do this
while the condition is true.
The statement IF condition THEN statement1
, or in the
full form, IF condition THEN statement1 ELSE statement2
means to check if the condition is true, then if it is, to execute
statement1, or if it is false, to execute statement2 (if it is
present, otherwise, do nothing).
If you need to specify more statements in a place where only one is expected (in iteration,
loop, or conditional), you enclose the statements between BEGIN
and
END
. For example,
IF condition THEN BEGIN statement1 statement2 END
There is a condition field in the loop and conditional statements. The condition is one of the following:
Condition | is true, if... |
FRONT-IS-CLEAR | the field ahead of Karel is clear |
FRONT-IS-BLOCKED | the field ahead of Karel is blocked |
LEFT-IS-CLEAR | the field on the left hand side of Karel is clear |
LEFT-IS-BLOCKED | the field on the left hand side of Karel is blocked |
RIGHT-IS-CLEAR | the field on the right hand side of Karel is clear |
RIGHT-IS-BLOCKED | the field on the right hand side of Karel is blocked |
BACK-IS-CLEAR | the field behind Karel is clear |
BACK-IS-BLOCKED | the field behind Karel is blocked |
NEXT-TO-A-BEEPER | the field on which Karel is contains a beeper |
NOT-NEXT-TO-A-BEEPER | the field on which Karel is does not contain a beeper |
ANY-BEEPERS-IN-BEEPER-BAG | Karel has at least one beeper in his bag |
NO-BEEPERS-IN-BEEPER-BAG | Karel has no beepers in his bag |
FACING-NORTH | Karel is facing towards north |
NOT-FACING-NORTH | Karel is not facing towards north |
FACING-SOUTH | Karel is facing towards south |
NOT-FACING-SOUTH | Karel is not facing towards south |
FACING-EAST | Karel is facing towards east |
NOT-FACING-EAST | Karel is not facing towards east |
FACING-WEST | Karel is facing towards west |
NOT-FACING-WEST | Karel is not facing towards west |
MOVE | Karel moves one step in the direction he is facing |
TURNLEFT | Karel turns 90 degress in the counterclockwise direction |
PICKBEEPER | Karel picks one beeper from the field he is on, and puts it in his bag |
PUTBEEPER | Karel gets one beeper from his bag and puts it onto the field he is on |
TURNOFF | Karel turns himself off, the program stops (it is required for the program to terminate with this instruction!) |
The following commands are considered to be errors:
For completeness, the complete formal syntax of the Karel programming language is as follows:
program | ::= | BEGINNING-OF-PROGRAM definition* BEGINNING-OF-EXECUTION statement* END-OF-EXECUTION END-OF-PROGRAM |
definition | ::= | DEFINE-NEW-INSTRUCTION identifier AS statement |
statement | ::= | block | iteration | loop | conditional | instruction |
block | ::= | BEGIN statement* END |
iteration | ::= | ITERATE number TIMES statement |
loop | ::= | WHILE condition DO statement |
conditional | ::= | IF condition THEN statement [ ELSE statement ] |
instruction | ::= | MOVE | TURNLEFT | PICKBEEPER | PUTBEEPER | TURNOFF | identifier |
condition | ::= | FRONT-IS-CLEAR | FRONT-IS-BLOCKED | LEFT-IS-CLEAR | LEFT-IS-BLOCKED | RIGHT-IS-CLEAR | RIGHT-IS-BLOCKED | BACK-IS-CLEAR | BACK-IS-BLOCKED | NEXT-TO-A-BEEPER | NOT-NEXT-TO-A-BEEPER | ANY-BEEPERS-IN-BEEPER-BAG | NO-BEEPERS-IN-BEEPER-BAG | FACING-NORTH | NOT-FACING-NORTH | FACING-SOUTH | NOT-FACING-SOUTH | FACING-EAST | NOT-FACING-EAST | FACING-WEST | NOT-FACING-WEST |
identifier | ::= | letter ( letter | digit )* |
number | ::= | digit+ |
letter | ::= | 'A' | 'B' | ... | 'Z' | '-' |
digit | ::= | '0' | ... | '9' |