用FSyacc解析时有意义的错误 [英] Meaningful errors during parsing with FSyacc
本文介绍了用FSyacc解析时有意义的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用F#Power Pack中的fsyacc / fslex来解析一些源代码。
I'm using fsyacc/fslex from F# Power Pack to parse some source code.
要检测错误,我使用以下代码:
To detect errors I use the following code:
use inputChannel = new StreamReader(File.OpenRead tempFileName)
let lexbuf = Lexing.LexBuffer<_>.FromTextReader inputChannel
let ast = try
Parser.start Lexer.tokenize lexbuf
with e ->
let pos = lexbuf.EndPos
let line = pos.Line
let column = pos.Column
let message = e.Message
let lastToken = new System.String(lexbuf.Lexeme)
printf "Parse failed at line %d, column %d:\n" line column
printf "Last loken: %s" lastToken
printf "\n"
exit 1
但是,当这段代码在解析多行时抛出错误消息源文件,我错了行和列位置:
But when this code throws the error message on parsing multi-line source file, I'm getting wrong line and column position:
Parse failed at line 0, column 10899:
如何正确获取行号,发生错误?
How can I get line number correctly on which error has been occurred?
推荐答案
在lexing期间,您需要手动增加行号,其规则如
During lexing, you need to manually increment the line number with a rule like
...
let newline = ('\n' | '\r' '\n')
rule tokenize = parse
| newline { lexbuf.EndPos <- lexbuf.EndPos.NextLine; tokenize lexbuf }
...
这篇关于用FSyacc解析时有意义的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文