使用ocamlyacc从不减少规则
我在用ocaml编译lexer时遇到一个警告:使用ocamlyacc从不减少规则,ocaml,ocamllex,Ocaml,Ocamllex,我在用ocaml编译lexer时遇到一个警告: File "lexer.mll", line 42, characters 26-57: Warning 10: this expression should have type unit. 我想解析字符串,我制定了一个特殊的规则,当lexer读取一个引号时开始,当我读取另一个引号时结束,在这种情况下,返回字符串并为每个其他字符调用规则标记 以下是文件: { open Int32 open Lexing open Pars
File "lexer.mll", line 42, characters 26-57:
Warning 10: this expression should have type unit.
我想解析字符串,我制定了一个特殊的规则,当lexer读取一个引号时开始,当我读取另一个引号时结束,在这种情况下,返回字符串并为每个其他字符调用规则标记
以下是文件:
{
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 }
{
打开Int32
开放词法
开放解析器
字符串*Lexing.position*Lexing.position的异常词典错误
让string=Buffer.create 50
让错误s lexbuf=
raise(词汇错误(s,Lexing.lexeme\u start\u p lexbuf,Lexing.lexeme\u end\u p lexbuf))
让kwords=[“boolean”,boolean;“class”,class;“else”,else;
“扩展”,扩展;“for”,for;“if”,if;“instanceof”,
INSTANCEOF;“int”,int;“new”,new;“null”,null;“public”,
公共;“返回”,返回;“静态”,静态;“此”,此;
“void”,void;“String”,String;]
设ident_或_kword s=
尝试
列表.assoc s kwords
具有
未找到->标识
}
让blank=['''\t'\n']+
让数字=['0'-'9']
设alpha=['a'-'z''a'-'z']
设ident=((alpha |'|'|')(alpha |'|'|位)*)
let car=[''-'~']
让我们以“=”结尾
规则注释=解析
|“*/”{token lexbuf}
|eof{error“文件结尾”lexbuf}
|{comment lexbuf}
和chaine=parse
|end_quote{CSTRING(Buffer.contents string);token lexbuf}
|car作为c{Buffer.add_char string c;token lexbuf}
|eof{error“文件结尾”lexbuf}
|_uASC{error(String.make 1 c)lexbuf}
and token=parse
|空白{token lexbuf}
|“/*”{comment lexbuf}
|标识为id{ident_或_kword id}
|“{chaine lexbuf}
|“.{成员}”
|'='{ASSIGN}
|“==”{EQ}
|“!=”{DIFF}
|“{更大}”
|“>=”{GREATER_EQ}
|“++”{PLUS_PLUS}
|“+”{PLUS}
|“--”{负{u负}
|'-'{负}
|'*'{TIMES}
|“/”{DIV}
|“%”{MOD}
|“&&”{AND}
|“| |”{或}
| '!' {BANG}
|“({LP}
|“)”{RP}
|{{LB}
|'}{RB}
|'['{LC}
|']'{RC}
| ';' {SC}
|“,”{COMMA}
|“true | false”作为bool{CBOOL(bool_of_string bool)}
|数字+整数{
尝试
CINT(由字符串int组成)
具有
_->错误int-lexbuf}
|eof{eof}
|_uASC{error(String.make 1 c)lexbuf}
我无法尝试您的代码,因为我没有您的解析器模块
在我看来,你需要这个:
| end_quote { CSTRING(Buffer.contents string) }
在代码中,编译器抱怨此表达式不是unit
类型。事实上,它的值是一个令牌,这是您想要返回的。我敢肯定,现在没有必要打电话给扫描仪。你已经有你的代币了