Syntax

Syntax

Robbert Haarman

2010-12-11


Introduction

Syntax can strongly affect the domains in which a language will be used. For interactive use, we prefer languages that have few and short syntactical elements; no superfluous parentheses, commas, or semicolons. For programs that go beyond simple commands, it's important that commands nest well and that the nested code is legible; we might want to have some syntactic tokens here that reflect the structure of the program.

Mana attempts to strike a good balance here, by requiring only whitespace to separate tokens in simple commands, while providing parentheses and curly braces for nesting. Commands are typically one per line, but it is possible to spread commands over multiple lines using backslashes, or write multiple commands on a single line using semicolons.


Mana Syntax in EBNF

The following grammar definition is in ISO EBNF(ISO/IEC 14977 [pdf]).

program = [ commands ] eof ;

commands = [ sep ] , command , { sep command } , [ sep ] ;

command = expression { expression } ;

expression =
	integer |
	string |
	symbol |
	'(' , command , ')' |
	'{' , commands , '}' ;

sep = separator { separator }

integer = digit , { digit } ;

string = '"' , { ( any - '"' ) | ( '\' , ( '"' '\' ) ) } , '"' ;

symbol = ( letter | other ) , { digit | letter | other } ;

digit =
	'0' | '1' | '2' | '3' | '4' |
	'5' | '6' | '7' | '8' | '9' ;

letter =
	'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' |
	'h' | 'i' | 'j' | 'k' | 'l' | 'm' | 'n' |
	'o' | 'p' | 'q' | 'r' | 's' | 't' | 'u' |
	'v' | 'w' | 'x' | 'y' | 'z' |
	'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' |
	'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' |
	'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' |
	'V' | 'W' | 'X' | 'Y' | 'Z' ;

other =
	'!' | '*' | '+' | '-' | '/' | '<' | '=' |
	'>' | '?' ;

separator = ';' | '\n' ;

comment = '#' , ( any - '\n' ) , '\n' ;

whitespace = ' ' | '\t' | ( '\\' , '\n' ) ; 

Mana Syntax in Words

A program consists of zero or more commands. Commands are separated by newlines and/or semicolons.

A command consists of one or more expressions.

Expressions can be integers, strings, symbols, nested commands, or blocks.

Integers consist of one or more digits.

Strings consist of any sequence of characters contained in double quotes. Backslashes may be used to insert certain special characters into strings.

Symbols consist of letters, digits, and/or the characters '!', '*', '+', '-', '/', '<', '=', '>', or '?', but may not start with a digit.

A nested command is a single command, enclosed in parentheses.

Blocks are zero or more commands, enclosed in curly braces.

Comments (which are ignored) start in a hash mark and run to the end of the line.

Whitespace (which is ignored) is any combination of space, tab, and a backslash followed by a newline.


Examples

Below are some examples of Mana syntax.

# The string He said: "Hello!"
"He said: \"Hello!\""

# Calling the function answer with the argument 42
answer 42

# Defining a function that computes the factorial
function factorial n {
	if (le n 1) {
		1
	} else {
		mul n (factorial (sub n 1))
	}
}

# Distributineg a command over multiple lines
foo bar \
  baz qux
Valid XHTML 1.1! Valid CSS! Viewable with Any Browser