-
Notifications
You must be signed in to change notification settings - Fork 1
Home
OCamlyacc generates a parser from a set rules that are defined by the user in syntax similar to BNF, and OCamllex translates a set of regular expressions to a lexical analyzer. The goal of gt is to allow the user to easily define a context-free grammar in one file and then generate the corresponding parser, lexical analyzer and syntax trees based on the rules defined in the file. In addition, gt adds support for EBNF notation. For a more detailed explanation of gt please read the manual located here https://github.com/jjmeyer0/gt/blob/master/doc/manual/main.pdf.
Below are a few basic steps to follow in order to install and use gt from the terminal. This guide is assuming that the user has already obtained gt. If gt has not been downloaded please obtain the newest version here (https://github.com/jjmeyer0/gt). First, make sure OCaml is installed on your computer. To test if your machine has OCaml open a terminal and type the following command.
ocaml -version
If OCaml is installed you should get a message saying what version is installed. If you do not have OCaml go to the following page, download the latest version and follow the directions that are included to install OCaml.
http://caml.inria.fr/download.en.html
If new mlcf files have been created or the existing ones have been altered the following steps will update the corresponding OCaml files that mlcf alters. First, navigate to gt/ from a terminal. Next, execute the following commands:
make clean
make update
Lastly, if the user plans to view syntax trees Graphviz is needed. To obtain Graphviz follow the link below to download the version that works on your machine. The user will still be able to generate the syntax trees without Graphviz, but they will not be able to view or export them without Graphviz.
http://graphviz.org/Download.php
Usage of gt is shown below. Below, file is referring to a path of a parsable grammar file. First, from a terminal, navigate to gt/src/. Then execute the following commands.
make clean
make
./gt file
make emitted
The command "make clean" will remove all of the compiled OCaml files. The next command "make" will compile gt and generate a file called gt, the grammar tool that will generate a parser from a grammar definition. To use gt use the command "./gt file" where file is a path to a grammar file. Now that gt has generated all of the necessary files use the command "make emitted" to compile your parser. Once this has been done a parser with the same name as the grammar name in the corresponding grammar file has been created.
./grammar_name [options] <file>
The options are:
-p Reprints the contents of <file> to
<file>pp.txt. Without this option
the contents are printed to the terminal.
-a Prints a text view of <file>'s AST to
<file>ast.txt.
-g Outputs a Graphviz file named <file>gviz.dot
that contains a visual representation of <file>'s
syntax tree.
-help Shows this.
Grammar Definition: GT Syntax
Example 1: BNF
Example 2: EBNF 1
Example 3: EBNF 2