I am currently working on a Pascal Parser using OCaml and Menhir. When I compile my parser.mly, which contains the following scriptlet of code, Menhir simply says "Warning: if_cmd generates empty language" This might happen when the rule "if_cmd" can't be reached from the initial (correct me, if I'm wrong), but logically, it was supposed to be reached. I can't figure out why this warning is happening. For example, if I use an input file for the grammar specified in parser.mly with these statements:
program ex1;
x:=1
y:=(2)
it gives me the desired answer. But if I use this as input
program ex1;
x:=1
y:=(2)
if(x and 2) then
y:=3
it states Exception: Syntax.Error
, probably because of that Warning
With these 4 following files within the same folder, I've used the ocamlbuild -use-menhir main.byte
command, which generates the just mentioned Warning
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