Search code examples
ocamlyacclexocamllexocamlyacc

How to define unrecognized rules in Ocamlyacc


I'm working on company projet, where i have to create a compilator for a language using Ocamlyacc and Ocamllex. I want to know if is it possible to define a rule in my Ocamlyacc Parser that can tell me that no rules of my grammar matching the syntax of an input.

I have to insist that i'am a beginner in Ocamllex/Ocamlyacc

Thank you a lot for your help.


Solution

  • If no rule in your grammar matches the input, then Parsing.Parse_error exception is raised. Usually, this is what you want.

    There is also a special token called error that allows you to resynchronize your parser state. You can use it in your rules, as it was a real token produced by a lexer, cf., eof token.

    Also, I would suggest to use menhir instead of more venerable ocamlyacc. It is easier to use and debug, and it also comes with a good library of predefined grammars.