DOC PREVIEW
Penn CIT 594 - Recognizers

This preview shows page 1-2-3-27-28-29 out of 29 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

RecognizersParsers and recognizersBuilding a recognizerReview of BNFRecognizing simple alternatives, IRecognizing simple alternatives, IIJava codeHelper methodsRecognizing simple alternatives, IIIFirst implementation of symbolImplementing symbolnextTokenMatches #1nextTokenMatches #2addOperator repriseSequences, ISequences, IIImplementing a fancier pushBack()Sequences, IIISequences, IVSimple sequences in Javafalse vs. errorSequences and alternativesRecursion, IRecursion, IIExtended BNF—optional partsExtended BNF—zero or moreBack to parsersConclusionsThe EndJan 15, 2019Recognizers2Parsers and recognizersGiven a grammar (say, in BNF) and a string,A recognizer will tell whether the string belongs to the language defined by the grammarA parser will try to build a tree corresponding to the string, according to the rules of the grammarInput string Recognizer result Parser result2 + 3 * 4 true2 + 3 * false Error3Building a recognizerOne way of building a recognizer from a grammar is called recursive descentRecursive descent is pretty easy to implement, once you figure out the basic ideasRecursive descent is a great way to build a “quick and dirty” recognizer or parserProduction-quality parsers use much more sophisticated and efficient techniquesIn the following slides, I’ll talk about how to do recursive descent, and give some examples in Java4Review of BNF“Plain” BNF< > indicate a nonterminal that needs to be further expanded, for example, <variable>Symbols not enclosed in < > are terminals; they represent themselves, for example, if, while, (The symbol ::= means is defined asThe symbol | means or; it separates alternatives, for example, <addop> ::= + | -Extended BNF[ ] enclose an optional part of the ruleExample:<if statement> ::= if ( <condition> ) <statement> [ else <statement> ]{ } mean the enclosed can be repeated zero or more timesExample:<parameter list> ::= ( ) | ( { <parameter> , } <parameter> )5Recognizing simple alternatives, IConsider the following BNF rule:<add_operator> ::= + | -That is, an add operator is a plus sign or a minus signTo recognize an add operator, we need to get the next token, and test whether it is one of these charactersIf it is a plus or a minus, we simply return trueBut what if it isn’t?We not only need to return false, but we also need to put the token back because it doesn’t belong to us, and some other grammar rule probably wants itOur tokenizer needs to be able to take back tokensUsually, it’s enough to be able to put just one token backMore complex grammars may require the ability to put back several tokens6Recognizing simple alternatives, IIOur rule is <add_operator> ::= + | -Our method for recognizing an <add_operator> (which we will simply call addOperator) looks like this:public boolean addOperator() { Get the next token, call it t If t is a “+”, return true If t is a “-”, return true If t is anything else, put the token back return false}7Java codepublic boolean addOperator() { Token t = myTokenizer.next(); if (t.type == Type.SYMBOL && t.value.equals("+")) { return true; } if (t.type == Type.SYMBOL && t.value.equals("-")) { return true; } myTokenizer.pushBack(1); return false;}While this code isn’t particularly long or hard to read, we are going to have a lot of very similar methods8Helper methodsRemember the DRY principle: Don’t Repeat YourselfIf we turn each BNF production directly into Java, we will be writing a lot of very similar codeWe should write some auxiliary or “helper” methods to hide some of the details for usFirst helper method:private boolean symbol(String expectedSymbol)Gets the next token and tests whether it matches the expectedSymbolIf it matches, returns trueIf it doesn’t match, puts the symbol back and returns falseWe’ll look more closely at this method in a moment9Recognizing simple alternatives, IIIOur rule is <add_operator> ::= + | -Our pseudocode is:public boolean addOperator() { Get the next token, call it t If t is a “+”, return true If t is a “-”, return true If t is anything else, put the token back return false}Thanks to our helper method, our actual Java code is:public boolean addOperator() { return symbol("+") || symbol("-");}10First implementation of symbolHere’s what symbol does:Gets a tokenMakes sure that the token is a symbolCompares the symbol to the desired symbol (by value)If all the above is satisfied, returns true Else (if not satisfied) puts the token back, and returns falseprivate boolean symbol(String value) { Token t = tokenizer.next(); if (t.type == Type.SYMBOL && value.equals(t.value())) { return true; } else { tokenizer.pushBack(1); return false; }}11Implementing symbolWe can implement methods name, number, and maybe eol the same wayAll this code will look pretty much alikeThe main difference is in checking for the typeThe DRY principle suggests we should use a helper method for symbolprivate boolean symbol(String expectedValue) { return nextTokenMatches(Type.SYMBOL, expectedValue);}12nextTokenMatches #1The nextTokenMatches method should:Get a tokenCompare types and valuesReturn true if the token is as expectedPut the token back and return false if it doesn’t matchprivate boolean nextTokenMatches(Type type, String value) { Token t = tokenizer.next(); if (type == t.type() && value.equals(t.value())) { return true; } else { tokenizer.pushBack(1); return false; }}13nextTokenMatches #2The previous method is fine for symbols, but what if we only care about the type?For example, we want to get a number—any numberWe need to compare only type, not valueprivate boolean nextTokenMatches(Type type, String value) { Token t = tokenizer.next(); omit this parameter if (type == t.type() && value.equals(t.getValue())) return true; else tokenizer.pushBack(1); omit this test return false;}It’s easier to overload nextTokenMatches than to combine the two versions, and both versions are fairly short, so we are probably better off with the code duplication14addOperator reprisepublic boolean addOperator() { return symbol("+") || symbol("-");}private boolean symbol(String


View Full Document

Penn CIT 594 - Recognizers

Documents in this Course
Trees

Trees

17 pages

Searching

Searching

24 pages

Pruning

Pruning

11 pages

Arrays

Arrays

17 pages

Stacks

Stacks

30 pages

Recursion

Recursion

25 pages

Hashing

Hashing

24 pages

Recursion

Recursion

24 pages

Graphs

Graphs

25 pages

Storage

Storage

37 pages

Trees

Trees

21 pages

Arrays

Arrays

24 pages

Hashing

Hashing

24 pages

Recursion

Recursion

25 pages

Graphs

Graphs

23 pages

Graphs

Graphs

25 pages

Stacks

Stacks

25 pages

Recursion

Recursion

25 pages

Quicksort

Quicksort

21 pages

Quicksort

Quicksort

21 pages

Graphs

Graphs

25 pages

Recursion

Recursion

25 pages

Searching

Searching

24 pages

Counting

Counting

20 pages

HTML

HTML

18 pages

Recursion

Recursion

24 pages

Pruning

Pruning

11 pages

Graphs

Graphs

25 pages

Load more
Download Recognizers
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Recognizers and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Recognizers 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?