up: Table of contents | prev: Introduction | next: Lexical Analyzer

1.2 Minilang Syntax

The notation used for representing the language syntax in this document is described here.

Types

The language has three types: int for representing integer values, bool for representing truth values and void for representing lack of type:

type int | bool | void

Literals

The literal values of the int type are represented as non-empty sequences of decimal digits 0...9. 0 and 123 are examples of integer literals. The literal values of the bool type are represented by keywords true and false.

literal boolean‑literal | integer‑literal
boolean‑literal true | false
integer‑literal digit‑sequence
digit‑sequence [0-9]+

Identifiers

The language has functions and variables.

The names of functions and variables are defined to be Unicode identifiers, such as xyzzy or öljy, so also non-ASCII letters in the identifiers are allowed:

identifier Unicode identifier

Expressions

The literals, variables and function invocations can be combined as expressions using operators.

A literal or an identifier or an expression in parentheses is a primary expression:

primary‑expression literal | identifier | ( expression )

123, xyzzy and (1 + 2) are examples of primary expressions.

A primary expression is also a postfix expression. A primary expression followed by parenthesized list of arguments is a postfix expression, namely a function invocation:

postfix‑expression primary‑expression (( expression‑list? ))*
expression-list expression (, expression)*

foo(), fibonacci(10), and bar(1, 2, 3) are examples of function invocations.

A postfix expression is also a unary expression. An unary operator '+', '-' or '!' followed by a unary expression is a unary expression:

unary‑expression (+ | - | !) unary‑expression | postfix‑expression

xyzzy, 0, +5, -10 and !true are exmples of unary expressions.

A unary expression is also a multiplicative expression. Unary expressions combined with multiplicative operators ('*', '/' and '%') are multiplicative expressions.

multiplicative‑expression unary‑expression ((* | / | %) unary‑expression)*

10, xyzzy * 2, 100 / 5 and (2 + 3) * 4 % 7 are examples of multiplicative expressions.

A multiplicative expression is also an additive expression. Multiplicative expressions combined with additive operators ('+' and '-') are additive expressions.

additive‑expression multiplicative‑expression ((+ | -) multiplicative‑expression)*

1 + 2, xyzzy and (3 - 5) + 2 are examples of additive expressions.

An additive expression is also a relational expression. Additive expressions combined with relational operators '<', '>', '<=' and '>=' are relational expressions.

relational‑expression additive‑expression ((< | > | <= | >=) additive‑expression)*

3 >= 7, xyzzy < 0, and 100 are examples of relational expressions.

A relational expression is also an equality expression. Relational expressions combined with equality operators '==' and '!=' are equality expressions.

equality‑expression relational‑expression ((== | !=) relational‑expression)*

xyzzy == 0, a != b and 100 are examples of equality expressions.

An equality expression is an expression.

expression equality‑expression

Statements

The language has six statements: an if-statement, a while-statement, a return-statement, a construction statement, an assignment statement and a compound statement. Besides syntax, the execution semantics is described briefly.

statement if‑statement | while‑statement | return‑statement | construction‑statement | assignment‑statement | compound‑statement

An if-statement tests a condition expression that shall have type bool. If the condition evaluates to true, the first statement is executed. The if-statement can have an else part. If the condition evaluates to false, the statement of the else part is executed:

if‑statement if ( expression ) statement ( else statement )?

if ( xyzzy > 0 ) return 1; else return 2; is an example of an if-statement.

A while-statement evaluates a condition expression that shall have type bool. As long as the condition evaluates to true the statement is executed:

while‑statement while ( expression ) statement

while ( i > 0 ) i = i - 1 ; is an example of a while-statement.

A return-statement returns control from the current function to the caller of the function. If the function has type void the return-statement must not have an expression, otherwise it must have an expression whose evaluated value is returned to the caller:

return‑statement return expression? ;

return 10 ; is an example of a return statement.

A construction statement creates a local variable with a name of the given identifier and initializes it to the value that is the value of the evaluated expression:

construction‑statement type identifier = expression ;

int xyzzy = 100 ; is an example of a construction statement.

An assignment statement assigns a value that is the value of the evaluated expression to a local variable whose name is the identifier:

assignment‑statement identifier = expression ;

Note: because a function cannot return an lvalue in this language, the target of the assignment is just an identifier, not an expression.

x = foo(10) ; is an example of an assignment statement.

A compound statement executes a sequence of statements enclosed in braces:

compound‑statement { statement* }

{ x = x + 1 ; y = y - 1 ; } is an example of a compound statement.

Functions

A function represents computation or is evaluated for side-effects. A function has a return type, a possibly empty list of parameters and a body, a compound statement:

function type identifier ( parameter‑list? ) compound‑statement
parameter‑list parameter (, parameter )*
parameter type identifier

int add(int x, int y) { return x + y ; } is an example of a function.

Source Files

A source file consists of a possibly empty sequence of functions:

source‑file function*

int add(int x, int y) { return x + y ; } int sub(int x, int y) { return x - y ; } is an example of a source file.

up: Table of contents | prev: Introduction | next: Lexical Analyzer