Karel programming language documentation

Overview

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.

A simple example program

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

Basic syntax

A Karel program contains a main executable part and a list of new instructions. It has the following form:

BEGINNING-OF-PROGRAM

 new instructions

 BEGINNING-OF-EXECUTION
  main executable code
 END-OF-EXECUTION

END-OF-PROGRAM
Where new instructions is a list of definitions of the following form:
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.

Syntactical elements

Control statements

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:

Iteration

The statement ITERATE number TIMES statement means to repeat the execution of the statement, number times.

Loop

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.

Conditional

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).

Block

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

Conditions

There is a condition field in the loop and conditional statements. The condition is one of the following:
Conditionis true, if...
FRONT-IS-CLEARthe field ahead of Karel is clear
FRONT-IS-BLOCKEDthe field ahead of Karel is blocked
LEFT-IS-CLEARthe field on the left hand side of Karel is clear
LEFT-IS-BLOCKEDthe field on the left hand side of Karel is blocked
RIGHT-IS-CLEARthe field on the right hand side of Karel is clear
RIGHT-IS-BLOCKEDthe field on the right hand side of Karel is blocked
BACK-IS-CLEARthe field behind Karel is clear
BACK-IS-BLOCKEDthe field behind Karel is blocked
NEXT-TO-A-BEEPERthe field on which Karel is contains a beeper
NOT-NEXT-TO-A-BEEPERthe field on which Karel is does not contain a beeper
ANY-BEEPERS-IN-BEEPER-BAGKarel has at least one beeper in his bag
NO-BEEPERS-IN-BEEPER-BAGKarel has no beepers in his bag
FACING-NORTHKarel is facing towards north
NOT-FACING-NORTHKarel is not facing towards north
FACING-SOUTHKarel is facing towards south
NOT-FACING-SOUTHKarel is not facing towards south
FACING-EASTKarel is facing towards east
NOT-FACING-EASTKarel is not facing towards east
FACING-WESTKarel is facing towards west
NOT-FACING-WESTKarel is not facing towards west

Built-in instructions

MOVEKarel moves one step in the direction he is facing
TURNLEFTKarel turns 90 degress in the counterclockwise direction
PICKBEEPERKarel picks one beeper from the field he is on, and puts it in his bag
PUTBEEPERKarel gets one beeper from his bag and puts it onto the field he is on
TURNOFFKarel turns himself off, the program stops (it is required for the program to terminate with this instruction!)

Execution errors

The following commands are considered to be errors:

Formal syntax

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'