Compiler Design

Chimera Language Spec

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


Contents


Lexical Elements

In the following sections, a letter is any character from the English alphabet from A to Z (both lowercase and uppercase). 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.

Comments

There are two type of comments: line comments and block comments. Line comments start with two slashes // and conclude at the end of the line. Block comments start with a slash and an asterisk /* and end with an asterisk and a slash */. Comments do not nest and cannot be placed inside string literals.

Identifiers

An identifier is composed of a letter and a sequence of zero or more letters, digits and the underscore character (_). Uppercase and lowercase letters are considered different. Identifiers can be of any length.

Integers

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

Strings

A string literal es a sequence of zero or more characters delimited by double quotes, for example: "this is a string". They cannot contain newline characters. A double quote character can be included in a string literal by using a pair of consecutive double quotes (""), for example: "this is another ""string""". In this example, the corresponding string is really only comprised of the following characters: this is another "string".

Booleans

Literal booleans are true and false.


Syntax

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

program [ constconstant-declaration›+ ]
[ varvariable-declaration›+ ]
procedure-declaration›*
programstatement›* end ;
constant-declaration identifier:=literal;
variable-declaration identifier› ( ,identifier› )* :type;
literal simple-literal› | ‹list
simple-literal integer-literal
| ‹string-literal
| ‹boolean-literal
type simple-type› | ‹list-type
simple-type integer | string | boolean
list-type list ofsimple-type
list { [ ‹simple-literal› ( ,simple-literal› )* ] }
procedure-declaration procedureidentifier
(parameter-declaration›* ) [ :type› ] ;
[ constconstant-declaration›+ ]
[ varvariable-declaration›+ ]
beginstatement›* end ;
parameter-declaration identifier› ( ,identifier› )* :type;
statement assignment-statement
| ‹call-statement
| ‹if-statement
| ‹loop-statement
| ‹for-statement
| ‹return-statement
| ‹exit-statement
assignment-statement identifier› [ [expression] ] :=expression;
call-statement identifier( [ ‹expression› ( ,expression› )* ] ) ;
if-statement ifexpressionthenstatement›*
( elseifexpressionthenstatement›* )*
[ elsestatement›* ] end ;
loop-statement loopstatement›* end ;
for-statement foridentifierinexpressiondo
statement›* end ;
return-statement return [ ‹expression› ] ;
exit-statement exit ;
expression logic-expression
logic-expression logic-expression› ‹logic-operator› ‹relational-expression
| ‹relational-expression
logic-operator and | or | | xor
relational-expression relational-expression› ‹relational-operator› ‹sum-expression
| ‹sum-expression
relational-operator = | <> | < | > | <= | >=
sum-expression sum-expression› ‹sum-operator› ‹mul-expression
| ‹mul-expression
sum-operator + | -
mul-expression mul-expression› ‹mul-operator› ‹unary-expression
|  ‹unary-expression
mul-operator * | div | rem
unary-expression notunary-expression
| -unary-expression
| ‹simple-expression
simple-expression ( (expression) | ‹call› | ‹identifier› | ‹literal› )
[ [expression] ]
call identifier( [ ‹expression› ( ,expression› )* ] )


Semantics

Generalities

Statements

For the following descriptions, an l-value must be an expression that refers to a variable or a list index expression (lst[index]). It cannot be a constant (an identifier declared in a const section of the program). In the case of const lists, the identifier referring to the list is what is constant, and not to the contents of the list itself.

Statement Description
l-value := exp ; Assign to l-value the result of evaluating exp. l-value must have been previously declared and must be the same type as exp.
proc ( args, ...) ; Call procedure proc, evaluating (from left to right) and sending args, ... as its actual parameters. proc must have been previously declared and must not have a return type. The type and number of actual and formal parameters must match. Al parameters are passed by value.
if exp1 then
  stat1, ...
( elseif exp2 then
  stat2, ... )*
[ else
  statn, ...]
end ;
Evaluate exp1 and if it results true, execute all the statements stat1, ... . Otherwise evaluate exp2 and if it results true, execute statements stat2, ... , and so on. If none of exp1, exp2, ... results true, execute the statements statn, ... after the else, in case such section is present. All expressions exp1, exp2, ... must be of type boolean.
loop stat, ... end ; Execute repeatedly the statements stat, ... . To end the loop an exit statement must be used.
for var in exp do
  stat, ...
end ;
Iterate over the result of evaluating exp. The element of the current iteration is assigned to var and the statements stat, ... are executed. exp must be of type list of T, and var must be a previously declared variable of type T. It is possible to terminate the loop prematurely using the exit statement.
return [ exp ] ; Ends a procedure returning the result of evaluating exp if provided. If exp is not provided, the procedure containing this statement must not have a declared return type, or must be a statement inside the program section. In case exp is provided it must be of the same type as the declared type of the procedure containing this statement.
exit ; Terminates the execution of the nearest enclosing loop or for statement in which it appears. Control passes to the statement that follows the terminated statement. It is an error to use this statement outside a looping statement.

Operators

Operator Name Type Notes
op1 op2 result
op1 and op2 Short Circuit Conjunction boolean boolean boolean Evaluates op1. It it results true, evaluates and returns the result of op2. Otherwise returns false.
op1 or op2 Short Circuit Disjunction boolean boolean boolean Evaluates op1. If it results false, evaluates and returns the result of op2. Otherwise returns true.
op1 xor op2 Exclusive Disjunction boolean boolean boolean Evaluates op1 and op2. If both result with the same value, returns false. Otherwise returns true.
op1 = op2 Boolean Equality boolean boolean boolean Returns true if op1 and op2 have the same value, otherwise returns false.
op1 = op2 Integer Equality integer integer boolean Returns true if op1 and op2 have the same value, otherwise returns false.
op1 <> op2 Boolean Inequality boolean boolean boolean Returns true if op1 and op2 have different values, otherwise returns false.
op1 <> op2 Integer Inequality integer integer boolean Returns true if op1 and op2 have different values, otherwise returns false.
op1 < op2 Less Than integer integer boolean Returns true if op1 is less than op2, otherwise returns false.
op1 > op2 Greater Than integer integer boolean Returns true if op1 is greater than op2, otherwise returns false.
op1 <= op2 Less Or Equal To integer integer boolean Returns true if op1 is less or equal to op2, otherwise returns false.
op1 >= op2 Greater Or Equal To integer integer boolean Returns true if op1 is greater or equal to op2, otherwise returns false.
op1 + op2 Addition integer integer integer Returns the addition of op1 plus op2. Generates an exception if the result is out of bounds.
op1 - op2 Substraction integer integer integer Returns the substraction of op1 minus op2. Generates an exception if the result is out of bounds.
op1 * op2 Multiplication integer integer integer Returns the multiplication of op1 times op2. Generates an exception if the result is out of bounds.
op1 div op2 Quotient integer integer integer Returns the quotient of dividing op1 by op2. Generates an exception if op2 is zero.
op1 rem op2 Remainder integer integer integer Returns the remainder of dividing op1 by op2. Generates an exception if op2 is zero.
not op1 Complement boolean N/A boolean Returns true if op1 is false, otherwise returns false.
- op1 Negation integer N/A integer Returns the negation (reversing the sign) of op1. Generates an exception if the result is out of bounds.
op1[op2] List Index list of T integer T Returns the element contained in the index (the result of evaluating op2) of the list (the result of evaluating op1). The index of the first element is zero. Generates an exception if op2 is out of bounds of the list.
proc(...) Call The number and types of the actual parameters, as well as the return type, depend on how the procedure is declared.

Returns the result of calling proc, which must of been previously declared.

The type and number of actual and formal parameters must match. Parameters are passed by value.

Parameters and local variables exist in a different namespace from global names.

A call may be recursive.


API Specification

This section documents all the procedures from the Chimera language standard library.

Category Procedure Signature Description
Input/Output Operations WrInt(
  i: integer;
);
Writes to the standard output the value of i. Does not print a new line.
WrStr(
  s: string;
);
Writes to the standard output the value of s. Does not print a new line.
WrBool(
  b: boolean;
);
Writes to the standard output the value of b. Does not print a new line.
WrLn(); Prints a new line to the standard output.
RdInt(): integer; Reads from the standard input an integer and returns its value. Does not return until a valid integer has been read.
RdStr(): string; Reads from the standard input a string (until the end of line) and returns its value.
String Operations AtStr(
  s: string;
  i: integer;
): string;
Returns as a new string the character contained at index i of string s. First character is at index 0. Throws an exception if index i is out of bounds.
LenStr(
  s: string;
): integer;
Returns the number of characters contained in string s.
CmpStr(
  s1, s2: string;
): integer;
Compares lexicographically the contents of two strings. Returns zero if s1 is equal to s2, a negative number if s1 is less than s2, or a positive number if s1 is greater than s2.
CatStr(
  s1, s2: string;
): string;
Returns a new string resulting from the concatenation of s1 and s2.
List Operations LenLstInt(
  loi: list of integer;
): integer;
Returns the number of elements contained in loi.
LenLstStr(
  los: list of string;
): integer;
Returns the number of elements contained in los.
LenLstBool(
  lob: list of boolean;
): integer;
Returns the number of elements contained in lob.
NewLstInt(
  size: integer;
): list of integer;
Creates and returns a new list of integers of the given size. All list elements are initialized with zero.
NewLstStr(
  size: integer;
): list of string;
Creates and returns a new list of strings of the given size. All list elements are initialized with "" (empty string).
NewLstBool(
  size: integer;
): list of boolean;
Creates and returns a new list of booleans of the given size. All list elements are initialized with false.
Conversion Operations IntToStr(
  i: integer;
): string;
Returns the result of converting i into a string.
StrToInt(
  s: string;
): integer;
Returns the result of converting s into an integer. Throws an exception if the conversion cannot be carried out.


Source Code Examples

Source file Description Date added
hello.chimera Prints "Hello World!" in the screen. 26-Aug-2019
palindrome.chimera Determines if a string is a palindrome. 26-Aug-2019
variables.chimera Using variables and constants with different scopes. 26-Aug-2019
binary.chimera Converts numbers into binary. 26-Aug-2019
factorial.chimera Computes factorials using iteration and recursion. 26-Aug-2019
lists.chimera Implementation of typical list operations. 26-Aug-2019