250 likes | 405 Views
Parsing, AST & Semantic. Tiger Compiler Project. By Mu Li. Phases. Preliminary. We assume you are familiar with according theory backgrounds mentioned in dragon book (or tiger book). Test your scanner. Provided in LexerTest.java. Parsing. Use CUP to implement the parser Easy
E N D
Parsing, AST & Semantic Tiger Compiler Project By Mu Li
Phases Li, Mu (limu.cn@gmail.com)
Preliminary • We assume you are familiar with according theory backgrounds mentioned in dragon book (or tiger book) Li, Mu (limu.cn@gmail.com)
Test your scanner • Provided in LexerTest.java Li, Mu (limu.cn@gmail.com)
Parsing • Use CUP to implement the parser • Easy • Only grammar specification is needed Li, Mu (limu.cn@gmail.com)
Grammar specification • Terminals and non-terminals • Precedences • The grammar Li, Mu (limu.cn@gmail.com)
The grammar • Following the expressions defined in Tiger Manual … Li, Mu (limu.cn@gmail.com)
Communication between CUP and Jflex (I) • Notice %Cup in your tiger.flex, which make Jflex implement java_cup.runtime.Scanner Li, Mu (limu.cn@gmail.com)
Communication between CUP and Jflex (II) • Run CUP, parser.java and sym.java obtained • More information, you should read the documents provided by Jflex and CUP Li, Mu (limu.cn@gmail.com)
Test your parser • Provided in CupTest.java Li, Mu (limu.cn@gmail.com)
Abstract Syntax Tree (AST) • (a := 5; a+1) can be translated into Li, Mu (limu.cn@gmail.com)
Construte AST (I) • Tree’s nodes are defined in tiger.absyn • Your should make your compiler know the structure by CUP … … Li, Mu (limu.cn@gmail.com)
Constructe AST (II) • OpExp defined as: Li, Mu (limu.cn@gmail.com)
Printing AST • Using class tiger.absyn.Print Li, Mu (limu.cn@gmail.com)
Semantic • Check type Li, Mu (limu.cn@gmail.com)
Error code (I) let type a=b type b=c type c=int var x:a:=3 in a:=“hello world” end Li, Mu (limu.cn@gmail.com)
Error code (II) let type a=b type b=c type c=a …… in …… end Li, Mu (limu.cn@gmail.com)
Error code (III) let function g (a:int , b:string):int = a in g("one") end Li, Mu (limu.cn@gmail.com)
Error code (IV) let type arrtype = array of int type rectype = {name:string, id: int} var rec := rectype {name="aname", id=0} var arr := arrtype [3] of 0 in if rec <> arr then 3 else 4 end Li, Mu (limu.cn@gmail.com)
Error code (V) for i:=10 to " " do i := i - 1 Li, Mu (limu.cn@gmail.com)
Implement (I) Li, Mu (limu.cn@gmail.com)
Implement (II) Li, Mu (limu.cn@gmail.com)
Implement (III) Li, Mu (limu.cn@gmail.com)
Check the types Li, Mu (limu.cn@gmail.com)
Midterm submit • You should print the AST and check the types • The specification is on our website • Deadline is 30th Mar. • NO cheating! Li, Mu (limu.cn@gmail.com)