我如何预先形成' lookahead'在OCaml词法分析器/如何回滚词位?

时间:2018-03-02 23:15:21

标签: parsing ocaml lr ocamllex menhir

好吧,我正在OCaml中编写我的第一个解析器,我立刻以某种方式设法制作了一个无限循环的解析器。

特别值得注意的是,我正在尝试根据Scheme规范的规则来识别标识符(我不明白我在做什么,显然) - 并且那里有一些语言关于要求它们后跟分隔符的标识符。我现在的方法是使用delimited_identifier正则表达式,其中包含delimiter个字符之一,主要词法分析器不应该被消费 ...然后一次&# 39;匹配后,Sedlexing.rollback(好,my wrapper thereof)恢复该词汇的读数,然后传递给吃掉实际标识符的sublexer,希望将分隔符留在缓冲区中,由父词法分子作为不同的词汇进行处理。

我使用MenhirSedlex,主要是从@smolkaj' s ocaml-parsing example-repo和RWO's parsing chapter合成示例;这是我当前parserlexer最简单的缩减:

%token LPAR RPAR LVEC APOS TICK COMMA COMMA_AT DQUO SEMI EOF
%token <string> IDENTIFIER
(* %token <bool> BOOL *)
(* %token <int> NUM10 *)
(* %token <string> STREL *)

%start <Parser.AST.t> program

%%

program:
  | p = list(expression); EOF { p }
  ;

expression:
  | i = IDENTIFIER { Parser.AST.Atom i }

%%

......和......

(** Regular expressions *)
let newline = [%sedlex.regexp? '\r' | '\n' | "\r\n" ]
let whitespace = [%sedlex.regexp? ' ' | newline ]
let delimiter = [%sedlex.regexp? eof | whitespace | '(' | ')' | '"' | ';' ]

let digit = [%sedlex.regexp? '0'..'9']
let letter = [%sedlex.regexp? 'A'..'Z' | 'a'..'z']

let special_initial = [%sedlex.regexp?
   '!' | '$' | '%' | '&' | '*' | '/' | ':' | '<' | '=' | '>' | '?' | '^' | '_' | '~' ]
let initial = [%sedlex.regexp? letter | special_initial ]

let special_subsequent = [%sedlex.regexp? '+' | '-' | '.' | '@' ]
let subsequent = [%sedlex.regexp? initial | digit | special_subsequent ]

let peculiar_identifier = [%sedlex.regexp? '+' | '-' | "..." ]
let identifier = [%sedlex.regexp? initial, Star subsequent | peculiar_identifier ]
let delimited_identifier = [%sedlex.regexp? identifier, delimiter ]


(** Swallow whitespace and comments. *)
let rec swallow_atmosphere buf =
   match%sedlex buf with
   | Plus whitespace -> swallow_atmosphere buf
   | ";" -> swallow_comment buf
   | _ -> ()

and swallow_comment buf =
   match%sedlex buf with
   | newline -> swallow_atmosphere buf
   | any -> swallow_comment buf
   | _ -> assert false

(** Return the next token. *)
let rec token buf =
   swallow_atmosphere buf;
   match%sedlex buf with
   | eof -> EOF

   | delimited_identifier ->
     Sedlexing.rollback buf;
     identifier buf

   | '(' -> LPAR
   | ')' -> RPAR

   | _ -> illegal buf (Char.chr (next buf))

and identifier buf =
   match%sedlex buf with
   | _ -> IDENTIFIER (Sedlexing.Utf8.lexeme buf)

(是的,它基本上是一种无操作/最简单的事情。我正在努力学习!:x

不幸的是,这种组合会在解析自动机中产生无限循环:

State 0:
Lookahead token is now IDENTIFIER (1-1)
Shifting (IDENTIFIER) to state 1
State 1:
Lookahead token is now IDENTIFIER (1-1)
Reducing production expression -> IDENTIFIER 
State 5:
Shifting (IDENTIFIER) to state 1
State 1:
Lookahead token is now IDENTIFIER (1-1)
Reducing production expression -> IDENTIFIER 
State 5:
Shifting (IDENTIFIER) to state 1
State 1:
...

我刚才解析和lexing以及所有这些;任何的建议都受欢迎。我确定这只是一个新手的错误,但是......

谢谢!

1 个答案:

答案 0 :(得分:3)

如前所述,在词法分析器中实现太多逻辑是一个坏主意。 但是,无限循环不是来自回滚,而是来自identifier的定义:

 identifier buf =
   match%sedlex buf with
   | _ -> IDENTIFIER (Sedlexing.Utf8.lexeme buf)
此定义中的

_匹配包含所有可能字符的语言中最短的单词。换句话说,_总是匹配空单词μ而不消耗其输入的任何部分,将解析器发送到无限循环。

相关问题