Compiler Design

Buttercup Language Spec

This document contains the technical specification of the Buttercup programming language.

Lexical Elements

In the following sections, a letter is any character from the English alphabet from lowercase to uppercase A to Z. A digit is any character from 0 to 9. In general, spaces, tabulators, and newlines are used as delimiters between lexical units, but are otherwise ignored.


Buttercup only has line comments: they start with the character ; (semicolon) and conclude at the end of the line.


An identifier is a sequence of one or more letters. Uppercase and lowercase letters are considered different. Identifiers can be of any length.


An integer literal is a sequence of one or more digits. Literals are in base ten. Their range is from 0 to 231 – 1.


Literal booleans are #t and #f.


The mono-space elements represent terminal symbols (tokens).

program declaration›* ‹statement›*
declaration type› ‹identifier
type int | bool
statement assignment
| ‹print
| ‹condition
assignment identifier=expression
print printexpression
condition ifexpressionthenstatement›* end
expression expression› ‹operator› ‹simple_expression
| ‹simple_expression
operator & | < | + | *
simple_expression identifier
| ‹integer
| ‹boolean
| (expression)
| -simple_expression



Statements are executed sequentially starting from the first one.

An integer literal is of type int.

#t and #f literals are of type bool.


Statement Description
var = exp Place in var the result of evaluating exp. var must be previously declared. var and exp must have the same type.
print exp Evaluates exp and prints the result to the standard output.
if exp then
Evaluates exp, which must be of type bool. If the result is #t, executes statements..., otherwise has no further effect.


Operator Name Type Notes
op1 op2 result
op1 & op2 And bool bool bool Returns the logical conjunction of op1 and op2. That is, returns #t only if op1 and op2 are #t, otherwise returns #f.
op1 < op2 Less than int int bool Returns #t if op1 is less than op2, otherwise returns #f.
op1 + op2 Add int int int Returns the addition of op1 plus op2. Generates an exception if the result is out of bounds.
op1 * op2 Multiply int int int Returns the multiplication of op1 times op2. Generates an exception if the result is out of bounds.
- op1 Negation int N/A int Returns the negation (reversing the sign) of op1. Generates an exception if the result is out of bounds.