Parsing, AST & Semantic

Post on 23-Jan-2016

40 views 0 download

Tags:

description

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 - PowerPoint PPT Presentation

transcript

Tiger Compiler Project

By Mu Li

2007-3-8Li, Mu (limu.cn@gmail.com) 2

We assume you are familiar with according theory backgrounds mentioned in dragon book (or tiger book)

2007-3-8Li, Mu (limu.cn@gmail.com) 3

Provided in LexerTest.java

2007-3-8Li, Mu (limu.cn@gmail.com) 4

Use CUP to implement the parser◦Easy◦Only grammar specification is needed

2007-3-8Li, Mu (limu.cn@gmail.com) 5

Terminals and non-terminals

Precedences

The grammar

2007-3-8Li, Mu (limu.cn@gmail.com) 6

Following the expressions defined in Tiger Manual

2007-3-8Li, Mu (limu.cn@gmail.com) 7

Notice %Cup in your tiger.flex, which make Jflex implement java_cup.runtime.Scanner

2007-3-8Li, Mu (limu.cn@gmail.com) 8

Run CUP, parser.java and sym.java obtained

More information, you should read the documents provided by Jflex and CUP

2007-3-8Li, Mu (limu.cn@gmail.com) 9

Provided in CupTest.java

2007-3-8Li, Mu (limu.cn@gmail.com) 10

(a := 5; a+1) can be translated into

2007-3-8Li, Mu (limu.cn@gmail.com) 11

Tree’s nodes are defined in tiger.absyn

Your should make your compiler know the structure by CUP

2007-3-8Li, Mu (limu.cn@gmail.com) 12

OpExp defined as:

2007-3-8Li, Mu (limu.cn@gmail.com) 13

Using class tiger.absyn.Print

2007-3-8Li, Mu (limu.cn@gmail.com) 14

Check type

2007-3-8Li, Mu (limu.cn@gmail.com) 15

let type a=btype b=ctype c=intvar x:a:=3

in a:=“hello world”

end

2007-3-8Li, Mu (limu.cn@gmail.com) 16

let type a=btype b=ctype c=a……

in ……

end

2007-3-8Li, Mu (limu.cn@gmail.com) 17

letfunction g (a:int , b:string):int = a

ing("one")

end

2007-3-8Li, Mu (limu.cn@gmail.com) 18

let

type arrtype = array of inttype rectype = {name:string, id: int}

var rec := rectype {name="aname", id=0}var arr := arrtype [3] of 0

inif rec <> arr then 3 else 4

end

2007-3-8Li, Mu (limu.cn@gmail.com) 19

for i:=10 to " " do i := i - 1

2007-3-8Li, Mu (limu.cn@gmail.com) 20

2007-3-8Li, Mu (limu.cn@gmail.com) 21

2007-3-8Li, Mu (limu.cn@gmail.com) 22

2007-3-8Li, Mu (limu.cn@gmail.com) 23

2007-3-8Li, Mu (limu.cn@gmail.com) 24

You should print the AST and check the types

The specification is on our website Deadline is 30th Mar. NO cheating!

2007-3-8Li, Mu (limu.cn@gmail.com) 25