_______________________________________________
Haskell-Cafe mailing list
Haskel...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Algebraic data types in Haskell and other modern functional languages
are so convenient for describing syntax trees that you don't have need
for a 'tree builder' vis-a-vis Java Tree Builder or JJTree that you
might use in Java.
The original Parsec distribution has parsers and ASTs for Henk and
Andrew Appel's Tiger language and is available here:
http://legacy.cs.uu.nl/daan/parsec.html
Unfortunately the package on Hackage has removed the examples. Parsec
can be considered a progression of the ideas detailed in Hutton and
Meijer's "Monadic Parser Combinators".
Best wishes
Stephen
2009/12/27 CK Kashyap <ck_ka...@yahoo.com>:
> However, the parser developed in the paper does not generate an AST - I feel, I'd grasp the whole thing a lot better if I could go over a sample that generates an AST from a simple expression (or even a standard language such as C or Java) ... Can someone please point me to a sample that generates AST - preferably with the simple parser combinator given in the paper.
It parses something like "x+y+z*pi"
import Control.Applicative
import Text.ParserCombinators.Parsec hiding ((<|>))
data Expr = Variable String
| Add Expr Expr
| Mul Expr Expr deriving (Show)
parseAST :: Parser Expr
parseAST = parseAdd
parseAdd :: Parser Expr
parseAdd = parseMul >>= \e -> ((string "+" >> (Add e <$> parseAdd)) <|>
(return e))
parseMul :: Parser Expr
parseMul = parseBase >>= \e -> ((string "*" >> (Mul e <$> parseMul)) <|>
(return e))
parseBase :: Parser Expr
parseBase = (string "(" *> parseAST <* string ")") <|>
(Variable <$> many1 letter)
Regards