2013-10-04 32 views
0

我有一個警告編譯我的詞法分析器與OCaml的降低:規則從未與ocamlyacc

File "lexer.mll", line 42, characters 26-57: 
Warning 10: this expression should have type unit. 

而且我想解析字符串,我做它啓動時讀取詞法分析器報價結束時,一個特殊的規則我讀了另一個報價,並在這種情況下返回字符串,並調用每個其他字符的規則標記。

這裏的文件:

{ 
    open Int32 
    open Lexing 
    open Parser 

    exception LexicalError of string * Lexing.position * Lexing.position 

    let string = Buffer.create 50 

    let error s lexbuf = 
     raise (LexicalError(s, Lexing.lexeme_start_p lexbuf, Lexing.lexeme_end_p lexbuf)) 


    let kwords = [ "boolean", BOOLEAN; "class", CLASS; "else", ELSE; 
        "extends", EXTENDS; "for", FOR; "if", IF; "instanceof", 
        INSTANCEOF; "int", INT; "new", NEW; "null", NULL; "public", 
        PUBLIC; "return", RETURN; "static", STATIC; "this", THIS; 
        "void", VOID; "String", STRING; ] 

    let ident_or_kword s = 
     try 
      List.assoc s kwords 
     with 
      Not_found -> IDENT(s) 
} 

let blank = [' ' '\t' '\n']+ 
let digit = ['0'-'9'] 
let alpha = ['a'-'z''A'-'Z'] 
let ident = ((alpha | '_')(alpha | '_' | digit)*) 
let car = [' '-'~'] 
let end_quote = '"' 

rule comment = parse 
| "*/"     { token lexbuf } 
| eof     { error "End Of File" lexbuf } 
| _      { comment lexbuf } 

and chaine = parse 
| end_quote    { CSTRING(Buffer.contents string); token lexbuf } 
| car as c    { Buffer.add_char string c; token lexbuf } 
| eof     { error "End Of File" lexbuf } 
| _ as c    { error (String.make 1 c) lexbuf } 

and token = parse 
| blank     { token lexbuf } 
| "/*"     { comment lexbuf } 
| ident as id   { ident_or_kword id } 
| '"'     { chaine lexbuf } 
| '.'     { MEMBER } 
| '='     { ASSIGN } 
| "=="     { EQ } 
| "!="     { DIFF } 
| '<'     { LESS } 
| "<="     { LESS_EQ } 
| '>'     { GREATER } 
| ">="     { GREATER_EQ } 
| "++"     { PLUS_PLUS } 
| '+'     { PLUS } 
| "--"     { MINUS_MINUS } 
| '-'     { MINUS } 
| '*'     { TIMES } 
| '/'     { DIV } 
| '%'     { MOD } 
| "&&"     { AND } 
| "||"     { OR } 
| '!'     { BANG } 
| '('     { LP } 
| ')'     { RP } 
| '{'     { LB } 
| '}'     { RB } 
| '['     { LC } 
| ']'     { RC } 
| ';'     { SC } 
| ','     { COMMA } 
| "true|false" as bool { CBOOL(bool_of_string bool) } 
| digit+ as int   { 
      try 
       CINT(of_string int) 
      with 
       _ -> error int lexbuf } 
| eof     { EOF } 
| _ as c    { error (String.make 1 c) lexbuf } 

回答

1

我不能嘗試你的代碼,因爲我沒有你的解析器模塊。

它看起來對我來說,你需要有這樣的:

| end_quote    { CSTRING(Buffer.contents string) } 

在你的代碼,編譯器是抱怨,這表達unit類型的不行。事實上,它的價值是一個令牌,這就是你想要返回的東西。這個時候不需要調用掃描器,我很確定。你已經有了你的代幣。