期待LexBuffer <char>但是给出了一个LexBuffer <byte>类型'char'与'byte'类型不匹配</byte> </char>

时间:2010-04-26 13:53:14

标签: f# fslex

  

类型不匹配。期待LexBuffer<char>但给定LexBuffer<byte>类型'char'与'byte'类型不匹配

这是我在使用fslex时收到的错误消息。我试过手动检查每次出现的lexbuf及其类型。到处都是LexBuffer<char>。但编译器仍然给我上述错误。你能否告诉我为什么会出现这种错误以及如何解决它。

{
    open System
    open Microsoft.FSharp.Text.Lexing
    open Microsoft.FSharp.Text.Parsing

    let lexeme (lexbuf : LexBuffer<char>) = new System.String(lexbuf.Lexeme)
    let newline (lexbuf:LexBuffer<char>) = lexbuf.EndPos <- lexbuf.EndPos.NextLine
    let unexpected_char (lexbuf:LexBuffer<char>) = failwith ("Unexpected character '"+(lexeme lexbuf)+"'")
}

let char = ['a'-'z' 'A'-'Z']
let digit = ['0'-'9']
let float = '-'?digit+ '.' digit+
let ident = char+ (char | digit)*
let whitespace = [' ' '\t']
let newline = ('\n' | '\r' '\n')

rule tokenize = parse
    | "maximize" { MAXIMIZE }
    | "minimize" { MINIMIZE }
    | "where" { WHERE }
    | '+' { PLUS }
    | '-' { MINUS }
    | '*' { MULTIPLY }
    | '=' { EQUALS }
    | '>' { STRICTGREATERTHAN }
    | '<' { STRICTLESSTHAN }
    | ">=" { GREATERTHANEQUALS }
    | "<=" { LESSTHANEQUALS }
    | '[' { LSQUARE }
    | ']' { RSQUARE }
    | whitespace { tokenize lexbuf }
    | newline { newline lexbuf; tokenize lexbuf }     
    | ident { ID (lexeme lexbuf) }
    | float { FLOAT (Double.Parse(lexeme lexbuf)) } 
    | ';' { SEMICOLON }
    | eof { EOF }
    | _ { unexpected_char lexbuf } 

3 个答案:

答案 0 :(得分:6)

也许你需要生成一个unicode词法分析器。 unicode词法分析器使用LexBuffer < char >而不是LexBuffer < byte >

  • FsLex的“unicode”参数是可选的,但如果启用则生成unicode词法分析器。

http://blogs.msdn.com/dsyme/archive/2009/10/21/some-smaller-features-in-the-latest-release-of-f.aspx

答案 1 :(得分:1)

您是否尝试过插入显式广告?

答案 2 :(得分:0)

我的lexer文件定义有一个错误我相信,它是在我做了以下我的词法分析器定义时编译的。专家可以更深入地了解原因,而我的理解是词法分析器中使用的lexbuf的类型应该以某种方式与解析器生成的定义相关

{

open System
open LanguageParser
open Microsoft.FSharp.Text.Lexing
open Microsoft.FSharp.Text.Parsing
open System.Text

let newline (lexbuf:LexBuffer<_>) = lexbuf.EndPos <- lexbuf.EndPos.NextLine

}

let char = ['a'-'z' 'A'-'Z']
let digit = ['0'-'9']
let float = '-'?digit+ '.' digit+
let ident = char+ (char | digit)*
let whitespace = [' ' '\t']
let newline = ('\n' | '\r' '\n')

rule tokenize = parse
    | "maximize" { MAXIMIZE }
    | "minimize" { MINIMIZE }
    | "where" { WHERE }
    | '+' { PLUS }
    | '-' { MINUS }
    | '*' { MULTIPLY }
    | '=' { EQUALS }
    | '>' { STRICTGREATERTHAN }
    | '<' { STRICTLESSTHAN }
    | ">=" { GREATERTHANEQUALS }
    | "<=" { LESSTHANEQUALS }
    | '[' { LSQUARE }
    | ']' { RSQUARE }
    | whitespace { tokenize lexbuf }
    | newline { newline lexbuf; tokenize lexbuf } 
    | ident { ID <| Encoding.UTF8.GetString(lexbuf.Lexeme) }
    | float { FLOAT <| Double.Parse(Encoding.UTF8.GetString(lexbuf.Lexeme)) } 
    | ';' { SEMICOLON }
    | eof { EOF }
    | _ { failwith ("Unexpected Character") }