错误:使用Menhir和OCaml

时间:2016-05-05 18:12:23

标签: compiler-construction ocaml menhir

我目前正在使用OCaml和Menhir开发Pascal Parser。当我编译包含以下代码scriptlet的parser.mly时,Menhir只是说"警告:if_cmd生成空语" 这可能发生在规则" if_cmd& #34;无法从最初到达(纠正我,如果我错了),但从逻辑上讲,应该达到它。我无法弄清楚为什么会发生这种警告。 例如,如果我使用输入文件为parser.mly中指定的语法使用这些语句:

program ex1; 
x:=1
y:=(2)
它给了我想要的答案。但如果我用它作为输入

program ex1; 
x:=1
y:=(2)
if(x and 2) then
   y:=3

它声明Exception: Syntax.Error,可能是因为警告

将这4个以下文件放在同一个文件夹中,我使用了ocamlbuild -use-menhir main.byte命令,该命令会生成刚刚提到的警告

lexer.mll

{
   open Lexing
   open Printf
   open Parser

   let incr_line_number lexbuf = 
   let pos = lexbuf.lex_curr_p in
             lexbuf.lex_curr_p <- { pos with
             pos_lnum = pos.pos_lnum + 1;
             pos_bol = pos.pos_cnum;
            }
}


let digit = ['0' - '9']
let integer = digit+

let letter = ['a' - 'z' 'A' - 'Z']
let identifier = letter ( letter | digit | '_')*

let blank = [' ' '\t']+
let newLine = '\r' | '\n' | "\r\n"

rule token = parse
  blank    { token lexbuf }
| newLine  { incr_line_number lexbuf; token lexbuf }
| integer as num { let number = int_of_string num in 
                LITINT number  }
| '('                            { OPENPAR }
| ')'                            { CLOSEPAR }
| ';'                            { SEMICOLON }
| "program " + identifier as id  { PROGRAM (id) }
| "begin"                        { BEGIN }
| "end"                          { END }
| "if"                           { IF }
| "then"                         { THEN }
| "else"                         { ELSE }
| ":="                           { ASSIGN }
| "and"                          { AND }
| "or"                           { OR } 
| identifier as id               { ID id }
| eof                            { EOF }

parser.mly

%{
  open Ast (* Abstract Syntax Tree*)
%}

%token <int> LITINT
%token <string> PROGRAM
%token <string> ID
%token BEGIN END
%token SEMICOLON
%token ASSIGN
%token IF THEN ELSE
%token OPENPAR CLOSEPAR
%token AND OR
%token EOF

%left OR
%left AND

%start program
%type <Ast.program> program

%%

program: prog=PROGRAM SEMICOLON
      c = command*
      EOF
      { Program (prog, List.flatten c) }

command: c = assignment_cmd                     { c }
       | c = if_cmd                             { c }
       | BEGIN cs=command+ END SEMICOLON        { Block cs }

assignment_cmd: id = ID ASSIGN e = expression { AssignmentCmd (id, e) }

if_cmd: IF e=expression THEN 
            c1=command
            s=option(ELSE c2=command {c2})
        { IfCmd (e, c1, s) }

expression: id=ID                           { ExpVar id }
     | i=LITINT                             { ExpInt i }
     | e1=expression op=oper e2=expression  { ExpOp (op, e1, e2) }
     | OPENPAR e=expression CLOSEPAR        { e }

%inline oper:
     | AND      { And }
     | OR       { Or }

ast.ml

type ident = string

type program = Program of ident * commands
and commands = command list
and command = AssignmentCmd of ident * expression
            | IfCmd of expression * command * (command option)
            | Block of commands

and expression = ExpVar of ident
               | ExpInt of int
               | ExpOp of oper * expression * expression

and oper = 
         | And
         | Or

main.ml

open Ast

let parse_file name =
                        let ic = open_in name in
                        let lexbuf = Lexing.from_channel ic in
                        let ast = Parser.program Lexer.token lexbuf in
                        let _ = close_in ic in
                        ast

0 个答案:

没有答案