1 / 20

Miscellaneous Topics

Deque Recursion and Grammars. Miscellaneous Topics. Other Data Structures. Deque - a double-ended queue Balanced Trees There are several ADTs for trees that keep the tree balanced at all times. Deque. A deque is a double-ended queue

vadin
Télécharger la présentation

Miscellaneous Topics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Deque Recursion and Grammars Miscellaneous Topics CS 225

  2. CS 225 Other Data Structures • Deque - a double-ended queue • Balanced Trees • There are several ADTs for trees that keep the tree balanced at all times

  3. CS 225 Deque • A deque is a double-ended queue • Like a queue except that both insertion and removal can occur from either end • Both stacks and queues can be implmented using a deque • Can be implemented using either dynamic arrays or doubly-linked lists

  4. CS 225 Deque Operations • Insert at back • insert at front • remove last • remove first • peek first • peek last

  5. CS 225 Deques in Java • Deque Interface (Java 1.6) • ArrayDeque( Java 1.6) • LinkedList implements a doubly-linked list so it has all the deque operations

  6. CS 225 Grammars and Recursion • A grammar is a formal description of a language • For natural languages, this is hard • In order to be able to use a program to translate a program, programming languages need to be relatively simple. • Regular expressions can be used to specify the simplest grammars • BNF is a notation that was invented to describe programming languages

  7. CS 225 Backus-Naur Form • Generally referred to as BNF • Most widely known method for describing programming language syntax • BNF description of a language consists of a set of rules that can be used to generate statements in the language

  8. CS 225 BNF Rules • Left hand side of a rule is a non-terminals; something that is built from smaller pieces • there may be several rules for a single non-terminal • Right hand side of a rule consists of terminals and other non-terminals in the order they need to occur • Terminals are typically keywords and symbols • A grammar is a collection of rules

  9. CS 225 Sample Grammar • <S> -> <A><B> • <S> -> <B> • <A> -> aa<A> • <A> -> a • <B> -> b<B> • <B> -> b • <S> is known as the start symbol

  10. CS 225 Derivations • BNF is a generative device • Use a grammar to generate sentences that belong to the language the grammar describes • A derivation is a repeated application of rules, starting with the start symbol and ending with a sentence (all terminal symbols)

  11. CS 225 Sample Derivations <S> -> <B> -> b <S> -> <A><B> -> a<B>-> ab <S> -> <A><B> -> aa<A><B> -> aaaa<A><B> -> aaaaa<B> -> aaaaab<B> -> aaaaabb<B> -> aaaaabbb

  12. CS 225 Extended BNF • Optional parts are placed in brackets [ ] <proc_call> -> ident [(<expr_list>)] • Alternative parts of RHSs are placed inside parentheses and separated via vertical bars <term> → <term>(+|-) const • Repetitions (0 or more) are placed inside braces { } <ident> → letter {letter|digit}

  13. CS 225 Sample Grammar • <S> -> <A><B> | <B> • <A> -> aa<A> | a • <B> -> b<B> | b

  14. CS 225 • What does this have to do with recursion? • One of the common techniques for parsing a program (checking its syntax and putting it into a form that can be translated into an executable format) uses the BNF rules to implement a set of recursive methods

  15. CS 225 Recursive-Descent Parsing • There is a method for each nonterminal in the grammar • If there are several rules, the method needs to determine which to use • The method checks that each element in the rule is present • EBNF is ideally suited for being the basis for a recursive-descent parser, because EBNF minimizes the number of nonterminals

  16. CS 225 Recursive-Descent Methods • For a single rule: • For each terminal symbol in the RHS, compare it with the next input token; if they match, continue, else there is an error • For each nonterminal symbol in the RHS, call its associated parsing subprogram

  17. CS 225 Recursive-Descent Methods • A nonterminal that has more than one RHS requires an initial process to determine which RHS it is to parse • The correct RHS is chosen on the basis of the next token of input • The next token is compared with the first token that can be generated by each RHS until a match is found • If no match is found, it is a syntax error

  18. CS 225 Example • Our sample grammar would have three methods • S() • A() • B() • Algorithm for S() if next is a call A() call B()

  19. CS 225 Algorithm for A() checks for an a if present check for a second a if present call A if next is a get a else fail

  20. CS 225 Algorithm for B() checks for an b if present check for a second b if present call B else fail

More Related