HOME

TheInfoList



OR:

Parsec is a
library A library is a collection of Book, books, and possibly other Document, materials and Media (communication), media, that is accessible for use by its members and members of allied institutions. Libraries provide physical (hard copies) or electron ...
for writing
parser Parsing, syntax analysis, or syntactic analysis is a process of analyzing a string of symbols, either in natural language, computer languages or data structures, conforming to the rules of a formal grammar by breaking it into parts. The term '' ...
s written in the
programming language A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
Haskell Haskell () is a general-purpose, statically typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research, and industrial applications, Haskell pioneered several programming language ...
. It is based on higher-order parser combinators, so a complicated parser can be made out of many smaller ones. It has been reimplemented in many other languages, including Erlang,
Elixir An elixir is a sweet liquid used for medical purposes, to be taken orally and intended to cure one's illness. When used as a dosage form, pharmaceutical preparation, an elixir contains at least one active ingredient designed to be taken orall ...
,
OCaml OCaml ( , formerly Objective Caml) is a General-purpose programming language, general-purpose, High-level programming language, high-level, Comparison of multi-paradigm programming languages, multi-paradigm programming language which extends the ...
, Racket, F#, and the
imperative programming In computer science, imperative programming is a programming paradigm of software that uses Statement (computer science), statements that change a program's state (computer science), state. In much the same way that the imperative mood in natural ...
languages C#, and
Java Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
. Because a parser combinator-based program is generally slower than a parser generator-based program, Parsec is normally used for small domain-specific languages, while Happy is used for
compiler In computing, a compiler is a computer program that Translator (computing), translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primaril ...
s such as the Glasgow Haskell Compiler (GHC). Other Haskell parser combinator libraries that have been derived from Parsec include Megaparsec and Attoparsec. Parsec is
free software Free software, libre software, libreware sometimes known as freedom-respecting software is computer software distributed open-source license, under terms that allow users to run the software for any purpose as well as to study, change, distribut ...
released under the BSD-3-Clause license.


Example

Parsers written in Parsec start with simpler parsers, such as ones that recognize certain strings, and combine them to build a parser with more complicated behavior. For example, digit parses a digit, and string parses a specific string (like "hello"). Parser combinator libraries like Parsec provide utility functions to run the parsers on real values. A parser to recognize a single digit from a string can be split into two functions: one to create the parser, and a main function that calls one of these utility functions (parse in this case) to run the parser: import Text.Parsec -- has general parsing utility functions import Text.Parsec.Char -- contains specific basic combinators type Parser = Stream s m Char => ParsecT s u m String parser :: Parser parser = string "hello" main :: IO () main = print (parse parser "" "hello world") -- prints 'Right "hello"' We define a Parser type to make the type signature of parser easier to read. If we wanted to alter this program, say to read either the string "hello" or the string "goodbye", we could use the operator <, >, provided by the Alternative typeclass, to combine two parsers into a single parser that tries either: parser = string "hello" <, > string "goodbye"


References


External links


Parsec on the Haskell wikiParsec on Hackage
* Free computer libraries Free software programmed in Haskell Parser generators Articles with example Haskell code {{Free-software-stub