use*_*465 4 compiler-construction haskell alex happy
我正在为实验语言进行语义分析.我正在使用Alex和Happy来生成词法分析器和解析器(实际上我正在使用BNFC工具来生成Alex和Happy文件).每当出现语义错误(比如类型错误)时,我都想收到包含行号和列号的错误消息.
似乎我必须在构建符号表或AST时存储行号信息.如果我可以以某种方式访问Happy文件的规则部分中的位置信息,那么我的问题就会解决.
任何有关这方面的建议都将受到高度赞赏.
我尝试实现下面建议的答案,但遗憾的是没有取得任何成功.让我们考虑一个非常简单的语法: -
Expr -> Expr + Term
| Term
Term -> Int
Run Code Online (Sandbox Code Playgroud)
我的词法分析器如下所示.
%wrapper "posn"
$digit = 0-9 -- digits
$alpha = [a-zA-Z] -- alphabetic characters
tokens :-
$white+ ;
"--".* ;
$digit+ { \p s -> L {getPos = p , unPos = Tok_Int (read s) }}
\+ { \p s -> L {getPos = p , unPos = Tok_Plus} }
{
data L a = L{ getPos :: AlexPosn, unPos :: a } deriving (Eq,Show)
data Token =
Tok_Plus
| Tok_Int Int
deriving (Eq,Show)
getToken :: IO [L Token]
getToken = do
args <- getArgs
case length args == 0 of
True -> do
error $ "\n****************Error: Expecting file name as an argument.\n"
False -> do
let fname = args !! 0
conts <- readFile fname
let tokens = alexScanTokens conts
return tokens
}
Run Code Online (Sandbox Code Playgroud)
我的Yacc文件如下,这是我正在努力的地方.如何在我的语法树中嵌入位置信息.
{
{-# OPTIONS_GHC -fno-warn-incomplete-patterns -fno-warn-overlapping-patterns #-}
module Parser where
import Lexer
}
%name pExpr Exp
%name pTerm Term
%tokentype {L Token}
%error { parseError }
%token
int { L { getPos = _,unPos = Tok_Int $$ } }
'+' { L { getPos = _,unPos = Tok_Plus } }
%%
Exp :: {L Expr}
Exp : Exp '+' Term { L { getPos = getPos $1 , unPos = EAdd (unPos $1) (unPos $3) } }
| Term { $1 }
Term :: {L Expr}
Term : int { L {getPos = getPos $1, unPos = EInt (unPos $1) } }
{
data Expr = EAdd Expr Expr
| EInt Int
deriving (Eq,Show)
returnM :: a -> Err a
returnM = return
thenM :: Err a -> (a -> Err b) -> Err b
thenM = (>>=)
parseError :: [L Token] -> a
parseError _ = error "Parse error"
}
Run Code Online (Sandbox Code Playgroud)
尝试编译生成的Haskell文件时,我收到以下类型错误.
Parser.hs:109:39:
Couldn't match expected type `L a0' with actual type `Int'
In the first argument of `getPos', namely `happy_var_1'
In the `getPos' field of a record
In the first argument of `HappyAbsSyn5', namely
`(L {getPos = getPos happy_var_1,
unPos = EInt (unPos happy_var_1)})'
Parser.hs:109:73:
Couldn't match expected type `L Int' with actual type `Int'
In the first argument of `unPos', namely `happy_var_1'
In the first argument of `EInt', namely `(unPos happy_var_1)'
In the `unPos' field of a record
Run Code Online (Sandbox Code Playgroud)
你们能告诉我如何使这件事有效吗?
如果您的词法分析器输出中有位置信息,则可以访问快乐规则中的位置信息.这正是GHC本身如何将SrcLoc
其置于其自己的Haskell代码内部表示中的方式.
基本上,你可以使用的posn
亚历克斯包装注入的位置信息到您的令牌类型:
data L a = L{ getPos :: AlexPosn, unPos :: a }
Run Code Online (Sandbox Code Playgroud)
(所以你的Alex tokenizer会返回L Token
值); 然后你把个人标记位置在你快乐的规则成非终结的位置(这样你就可以,例如,有从规则Expr + Expr
到L (combinedPosn [getPos $1, getPos $2, getPos $3] $ PlusExpr (unPos $1) (unPos $3)
.
归档时间: |
|
查看次数: |
672 次 |
最近记录: |