CpSc 423, Language Processor Techniques

Example Programs

Here are some example programs using the programming language grammar for this semester's assignments.

MODULE factorial;
(* Compute n! *)
VAR
	n, i, sum : INTEGER;
BEGIN
	n := 9;
	sum := 0;
	i := 1;
	WHILE (i < 9) OR (i = 9) DO
		sum := sum * n;
		n := n - 1;
		i := i + 1
	END;
	WriteLn(sum)
END factorial.


MODULE fibonacci;
(* Compute the first n fibonacci numbers *)
VAR
	n, f1, f2, temp : INTEGER;
BEGIN
	n := 10;
	f1 := 0;
	f2 := 1;
	WriteLn(f2);
	WHILE n > 0 DO
		temp := f2;
		f2 := f1 + f2;
		f1 := temp;
		WriteLn(f2);
		n := n - 1
	END
END fibonacci.