我正在爲實驗性語言進行語義分析。我使用Alex和Happy來生成詞法分析器和解析器(實際上我使用BNFC工具來生成Alex和Happy文件)。每當出現語義錯誤時(例如類型錯誤),我都希望得到帶有行號和列號的錯誤消息。在語義分析階段獲取行號信息(使用Alex,Happy)
看來,我將不得不存儲行號信息,同時建立我的符號表或AST。如果我可以以某種方式訪問Happy文件的規則部分中的位置信息,我的問題將被解決。
在這方面的任何建議將不勝感激。
我試着實現下面建議的答案,但不幸的是沒有任何成功與此。讓我們考慮一個非常簡單的語法: -
Expr -> Expr + Term
| Term
Term -> Int
我的詞法分析器如下圖所示。
%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
}
我的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"
}
當試圖編譯生成的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
你們可以告訴我如何讓這個東西有效嗎?