CS5031 Exercises 1: ECO

1. The following is the syntax of a simple language, ECO:

name			identifies an element of the language,
name : something ;	defines the named left hand element as equivalent to the right hand something,
|			separates alternative definitions,
{ something }		means that the enclosed something occurs none or more times,
"text"			indicates characters present in an actual program in the language.

program 	: statement { statement } ;
statement	: declarative ";" | executable ";" ;
declarative	: type identifier { "," identifier } ;
type    	: "CARNIV" | "VEGET" | "MALE" | "FEMALE" | "VEGETABLE" ;
executable	: "BEGIN" { statement } "END" | action { "AND" action } ;
action  	: identifier op rest ;
rest    	: identifier { "WITH" identifier } ;
identifier	: letter { letter } ;
op      	: "EATS" | "IS EATEN BY" | "HATES" | "LOVES" ;
letter  	: "a" | "b" | . . . | "z" ;
        	(i.e. all lower case characters of the English alphabet)


New lines and spaces are ignored everywhere (e.g. do not separate words).

Find all the syntax errors in the following ECO programs:

(a)	CARNIV lion, tiger, cat, man, dog;
	VEGET minnow, man, dog;
	lion EATS man WITH dog;
	lion WITH tiger EATS man;
	minnow IS EATEN BY cat

(b)	VEGETABLE onions, leeks;
	VEGET fred, albert;
	fred HATES onions WITH leeks;
	BEGIN fred EATS onions; albert EATS leeks; END
	CARNIV lion; albert IS EATEN BY lion AND lion EATS onions; onions IS EATEN BY
	fred WITH leeks;

(c)	MALE jack; FEMALE joy; CARNIV joy;
	jack LOVES joy; joy HATES jack;
	jack IS EATEN BY joy; lion EATS jack WITH joy;
	lion LOVES jack with joy;
	jack AND joy HATES lion;

2. The following are the static semantic rules for ECO:

(a) Each identifier must appear in (one or more) declaratives before it appears in any executables, and it must not appear in declaratives after it has appeared in executables.

(b) An identifier may appear in several declaratives and then is of several types simultaneously.

(c) identifier EATS rest means identifier EATS each of the identifiers in rest
identifier IS EATEN BY rest means each of the identifiers in rest EATS identifier
identifier HATES rest means identifier HATES each of the identifiers in rest
identifier LOVES rest means identifier LOVES each of the identifiers in rest

i) An identifier declared to be only CARNIV may only EAT identifiers declared to be:
one or more of CARNIV or MALE or FEMALE,
but not those also declared to be one or more of VEGET or VEGETABLE.

ii) An identifier declared to be VEGET may only EAT identifiers declared to be VEGETABLE.

iii) An identifier declared to be both CARNIV and VEGET may EAT any identifier.

iv) Any identifier can LOVE or HATE any identifier. The identifiers must have been declared.

v) Anything not explicitly permitted above is illegal.

Find all the static semantic errors in the ECO programs above.