Syntax of subset of C processed by compiler

A program consists of a single function definition.

A function definition looks like this:

int functionName (int varName, int varName, ...) {
  functionBody
}

or

void functionName (int varName, int varName, ...) {
  functionBody
}

It has a return type (which must be either int or void), a name (any name of your choosing), and a list of zero or more parameters, separated by commas and enclosed in parentheses. Each parameter has a type (which must be int) followed by a parameter name (of your choosing). The function body is enclosed with braces.

A function body is a list of zero or more local variable declarations followed by a list of zero or more statements.

A local variable declaration looks like this:

int varName = constant;

It has a type (which must be int), a variable name (of your choosing), an equals sign, an integer constant, and a semicolon.

There are five kinds of statements:

There are many kinds of exps:

Sample Programs

This function returns the sum of its two parameters.

int add (int a, int b) {
  return a+b;
}

This function returns the larger of its two parameters.

int largest (int a, int b) {
  if (a > b) {
    return a;
  }
  else {
    return b;
  }
}

This function orders the three parameters into ascending order.

void sort (int a, int b, int c) {
  int temp = 0;
  if (a > b) {
    temp = a;
    a = b;
    b = temp;
  }
  if (b > c) {
    temp = b;
    b = c;
    c = temp;
  }
  if (a > b) {
    temp = a;
    a = b;
    b = temp;
  }
}