简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM