parsing, ast & semantic

25
Tiger Compiler Project By Mu Li

Upload: alden

Post on 23-Jan-2016

45 views

Category:

Documents


0 download

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

Page 1: Parsing, AST & Semantic

Tiger Compiler Project

By Mu Li

Page 2: Parsing, AST & Semantic

2007-3-8Li, Mu ([email protected]) 2

Page 3: Parsing, AST & Semantic

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

2007-3-8Li, Mu ([email protected]) 3

Page 4: Parsing, AST & Semantic

Provided in LexerTest.java

2007-3-8Li, Mu ([email protected]) 4

Page 5: Parsing, AST & Semantic

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

2007-3-8Li, Mu ([email protected]) 5

Page 6: Parsing, AST & Semantic

Terminals and non-terminals

Precedences

The grammar

2007-3-8Li, Mu ([email protected]) 6

Page 7: Parsing, AST & Semantic

Following the expressions defined in Tiger Manual

2007-3-8Li, Mu ([email protected]) 7

Page 8: Parsing, AST & Semantic

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

2007-3-8Li, Mu ([email protected]) 8

Page 9: Parsing, AST & Semantic

Run CUP, parser.java and sym.java obtained

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

2007-3-8Li, Mu ([email protected]) 9

Page 10: Parsing, AST & Semantic

Provided in CupTest.java

2007-3-8Li, Mu ([email protected]) 10

Page 11: Parsing, AST & Semantic

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

2007-3-8Li, Mu ([email protected]) 11

Page 12: Parsing, AST & Semantic

Tree’s nodes are defined in tiger.absyn

Your should make your compiler know the structure by CUP

2007-3-8Li, Mu ([email protected]) 12

Page 13: Parsing, AST & Semantic

OpExp defined as:

2007-3-8Li, Mu ([email protected]) 13

Page 14: Parsing, AST & Semantic

Using class tiger.absyn.Print

2007-3-8Li, Mu ([email protected]) 14

Page 15: Parsing, AST & Semantic

Check type

2007-3-8Li, Mu ([email protected]) 15

Page 16: Parsing, AST & Semantic

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

in a:=“hello world”

end

2007-3-8Li, Mu ([email protected]) 16

Page 17: Parsing, AST & Semantic

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

in ……

end

2007-3-8Li, Mu ([email protected]) 17

Page 18: Parsing, AST & Semantic

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

ing("one")

end

2007-3-8Li, Mu ([email protected]) 18

Page 19: Parsing, AST & Semantic

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 ([email protected]) 19

Page 20: Parsing, AST & Semantic

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

2007-3-8Li, Mu ([email protected]) 20

Page 21: Parsing, AST & Semantic

2007-3-8Li, Mu ([email protected]) 21

Page 22: Parsing, AST & Semantic

2007-3-8Li, Mu ([email protected]) 22

Page 23: Parsing, AST & Semantic

2007-3-8Li, Mu ([email protected]) 23

Page 24: Parsing, AST & Semantic

2007-3-8Li, Mu ([email protected]) 24

Page 25: Parsing, AST & Semantic

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 ([email protected]) 25