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
WM nguyên lý ngôn ngữ lập trình nguyễn hứa phùng abstract syntax tree sinhvienzone com

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

Thông tin tài liệu

C o e on nZ Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn 06, 2013 hV ie HCMC University of Technology, Viet Nam in m Abstract Syntax Tree https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Abstract Syntax Tree C o e Definition ◦ tree representation of the abtract syntax structure of source code on ◦ differ from concrete syntax tree (parse tree) by some details ignored ie IF AST THEN hV Parse Tree nZ ◦ help subsequence phases not depend on parse process in m Abstract Syntract Tree (AST) ELSE https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Abstract Syntax Tree C o Expression AST on e 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 nZ * (5 + 3) * ( ie ) + hV in m AST for an expression in Scala e1 Lit i BinExp e2 * BinExp e1 e2 Lit i + BinExp("*",Lit(4),BinExp("+",Lit(5),Lit(3))) https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Abstract Syntax Tree Lit i C o e Recognizer on def fact: Parser[Any] = wholeNumber | "(" ~ exp ~ ")" ie nZ Parser def fact: Parser[Exp] = wholeNumber ^^ {case x => Lit(Integer.parseInt(x))} | "(" ~> exp Lit(12) ( 120 ) => Lit(120) in m Generate AST in Scala https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Abstract Syntax Tree C o Recognizer e def term: Parser[Any] = fact ~ rep(("*"|"/") ~ fact) nZ on 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 / => Lit(12) ~ [( "*" ~ Lit(34)); ("/" ~ Lit(6))] ("/" ~ Lit(6)) hV λ BinExp ie λ Lit(12) ("*" ~ Lit(34)) in m Generate AST in Scala (cont’d) / BinExp Lit(6) * Lit(12) Lit(34) BinExp("/",BinExp("*",Lit(12),Lit(34)),Lit(6)) https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Abstract Syntax Tree C o e on AST vs Parse tree nZ AST representation in Scala hV ie how to build AST in Scala in m Summary https://fb.com/sinhvienzonevn Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Abstract Syntax Tree ... parse process in m Abstract Syntract Tree (AST) ELSE https://fb .com/ sinhvienzonevn Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Abstract Syntax Tree C o Expression... ◦ tree representation of the abtract syntax structure of source code on ◦ differ from concrete syntax tree (parse tree) by some details ignored ie IF AST THEN hV Parse Tree. .. BinExp("/",BinExp("*",Lit(12),Lit(34)),Lit(6)) https://fb .com/ sinhvienzonevn Dr Nguyen Hua Phung phung@cse.hcmut.edu.vn Abstract Syntax Tree C o e on AST vs Parse tree nZ AST representation in Scala hV ie how

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

Tài liệu cùng người dùng

Tài liệu liên quan