Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Happy generated parser fails to compile. (Rohit Garg)
   2. Re:  Happy generated parser fails to compile. (Stephen Tetley)


----------------------------------------------------------------------

Message: 1
Date: Sun, 26 Sep 2010 20:28:05 +0530
From: Rohit Garg <[email protected]>
Subject: [Haskell-beginners] Happy generated parser fails to compile.
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi,

Following is a simple parser definition I am trying to write for
simple numerical expressions. A sequence of numerical expressions
actually. Happy reports that there are 3 un-used terminals. AFAIK,
they should be let, in and '='. Is that correct?

Although, happy generates a .hs file without any other message. GHC
gives a type mismatch error. I don't suppose it could be a lexer
error, could it?

a) Could this be a bug in Happy (very unlikely, IMO) it is generating
code that has type mismatches.

b) If there is a bug in my parser definition, shouldn't Happy beep on it?

Any suggestions to fix it will be highly appreciated.

Thanks,
===================
{
module Main where
import Tokens_posn
}

%name calc
%tokentype { Token }

%token  let             { Let _     }
        in              { In  _     }
        ';'             { Newstatement _ }
        int             { Int _ $$  }
        var             { Var _ $$  }
        '='             { Sym _ '=' }
        '+'             { Sym _ '+' }
        '-'             { Sym _ '-' }
        '*'             { Sym _ '*' }
        '/'             { Sym _ '/' }
        '('             { Sym _ '(' }
        ')'             { Sym _ ')' }

%%

Exprs   :: { [ Expr ] }
Exprs   : Exprs Expr ';'                { $2 : $1 }
        | Expr ';'                      { [$1] }

Expr : Expr '+' Term            { PlusE  $1 $3  }
     | Expr '-' Term            { MinusE $1 $3  }
     | Term                     { $1            }

Term : Term '*' Factor          { TimesE $1 $3  }
     | Term '/' Factor          { DivE $1 $3    }
     | Factor                   { $1            }

Factor : '-' Atom               { NegE $2       }
       | Atom                   { $1            }

Atom : int                      { IntE $1   }
       | var                    { VarE $1       }
       | '(' Expr ')'           { $2            }

{
data Expr =
        LetE   String Expr Expr |
        PlusE  Expr Expr        |
        MinusE Expr Expr        |
        TimesE Expr Expr        |
        DivE   Expr Expr        |
        NegE   Expr           |
        IntE   Int            |
        VarE   String
        deriving Show

flist :: [Int] -> Int -> [Int]
flist x a = a : x

main:: IO ()
main =  interact (show.runCalc)

runCalc :: String -> Expr
runCalc = calc . alexScanTokens

happyError :: [Token] -> a
happyError tks = error ("Parse error at " ++ lcn ++ "\n")
        where
        lcn =   case tks of
                  [] -> "end of file"
                  tk:_ -> "line " ++ show l ++ ", column " ++ show c
                        where
                        AlexPn _ l c = token_posn tk
}

-- 
Rohit Garg

http://rpg-314.blogspot.com/


------------------------------

Message: 2
Date: Sun, 26 Sep 2010 17:16:09 +0100
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Happy generated parser fails to
        compile.
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hello

If you run happy with the info option and a file name for the output,
happy will produce a report on the grammar:


> happy -info=INFO.txt Main.g


You are right that the three unused termianls are 'let' 'in' and '='

terminal let is unused
terminal in is unused
terminal '=' is unused


You are also importing a file Tokens_posn (presumably the lexer?)
which you haven't posted - without being able to see this or the
actual error message its rather hard to decode the error, however if
you are using the file Token_posn.x from the Alex distribution there
are two problems - one is you need Newstatement as a token, the other
is runCalc has this type:


runCalc :: String -> [Expr]
runCalc ss = calc $ alexScanTokens ss

Best wishes

Stephen


------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 27, Issue 54
*****************************************

Reply via email to