compiler-constructionocamlocamlyacc

Does precedence matter in ocaml parser?


For example if I was writing a parser.mly file like, and I have this written for expressions

expr:
    expr PLUS   expr { Binop($1, Add,   $3)   }
  | expr DIVIDE expr { Binop($1, Divide,   $3)   }

Would this be the same as

expr:
    expr DIVIDE   expr { Binop($1, Divide,   $3)   }
  | expr PLUS expr { Binop($1, Add,   $3)   }

Like I guess I'm asking if the order in which things are listed vertically matter in determining parsing precedence?


Solution

  • Both ocamlyacc and menhir require that you explicitly declare precedence, using the %left, %right and %nonassoc declarations. The order of productions is not relevant.