![]() This rule is going to return a list of whatever field returns.įield =, field-value, I could have invoked the sepBy function with prefix notation ( sepBy field (char ‘,’)), which would have saved me the back-ticks, but in that case the word “By” is followed by “field”, which doesn’t read right to me. This will parse fields, separated by a single comma. This rule fits a common case in parsing of elements separated by other elements. That’s a literal translation, but Parsec provides a function that will make this a lot simpler. Then we return the first field followed by the remainder. So, we do a case on what we got and construct the remainder of the field list as either another field_list or an empty list, respectively. The optionMaybe function returns a Maybe type, which is either Just the syntactic element we’re looking for, or Nothing if that element isn’t provided. We obtain the first field, then look for an optional comma. We can state that rule in Haskell as:Įasy, huh? The many function is provided by Parsec, and means exactly the same thing as the braces in Extended Backus-Naur Form (EBNF). Which indicates that a csv-file is composed of many rows. Don’t worry, Haskell functions are often one-liners. Now, we’ll proceed to convert each line of the CSV BNF we presented earlier into a Haskell function. In order to make use of this module, we have to import it: Parsec is a parsing module that comes distributed with the Glasgow Haskell Compiler (GHC). To work this bit o’ magic, we first need a proper spellbook. Today we’ll see just how easy it is to translate those rules into Haskell code that will parse a CSV file. In a previous post, I provided a general overview of Backus-Naur Form (BNF) and an example set of BNF rules for comma-separated values (CSVs). Parsing CSV by feeding BNF to Haskell’s Parsec moduleĬhip Camden shows how easy it is to translate Backus-Naur Form rules into Haskell code that will parse a comma-separated file.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |