1. Trang chủ
  2. » Giáo án - Bài giảng

WM nguyên lý ngôn ngữ lập trình nguyễn hứa phùng abstract syntax tree sinhvienzone com

6 46 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 6
Dung lượng 72,03 KB

Nội dung

Abstract Syntax TreeDr.. Nguyen Hua Phung phung@cse.hcmut.edu.vn HCMC University of Technology, Viet Nam 06, 2013 Dr.. Nguyen Hua Phung phung@cse.hcmut.edu.vn Abstract Syntax Tree SinhVi

Trang 1

Abstract Syntax Tree

Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn

HCMC University of Technology, Viet Nam

06, 2013

Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Abstract Syntax Tree SinhVienZone.com https://fb.com/sinhvienzonevn

SinhVienZone.Com

Trang 2

Abstract Syntract Tree (AST)

Definition

◦ tree representation of the abtract syntax structure of source code

◦ differ from concrete syntax tree (parse tree) by some details ignored

◦ help subsequence phases not depend on parse process

Parse Tree

<ifstmt>

AST

<ifstmt>

Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Abstract Syntax Tree SinhVienZone.com https://fb.com/sinhvienzonevn

SinhVienZone.Com

Trang 3

AST for an expression in Scala Expression AST

trait Exp case class BinExp(op:String,e1:Exp,e2:Exp) extends Exp case class UnaExp(op:String,e:Exp) extends Exp

case class Lit(i:Int) extends Exp

<exp>

<term>

<term> * <fact>

<fact> ( <exp> )

4 <exp> + <term>

<term> <fact>

5

BinExp

i

4 * (5 + 3)

BinExp("*",Lit(4),BinExp("+",Lit(5),Lit(3)))

Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Abstract Syntax Tree SinhVienZone.com https://fb.com/sinhvienzonevn

SinhVienZone.Com

Trang 4

Generate AST in Scala

Recognizer

def fact: Parser[Any] = wholeNumber | "(" ~ exp ~ ")"

Parser

def fact: Parser[Exp] = wholeNumber^^ {case x => Lit(Integer.parseInt(x))}

| "(" ~>exp<~ ")"

12 => Lit(12) ( 120 ) => Lit(120)

Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Abstract Syntax Tree SinhVienZone.com https://fb.com/sinhvienzonevn

SinhVienZone.Com

Trang 5

Generate AST in Scala (cont’d) Recognizer

def term: Parser[Any] = fact ~ rep(("*"|"/") ~ fact)

Parser

def term: Parser[Exp]= fact ~ rep(("*"|"/") ~ fact)^^ { case a ~ il => il.foldLeft(a)((b,x)=> x match {

case c~d =>BinExp(c,b,d) })

12 * 34 / 6 => Lit(12) ~ [( "*" ~ Lit(34)); ("/" ~ Lit(6))]

λ

λ ("/" ~ Lit(6)) Lit(12) ("*" ~ Lit(34))

BinExp

BinExp("/",BinExp("*",Lit(12),Lit(34)),Lit(6))

Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Abstract Syntax Tree SinhVienZone.com https://fb.com/sinhvienzonevn

SinhVienZone.Com

Trang 6

Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Abstract Syntax Tree SinhVienZone.com https://fb.com/sinhvienzonevn

SinhVienZone.Com

Ngày đăng: 30/01/2020, 22:16

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w