ocamlocamllexocamlyacc

Not able to compile MLY file using Dune


I am writing my first Ocamllex and Ocamlyacc program by following this tutorial

My dune file looks like

(executable
 (public_name Calculator)
 (name main))
(ocamllex lexer)
(ocamlyacc parser)

My lexer.mll file is

{
  open Parser
}

rule read = parse
  | eof { EOF }

And parser.mly file is

%token EOF

%start <unit> prog

%%

prog:
  | EOF { () }
  ;

My main.ml file is

type expr = unit 

(** [parse s] parses string [s] into an AST. *)
let parse (s: string): expr = 
  let lexbuf = Lexing.from_string s in 
  let ast = Parser.prog Lexer.read lexbuf in 
  ast

(** [interp s] interprets [s] by Lexing and parsing it, 
    evaluating it, and converting the result into string *)
let interp (s: string) : string = 
  failwith "unimplemented"

When I say dune build I just get an error

File "bin/parser.mly", line 3: syntax error
%start <unit> prog

However the tutorial I am following (linked above) the same code compiles and runs. and it shows how to parse and empty string. I don't know much about this error because its not tell me what the syntax error is. I have written the code exactly as shown in the video.


Solution

  • I think ocamlyacc doesn’t have that %start <type> symbol syntax. You should separately specify the type using %type:

    %type <unit> prog
    %start prog