Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10RexEx and Parsing10-31-2011Opening DiscussionDo you have any questions about the quiz?Minute essay comments:RegEx with multiple parts. Doubles.Help with all types of parsing.Performance of RegEx.For Loops and RegExRemember that for-loops do pattern matches for storing values. They also skip anything that doesn't match the pattern.This makes them ideal when running through the results of findAllIn.Examples of RegExLet's run through some different examples of using regular expressions.Decimal numbersPoints in 2-D or 3-DDatesPolynomialsCF Grammars and Internal DSLsThere are times when you might want to include elements in your programs that go beyond regular grammars.An example of this would be an internal DSL (Domain Specific Language). This is like a little language that is understood in your program.Mathematical formulas count as these, but so would simple commands that have some structure to them.Example CF GrammarHere is a CF grammar for math expressions:expr ::= term { “+” term | “-” term }term ::= factor { “*” factor | “/” factor }factor ::= floatingPointNumber | “(“ expr “)”Use {} for 0 or more and [] for 0 or 1.Lots of languages here:http://www.antlr.org/grammar/listScala Parsersimport scala.util.parsing.combinator._class Arith extends JavaTokenParsers {def expr:Parser[Any] = term~rep(“+”~term | “-”~term)def term:Parser[Any] = factor~rep(“*”~factor | “/”~factor)def factor:Parser[Any] = floatingPointNumber | “(“~expr~”)”}Conversion RulesPut in a class that extends one of the Parsers.Productions become methods.Results are Parsers. Next class we'll see how to make it more specific than Any.Consecutive symbols are adjoined with ~.The {...} is replaced with rep(...).The […] is replaced with opt(...).Using the ParserCall parseAll or parse on your class.Takes two arguments:First argument is the parser to use.Second argument is the string to parse.Let's code this all up and see it in action.Minute EssayQuestions? Can you think of anyplace you might use
View Full Document