[Haskell-cafe] Happy Parser problem

Aaron Gray aaronngray.lists at gmail.com
Fri Dec 31 14:21:45 CET 2010


I am trying to get a grammar where keywords are also valid identifiers.

Been messing round with the following Happy grammar :-

%token
      'let'           { TokenIdent "let" }
      'in'            { TokenIdent "in" }
      ident           { TokenIdent $$ }
      int             { TokenInt $$ }
      '='             { TokenEq }
      '+'             { TokenPlus }
      '-'             { TokenMinus }
      '*'             { TokenTimes }
      '/'             { TokenDiv }
      '('             { TokenOB }
      ')'             { TokenCB }

%%

Exp   : 'let' Var '=' Exp 'in' Exp  { Let $2 $4 $6 }
      | Exp1                    { Exp1 $1 }

Exp1  : Exp1 '+' Term           { Plus $1 $3 }
      | Exp1 '-' Term           { Minus $1 $3 }
      | Term                    { Term $1 }

Term  : Term '*' Factor         { Times $1 $3 }
      | Term '/' Factor         { Div $1 $3 }
      | Factor                  { Factor $1 }

Factor :: { Factor }
      : int                     { Int $1 }
      | ident                   { Var $1 }
      | '(' Exp ')'             { Brack $2 }

Var :: { Factor }
      : ident                   { Var $1 }
      | 'let'                   { Var "let" }

Here 'Var' should be able to represent a 'let' identifier, as well as a
keyword. Happy accepts the grammar but it does not parser the expected 'let
let = x in let'.

I have attached the full Happy grammar.

I don't think this is an LR thing, but could be wrong.

Many thanks in advance,

Aaron
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20101231/433a834d/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: exprtree.y
Type: application/octet-stream
Size: 2474 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20101231/433a834d/attachment.obj>


More information about the Haskell-Cafe mailing list