c# barcode generator library open source The Calculator Parser in Font

Making Data Matrix 2d barcode in Font The Calculator Parser

The Calculator Parser
Encoding Data Matrix 2d Barcode In None
Using Barcode creator for Font Control to generate, create ECC200 image in Font applications.
www.OnBarcode.com
Encode UPC-A Supplement 5 In None
Using Barcode creator for Font Control to generate, create UPC Symbol image in Font applications.
www.OnBarcode.com
In this section, we re going to using the parser combinator to build a four-function calculator. Yep, it s time to swat flies with a Buick. You ll see how easy it is to describe what we want to build, create a Parser for it, and then make sure the Parser returns the correct things. But first, let s define a utility trait that will allow us to more easily run the Parsers from the Scala REPL. The RunParser trait can be mixed into any Parser and adds a run method.
Encoding UCC - 12 In None
Using Barcode encoder for Font Control to generate, create EAN / UCC - 14 image in Font applications.
www.OnBarcode.com
Data Matrix 2d Barcode Creation In None
Using Barcode generation for Font Control to generate, create Data Matrix ECC200 image in Font applications.
www.OnBarcode.com
import scala.util.parsing.combinator._ trait RunParser { this: RegexParsers => type RootType def root: Parser[RootType] def run(in: String): ParseResult[RootType] = parseAll(root, in) }
Print Quick Response Code In None
Using Barcode printer for Font Control to generate, create Denso QR Bar Code image in Font applications.
www.OnBarcode.com
UPC - 13 Generation In None
Using Barcode maker for Font Control to generate, create EAN / UCC - 13 image in Font applications.
www.OnBarcode.com
The RunParser trait can be mixed into a class that extends RegexParsers. By mixing RunParser into your Parser, you can type MyParser.run("Thing to test") and see the result. It s a convenience trait.
Printing Code 128 In None
Using Barcode generation for Font Control to generate, create ANSI/AIM Code 128 image in Font applications.
www.OnBarcode.com
Drawing RM4SCC In None
Using Barcode creation for Font Control to generate, create RM4SCC image in Font applications.
www.OnBarcode.com
CHAPTER 8 PARSERS BECAUSE BNF IS NOT JUST FOR ACADEMICS ANYMORE
Encode DataMatrix In None
Using Barcode generation for Online Control to generate, create Data Matrix ECC200 image in Online applications.
www.OnBarcode.com
Encoding Data Matrix ECC200 In .NET Framework
Using Barcode generation for .NET Control to generate, create Data Matrix 2d barcode image in .NET applications.
www.OnBarcode.com
We ll define the skeleton of our four-function calculator. Let s first describe how our calculator works. A sum expression is a product expression followed by zero or more + or symbols followed by a product expression. A product expression is a factor followed by zero or more * or / symbols followed by another factor. This means that the precedence of production expressions is higher than the precedence of sum expressions. Finally, we define a factor as a number or parentheses around a sum expression. In BNF,3 we d write
Generating PDF 417 In None
Using Barcode printer for Office Excel Control to generate, create PDF417 image in Excel applications.
www.OnBarcode.com
Generate EAN13 In .NET
Using Barcode generation for Reporting Service Control to generate, create GS1 - 13 image in Reporting Service applications.
www.OnBarcode.com
<sumExpr> ::= <prodExpr> [("+" <prodExpr>) | ("-" <prodExpr>)] <prodExpr> ::= <factor> [("*" <factor>) | ("/" <factor>)] <factor> ::= <float> | ("(" <sumExpr> ")")
Read Barcode In .NET
Using Barcode reader for .NET framework Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Decode Code 128B In .NET Framework
Using Barcode scanner for .NET framework Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
We ve described our parsing rules in English and BNF. Now let s see how that translates to Scala.
Linear 1D Barcode Encoder In Visual Studio .NET
Using Barcode encoder for ASP.NET Control to generate, create Linear 1D Barcode image in ASP.NET applications.
www.OnBarcode.com
Decode Barcode In VB.NET
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
object CalcSkel extends JavaTokenParsers with RunParser { lazy val sumExpr = multExpr ~ rep("+" ~ multExpr | "-" ~ multExpr) lazy val multExpr = factor ~ rep("*" ~ factor | "/" ~ factor) lazy val factor: Parser[Any] = floatingPointNumber | "(" ~ sumExpr ~ ")" type RootType = Any def root = sumExpr }
Create Barcode In None
Using Barcode creation for Microsoft Word Control to generate, create Barcode image in Office Word applications.
www.OnBarcode.com
GS1 RSS Maker In Visual Studio .NET
Using Barcode generator for Visual Studio .NET Control to generate, create GS1 DataBar Limited image in .NET applications.
www.OnBarcode.com
We ve extended JavaTokenParsers, which gives us access to a bunch of stuff that will parse tokens as defined by the Java Language Specification.4 We re taking advantage of floatingPointNumber and automatic white space consumption between elements. Cool. Let s see how this works in the REPL.
USS-128 Reader In Visual Basic .NET
Using Barcode recognizer for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Print Code 3/9 In VS .NET
Using Barcode creator for Reporting Service Control to generate, create ANSI/AIM Code 39 image in Reporting Service applications.
www.OnBarcode.com
scala> CalcSkel.run("1")
res0: [1.2] parsed: ((1~List())~List())
scala> CalcSkel.run("1 + 1")
3. BNF stands for Backus-Naur Form, a common way to describe grammars. See http:// en.wikipedia.org/wiki/Backus Naur_form. 4. http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html
CHAPTER 8 PARSERS BECAUSE BNF IS NOT JUST FOR ACADEMICS ANYMORE
res1: [1.6] parsed: ((1~List())~List((+~(1~List()))))
scala> CalcSkel.run("1 + 1 / 17")
res2: [1.11] parsed: ((1~List())~List((+~(1~List((/~17))))))
scala> CalcSkel.run("1 + 1 / archer")
res3: CalcSkel.ParseResult[CalcSkel.RootType] = [1.9] failure: `(' expected but ` ' found 1 + 1 / archer ^
This is pretty encouraging. Our English and BNF descriptions of what we wanted to parse correspond very closely to our Scala code. Furthermore, our parse correctly parses valid input and rejects input with errors in it. The results, however, are pretty tough to read. Next, let s turn the results into something that performs the calculations. Our Parser doesn t change, but we add a function to convert the parsed items into a Double. First comes Listing 8-1, and then we ll comb through the code.
Listing 8-1. Calculator Parser import scala.util.parsing.combinator._ object Calc extends JavaTokenParsers with RunParser { lazy val sumExpr = prodExpr ~ rep("+" ~> prodExpr ^^ (d => (x: Double) => x + d) | "-" ~> prodExpr ^^ (d => (x: Double) => x - d)) ^^ { case seed ~ fs => fs.foldLeft(seed)((a, f) => f(a)) }
CHAPTER 8 PARSERS BECAUSE BNF IS NOT JUST FOR ACADEMICS ANYMORE
lazy val prodExpr = factor ~ rep("*" ~> factor ^^ (d => (x: Double) => x * d) | "/" ~> factor ^^ (d => (x: Double) => x / d)) ^^ { case seed ~ fs => fs.foldLeft(seed)((a, f) => f(a)) } lazy val factor: Parser[Double] = floatingPointNumber ^^ (_.toDouble) | "(" ~> sumExpr <~ ")" type RootType = Double def root = sumExpr }
First we import the appropriate classes and then get down to business:
import scala.util.parsing.combinator._ object Calc extends JavaTokenParsers with RunParser { lazy val sumExpr = prodExpr ~ rep("+" ~> prodExpr ^^ (d => (x: Double) => x + d) | "-" ~> prodExpr ^^ (d => (x: Double) => x - d)) ^^ { case seed ~ fs => fs.foldLeft(seed)((a, f) => f(a)) }
The rep method results in a List of whatever is parsed by the parameter of rep. When we match the + ~> prodExpr, we convert this into a function that adds the two numbers. Please note the ~> method. This method matches both items but only passes the stuff on the right to the converter function. There s a corresponding <~ operator. Back to the code. We ve got a prodExpr, which is a Parser[Double] and then a Parser[List[Double => Double]], and we need to convert this into a Parser[Double]. The line
case seed ~ fs => fs.foldLeft(seed)((a, f) => f(a))
extracts the seed and the list of functions (add or subtract) and uses foldLeft to perform the calculation. We do the same for multiplication and division:
lazy val prodExpr = factor ~ rep("*" ~> factor ^^ (d => (x: Double) => x * d) | "/" ~> factor ^^ (d => (x: Double) => x / d)) ^^ { case seed ~ fs => fs.foldLeft(seed)((a, f) => f(a)) }
Copyright © OnBarcode.com . All rights reserved.