abstract syntax trees - computer science and...

18
Abstract Syntax Trees 27 February 2019 OSU CSE 1

Upload: others

Post on 31-Dec-2019

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Abstract Syntax Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/21.Abstract-Syntax-Trees.pdfAbstract Syntax Tree • An . abstract

Abstract Syntax Trees

27 February 2019 OSU CSE 1

Page 2: Abstract Syntax Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/21.Abstract-Syntax-Trees.pdfAbstract Syntax Tree • An . abstract

Abstract Syntax Tree• An abstract syntax tree (AST) is a tree

model of an entire program or a certain “program structure” (e.g., a statement or an expression in a Java program)

• An AST is “abstract” in the sense that some of the actual characters used in the “concrete” program text do not appear in the AST

27 February 2019 OSU CSE 2

Page 3: Abstract Syntax Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/21.Abstract-Syntax-Trees.pdfAbstract Syntax Tree • An . abstract

Example: A Java Statementwhile (k < 7) {

foo(k);

k++;

}

27 February 2019 OSU CSE 3

while

cond body

<

k 7

call incr

foo

k

k

Page 4: Abstract Syntax Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/21.Abstract-Syntax-Trees.pdfAbstract Syntax Tree • An . abstract

Example: A Java Statementwhile (k < 7) {

foo(k);

k++;

}

27 February 2019 OSU CSE 4

while

cond body

<

k 7

call incr

foo

k

k

You should see the connections! (This may

not be an actual Java AST, however; it is just an

illustration of the idea.)

Page 5: Abstract Syntax Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/21.Abstract-Syntax-Trees.pdfAbstract Syntax Tree • An . abstract

Example: A BL StatementWHILE true DO

moveinfect

END WHILE

27 February 2019 OSU CSE 5

WHILETRUE

BLOCK

CALLmove

CALLinfect

Page 6: Abstract Syntax Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/21.Abstract-Syntax-Trees.pdfAbstract Syntax Tree • An . abstract

Example: A BL StatementWHILE true DO

moveinfect

END WHILE

27 February 2019 OSU CSE 6

WHILETRUE

BLOCK

CALLmove

CALLinfect

You should see the connections! (This is an

actual AST for BL; notice it uses a different “design”.)

Page 7: Abstract Syntax Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/21.Abstract-Syntax-Trees.pdfAbstract Syntax Tree • An . abstract

BL Statement Kinds

27 February 2019 OSU CSE 7

IF test THEN

END IF

WHILE test DO

END WHILE

IF test THEN

ELSE

END IF

instruction

BLOCK BLOCK BLOCK

BLOCK

Page 8: Abstract Syntax Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/21.Abstract-Syntax-Trees.pdfAbstract Syntax Tree • An . abstract

BL Statement Kinds

27 February 2019 OSU CSE 8

IF test THEN

END IF

WHILE test DO

END WHILE

IF test THEN

ELSE

END IF

instruction

BLOCK BLOCK BLOCK

BLOCKAny sequence of zero or

more statements nested in an IF or WHILE construct

is called a block.

Page 9: Abstract Syntax Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/21.Abstract-Syntax-Trees.pdfAbstract Syntax Tree • An . abstract

BL Example AST

CALL Statement

27 February 2019 OSU CSE 9

turnleft CALLturnleft

Page 10: Abstract Syntax Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/21.Abstract-Syntax-Trees.pdfAbstract Syntax Tree • An . abstract

BL Example AST

IF Statement

27 February 2019 OSU CSE 10

IFNEXT_IS_ENEMYIF next-is-enemy THEN

turnleftmove

END IFBLOCK

CALLturnleft

CALLmove

Page 11: Abstract Syntax Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/21.Abstract-Syntax-Trees.pdfAbstract Syntax Tree • An . abstract

BL Example AST

IF_ELSE Statement

27 February 2019 OSU CSE 11

IF_ELSENEXT_IS_ENEMYIF next-is-enemy THEN

turnleftELSE

moveEND IF BLOCK

CALLturnleft

CALLmove

BLOCK

Page 12: Abstract Syntax Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/21.Abstract-Syntax-Trees.pdfAbstract Syntax Tree • An . abstract

BL Example AST

WHILE Statement

27 February 2019 OSU CSE 12

WHILENEXT_IS_ENEMYWHILE next-is-enemy DO

turnleftmove

END WHILEBLOCK

CALLturnleft

CALLmove

Page 13: Abstract Syntax Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/21.Abstract-Syntax-Trees.pdfAbstract Syntax Tree • An . abstract

Why BLOCK?• Draw the AST for this BL code with and

without the intermediate notion of BLOCK:IF next-is-empty THEN

moveturnright

ELSEinfect

END IF

27 February 2019 OSU CSE 13

Page 14: Abstract Syntax Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/21.Abstract-Syntax-Trees.pdfAbstract Syntax Tree • An . abstract

Why BLOCK?• Draw the AST for this code with and

without the intermediate notion of BLOCK:IF next-is-empty THEN

moveturnright

ELSEinfect

END IF

27 February 2019 OSU CSE 14

If it’s not clear, draw the AST for this code with and without BLOCK:

IF next-is-empty THENmove

ELSEturnrightinfect

END IF

Page 15: Abstract Syntax Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/21.Abstract-Syntax-Trees.pdfAbstract Syntax Tree • An . abstract

AST Node Labels

• An AST for BL is a tree of ... what?• Each node has some of the following:

– The kind of statement (e.g., BLOCK, WHILE)– The test condition (e.g., NEXT_IS_EMPTY,

TRUE)– The call of an instruction (e.g., infect, move),

realizing that this may be an instruction defined elsewhere in the program (e.g., FindObstacle in an earlier BL example)

27 February 2019 OSU CSE 15

Page 16: Abstract Syntax Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/21.Abstract-Syntax-Trees.pdfAbstract Syntax Tree • An . abstract

AST Node Labels

• An AST for BL is a tree of ... what?• Each node has some of the following:

– The kind of statement (e.g., BLOCK, WHILE)– The test condition (e.g., NEXT_IS_EMPTY,

TRUE)– The call of an instruction (e.g., infect, move),

realizing that this may be an instruction defined elsewhere in the program (e.g., FindObstacle in an earlier BL example)

27 February 2019 OSU CSE 16

This mathematical 3-tuple of information (of which either test or call

might be relevant, depending on the kind) will be called a STATEMENT_LABEL.

Page 17: Abstract Syntax Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/21.Abstract-Syntax-Trees.pdfAbstract Syntax Tree • An . abstract

AST Node Labels

• An AST for BL is a tree of ... what?• Each node has some of the following:

– The kind of statement (e.g., BLOCK, WHILE)– The test condition (e.g., NEXT_IS_EMPTY,

TRUE)– The call of an instruction (e.g., infect, move),

realizing that this may be an instruction defined elsewhere in the program (e.g., FindObstacle in an earlier BL example)

27 February 2019 OSU CSE 17

The mathematical model of an AST for a BL statement is therefore a

tree of STATEMENT_LABEL.

Page 18: Abstract Syntax Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/21.Abstract-Syntax-Trees.pdfAbstract Syntax Tree • An . abstract

Resources• Wikipedia: Abstract Syntax Tree

– http://en.wikipedia.org/wiki/Abstract_syntax_tree

27 February 2019 OSU CSE 18