200 likes | 278 Views
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
E N D
Deque Recursion and Grammars Miscellaneous Topics CS 225
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
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
CS 225 Deque Operations • Insert at back • insert at front • remove last • remove first • peek first • peek last
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
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
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
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
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
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)
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
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}
CS 225 Sample Grammar • <S> -> <A><B> | <B> • <A> -> aa<A> | a • <B> -> b<B> | b
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
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
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
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
CS 225 Example • Our sample grammar would have three methods • S() • A() • B() • Algorithm for S() if next is a call A() call B()
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
CS 225 Algorithm for B() checks for an b if present check for a second b if present call B else fail