jakewheat / intro_to_parsing Goto Github PK
View Code? Open in Web Editor NEWIntroduction to parsing with Haskell and Parsec
Home Page: http://jakewheat.github.io/intro_to_parsing/
License: BSD 3-Clause "New" or "Revised" License
Introduction to parsing with Haskell and Parsec
Home Page: http://jakewheat.github.io/intro_to_parsing/
License: BSD 3-Clause "New" or "Revised" License
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.
And it's outstanding!
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
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 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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.