- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
LEXING AND PARSING in VB.NET
CHAPTER 16 LEXING AND PARSING Data Matrix 2d Barcode Maker In VB.NET Using Barcode maker for .NET Control to generate, create ECC200 image in VS .NET applications. www.OnBarcode.comRead ECC200 In VB.NET Using Barcode decoder for .NET framework Control to read, scan read, scan image in .NET applications. www.OnBarcode.comHere is an example use: > parseEmployee line;; val it : string * string * System.DateTime * string = ("Smith", "John", 20/01/1986 00:00:00 { ... }, "Software Developer") Painting Barcode In VB.NET Using Barcode printer for .NET Control to generate, create Barcode image in VS .NET applications. www.OnBarcode.comCode128 Generation In Visual Basic .NET Using Barcode printer for VS .NET Control to generate, create Code 128 image in .NET framework applications. www.OnBarcode.comOn-Demand Reading of Files
Encode EAN128 In Visual Basic .NET Using Barcode encoder for Visual Studio .NET Control to generate, create EAN 128 image in VS .NET applications. www.OnBarcode.comQR-Code Drawer In Visual Basic .NET Using Barcode maker for VS .NET Control to generate, create QR Code ISO/IEC18004 image in .NET applications. www.OnBarcode.comYou can turn a file into an on-demand sequence of results using a sequence builder: open System.IO let readEmployees (fileName : string) = seq { use reader = File.OpenText fileName while not reader.EndOfStream do yield reader.ReadLine() |> parseEmployee } The following example takes the first three entries from an artificially generated file containing 10,000 copies of the same employee: > File.WriteAllLines("employees.txt", Array.create 10000 line);; val it : unit > let firstThree = readEmployees("employees.txt") |> Seq.take 3;; val firstThree : (string * string * System.DateTime * string) list > for (last,first,startDate,title) in firstThree do printfn "%s %s started on %A" first last startDate;; John Smith started on 20/01/1986 00:00:00 John Smith started on 20/01/1986 00:00:00 John Smith started on 20/01/1986 00:00:00 This technique is often used to do exploratory analysis of large data files. After the algorithm is refined using a prefix of the data, the analysis can then easily be run directly over the full data file. DataMatrix Generation In Visual Basic .NET Using Barcode creation for .NET framework Control to generate, create Data Matrix 2d barcode image in .NET framework applications. www.OnBarcode.comNW-7 Creator In VB.NET Using Barcode creator for VS .NET Control to generate, create Rationalized Codabar image in .NET applications. www.OnBarcode.comUsing Regular Expressions
DataMatrix Encoder In Visual C# Using Barcode creator for Visual Studio .NET Control to generate, create Data Matrix 2d barcode image in .NET framework applications. www.OnBarcode.comCreate Data Matrix 2d Barcode In None Using Barcode creator for Microsoft Excel Control to generate, create DataMatrix image in Excel applications. www.OnBarcode.comAnother technique that s frequently used to extract information from strings is to use regular expressions. The System.Text.RegularExpressions namespace provides convenient string-matching and -replacement functions. For example, let s say you have a log file containing a record of HTML GET requests. Here is a sample request: GET /favicon.ico HTTP/1.1 Make 2D Barcode In .NET Using Barcode maker for .NET framework Control to generate, create 2D image in .NET framework applications. www.OnBarcode.comDrawing Data Matrix ECC200 In Java Using Barcode drawer for Java Control to generate, create Data Matrix image in Java applications. www.OnBarcode.comCHAPTER 16 LEXING AND PARSING
EAN13 Recognizer In Java Using Barcode scanner for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comPaint PDF 417 In Visual C# Using Barcode encoder for .NET Control to generate, create PDF-417 2d barcode image in .NET applications. www.OnBarcode.comThe following code captures the name of the requested resource (favicon.ico) and the lower version number of the HTML protocol (1) used: open System.Text.RegularExpressions let parseHttpRequest line = let result = Regex.Match(line, @"GET (.* ) HTTP/1\.([01])$") let file = result.Groups.[1].Value let version = result.Groups.[2].Value file, version The relevant fields are extracted by using the Groups attribute of the regular expression match to access the matched strings for each parenthesized group in the regular expression. Scan Barcode In VS .NET Using Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications. www.OnBarcode.comEAN / UCC - 13 Maker In Visual C# Using Barcode printer for .NET Control to generate, create EAN 13 image in .NET framework applications. www.OnBarcode.comTokenizing with FsLex
Reading PDF 417 In None Using Barcode scanner for Software Control to read, scan read, scan image in Software applications. www.OnBarcode.comCode 128 Generation In Java Using Barcode encoder for Android Control to generate, create Code128 image in Android applications. www.OnBarcode.comAlthough it s possible to hand-code lexers by using a range of ad hoc techniques such as those discussed in the previous section or by writing functions that explicitly manipulate lists of characters, doing so can be boring and time consuming. Instead, it s often easier to rely on a lexer generator to do this job for you. This section looks at how to use the fslex tool that comes with the F# Power Pack to perform lexical analysis. The F# Power Pack also includes fsyacc, which is also available as a separate download from MSDN and CodePlex. When you use fslex or fsyacc generated code in your projects, you must include a library reference to FSharp.PowerPack.dll from the F# Power Pack. Let s start with a simple example. Listing 16-1 shows a lexer that replaces all < and > characters in an input stream with their HTML equivalents, < and >. Listing 16-2 shows a small program that uses this generated lexer. Listing 16-1. Replacing Characters with Their HTML Equivalents: text2htmllex.fsl { module Text2HtmlLex let lexeme = Lexing.LexBuffer<_>.LexemeString (* You can add your helper functions here *) } rule convertHtml chan = parse | '<' { fprintf chan "<" convertHtml chan lexbuf } | '>' { fprintf chan ">" convertHtml chan lexbuf } | eof { () } | _ { fprintf chan "%s" (lexeme lexbuf) convertHtml chan lexbuf } Listing 16-2. Replacing Characters with Their HTML Equivalents: text2html.fs open System.IO open System.Text let main() = Decode Barcode In Java Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comQR Drawer In None Using Barcode creation for Font Control to generate, create QR Code JIS X 0510 image in Font applications. www.OnBarcode.comCHAPTER 16 LEXING AND PARSING
let args = System.Environment.GetCommandLineArgs() if args.Length <= 2 then let exe = Path.GetFileName(args.[0]) eprintfn "Usage: %s dir pattern" exe exit 1 let directory = args.[1] let pattern = args.[2] for fileName in Directory.GetFiles(directory, pattern) do // Open a file stream for the file name use inputReader = File.OpenText fileName // Create a lex buffer for use with the generated lexer. The lex buffer // reads the inputReader stream. let lexBuffer = Lexing.LexBuffer<_>.FromTextReader inputReader // Open an output channel let outputFile = Path.ChangeExtension(fileName,"html") use outputWriter = (new StreamWriter(outputFile) :> TextWriter) // Write the header fprintfn outputWriter "<html>\n<head></head>\n<pre>" // Run the generated lexer Text2htmllex.convertHtml outputWriter lexBuffer // Write the footer fprintfn outputWriter "</pre>\n</html>\n" main() You can produce an F# source file from the previous lexer definition by running the following command at the Windows command prompt: fslex text2htmllex.fsl --unicode This produces text2htmllex.fs, which contains the implementation of the lexer convertHtml. This lexer is imperative, in that it prints to an output stream instead of returning tokens. The signature of the entry point to the generated lexer is as follows: val Text2HtmlLex.convertHtml: System.IO.TextWriter -> Lexing.Lexbuffer<char> -> unit You can now compile the driver and the lexer together: fsc text2htmllex.fs text2html.fs r FSharp.PowerPack.dll You can run the resulting program as follows, giving a source directory and a file pattern and producing an .html version of each file that matches by applying the HTML conversion: text2html . *.txt
|
|