Code Monkey home page Code Monkey logo

intro_to_parsing's Introduction

Intro to Parsing with Parsec in Haskell

Overview

WIP, a tutorial which demonstrates the basics of Parsec and goes on to build a SQL query parser.

You can view this tutorial as HTML online here:

and you can view the files directly in the github repository here:

Summary of sections

Introduction to parsing with Parsec, including a review of Text.Parsec.Char functions.

Creating a very simple expression language parser, and introducing some functions from Text.Parsec.Combinator.

Rewriting the simple expression parser code in a more succinct style.

Review and examples of all functions from Text.Parsec.Combinator, and some from Control.Applicative and Control.Monad.

The utility functions used in the previous tutorials, plus some notes on types in Parsec.

This covers using the Text.Parsec.Expr for expression parsing with prefix, postfix and infix operators with fixity.

Looks at an issue we have with the way the symbol parser in the Text.Parsec.Expr tutorial was used, and some possible fixes.

This covers the Text.Parsec.Perm module which is used for parsing different things in flexible order.

This covers Text.Parsec.Token which can be used to create token parsers easily.

This covers building a parser a subset of value expressions from SQL, which are an extension of the simple expression types and parsers covered in previous tutorials.

This covers building a parser to parse query expressions with select lists, simple from, where, group by, having and order by.

This extend the parser for query expressions to support a from clause with much more features including joins.

Here is the code from ValueExpressions, QueryExpressions and FromClause plus tests put together and rearranged as a coherent standalone module.

This quick module covers a simple pretty printer for our SQL ast.

In this document, we will explore error messages with parsec and how restructuring parser code can lead to better or worse error messages.

Going further

If you are interested in SQL parsing, check out the project to build a complete SQL parser here: http://jakewheat.github.io/simple-sql-parser/latest. The parsing code in the simple-sql-parser project is based on this tutorial code.

Extras

an executable which contains the boilerplate to run a parsec parser on a string passed as an argument

an executable which contains the boilerplate to run a parsec parser on a file passed as an argument

License: BSD3

intro_to_parsing's People

Contributors

bitemyapp avatar cris avatar jakewheat avatar nickager avatar sgalal avatar xhliu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

intro_to_parsing's Issues

Typo in 3.6.1

Following 3.6.1 as it is results in:

*Main> regularParse simpleExpr5 "(1+a)"
Left (line 1, column 3):
unexpected "+"
expecting digit or ")"

The expected is:

*Main> parseWithWhitespace simpleExpr5 "(1+a)"
Right (Parens (Add (Num 1) (Var "a")))

I think this happens because:

term5 :: Parser SimpleExpr 
term5 = term term5

simpleExpr5 :: Parser SimpleExpr
simpleExpr5 = try addE5 <|> term5

term5 doesn't know how to parse additions, but simpleExpr5 does. When parsing (1+2), addE5 fails and lets term5 try, which parses the parentheses. But then term5 cannot parse the sub-expression 1+2, hence the error.

On my side, I was able to fix this by doing:

term5 :: Parser SimpleExpr 
term5 = term simpleExpr5

Is this the correct/most elegant fix?

Thanks for putting together such a well-written tutorial on parsec!

Tutorial is out of date

Tutorial on parsec is out of date, doesn't work with current latest Haskell/GHC distribution for various reason. I would be glad to submit a pull request to fix it.

suggest `parseTest` rather than `regularParse`

i was following this guide and i rolled out my simplified versions of the parsing help functions you have here. eventually i found out that i just rewrote parseTest.

by suggesting the users to test with parseTest from the beginning you make the guide way easier to follow because we wouldn't need to clone, compile, etcetera

incorrect explanation for the associativity of `a <$> b <$> c`

line 196 of ApplicativeStyle.lhs claims that

a <$> b <$> c

is parsed as

a <$> (b <$> c)

This is incorrect:

λ> :info (<$>)
(<$>) :: Functor f => (a -> b) -> f a -> f b
    -- Defined in ‘Data.Functor’
infixl 4 <$>

It actually associates to the left. However here it doesn't matter, since we have

instance Functor ((->) r) -- Defined in ‘GHC.Base’

So essentially we are writing

(a . b) <$> c

which is the same as

a <$> (b <$> c)

due to functor law.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.