0

我目前正在使用 OCaml 和 Menhir 开发 Pascal Parser。当我编译包含以下代码小脚本的 parser.mly 时,Menhir 只是说“警告:if_cmd 生成空语言”这可能发生在无法从初始值到达规则“if_cmd”时(纠正我,如果我'我错了),但从逻辑上讲,它应该达到。我无法弄清楚为什么会发生此警告。例如,如果我使用 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命令,该命令会生成刚刚提到的警告

词法分析器

{
   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 }

解析器

%{
  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

主文件

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
4

0 回答 0