E Language Compiler Course: A Simplified C-like Language, Exercises of Compilers

compiler construction file for students

Typology: Exercises

2022/2023

Uploaded on 02/10/2023

rui-chen
rui-chen 🇨🇳

1 document

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
The Language E
MUST compiler course
Fall 2021
Instructor: Zhiyao Liang
1. Motivation
We design a language that can be conveniently used to compute the values of expressions,
therefore the name E (first letter of expression). The language is like a simplified version of C, while
its type num is different from C.
2. Language features
2.1 Comments
/* some comments */
Same as in C.
2.2 Basic data type
char : The character type, same as in C, it is an unsigned integer of 1 byte.
char x; /* x is a char variable */
x = 'a'; /* 'a' is a constant */
int : The integer type, same as int in C, it can be positive or negative, with 4 bytes.
int x, y; /* two int variables */
x = -5000; /* -5000 is an int constant */
y = 2021;
num : The number type, representing a fraction. It replaces the float and double types in C.
A fraction, described in math notation like , can be represented as a num in the language
, in which the the numerator is called the top, while b the denominator is called the
bot.
For a num, its bot is always a positive int, while its top can be any int.
b
t
E t
pf3
pf4
pf5

Partial preview of the text

Download E Language Compiler Course: A Simplified C-like Language and more Exercises Compilers in PDF only on Docsity!

The Language E

MUST compiler course

Fall 2021

Instructor: Zhiyao Liang

1. Motivation

We design a language that can be conveniently used to compute the values of expressions,

therefore the name E (first letter of expression). The language is like a simplified version of C, while

its type num is different from C.

2. Language features

2.1 Comments

/* some comments */

Same as in C.

2.2 Basic data type

char : The character type, same as in C, it is an unsigned integer of 1 byte.

char x; /* x is a char variable / x = 'a'; / 'a' is a constant */

int : The integer type, same as int in C, it can be positive or negative, with 4 bytes.

int x, y; /* two int variables / x = - 5000 ; / -5000 is an int constant */ y = 2021 ;

num : The number type, representing a fraction. It replaces the float and double types in C.

A fraction, described in math notation like , can be represented as a num in the language

, in which the the numerator is called the top , while b the denominator is called the

bot.

For a num , its bot is always a positive int , while its top can be any int.

b t

E t

A constant of a num has the form top|bot , where bot cannot be 0 or negative. For

example 20|27 , -3|.

num n = 3 | 5 ; /* variables can be initialized in its definition statement / num y = n * - 99 | 100 ; / numbers can be applied with math operations */

address type: for each different type, the corresponding address type is a different type, like

address of char, address of int, and so on.

int x ; /* declare a variable of type int / char y ; num n = 3 | 5 ; num * np ; / np is a pointer to a num variable / np = &n; / &n computes the address of the num variable n */ int * xp = &x; char * yp; yp = &y;

For simplicity of the compiler project, we do not need to consider address of of another pointer.

2.3 Array

Arrays are defined in the same way as in C. For example, with the form

typeName arrayName [positive_int_const]

For the simplicity of the compiler project, nested array is not allowed in the E language.

An array name, as an expression, its value can represent the address of its first element when

an address is needed, same as in C.

void print_message(char * arr); char greeting[ 80 ] = "How do you do."; print_message(greeting); /* array name can be used as an address of char */

An array can be initialized in an array declaration statement like :

2.4 String

Like in C, a string is recorded in an array of characters, whose content ends with a null

character '\0'.

Same as in C, a string constant is surrounded by double quotes, in the form like

"some string here".

A string constant is only used in two cases:

1. Initializing a character array with a string. For example:

int arr[ 8 ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 }; /* The missing part element is 0 / num arr2[] = { 1 | 2 , 2 | 3 , - 3 | 4 , - 4 | 5 , 50 | 99 } ; / an array of num with 5 elements */

Therefore, this operator is ignored in E, while the unary - operator (negation) is considered

in E.

Precedence Operator Description arity associativity

1 [ ] array subscripting 2 left-to-right

2 - unary minus (negation) 1 right-to-left

! logical NOT 1

* Indirection, (dereference) 1

& Address of 1

multiplication, division, and remainder

(modulor)

2 left-to-right

4 + - addition , substraction 2 left-to-right

for relational operators of , , ,

and

2 left-to-right

6 == != for relational equal and non-equal 2 left-to-right

7 && logical AND 2 left-to-right

8 || logical OR 2 left-to-right

9 = assignment 2 right-to-left

10 , comma 2 left-to-right

2.7 Expressions

Expressions are defined the say as in C. An expression is one of the following kind:

An atomic expression:

A constant: like 5 3|

A name (variable name, array name): like x , some_array.

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, constructed by connecting some smaller expression(s) with an

operator.

a function call.

Note that operator ( ) is considered in E. Otherwise, this will be a case of an

operator expression.

an expression surrounded by ( ) , like: ( 3+4/5 ).

< ≤ > ≥ = =

2.8 Statements

Statements are constructed similarly to the statements in C, including the following kinds:

Declaration statement.

num x; /* variable declaration /num arr[ 8 ]; /*array declaration */ void fun(num x); /function declaration */`

Expression statement, e.g. some_function(3, 3|5) ; 3+5;.

x = y + 3 ; /* expression statement / somefunction(void); / a function call is an expression*/

Null statement, which is a simple ;.

Selection statement:

The if statement is the same as in C. It optionally has an else part.

Loop statement:

The while statement is the same as in C.

compound statement, which is a block with { and } at the two ends.

Note that a function definition is not considered a as statement.

2.9 Input and output functions.

There are several built-in functions that can be directly used in E programs without the

corresponding function definitions.

The prototypes of these built-in functions are their computation are described as follows

char rc(void); /* rc means receiving a character

int ri(void); /* receiving an integer */

num rn(void); /* receiving a num */

void rs(char * a) ; /* receive a string and save it at the address a */

void pc(char c); /* pc means printing a character */

void pi(int x); /* print an integer */

void pn(num n); /* print a number */

void ps(char * s); /* print a string */

2.10 keywords

char, int, num, while, if, else, return.

2.11 Program