This is a general language reference for the C Minus programming language.
The following outlines the various lexical conventions of C Minus source files. C Minus source files are treated as ASCII text.
All keywords are reserved words and must be written exactly as they appear below.
For example, returned
surrounded by whitespace would be recognized as an identifier.
Keywords are case sensitive. Return
surrounded by whitespace would also be recognized
as an identifier.
Conditional execution construct.
Example:
int x = input();
if (x < 10) {
output(1);
}
Used with if
for conditional execution.
Example:
int x = input();
if (x == 10) {
output(10);
} else {
output(0);
}
Looping construct.
Example:
int x = 0;
while (x < 10) {
output(x);
x = x + 1;
}
Returns control from a function call.
There are two type specifiers: int
and void
. No other types are provided.
As with keywords, the types must be written exactly as they appear below.
Denotes an integer. The width of the variable is machine dependent.
Denotes the lack of a value. For example, a void
function does not return a value.
Relational operators. These evaluate the relationship between the expressions on either side of them.
If the relation is true, they evaluate to 1
, otherwise 0
.
The left hand side is less than or equal to the right hand side.
The left hand side is less than to the right hand side.
The left hand side is greater than the right hand side.
The left hand side is greater than or equal to the right hand side.
The left hand side is equal to the right hand side.
The left hand side is not equal to the right hand side.
There are 4 arithmetic operations available. In complex expressions, multiplication and division take precedence over addition and subtraction. There is no protection against integer overflow.
Integer addition.
Integer subtraction.
Integer multiplication.
Integer division.
Identifiers consist of a case sensitive string of one or more mixed case letters. For example, foo
, Foo
, fOO
and f
are
all acceptable and distinct identifiers. There is no maximum length of an identifier but particular implementions may impose one.
Literal values may appear in the source code.
Integer literals consist of a string of one or more digits. For example, 0
, 24
, 42
are all acceptable literals.
Integer literals must be positive numbers. For example, -42
is not acceptable. As a work around, the expression
0 - 42
may be used to represent -42
.
Integers other than 0
may not begin with 0
. For example, 042
is not allowed.
Comments begin with /*
and end with */
. Comments may not be nested (e.g. /* foo /* bar */ baz */
is not acceptable).
Comments may appear where whitespace may appear. They cannot be placed within tokens. For example, if/*x*/(x ===1)
is
OK but i/*x*/f (x==1)
is not acceptable. Comments may contain new lines.
Whitespace consists of spaces, new lines (\n
), and tabs (\t
).
Other whitespace characters are not acceptable (e.g. \r
, \f
, etc)
There are several rules which apply to all programs.
All functions must return and it must be used in accordance with the function's return type.
Either the last statement must be a return
statement or if the last statement is an if
/else
statement, all branches
must return
.
A valid source file must have at least one declaration.
Identifiers must be declared before use.
The use of an identifier is checked against the declaration (e.g. a variable cannot be called nor can a function be assigned).
Identifiers can only be declared once per scope. For example, two functions cannot be defined to have the same name.
Arrays must be subscripted except when passing an array as an argument to a function.
Zero length arrays are not allowed.
Variable declarations can only be of type int
. A void
variable is not acceptable.
Function calls must match the function signature. The number of arguments must match the number of parameters and the types must match.
The parameters list may either be void
or a comma separated list of variables. The types of the variables cannot be void
.
All programs must defined a main
function, and it must be the last declaration in the source file.
Very little about runtime behaviour is ensconced in the language definition. There is one exception.
When a negative value is used as an array index, the program halts.
Two I/O functions are provided for reading and writing numbers as numeric strings.
Accepts an integer string entered on standard input and returns it.
The input is terminated by a new line. For example, if standard
input is a text terminal, the user would type 4
, 2
, and press [enter]
;
this function would return the integer value 42
. There is no facility for error
detection nor truncation detection.
Outputs an integer value as a string followed by a new line to standard output.
For example, if output(42)
were called, 42\n
would be written to standard output.
- Identifiers
[A-Za-z]+
- Numbers
[0-9]+
- Keywords
while
if
else
int
void
return
- Special Characters
+
-
*
/
<
<=
>
>=
==
!=
=
;
,
(
)
[
]
{
}
Program
DeclarationList
DeclarationList
DeclarationList
Declaration
Declaration
VarDeclaration
FunDeclaration
VarDeclaration
TypeSepcifier
//Identifier// ;TypeSepcifier
//Identifier// [ //Number// ] ;
TypeSpecifier
- int
- void
FunDeclaration
TypeSpecifier
//Identifier// (Params
)CompoundStatement
Params
ParamList
- void
ParamList
ParamList
,Param
Param
TypeSpecifier
//Identifier//TypeSpecifier
//Identifier// [ ]
CompoundStatement
- {
LocalDeclarations
StatementList
}
- {
LocalDeclarations
LocalDeclarations
VarDeclaration
- //empty//
StatementList
StatementList
Statement
- //empty//
Statement
ExpressionStatement
CompoundStatement
SelectionStatement
IterationStatement
ReturnStatement
ExpressionStatement
Expression
;- ;
SelectionStatement
- if (
Expression
)Statement
- if (
Expression
)Statement
elseStatement
- if (
IterationStatement
- while (
Expression
)Statement
- while (
ReturnStatement
- return
Expression
; - return ;
- return
Expression
Var
=Expression
SimpleExpression
SimpleExpression
AdditiveExpression
Relop
AdditiveExpression
AdditiveExpression
Relop
- <=
- <
- >
- >=
- ==
- !=
AdditiveExpression
AdditiveExpression
Addop
Term
Term
Addop
- +
- -
Term
Term
Mulop
Factor
Factor
Mulop
- /
Factor
- (
Expression
) Var
Call
- //Number//
- (
Call
- //Identifier// (
Args
)
- //Identifier// (
Args
ArgsList
Empty
ArgsList
ArgsList
,Expression
Expression