07-RecursiveDescent

# E oee produces e o e e discussion 7 chapter 10

• Notes
• 12

This preview shows page 5 - 10 out of 12 pages.

E OEE produces: E O E E

Subscribe to view the full document.

Discussion #7 Chapter 10, Section 2 6/12 Recursive Descent Parsing Example Consider our prefix grammar: E N | OEE O + | - | * | / N 0 | 1 | … | 9 Design a series of recursive methods: E() to process N or O, E, E O() to process +, - , *, / N() to process numbers 0 thru 9
Discussion #7 Chapter 10, Section 2 7/12 Data Structure for Parse Tree class parseTree { char value parseTree leftChild parseTree rightSibling };

Subscribe to view the full document.

Discussion #7 Chapter 10, Section 2 8/12 Initialization: Call to Start Symbol parseTree buildTree() // build parse tree { nextChar = readChar() // read 1st character ptree = E() // start syntactic class if (ptree == error) return error if (nextChar) return error // check for string finished return ptree // return the full parse tree }
Discussion #7 Chapter 10, Section 2 9/12 Method for E parseTree E() // syntactic category E { parseTree ptree,sibling1,sibling2 if (isCharInString(nextChar, "+-*/")) // E -> OEE {FIRST(OEE)} { ptree = O() // Try to recognize O if (ptree == error) return error sibling1 = E() // Try to recognize E if (sibling1 == error) return error sibling2 = E() // Try to recognize E if (sibling2 == error) return error ptree.rightSibling = sibling1 // Success, link O->E->E ptree.rightSibling.rightSibling = sibling2 } else if (isCharInString(nextChar, "0123456789")) // E -> N { ptree = N() // Try to recognize N if (ptree == error) return error } else return error return new parseTree('E', ptree, null) }

Subscribe to view the full document.

You've reached the end of this preview.
• Winter '12
• MichaelGoodrich
• Formal grammar, Top-down parsing, Recursive descent parser, recursive descent, ptree

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern