The problem is that with the new rules, the grammar requires arbitrary lookahead to tell the difference between a varDecl
and an expStmt
. This comes from LT
being both a binary operator for expressions, and indicating the start of a tyargs
list for a parameterized type.
One possible fix would be to introduce a new keyword to denote a parameterized type or function (like the FUN
keyword currently used to introduce a function type), which would allow the parser to know in advance whether to treat a LT
as an operator or a type parameter list.
So you would instead add new rules like:
ty: TYPE ID tyargs
callExpr: CALL ID tyargs LPAREN expList RPAREN
Another possibility is to use lexer feedback via the symbol table -- have the lexer recognize identifiers that require type parameters (by looking up the names in the symbol table) and return a different token for them.
A third possibility is to use a stronger parser generator that can deal with more lookahead, such as bison's %glr-parser
option, or btyacc