This document contains the technical specification of the Griffin programming language.
These are the additions, modifications and corrections done to this document:
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.
There are two type of comments: line comments and block comments. Line comments start with two hyphens --
and conclude at the end of the line. Block comments start with a left parenthesis and an asterisk (*
and end with an asterisk and a right parenthesis *)
. Comments do not nest and cannot be placed inside string literals.
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.
An integer literal is a sequence of one or more digits. Literals are in base ten. Their range is from 0 to 231 – 1.
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"
.
Literal booleans are true
and false
.
The mono-space
elements represent terminal symbols (tokens).
‹program› | → | [ const ‹constant-declaration›+
][ var ‹variable-declaration›+ ]‹procedure-declaration›* program ‹statement›*
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 of ‹simple-type› |
‹list› | → | { [ ‹simple-literal›
( , ‹simple-literal›
)* ] } |
‹procedure-declaration› | → | procedure ‹identifier› ( ‹parameter-declaration›* )
[ : ‹type›
] ; [ const ‹constant-declaration›+
][ var ‹variable-declaration›+ ]begin ‹statement›* 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› | → | if ‹expression› then
‹statement›* ( elseif ‹expression› then
‹statement›* )*[ else
‹statement›* ] end
; |
‹loop-statement› | → | loop
‹statement›* end
; |
‹for-statement› | → | for ‹identifier› in ‹expression› do ‹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 | sand | or | sor
| 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› | → | not ‹unary-expression›
| - ‹unary-expression›
| ‹simple-expression› |
‹simple-expression› | → | ( ( ‹expression›
) | ‹call›
| ‹identifier›
| ‹literal›
)[ [ ‹expression›
] ] |
‹call› | → | ‹identifier›
( [ ‹expression›
( , ‹expression›
)* ] ) |
program
keyword.
integer
. A string literal is of type string
. A boolean literal is of type boolean
. An empty list literal ({}
) is compatible with any type of list.
integer
variables are initialized with 0
.
boolean
variables are initialized with false
.
string
variables are initialized with ""
(empty string).
{}
(empty list).
list
of the content type.
const
section must have at least one element.
return
statement in order to end. If this statement is omitted, the return values should be the same as the default initial values for variables depending on the type of the procedure.
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.
|
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 sand op2
|
Strict Conjunction |
boolean
|
boolean
|
boolean
|
Evaluates op1 and op2. If both result true , returns true . 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 sor op2
|
Strict Disjunction |
boolean
|
boolean
|
boolean
|
Evaluates op1 and op2. If both result false , returns false . 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. |
This section documents all the procedures from the Griffin language standard library.
Category | Procedure Signature | Description |
---|---|---|
Input/Output Operations |
WrInt(
|
Writes to the standard output the value of i . Does not print a new line.
|
WrStr(
|
Writes to the standard output the value of s . Does not print a new line.
|
|
WrBool(
|
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(
|
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(
|
Returns the number of characters contained in string s .
|
|
CmpStr(
|
Compares lexicographically the contents of two string s. 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(
|
Returns a new string resulting from the concatenation of s1 and s2 .
|
|
List Operations |
LenLstInt(
|
Returns the number of elements contained in loi .
|
LenLstStr(
|
Returns the number of elements contained in los .
|
|
LenLstBool(
|
Returns the number of elements contained in lob .
|
|
NewLstInt(
|
Creates and returns a new list of integer s of the given size . All list elements are initialized with zero.
|
|
NewLstStr(
|
Creates and returns a new list of string s of the given size . All list elements are initialized with "" (empty string).
|
|
NewLstBool(
|
Creates and returns a new list of boolean s of the given size . All list elements are initialized with false .
|
|
Conversion Operations |
IntToStr(
|
Returns the result of converting i into a string .
|
StrToInt(
|
Returns the result of converting s into an integer . Throws an exception if the conversion cannot be carried out.
|