The C-Minus Language
The C-Minus Language
CS106 — Compiler Principles and Construction
Fall 2011
MUST FIT
Zhiyao Liang
C-Minus Dr. Zhiyao Liang
1
Overview
C-Minus is a subset of C.
C-Minus contains integers, integer arrays, and functions.
C-Minus has local and global declarations and recursive functions.
C-Minus has if and while statements.
Each program has a declaration part and functi
C-Minus Dr. Zhiyao Liang
2
Sample program of C-
int fact( int x)
/* recursive factorial functions */
{ if (x > 1)
return x * fact(x-1);
else
return 1;
}
void main(void)
{ int x;
x = read();
if (x >0) write (fact(x));
}
C-Minus Dr. Zhiyao Liang
3
/* (A.3) A program to perform selection sort on a 10 element array */
int x[10];
int minloc (int a[], int low, int hight)
{ int i; int x; int k;
k = low;
x = a[low]
i = low + 1;
while (i < high)
{ if (a[i] < x)
{x = a[i]; k = i; }
i = i + 1;
}
return k;
}
C-Minus Dr. Zhiyao Liang
4
/* (A.3) A program to perform selection sort on a 10 element array
cont. */
void sort(int a[], int low, int hight)
{ int i; int k;
i = low ;
while (i < high-1)
{ int t;
k = minloc(a, I, high);
t = a[k];
a[k] = a[i];
a[i] = t;
i = i + 1;
}
}
C-Minus Dr. Zhiyao Liang
5
/* (A.3) A program to perform selection sort on a 10 element array
cont. */
void main(void)
{ int i;
i = 0;
while (i < 10)
{ x[i] = input();
i = i + 1;
}
sort(x, 0, 10);
i = 0;
while (i < 10)
{output(x[i]);
i = i + 1}
}
C-Minus Dr. Zhiyao Liang
6
Lexical Conventions of C- (Appendix A)
Keywords:
else if int return void while
Special symbols:
+ - * / < <= > >= == != =
; , ( ) [ ] { } /* */
Other tokens are ID and NUM, defined by:
ID = letter letter*
NUM = digit digt*
letter = |…|z|A|…Z
digit = 0|…|9
White space: contains: blanks, newlines, tabs
White space is ignored except those that separate tokens.
Comments are like /* … */
Comments can appear any place where space can appear.
C-Minus Dr. Zhiyao Liang
7
A DFA for a C-Minus scanner
C-Minus Dr. Zhiyao Liang
8
Syntax and Semantics of C-
To be continued
C-Minus Dr. Zhiyao Liang
9