{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Parsing Notes

Parsing Notes - Top-Down Parsing C8236 Data-Structure Based...

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Top-Down Parsing C8236 Data-Structure Based vs. Control-Flow Based 26 Jan 2012 Start with a grammar and a string to parse: Grammar: Note: Input String: E —) OEE I N Non-terminals are *+132 shown in purple, O _, + I * and termlnals ln blue for illustrative N _, 1 | 2 | 3 purposes. Next, create a parse tree and parse table to show that the input string is parse-able with the given grammar. Parse Tree Parse Table E O N + a): 1 2 3 # Data-Structure Based Approach: Stack Contents Remaining Input String Partial Parse Tree Red arrow represents next character. Push the start variable onto the stack: E ’ Pop E Push OEE (From parse table) Evaluation is left child ' first, followed by right siblings. Pop 0 Push * (From parse table) 6| (Continued) “_ Stack Contents _emaining Input String _artial Parse Tree Red arrow represents next character. Pop* Consume the first character in the input string. +132 continue this process until the input string is exhaused, the stack is empty, and the parse table is complete. The final parse table will look like the one on the first page. What classes would we need to implement the above process? Stack, with methods for push and mp. Lexer, with methods read and advance. ParseTable with method that returns stack and lexer control and takes the top of stack symbol and the input character and Lexer token. The key data flow concept is that a non-terminal at the top of the stack determines the order in which terminals and non-terminals are processed. (:3 The order of processing the non-terminals is as shown. ALL (:2 of 0's children are processed ‘- before the E that follows. -op of Sta-k Control-Flow Based Approach: Recursive descent version #1 (following the lecture notes): Represent each non-terminal and terminal as a common class. Let this class explicit represent the left-child right sibling order. @CN I calls this object which calls its children (Continued) p. 3 We won't use a stack class, but we will use the call stack (a portion of computer memory managed by the operating system). e.g. class parseTree { char value; parseTree leftChild; parseTree rightSibling; public class parseTree( , { this.value = / this.leftChild = this.rightSibling = 2 D. E «S X LIJ g as h E, O, and N (from the non-terminals in the grammar) will all be of type parseTree. So will each of the terminals. Call Stack: Input ( = Lexer output) The parse tree looks like this As shown in the lecture notes' slides, E calls other methods depending on whether the Lexer output is {*,+}0r{1,2,3}. E.IeftChild = O; E.rightSiinng = NULL; 0.rightSibling = E; 0.rightSibling.rightSibling = E; In the methods for O, we create a new parseTree with value = "*" and with no children or siblings. O has nothing to call so it returns. We advance the Lexer and proceed to evaluate O's right sibling: Note that the * has —-m been consumed from the input string. V (Continued) Stack Remaining Input The parse tree looks like this Notice the change in ' the stack contents: consumed. Note that the plus is . I a a (Continued) p. 5 up progression Notice the stack I o e r 0 and so on Recursive descent version #2 (From the example of parsing student grades from MISC on the class webpage.) No need to actually have classes for non-terminals. Keep track of children and right sibling. Just make sure the execution order works. ...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern