Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2007CS 2251 Miscellaneous Topics Deque Recursion and Grammars.

Similar presentations


Presentation on theme: "Fall 2007CS 2251 Miscellaneous Topics Deque Recursion and Grammars."— Presentation transcript:

1 Fall 2007CS 2251 Miscellaneous Topics Deque Recursion and Grammars

2 Fall 2007CS 2252 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 Fall 2007CS 2253 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 Fall 2007CS 2254 Deque Operations Insert at back insert at front remove last remove first peek first peek last

5 Fall 2007CS 2255 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 Fall 2007CS 2256 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 Fall 2007CS 2257 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 Fall 2007CS 2258 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 Fall 2007CS 2259 Sample Grammar -> -> aa -> a -> b is known as the start symbol

10 Fall 2007CS 22510 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 Fall 2007CS 22511 Sample Derivations -> -> b -> -> a -> ab -> -> aa -> aaaa -> aaaaa -> aaaaab -> aaaaabb -> aaaaabbb

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

13 Fall 2007CS 22513 Sample Grammar -> | -> aa | a -> b | b

14 Fall 2007CS 22514 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 Fall 2007CS 22515 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 Fall 2007CS 22516 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 Fall 2007CS 22517 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 Fall 2007CS 22518 Example Our sample grammar would have three methods –S() –A() –B() Algorithm for S() if next is a call A() call B()

19 Fall 2007CS 22519 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 Fall 2007CS 22520 Algorithm for B() checks for an b if present check for a second b if present call B else fail


Download ppt "Fall 2007CS 2251 Miscellaneous Topics Deque Recursion and Grammars."

Similar presentations


Ads by Google