0%

RCC: Retarded C-like Compiler

Course project for Compiler Principle at ZJU. Co-Authored with @Tinghao(Vitus) Xie.Visit Github repo.

1
2
3
4
5
6
int main(){
int k = 3;
for(i: 3 to 5){
printf("i =%d\n", i);
}
}

Features

  • Advanced self-defined types (nested struct and arrays)
  • MACRO support (#define/#define f(X),#ifdef/ifndef and nested)
  • Error detection and recovery (primary)

Language definitions

This language is a miniature of C. We highlight our differences as follows:

  • type system: char, int, double and n-dimensional array type; Pointer type is not supported in this version.
  • no controled jumps, gotos and labels , i.e. break, continue and switch statements are not supported.
  • scanf and printf are automaticly embedded in runtime. Do not override it.
  • calling convention of scanf is modified. e.g. you shall use scanf("%d",i); to read the value into variable i and drop the & symbol.
  • for loop switched to pascal-like, i.e. for(i: 0 to n){} and for(i: n downto 0){} snippet. i is only seen within the scope of this loop.
  • unary operators are not supported in this version.

You are encouraged to peruse the test examples in order to get a better understanding of the gramma. Here is a quicksort implementation in our language:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

int a[10005];
int sort(int left, int right)
{
int i = left;
int j = right;
int key = a[left],tmp;

if(left >= right)
{
return 0;
}
while(i < j)
{
while(i < j & key <= a[j])
{
j=j - 1;
}
tmp=a[i];
a[i]=a[j];
a[j]=tmp;

while(i < j & key >= a[i])
{
i=i + 1;
}
tmp=a[i];
a[i]=a[j];
a[j]=tmp;
}
a[i] = key;
sort(left, i - 1);
sort(i + 1, right);
return 0;
}

int main(){
int k,n;
scanf("%d",n);
for(k:0 to n-1){
scanf("%d",a[k]);
}
sort(0,n-1);
for(k:0 to n-1){
printf("%d\n",a[k]);
}
return 0;
}