abstract syntax trees
play

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


  1. Abstract Syntax Trees 27 February 2019 OSU CSE 1

  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

  3. Example: A Java Statement while (k < 7) { while foo(k); cond body k++; } < call incr k 7 foo k k 27 February 2019 OSU CSE 3

  4. Example: A Java Statement while (k < 7) { while foo(k); cond body k++; } < call incr You should see the connections! (This may k 7 foo k not be an actual Java AST, however; it is just an illustration of the idea.) k 27 February 2019 OSU CSE 4

  5. Example: A BL Statement WHILE true DO move WHILE infect TRUE END WHILE BLOCK CALL CALL move infect 27 February 2019 OSU CSE 5

  6. Example: A BL Statement WHILE true DO move WHILE infect TRUE END WHILE BLOCK You should see the connections! (This is an CALL CALL move infect actual AST for BL; notice it uses a different “design”.) 27 February 2019 OSU CSE 6

  7. BL Statement Kinds instruction IF test THEN IF test THEN WHILE test DO BLOCK BLOCK BLOCK END IF ELSE END WHILE BLOCK END IF 27 February 2019 OSU CSE 7

  8. BL Statement Kinds instruction IF test THEN IF test THEN WHILE test DO BLOCK BLOCK BLOCK END IF ELSE END WHILE Any sequence of zero or BLOCK more statements nested in an IF or WHILE construct END IF is called a block . 27 February 2019 OSU CSE 8

  9. CALL Statement BL Example AST turnleft CALL turnleft 27 February 2019 OSU CSE 9

  10. IF Statement BL Example AST IF IF next-is-enemy THEN NEXT_IS_ENEMY turnleft move END IF BLOCK CALL CALL turnleft move 27 February 2019 OSU CSE 10

  11. IF_ELSE Statement BL Example AST IF_ELSE IF next-is-enemy THEN NEXT_IS_ENEMY turnleft ELSE move BLOCK BLOCK END IF CALL CALL turnleft move 27 February 2019 OSU CSE 11

  12. WHILE Statement BL Example AST WHILE WHILE next-is-enemy DO NEXT_IS_ENEMY turnleft move END WHILE BLOCK CALL CALL turnleft move 27 February 2019 OSU CSE 12

  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

  14. Why BLOCK ? If it’s not clear, draw the • Draw the AST for this code with and AST for this code with without the intermediate notion of BLOCK : and without BLOCK : IF next-is-empty THEN IF next-is-empty THEN move move turnright ELSE ELSE turnright infect infect END IF END IF 27 February 2019 OSU CSE 14

  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

  16. This mathematical 3-tuple of AST Node Labels information (of which either test or call might be relevant, depending on the • An AST for BL is a tree of ... what? kind ) will be called a STATEMENT_LABEL . • 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

  17. AST Node Labels The mathematical model of an AST for a BL statement is therefore a • An AST for BL is a tree of ... what? tree of STATEMENT_LABEL . • 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

  18. Resources • Wikipedia: Abstract Syntax Tree – http://en.wikipedia.org/wiki/Abstract_syntax_tree 27 February 2019 OSU CSE 18

Recommend


More recommend