How to tell Happy to skip a token - Haskell

Welcome to the Functional Programming Zulip Chat Archive. You can join the chat here.

Pedro Minicz

Can I tell Happy to always ignore a certain special token? I would like for it to always ignore the Empty token and I'd rather not filter the tokens before sending them to Happy, if I can help it.

data Token
  = String String
  | Colon
  | ColonEqual
  | Comma
  | LParen
  | RParen
  | Empty
  ...
%token
  string        { String $$ }
  ':'           { Colon }
  ':='          { ColonEqual }
  ','           { Comma }
  '('           { LParen }
  ')'           { RParen }
  <ignore>      { Empty }
  ...
TheMatten

Pedro Minicz said:

Can I tell Happy to always ignore a certain special token? I would like for it to always ignore the Empty token and I'd rather not filter the tokens before sending them to Happy, if I can help it.

data Token
  = String String
  | Colon
  | ColonEqual
  | Comma
  | LParen
  | RParen
  | Empty
  ...

%token
  string        { String $$ }
  ':'           { Colon }
  ':='          { ColonEqual }
  ','           { Comma }
  '('           { LParen }
  ')'           { RParen }
  <ignore>      { Empty }
  ...

I think the simplest solution will be to just ignore it in your implementation of binding between lexer and parser
I mean, this is normally lexer's work, e.g. in case of comments

Pedro Minicz

I am using Empty tokens in the lexer as markers for delimiting errors. I ended up doing it in such a way that I remove Empty tokens while reporting errors after the lexer is done. It sounds a big contrived, probably is, but the final solution doesn't look as bad as it sounds.

Pedro Minicz

I simplified it and now I don't need Empty tokens anymore :smiley: