0

私は現在、OCaml と Menhir を使用して Pascal パーサーに取り組んでいます。次のコード スクリプトレットを含む parser.mly をコンパイルすると、Menhir は単に「警告: 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を生成するコマンドを使用しました

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
4

0 に答える 0