Tutorial 10

CourseCompilers & Interpreters
SemesterS1 2022

Tutorial 10

Question 1

Hint: Make the following rules:

  • String type and evaluation

  • Identifier type and evaluation

  • Let expression type & evaluation

  • Coerce int to string

  • String concatenation

  • In the rule e::=nside+elet id = e in e ende ::= n | s | id | e + e | \cdots|\text{let id = e in e end} observe that in the last alternative

    • The two e’s can be of different types - need to handle this case.

Rule 5 - String Value

symss : String\text{syms}\vdash\text{s : String}

Rule 6 - String Evaluation

symsSeS\text{syms}\vdash\text{S}\overset{e}{\rightarrow}\text{S}

Rule 7 - Identifier Type

iddom(syms)syms(id)=ConstEntry(T)symstypeof(id)=T\text{id}\in\text{dom(syms)}\\ \text{syms(id)}=\text{ConstEntry(T)}\\ \overline{ \text{syms}\vdash\text{typeof(id)=T} }

Rule 8 - Identifier Evaluation

symsid : T\text{syms}\vdash\text{id : T}

Rule 9 - Let Expression Type

symsid : Tsymse : Tsyms\text{syms}\vdash\text{id : T}\\ \text{syms}\vdash\text{e : T}\\ \overline{ \text{syms}\vdash }

Rule 5 - String type and evaluation

symssessymss : String\text{syms}\vdash\text{s}\overset{e}{\rightarrow}\text{s}\\ \text{syms}\vdash\text{s : String}

Rule Identifiers

iddom(syms)syms(id)=ConstEntry(T,V)symsideVsymsid : T\text{id}\in\text{dom(syms)}\\ \text{syms(id)}=\text{ConstEntry(T,V)}\\ \overline{ \text{syms}\vdash\text{id}\overset{e}{\rightarrow}V}\\ \text{syms}\vdash\text{id : T}

Rule Let

symse1 : T1symse2 : T2syms+{idConstEntry(T1, V1)}+e2 : T2syms{id}ConstEntry(T1,V1)}+ e2eV2symslet id = e1 in e2 : T2symslet id = e1 in e2eV2\text{syms}\vdash\text{e1 : T1}\\ \text{syms}\vdash\text{e2 : T2}\\ \text{syms} + \{id \rightarrow\text{ConstEntry(T1, V1)}\} + \text{e2 : T2}\\ \text{syms}\vdash\text{\{id\}}\rightarrow\text{ConstEntry(T1,V1)\}+ e2}\overset{e}\rightarrow V2\\ \overline{ \text{syms}\vdash\text{let id = e1 in e2 : T2}\\ } \text{syms}\vdash\text{let id = e1 in e2}\overset{e}{\rightarrow}V2

Rule Coerce Int to String

symse : intsymseensymse : stringsymseeintToString(n)\text{syms}\vdash\text{e : int}\\ \text{syms}\vdash\text{e}\overset{e}\rightarrow\text{n}\\ \overline{ \text{syms}\vdash\text{e : string} }\\ \text{syms}\vdash\text{e}\overset{e}\rightarrow \text{intToString(n)}

Rule String Concatenation

symse1:stringsymse2:stringsymse1en1vdashe2en2symse1 ++ e2:stringsymse1 ++ e2en1  ˆn2\text{syms}\vdash\text{e1:string}\\ \text{syms}\vdash\text{e2:string}\\ \text{syms}\vdash\text{e1}\overset{e}\rightarrow \text{n1}\\ \text{vdash}\vdash\text{e2}\overset{e}\rightarrow \text{n2}\\ \overline{ \text{syms}\vdash\text{e1 ++ e2} : \text{string} }\\ \text{syms}\vdash\text{e1 ++ e2}\overset{e}\rightarrow \text{n1 \^\ n2}

Question 2


SymTab parseTDS(SymTab symsin) {
		match(KW_TYPE);
		SymTab symsOut = parseD1(syms);
		SymTabl symsOut = parseDS(symsOut);
}

SymTab parseDS(SymTab symsIn) {
    if (token.isMatch(D)) {
				SymTab symOut = parse(symsIn);
				SymTab symsOut = parseDS(symsOut);
				return symsOut
		}
		return symsIn;
}

ID EQUALS T SEMICOLON
SymTab parseD(symsIn) {
		String name = token.getName(); // for x = int returns "x"
		match(ID)
		match(EQUALS)
		Type T = parseT(symsIn);
		match(SEMICOLON);
		// CHeck if symbol table already contains the ID - iff throw error
		if (symsIn.contains(name)) {
				error("Identifier " + name + " already defined in the symbol table");
        T = Type.ERROR_TYPE;
		}
		return symsIn.add(name, T);
    
}