University of Toronto
CSC467F Compilers and Interpreters Fall 2005

Source Language Reference Grammar

Notation:

Grammar:

program:	
        scope

statement: /* empty */,
	variable '=' expression ,
	'IF' expression 'THEN' statement
                ( 'ELSEIF' expression 'THEN' statement )**
                ( 'ELSE'  statement )* 'END' ,
	'WHILE'  expression  'DO' statement 'END' ,
	'BREAK' ,
	'RETURN' '(' expression* ')' ,
	'PUT' output ,
	'GET' input ,
	scope ,
	procedurename '(' arguments* ')' ,
	statement statement

scope: 'BEGIN' declaration* statement 'END'

expression:	
	integer ,
	'-' expression ,
	expression '+' expression ,
	expression '-' expression ,
	expression '*' expression ,
	expression '/' expression ,
	expression '^' expression ,
	'TRUE'  ,
	'FALSE'  ,
	'!' expression ,
	expression '&' expression ,
	expression '|' expression ,
	expression '='  expression ,
	expression '!=' expression ,
	expression '<' expression ,
	expression '<=' expression ,
	expression '>' expression ,
	expression '>='  expression ,
	'(' expression ')'  ,
	variable ,
	constant ,
	functionname '(' arguments* ')' ,
	parametername

declaration:	
	type  ':'  identifier ,
	type  ':'  identifier '=' expression ,
	'CONST' type ':' identifier '=' expression ,
	type  ':' identifier '[' expression ']' ,
	type  'FUNCTION' identifier '('  parameters* ')' scope ,
	'PROCEDURE'  identifier '('  parameters* ')' scope ,
	declaration declaration

output:	expression ,
	text ,
        'NEWLINE' ,
	output ',' output

input:	variable ,
	input ',' input

type:	'INTEGER'  ,
	'BOOLEAN' 

arguments:	
	expression ,
	arrayname ,
	variablename ,
	arguments ',' arguments

parameters:	
	type ':' identifier ,
	type ':' identifier '['  ']' ,
	'VAR' type ':' identifier ,
	parameters  ','  parameters

variable:	
	variablename ,
	arrayname '[' expression ']'

constant:	identifier
variablename:	identifier
arrayname:	identifier
functionname:	identifier
procedurename:	identifier
parametername:	identifier

Examples:

identifier examples: AM A1 A_B
integer examples: 0 32767
text examples: "aB )'$" "He said ""hello""."
comment example: /* comments are bracketed by * and / */

The project source language is case insensitive. Tokens may be separated by blanks, comments, or line boundaries. An identifier or keyword must be separated from a following identifier, keyword, or integer; in all other cases, tokens need not be separated. As the example indicates, quotation marks appearing inside text are denoted by pairs of quotation marks. Comments can be continued across a line boundary, but no other token can.

Each identifier must be declared before it is used. There are no type conversions. The precedence and associativity of operators is:

       0.  ! unary-
       1.  ^                        Right-associative
       2.  * /                      Left-associative
       3.  + binary-                Left-associative
       4.  =  !=  <  <=  >  >=
       6.  &                        Left-associative
       7.  |                        Left-associative

Unless otherwise specified, operators do not associate. So, a=b=c is illegal. Array indexes start at 1.


Frank Van Bussel
Last modified in September, 2005