Constructing a Parse Tree Initial Conditions Before we begin we - - PowerPoint PPT Presentation

constructing a parse tree initial conditions
SMART_READER_LITE
LIVE PREVIEW

Constructing a Parse Tree Initial Conditions Before we begin we - - PowerPoint PPT Presentation

Constructing a Parse Tree Initial Conditions Before we begin we need to have two things: 1. A Grammar 2. A string we wish to parse So then what is the procedure: 1. Use the grammar to produce a derivation resulting in the given string 2. Use


slide-1
SLIDE 1

Constructing a Parse Tree

slide-2
SLIDE 2

Initial Conditions

Before we begin we need to have two things:

  • 1. A Grammar
  • 2. A string we wish to parse

So then what is the procedure:

  • 1. Use the grammar to produce a derivation resulting in the given

string

  • 2. Use the derivation to produce the parse tree
slide-3
SLIDE 3

Example 1:

We have the following grammar: <S> ::= <round> <square> | <outer> <round> ::= ( <round> ) | ( ) <square> ::= [ <square> ] | [ ] <outer> ::= ( <outer> ] | ( <inner> ] <inner> ::= ) <inner> [ | ) [ We will derive the following string: (())[[]]

slide-4
SLIDE 4

Example 1: Producing the derivation

A derivation is produce using the following steps:

  • 1. Start with the start rule and select one of its options
  • 2. We then continue to replace each rule until we reach a terminal

working from left to right.

  • 3. We repeat step 2 until all non-terminals are replaced by

terminals, and we have produced the target string. Goal: Derive ( ( ) ) [ [ ] ] <S> => <round> <square> => ( <round> ) <square> => ( ( ) ) <square> => ( ( ) ) [ <square> ] => ( ( ) ) [ [ ] ] --> Finished

slide-5
SLIDE 5

Example 1: Producing the parse tree

<S> => <round> <square> <S> / \ <round> <square> => ( <round> ) <square> / | \ / | \ ( <round> ) / | \ => ( ( ) ) <square> / \ | | \ ( ) | | \ => ( ( ) ) [ <square> ] [ <square> ] => ( ( ) ) [ [ ] ] / \ [ ]

slide-6
SLIDE 6

Example 2

Grammar: <S1> ::= <S1> + <S2> | <S2> <S2> ::= <S2> * <S3> | <S3> <S3> ::= ( <S1> ) | a | b | c String: a + b * c

slide-7
SLIDE 7

Example 2: Derivation

Goal: Derive a + b * c <S1> => <S1> + <S2> => <S2> + <S2> => <S3> + <S2> => a + <S2> => a + <S2> * <S3> => a + <S3> * <S3> => a + b * <S3> => a + b * c

slide-8
SLIDE 8

Example 2: Parse tree

<S1> => <S1> + <S2> <S1> / | \ <S1> + <S2> => <S2> + <S2> | / | \ <S2> / | \ => <S3> + <S2> | / | \ <S3> | | | => a + <S2> | | | | a | | | => a + <S2> * <S3> <S2> * <S3> => a + <S3> * <S3> | | <S3> | => a + b * <S3> | | b | => a + b * c c

slide-9
SLIDE 9

We can also derive a string from the bottom up

Goal: Use Example 2 grammar to derive a + b * c In This case we start with the rightmost terminal and continue to replace with non-terminals until we reach the start rule. => a + b * c => a + b * <S3> => a + <S3> * <S3> => a + <S2> * <S3> => a + <S2> => <S3> + <S2> => <S2> + <S2> => <S1> + <S2> <S1> => <S1> + <S2> We then build the parse tree starting from the bottom

slide-10
SLIDE 10

Constructing Abstract Syntax Trees

slide-11
SLIDE 11

Parse Trees to ASTs

An Abstract Syntax Tree (AST) is a simplified form of a Parse Tree which is useful for interpreting/converting code from one language to another. Thus, it is useful for the compiling process. An AST typically is a Binary Tree and requires that there are no non-terminal symbols left in the tree, and this then requires that we have the following:

◮ A Parse Tree ◮ A notion of traversing the tree (we will assume an In Order

traversal)

slide-12
SLIDE 12

The Conversion Process

<S1> / | \ <S1> + <S2> | / | \ <S2> / | \ | / | \ <S3> | | | | | | | a | | | <S2> * <S3> | | <S3> | | | b | c

slide-13
SLIDE 13

Example

<S1> <S1> / | \ / | \ <S1> + <S2> a + <S2> | / | \ / | \ <S2> / | \ / | \ | / | \ / | \ <S3> | | | | | | | | | | | | | a | | | | | | <S2> * <S3> <S2> * <S3> | | | | <S3> | <S3> | | | | | b | b | c c

slide-14
SLIDE 14

Example

<S1> <S1> / | \ / | \ <S1> + <S2> a + <S2> | / | \ / | \ <S2> / | \ b | \ | / | \ | \ <S3> | | | | | | | | | | | a | | | | | <S2> * <S3> * <S3> | | | <S3> | | | | | b | | c c

slide-15
SLIDE 15

Example

<S1> <S1> / | \ / | \ <S1> + <S2> a + <S2> | / | \ / | \ <S2> / | \ b * \ | / | \ \ <S3> | | | | | | | | | a | | | | <S2> * <S3> <S3> | | | <S3> | | | | | b | | c c

slide-16
SLIDE 16

Example

<S1> <S1> / | \ / | \ <S1> + <S2> a + <S2> | / | \ / | \ <S2> / | \ b * c | / | \ <S3> | | | | | | | a | | | <S2> * <S3> | | <S3> | | | b | c

slide-17
SLIDE 17

Example

<S1> <S1> / | \ / | \ <S1> + <S2> a + * | / | \ / \ <S2> / | \ b c | / | \ <S3> | | | | | | | a | | | <S2> * <S3> | | <S3> | | | b | c

slide-18
SLIDE 18

Example

<S1> + / | \ / \ <S1> + <S2> a * | / | \ / \ <S2> / | \ b c | / | \ <S3> | | | | | | | a | | | <S2> * <S3> | | <S3> | | | b | c

slide-19
SLIDE 19

Example

<S> / \ <round> <square> / | \ / | \ ( <round> ) / | \ / \ | | \ ( ) | | \ [ <square> ] / \ [ ]

slide-20
SLIDE 20

Example

<S> <S> / \ / \ <round> <square> <round> <square> / | \ / | \ / | \ / | \ ( <round> ) / | \ ( <round> ) / | \ / \ | | \ / \ | | \ ( ) | | \ ( ) | | \ [ <square> ] [ <square> ] / \ / \ [ ] [ ]

slide-21
SLIDE 21

Example

<S> <S> / \ / \ <round> <square> <round> <square> / | \ / | \ / | \ / | \ ( <round> ) / | \ ( ) ) / | \ / \ | | \ / | | \ ( ) | | \ ( | | \ [ <square> ] [ <square> ] / \ / \ [ ] [ ]

slide-22
SLIDE 22

Example

<S> <S> / \ / \ <round> <square> ) <square> / | \ / | \ / | \ / | \ ( <round> ) / | \ ( ( ) / | \ / \ | | \ | | \ ( ) | | \ | | \ [ <square> ] [ <square> ] / \ / \ [ ] [ ]

slide-23
SLIDE 23

Example

<S> ) / \ / \ <round> <square> ( <square> / | \ / | \ / \ / | \ ( <round> ) / | \ ( ) / | \ / \ | | \ | | \ ( ) | | \ | | \ [ <square> ] [ <square> ] / \ / \ [ ] [ ]

slide-24
SLIDE 24

Example

<S> ) / \ / \ <round> <square> ( <square> / | \ / | \ / \ / | \ ( <round> ) / | \ ( ) / | \ / \ | | \ | | | ( ) | | \ | | | [ <square> ] [ ] ] / \ / [ ] [

slide-25
SLIDE 25

Example

<S> ) / \ / \ <round> <square> ( <square> / | \ / | \ / \ / | \ ( <round> ) / | \ ( ) [ ] ] / \ | | \ / ( ) | | \ [ [ <square> ] / \ [ ]

slide-26
SLIDE 26

Example

<S> ) / \ / \ <round> <square> ( ] / | \ / | \ / \ / | \ ( <round> ) / | \ ( ) [ [ ] / \ | | \ ( ) | | \ [ <square> ] / \ [ ]

slide-27
SLIDE 27

Example

<S> ) / \ / \ <round> <square> ( ] / | \ / | \ / \ / \ ( <round> ) / | \ ( ) [ ] / \ | | \ / ( ) | | \ [ [ <square> ] / \ [ ]