Revisions:

  1. (Jan 22) The description for the ternary conditional expression syntax had a typo, with two terms reversed; should now read R = (X if E else Y).
  2. (Jan 23) When returning from a function, the return value expression must be wrapped in parentheses.
  3. (Feb 2) Allow for empty scopes (that is, scopes with neither any declarations nor statements).
  4. (Feb 17) Permit printing integer or Boolean values.

Notation

Grammar

program: bareScope # program top-level

bareScope: declarations # only declarations
| declarations statements # both declarations and statements
| statements # only statements

scope: { bareScope } # scope enclosed with braces
| { } # empty scope

statements: variable = expression # assignment
| if expression scope # conditional statement
| if expression scope elseIfArms
| if expression scope else scope
| if expression scope elseIfArms else scope
| while expression scope # loop while expression is true
| repeat scope until expression # loop until expression is true
| break # exit out of enclosing loop
| break integer # exit out of multiple enclosing loops
| return ( expression ) # return from a function
| return # return from a procedure
| print outputs # print to standard output
| input inputs # input from standard input
| procedureName ( ) # procedure call (no arguments)
| procedureName ( arguments ) # procedure call (with arguments)
| scope # nested scope
| statements statements # sequence of statements

elseIfArms: else if expression scope # else-if arm
| elseIfArms elseIfArms # sequence of else-if arms

declarations: var variableNames compoundType # declare variables
| func procedureName ( ) scope # declare procedure (no arguments)
| func procedureName ( parameters ) scope # declare procedure (with arguments)
| func functionName ( ) scalarType scope # declare function (no arguments)
| func functionName ( parameters ) scalarType scope # declare function (with arguments)
| declarations declarations # sequence of declarations

variableNames: variableName
| variableNames , variableNames # comma separated sequence of variable names

scalarType: integer # integer type
| boolean # Boolean type

compoundType: scalarType
| [ bound ] scalarType # 1-dimensional array type
| [ bound ] [ bound ] scalarType # 2-dimensional array type

bound: integer # array of length integer

parameters: parameterNames scalarType # declare one or more formal parameters
| parameters , parameters # sequence of formal parameters

parameterNames: parameterName
| parameterNames , parameterNames # comma separated sequence of parameter names

arguments: expression
| arguments , arguments # comma separated sequence of arguments

outputs: expression # integer or Boolean expression value to be printed
| text # string constant to be printed
| newline # print newline
| outputs , outputs # sequence of output arguments

inputs: variable # input to this integer variable
| inputs , inputs # input sequence

variable: variableName # reference to scalar variable
| arrayName [ expression ] # reference to 1-dimensional array element
| arrayName [ expression ] [ expression ] # reference to 2-dimensional array element

expression: integer # literal integer constant
| - expression # unary minus
| expression + expression # addition
| expression - expression # subtraction
| expression * expression # multiplication
| expression / expression # integer division
| true # Boolean constant true
| false # Boolean constant false
| not expression # Boolean not
| expression and expression # Conditional Boolean and
| expression or expression # Conditional Boolean or
| expression = expression # equality comparison
| expression != expression # inequality comparison
| expression < expression # less-than comparison
| expression <= expression # less-than-or-equal comparison
| expression > expression # greater-than comparison
| expression >= expression # greater-than-or-equal comparison
| ( expression )
| ( expression if expression else expression ) # Python-style ternary conditional expression
| variable # reference to local variable
| functionName ( ) # function call (no arguments)
| functionName ( arguments ) # function call (with arguments)
| parameterName # reference to a formal parameter

variableName: identifier
arrayName: identifier
functionName: identifier
parameterName: identifier
procedureName: identifier

Notes

Expressions

Operator precedence