The Language N
MUST compiler course
Fall 2019
Instructor: Zhiyao Liang
1. Motifation
We design a language that can do simple calculation of numbers, therefore the name N.
2. Language features
2.1 Comments
/* some comments */
Same as in C.
2.2 Basic data type
• Number num A num constant is a fraction, which is a pair of integers. For example:
◦ 3|1 means the integer 3
◦ 3|5 means the fraction 3/5
◦ 5 is the same as 5:1
◦ +5 -3|5 are number with signs.
• Address of num: num *
An address of deeper levels, such as address of address, is not allowed.
• String:
◦ A string constant is in the form of “some string here”.
◦ A string constant is only used by the built-in function output_string.
◦ There is no variable for string.
2.3 Array:
Same as C. For example:
• num arr[9]; is a statement that declares an array of 9 elements with type num.
• num * arr[9]; declares an array of 9 addresses of num.
• Nested array is not allowed.
2.4 Function:
The syntax of functions are very similar to C.
• return type: <\br>
◦ void
◦ num
◦ num *
• parameter type:
◦ void
◦ num
◦ num *
• The function definition has the following form:
returnType functionName (parameters) –>
statements
🙂
The body of a function definition is a sequence of statements, that does not contain another function definition.
A smile face 🙂 marks the end of the body of a function definition
A function call is the same as in C, like max(x, 3|5)
2.5 Selection
• if statement. It optionally has an else part.
2.6 Loop
• while statement
2.7 Operators
• Arithmetic operators: + – * / %
◦ The modular operator % is not defined when the operands are not both integers. (Integers are fractions with the denominator as 1). Or you can define the modulo computation with fractions [1].
• Relational operators: > < >= == != The result is 0 or 1.
• Logical operators: && || !. The result is 0 or 1.
• Address operator: &. This operator can be applied to a variable of num and return the address of num.
• Index operator: []
• Assignment operator: =
The precedence, associativity, and arity (number of operands), and meaning of these operators are the same as in C. The result of an expression applying an operator is always a num.
Input and Output
The prototypes of these built-in functions are listed below.
• num read(void)
• void write(num)
• void print(str) Here the type str represent some string constant. str is not used explicitly as a type in the language N.
Expression
An expression is one of the following kind:
• A constant: like 5 3|5
• A variable: like x
◦ The naming policy of a variable is the same as in C: a sequence of letters, numbers, and underscore _, while the starting character is a letter of underscore.
• An operator expression, where some operator is applied (operators are discussed above).
• An expression wrapped by parenthesis, like: ( some_exp ).
Statement
A statement can be one of the following kind:
• A simple statement which ending with a ;, like:
◦ num x; /* variable declaration */
◦ num arr[8]; /*array declaration */
◦ void fun(num x); /*function declaration */
◦ x = y + 3; /* expression statement */
◦ somefunction(void);
/* function call, another kind of expression statement*/
• if statement
• while statement
• compound statement, which is a block with { and } at the two ends.
• function definition, which also be called a function statement.
keywords
while if else return num
Program
A program is a sequence of statements that are executed consequently. Unlike as C, there no need to have a main function as the entrance of a program. The execution of a program starts at the first statement.
Program example.
There are several .n files in the folder N_Program_examples.
References
1. “Is it possible to do modulo of a fraction” https://math.stackexchange.com/questions/864568/is-it-possible-to-do-modulo-of-a-fraction