我正在使用Happy在 Haskell 中构建一个 JavaScript 解析器,我遇到了一条错误消息,无论我多么努力,我都无法调试。
我不能在这里发布所有代码,因为它有数千行。我会尝试发布相关的内容,如果有人可以帮助我,我将不胜感激!
此错误消息很长,请多多包涵。我遗漏了大部分内容HappyAbsSyn
。如果有帮助,我可以提供完整的错误消息。
Parser.hs:800:28:
Couldn't match expected type `Expression'
with actual type `PrimaryExpr'
Expected type: Int
-> Int
-> Token
-> HappyState
Token
(HappyStk
(HappyAbsSyn
FuncDecl
... etc etc ...
PostFix)
-> P a1)
-> [HappyState
Token
(HappyStk
(HappyAbsSyn
FuncDecl
... etc etc ...
PostFix)
-> P a1)]
-> HappyStk
(HappyAbsSyn
FuncDecl
... etc etc ...
Actual type: Int
-> Int
-> Token
-> HappyState
Token
(HappyStk
(HappyAbsSyn
FuncDecl
... etc etc ...
t630)
-> P a0)
-> [HappyState
Token
(HappyStk
(HappyAbsSyn
FuncDecl
... etc etc ...
t630)
-> P a0)]
-> HappyStk
(HappyAbsSyn
FuncDecl
... etc etc ...
t630)
-> P a0
In the first argument of `happyGoto', namely `action_90'
In the expression: happyGoto action_90
了解?好的,所以我做的第一件事是查看 Parser.hs 中的第 800 行(Happy 生成的文件)。
799: action_60 (7) = happyGoto action_71
800: action_60 (28) = happyGoto action_90
801: action_60 (33) = happyGoto action_15
action_90
定义为:
action_90 (100) = happyShift action_224
action_90 _ = happyFail
除非我们了解代码对应的内容,否则这显然没有多大意义。我在编译时要求 Happy 生成一个信息文件,这是(我认为)适当的部分:
-----------------------------------------------------------------------------
Grammar
-----------------------------------------------------------------------------
%start_parse -> program (0)
... etc etc ...
statement -> ID ':' statement (28)
... etc etc ...
primaryExpr -> THIS (100)
... etc etc ...
所以看起来这与 ID 声明有关。在这一点上,我真的很困惑。有人对我应该从这里去哪里有任何建议吗?我猜要解决这个问题,您需要查看抽象语法。这里有一点。同样,如果有帮助,我很乐意展示更多内容。
data Expression
= Assignment Assignment
deriving Show
data Assignment
= CondExpr CondExpr
| Assign LeftExpr AssignOp Assignment
| AssignFuncDecl FuncDecl
deriving Show
data FuncDecl
= FuncDecl (Maybe String) [String] [Source]
deriving Show
data Statement
= EmptyStmt
| IfStmt IfStmt
| IterativeStmt IterativeStmt
| ExprStmt Expression
| Block [Statement]
| VarStmt [VarDecl]
| TryStmt TryStmt
| ContinueStmt (Maybe String)
| BreakStmt (Maybe String)
| ReturnStmt (Maybe Expression)
| WithStmt Expression Statement
| LabelledStmt String Statement
| Switch Switch
| ThrowExpr Expression
deriving Show
data PrimaryExpr
= ExpLitInt Integer
| ExpLitStr String
| ExpId String
| ExpBrackExp PrimaryExpr
| ExpThis
| ExpRegex String
| ExpArray ArrayLit
| ExpObject [(Property, Assignment)]
deriving Show
对不起,这么长。我在我的智慧在这里结束。任何帮助或指示都会很棒。