
Am Sonntag 10 Januar 2010 20:33:40 schrieb John Moore:
Hi, Can anyone explain how to turn the code from infix to Prefix. I want to include a let statement below. I not sure of how prefix works.
Can you elaborate? I don't understand what it is you want to do. Do you want to parse expressions in prefix notation (aka Polish notation), such as +(3,4) (or, more Lispy, (+ 3 4))? That'd be simple, but you wouldn't use buldExpressionParser for that (in Polish notation, neither associativity nor precedence have a meaning). Or do you want to add a parser for let-expressions to expr?
expr :: Parser Expr expr = buildExpressionParser table factor <?> "expression" table :: [[Operator Char st Expr]] table = [[op "*" Mul AssocLeft, op "/" Div AssocLeft] ,[op "+" Add AssocLeft, op "-" Sub AssocLeft] ] where op s f assoc = Infix (do{ string s; return f}) assoc factor :: Parser Expr factor = do{ char '(' ; x <- expr ; char ')' ; return x } <|> number <?> "simple expression" number :: Parser Expr number = do{ ds <- many1 alphaNum ; return (Val $ read ds) }
John