2013-10-04 2 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이 아닌 것을 불평 :이있을 필요가 같은

그것은 나에게 보인다. 실제로 그 값은 토큰입니다. 반환하려는 토큰입니다. 이 시점에서 스캐너를 부를 필요가 없습니다. 꽤 확신합니다. 이미 토큰을 가지고 있습니다.