Abstract Syntax Trees 27 February 2019 OSU CSE 1 Abstract Syntax - - PowerPoint PPT Presentation

abstract syntax trees
SMART_READER_LITE
LIVE PREVIEW

Abstract Syntax Trees 27 February 2019 OSU CSE 1 Abstract Syntax - - PowerPoint PPT Presentation

Abstract Syntax Trees 27 February 2019 OSU CSE 1 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


slide-1
SLIDE 1

Abstract Syntax Trees

27 February 2019 OSU CSE 1

slide-2
SLIDE 2

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

slide-3
SLIDE 3

Example: A Java Statement

while (k < 7) { foo(k); k++; }

27 February 2019 OSU CSE 3

while cond body < k 7 call incr foo k k

slide-4
SLIDE 4

Example: A Java Statement

while (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.)

slide-5
SLIDE 5

Example: A BL Statement

WHILE true DO move infect END WHILE

27 February 2019 OSU CSE 5

WHILE TRUE BLOCK CALL move CALL infect

slide-6
SLIDE 6

Example: A BL Statement

WHILE true DO move infect END WHILE

27 February 2019 OSU CSE 6

WHILE TRUE BLOCK CALL move CALL infect

You should see the connections! (This is an actual AST for BL; notice it uses a different “design”.)

slide-7
SLIDE 7

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

slide-8
SLIDE 8

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 BLOCK

Any sequence of zero or more statements nested in an IF or WHILE construct is called a block.

slide-9
SLIDE 9

BL Example AST

CALL Statement

27 February 2019 OSU CSE 9

turnleft

CALL turnleft

slide-10
SLIDE 10

BL Example AST

IF Statement

27 February 2019 OSU CSE 10

IF NEXT_IS_ENEMY IF next-is-enemy THEN turnleft move END IF BLOCK CALL turnleft CALL move

slide-11
SLIDE 11

BL Example AST

IF_ELSE Statement

27 February 2019 OSU CSE 11

IF_ELSE NEXT_IS_ENEMY IF next-is-enemy THEN turnleft ELSE move END IF BLOCK CALL turnleft CALL move BLOCK

slide-12
SLIDE 12

BL Example AST

WHILE Statement

27 February 2019 OSU CSE 12

WHILE NEXT_IS_ENEMY WHILE next-is-enemy DO turnleft move END WHILE BLOCK CALL turnleft CALL move

slide-13
SLIDE 13

Why BLOCK?

  • Draw the AST for this BL code with and

without the intermediate notion of BLOCK:

IF next-is-empty THEN move turnright ELSE infect END IF

27 February 2019 OSU CSE 13

slide-14
SLIDE 14

Why BLOCK?

  • Draw the AST for this code with and

without the intermediate notion of BLOCK:

IF next-is-empty THEN move turnright ELSE infect 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 THEN move ELSE turnright infect END IF

slide-15
SLIDE 15

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

slide-16
SLIDE 16

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.

slide-17
SLIDE 17

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.

slide-18
SLIDE 18

Resources

  • Wikipedia: Abstract Syntax Tree

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

27 February 2019 OSU CSE 18