Parse error with Happy and attributed grammar - parsing

I'm a newbie of Haskell and Happy and I'm trying to make a parser with an attributed grammar in Haskell with Happy. I've made a little grammar just for variable declarations, given to BNFC and everything since here works fine. Then I start to add the attributes in the parser and at this point I found my first problem: when I add %attributetype {MyAttribute a}
happy -gca Pargrammar.y works fine, but immediately after I add %attribute parseTree {a}
happy gives me this parse error
happy: Parse error
So I keep just %attributetype {MyAttribute a} and try to modify the basic type changing first
String :: { String } : L_quoted { $1 }
with this line here
String : L_quoted { $$= $1 }
but then my second problem arises: when I launch the Makefile I got this error here
Pargrammar.hs:344:19: error:
Variable not in scope: ($$=) :: t1 -> String -> t
Pargrammar.hs:1066:19: error:
• Couldn't match type ‘t1 -> t’ with ‘[Char]’
Expected type: HappyAbsSyn String
Actual type: HappyAbsSyn (t1 -> t)
• In the first argument of ‘fn’, namely ‘v1’
In the expression: fn v1
In an equation for ‘r’: r = fn v1
• Relevant bindings include
r :: HappyAbsSyn (t1 -> t) (bound at Pargrammar.hs:1066:12)
stk' :: HappyStk (HappyAbsSyn (t1 -> t))
(bound at Pargrammar.hs:1065:81)
v1 :: HappyAbsSyn (t1 -> t) (bound at Pargrammar.hs:1065:69)
fn :: HappyAbsSyn String -> HappyAbsSyn (t1 -> t)
(bound at Pargrammar.hs:1065:22)
happySpecReduce_1 :: Happy_GHC_Exts.Int#
-> (HappyAbsSyn String -> HappyAbsSyn (t1 -> t))
-> Happy_GHC_Exts.Int#
-> Token
-> Happy_GHC_Exts.Int#
-> Happy_IntList
-> HappyStk (HappyAbsSyn (t1 -> t))
-> [Token]
-> Err (HappyAbsSyn (t1 -> t))
(bound at Pargrammar.hs:1063:1)
and indeed at line 344 of the Pargrammar.hs I got this
happyIn8
($$= happy_var_1
)
but that $$= should not be there.
My Makefile just do this
all:
happy -gca Pargrammar.y
alex -g Lexgrammar.x
ghc --make Testgrammar.hs -o Testgrammar
clean:
-rm -f *.log *.aux *.hi *.o *.dvi
-rm -f Docgrammar.ps
distclean: clean
-rm -f Docgrammar.* Lexgrammar.* Pargrammar.* Layoutgrammar.* Skelgrammar.* Printgrammar.* Testgrammar.* Absgrammar.* Testgrammar ErrM.* SharedString.* ComposOp.* grammar.dtd XMLgrammar.* Makefile*
I really don't know how to solve this. If any of you can suggest me some ideas I would be grateful.

Related

How do I generalize a Parser which extracts values from tokens?

I'm creating a parser in Haskell for a simple language. The parser takes a list of tokens, generated by a separate tokenizer, and turns it into a structured tree. While I was writing this Parser, I came across a reoccurring pattern that I would like to generalize. Here are the relevant definitions:
--Parser Definition
data Parser a b = Parser { runParser :: [a] -> Maybe ([a], b) }
--Token Definition
data Token = TokOp { getTokOp :: Operator }
| TokInt { getTokInt :: Int }
| TokIdent { getTokIdent :: String }
| TokAssign
| TokLParen
| TokRParen
deriving (Eq, Show)
The parser also has instances defined for MonadPlus and all of its super classes. Here are two examples of the reoccurring Pattern I am trying to generalize:
-- Identifier Parser
identP :: Parser Token String
identP = Parser $ \input -> case input of
TokIdent s : rest -> Just (rest, s)
_ -> Nothing
--Integer Parser
intP :: Parser Token Int
intP = Parser $ \input -> case input of
TokInt n : rest -> Just (rest, n)
_ -> Nothing
As you can see, these two examples are incredibly similar, yet I see no way generalize it. Ideally I would want some function of type extractToken :: ?? -> Parser Token a where a is the value contained by the token. I am guessing the solution involves >>=, but I am not experienced enough with Monads to figure it out. This also may be impossible, I am not sure.
It seems difficult to avoid at least some boilerplate. One simple approach is to manually define the field selectors to return Maybes:
{-# LANGUAGE LambdaCase #-}
data Token = TokOp Operator
| TokInt Int
| TokIdent String
| TokAssign
| TokLParen
| TokRParen
deriving (Eq, Show)
getTokOp = \case { TokOp x -> Just x ; _ -> Nothing }
getTokInt = \case { TokInt x -> Just x ; _ -> Nothing }
getTokIdent = \case { TokIdent x -> Just x ; _ -> Nothing }
and then the rest is:
fieldP :: (Token -> Maybe a) -> Parser Token a
fieldP sel = Parser $ \case tok:rest -> (,) rest <$> sel tok
[] -> Nothing
opP = fieldP getTokOp
identP = fieldP getTokIdent
intP = fieldP getTokInt
You could derive the getXXX selectors using Template Haskell or generics, though it's likely not worth it.

How do I pattern match with Data.Text in Haskell?

I am currently in the process of writing a parser in Haskell. I have the following code.
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Data.Text
newtype Parser a = Parser { runParser :: Text -> Either Text (Text, a) }
char1 :: Char -> Parser Char
char1 c = Parser $ \case
(x:xs) | x == c -> Right (xs, x)
_ -> Left "Unexpected character"
It fails to compile with these two errors.
test.hs:12:6: error:
• Couldn't match expected type ‘Text’ with actual type ‘[Char]’
• In the pattern: x : xs
In a case alternative: (x : xs) | x == c -> Right (xs, x)
In the second argument of ‘($)’, namely
‘\case
(x : xs) | x == c -> Right (xs, x)
_ -> Left "Unexpected character"’
|
12 | (x:xs) | x == c -> Right (xs, x)
| ^^^^
test.hs:12:24: error:
• Couldn't match type ‘[Char]’ with ‘Text’
Expected type: Either Text (Text, Char)
Actual type: Either Text ([Char], Char)
• In the expression: Right (xs, x)
In a case alternative: (x : xs) | x == c -> Right (xs, x)
In the second argument of ‘($)’, namely
‘\case
(x : xs) | x == c -> Right (xs, x)
_ -> Left "Unexpected character"’
|
12 | (x:xs) | x == c -> Right (xs, x)
| ^^^^^^^^^^^^^
I can fix the error by replacing the Text data type with String but I would prefer to use the Text data type.
Is there a way to pattern match with the Data.Text type without first explicitly converting it to a string first? Perhaps there is a GHC extension that would allow me to do this?
Thanks in advance.
A a refinement to #DanielWagner's answer, you can combine view patterns and pattern synonyms to do this. You'll need a new constructor in place of :, but it might look like:
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ViewPatterns #-}
import Data.Text
pattern x :> xs <- (uncons -> Just (x, xs))
pattern Empty <- (uncons -> Nothing)
findInText :: (Char -> Bool) -> Text -> Maybe Char
findInText _ Empty = Nothing
findInText p (x :> xs) | p x = Just x
| otherwise = findInText p xs
The idea here is that a pattern x :> xs is a synonym for the pattern uncons -> Just (x, xs) which is a view pattern that operates by applying uncons to the scrutinee and pattern-matching the result with Just (x, xs) to population x and xs for the parent pattern.
As per the comment, there might be some concern about whether this usage ends up calling uncons more than once. With optimization entirely shut off (-O0), the generated core does have multiple uncons calls:
-- unoptimized -O0
findInText
= \ ds ds1 ->
case uncons ds1 of {
Nothing -> Nothing;
Just ipv ->
case uncons ds1 of {
Nothing -> ...
With optimization on (-O or -O2), everything gets inlined and the generated core is incredibly complicated because of the Unicode processing going on. However, if you also define:
findInText' :: (Char -> Bool) -> Text -> Maybe Char
findInText' p txt = case uncons txt of
Nothing -> Nothing
Just (x, xs) | p x -> Just x
| otherwise -> findInText' p xs
it turns out that GHC compiles findInText' to:
findInText' = findInText
so it looks like in this case at least, GHC doesn't do any extra work as a result of the view patterns.
You can match on a call to uncons.
case uncons text of
Just (x, xs) -> ...
Nothing -> ...
View patterns let you do this within the pattern instead of within the scrutinee, but require you to say uncons once for each pattern.
case text of
(uncons -> Just (x, xs)) -> ...
(uncons -> Nothing) -> ...

Coq: usage of `PartialOrder` typeclass

I am trying to define lexicographic ordering on strings over posets, but I'm not completely sure how to use the PartialOrder typeclass.
Require Import List RelationClasses.
Fail Inductive lex_leq {A : Type} `{po : PartialOrder A} : list A -> list A -> Prop :=
| lnil: forall l, lex_leq nil l
| lcons:
forall (hd1 hd2 : A) (tl1 tl2 : list A),
hd1 <= hd2 -> (* error *)
(hd1 = hd2 -> lex_leq tl1 tl2) ->
lex_leq (hd1 :: tl1) (hd2 :: tl2).
Partial output:
The term "hd1" has type "A" while it is expected to have type "nat".
Clearly <= is the wrong notation to use here; I'm wondering how I can obtain an ordering relation from my po instance.
One can bind the names explicitly to make things more obvious. Before we can do this we need to tell Coq not to complain about unbound variables using the Generalizable Variables command:
From Coq Require Import List RelationClasses.
Generalizable Variables A eqA R.
Inductive lex_leq `{PartialOrder A eqA R} : list A -> list A -> Prop :=
| lnil: forall l, lex_leq nil l
| lcons:
forall (hd1 hd2 : A) (tl1 tl2 : list A),
R hd1 hd2 ->
(hd1 = hd2 -> lex_leq tl1 tl2) ->
lex_leq (hd1 :: tl1) (hd2 :: tl2).
You can find more information in the manual (here).

Parser Error Reporting deriving the right instances

I am trying to build an error reporting parser in haskell. Currently I have been looking at a tutorial and this is what I have so far.
type Position = (Int, Int)
type Err = (String, Position)
newtype Parser1 a = Parser1 {parse1 :: StateT String (StateT Position (MaybeT
(Either Err))) a} deriving (Monad, MonadState String, Applicative, Functor)
runParser :: Parser1 a -> String -> Either Err (Maybe ((a, String), Position))
runParser p ts = runMaybeT $ runStateT (runStateT (parse1 p) ts) (0, 0)
basicItem = Parser1 $ do
state <- get
case state of
(x:xs) -> do {put xs; return x}
[] -> empty
item = Parser1 $ do
c <- basicItem
pos <- lift get
lift (put (f pos))
return c
f :: Char -> Position -> Position
f d (ln, c) = (ln + 1, 0)
f _ (ln, c) = (ln , c + 1)
This piece of code does not compile, I think it is to do with my item parser and the fact that I am trying to access the inner state namely position. I was wondering how in the deriving clause do I make Haskell derive the instances for both states in my parser type, so then I can access the inner state?
Edit 1:
I initially tried declaring basicItem as:
basicItem :: (MonadState String m, Alternative m) => m t
basicItem = do
state <- get
case state of
(x:xs) -> do {put xs; return x}
[] -> empty`
However, I kept getting the error:
I was wondering why it cannot deduce context of get from MonadState String m,
when in my deriving clause I have MonadState String.
The error for my initial question is here:

Problems with Parsers

i hope somebody can help me to understand the following code
type Parser a = String -> [(a,String)]
item :: Parser Char
item = \ s -> case s of
[] -> []
(x:xs) -> [(x,xs)]
returnP :: Parser a
returnP a = \s -> [(a,s)]
(>>=) :: Parser a -> (a -> Parser b) -> Parser b
p>>=f = \s -> case p s of
[(x,xs)]-> f x xs
_ -> []
twochars :: Parser (Char,Char)
twochars= item >>= \a -> item >>= \b -> returnP (a,b)
Everything seems to be clear but i dont understand the lampda function in the last line in the twochars-function. It would be nice if somebody can give me a explanation about that.
Rewriting the twochars function for clarity and it is basically:
twochars =
item >>= \a -> -- parse a character and call it `a`
item >>= \b -> -- parse another character and call it `b`
returnP (a,b) -- return the tuple of `a` and `b`
The lambdas here just introduce names for the parsed characters, and let them be passed along to a later part of the computation.
They correspond to the second argument in the bind you have defined:
(>>=) :: Parser a -- your item
-> (a -> Parser b) -- your lambda returning another parse result
-> Parser b

Resources