This is a tutorial on how to use ocamlyacc
which is distributed with
Ocaml language.
Lots of part of this document are borrowed from the bison manual.
All license term in this document is NOT related with ocamlyacc; it is ONLY for this document.
Please mail all comments and suggestions to me.
The companion tutorial for ocamllex
is available
at ocamllex tutorial.
The old tutorial and source of the examples used in this document can be found here.
`Ocamlyacc` is a general-purpose parser generator that converts a grammar description for an LALR(1) context-free grammar into a Ocaml program to parse that grammar.
This chapter introduces many of the basic concepts without which the details of Ocamlyacc will not make sense. If you do not already know how to use Ocamlyacc, we suggest you start by reading this chapter carefully.
Now we show and explain three sample programs written using Ocamlyacc: a reverse polish notation calculator, an algebraic (infix) notation calculator, and a multi-function calculator.
Ocamlyacc takes as input a context-free grammar specification and produces a Ocaml-language function that recognizes correct instances of the grammar. The Ocamlyacc grammar input file conventionally has a name ending in .mly.
The Ocamlyacc parser is actually Ocaml functions named after start symbols.
As Ocamlyacc reads tokens, it pushes them onto a stack along with their semantic values.
It is not usually acceptable to have a program terminate on a parse error. For example, a compiler should recover sufficiently to parse the rest of the input file and check it for errors; a calculator should accept another expression.
To debug the parser generated by ocamlyacc: ...
The usual way to invoke Bison is as follows: ocamlyacc filename.mly Here filename.mly is the grammar file name. The parser file’s name is made by replacing the .mly with .ml. Thus, the “ocamlyacc foo.mly” yields foo.ml. Options Ocamlyacc options
Bison license Bison License Copyright and permission Copyright and Permission