Parse Tree is a tree that represents the

  • Slides: 11
Download presentation
Parse Tree is a tree that represents the syntactic structure of a string according

Parse Tree is a tree that represents the syntactic structure of a string according to some formal grammar. – – is made up of nodes and branches. Saman Amarasinghe 1 6. 035 ©MIT Fall 2006

Parse Tree Example: Parse Tree (Left Associative) Given: (9 -5)+2 (9 - String 5)

Parse Tree Example: Parse Tree (Left Associative) Given: (9 -5)+2 (9 - String 5) + 2 String Saman Amarasinghe 2 6. 035 ©MIT Fall 2006

Parse Tree Example: Parse Tree (Right Associative) Given: (9 -5)+2 (9 - 5) +

Parse Tree Example: Parse Tree (Right Associative) Given: (9 -5)+2 (9 - 5) + String 2 String Saman Amarasinghe 3 6. 035 ©MIT Fall 2006

Annotated Parse Tree Example: Suppose a robot can be instructed to move east, north,

Annotated Parse Tree Example: Suppose a robot can be instructed to move east, north, west from its current position, construct the annotated parse tree of the given problem. seq. x=0 seq. y=1 instr. dx=-1 instr. dy=0 seq. x=1 seq. y=1 seq. x=1 seq. y=0 instr. dx=0 instr. dy=1 seq. x=0 seq. y=0 instr. dx=1 instr. dy=0 Begin East Saman Amarasinghe 4 West North 6. 035 ©MIT Fall 2006

Stack linearly ordered set of elements having the last-in, first-out (LIFO) discipline – Operations

Stack linearly ordered set of elements having the last-in, first-out (LIFO) discipline – Operations are done on top of the stack only and we have no access to other elements. – Basic operations: push and pop. – Representation: sequential or linked. – Saman Amarasinghe 5 6. 035 ©MIT Fall 2006

Stack Example: push a push b pop b a a a Top Stack Saman

Stack Example: push a push b pop b a a a Top Stack Saman Amarasinghe Top Stack 6 6. 035 ©MIT Fall 2006

Converting Infix to Postfix Expression Example: A+B*C Symbol A + Postfix String A A

Converting Infix to Postfix Expression Example: A+B*C Symbol A + Postfix String A A B AB * C AB ABC*+ Saman Amarasinghe 7 OPSTK + + +* +* + 6. 035 ©MIT Fall 2006

Converting Infix to Postfix Expression Example: A+B-C*D Symbol Postfix String A A + A

Converting Infix to Postfix Expression Example: A+B-C*D Symbol Postfix String A A + A B C * D AB AB ABC ABCD*- OPSTK + + +++-* ++ ABCD*-+ Saman Amarasinghe 8 6. 035 ©MIT Fall 2006

Converting Infix to Postfix Expression Example: (A+B)*C Symbol ( A Postfix String + B

Converting Infix to Postfix Expression Example: (A+B)*C Symbol ( A Postfix String + B ) A AB OPSTK ( ( (+ (+ AB+ AB+C * * * C A AB+C* Saman Amarasinghe 9 6. 035 ©MIT Fall 2006

Converting Infix to Postfix Expression Example: (2+3)*5 Symbol ( 2 Postfix String + 3

Converting Infix to Postfix Expression Example: (2+3)*5 Symbol ( 2 Postfix String + 3 ) 2 23 OPSTK ( ( (+ (+ 23+ 23+5 * * * 5 2 23+5* Saman Amarasinghe 10 6. 035 ©MIT Fall 2006

Evaluating Postfix Expression Example: 23+5* Symbol 2 3 OPND 1 OPND 2 Value OPNDSTK

Evaluating Postfix Expression Example: 23+5* Symbol 2 3 OPND 1 OPND 2 Value OPNDSTK 2 2, 3 + 5 * 2 3 5 5 5 25 5 5, 5 25 Saman Amarasinghe 11 6. 035 ©MIT Fall 2006