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