Sie sind auf Seite 1von 10

LAB SESSION 1 (REVISION 1)

INTRODUCTION TO OCAMLLEX/OCAMLYACC & ECLIPSE IDE USAGE

1. OBJECTIVE
The objectives of Lab 1 are (1) to play with the Ocamllex/Ocamlyacc & ECLIPSE IDE
tool; (2) to construct a simple lexer with few regular expressions (REs) using
Ocamllex/Ocamlyacc.

2. INTRODUCTION
Ocamllex/Ocamlyacc is a powerful tool for those who want to develop a language
compiler quickly and stably. To download and get more information about
Ocamllex/Ocamlyacc, visit http://caml.inria.fr/pub/docs/manual-ocaml/manual026.html.

ECLIPSE is a multi-language software development environment comprising an IDE


and a plug-in system to extend it. It is written primarily in Java and can be used to
develop applications in Java and, by means of the various plug-ins, in other languages as
well, including C, C++, COBOL, Python, Perl, PHP, and others. The initial codebase
originated from VisualAge. In its default form it is meant for Java developers, consisting
of the Java Development Tools (JDT). Users can extend its capabilities by installing
plug-ins written for the Eclipse software framework, such as development toolkits for
other programming languages, and can write and contribute their own plug-in modules.
(http://en.wikipedia.org/wiki/Eclipse_(software)).

3. LAB CONTENTS

3.1. Install Ocamlex/Ocamlyacc


Ocamllex/Ocamlyacc requires:
− Cywin, a Linux-like enviroment for Windows.
− Ocaml, a programming language
• Get and install Cywin at http://www.cygwin.com
Note: You do not need to install any packages in cygwin when prompted. Or, you can
choose Ocaml in cygwin packages and you don't have to do the next step. Remember to
choose version 3.12.x
• Get and install Ocaml at http://caml.inria.fr/download.en.html, choose the
Windows version for the port based on the MinGW toolchain

3.2. Edit Ocamlex/Ocamlyacc in Eclipse


OcaIDE, the Eclipse plug-in for Ocamllex/Ocamlyacc, requires:
− Java 5.0+
− Eclipse 3.3/3.4/3.5
• Get and install Java SE Development Kit (JDK) 6.0 at

http://java.sun.com/javase/downloads/index.jsp

• Get Eclipse IDE 3.5.2 (Galileo Packages) at

http://www.eclipse.org/downloads/packages/eclipse-classic-352/galileosr2

• Install OcaIDE

Open Eclipse, click on Help > Install New Software...

Enter http://www.algo-prog.info/ocaide/ as the update site.


Check the OcaIDE category or feature in the list.
Then, click Next twice, accept the agreement and click Finish.
Accept the installation (the plug-in is not digitally signed), and restart Eclipse when it
asks you to. As soon as Eclipse is restarted, you can start using the plug-in.

You can find more information about OcaIDE installation at http://www.algo-


prog.info/ocaide/

Open Eclipse and choose the Eclipse workspace for the first run
Change the current perspective into Ocaml (Window -> Open Perspective -> Other,
choose Ocaml)

Create a new Ocaml project


Make a new lexical file to edit.
Note: If you receive the error warning that you haven't downloaded cygwin and set the
environment variables, that means you have to do the latter one. To do that, consult these
webpage:

• How to define an environment variable


http://www.support.tabs3.com/main/R10463.htm

• Environment variables of Cygwin http://www.cygwin.com/cygwin-ug-net/setup-


env.html

3.3. Ocamlex/Ocamlyacc tutorial


Open a command window (Start -> Run -> Enter “cmd”), then change the current
directory to Example1.
Run lexgen.bat to generate a lexical analyzer from REs in file lex.mll.
Note: File lex.mll contains REs to recognise patterns of numerical expressions.
Run compile.bat to make a driver to test the lexical analyzer.
Run run.bat to execute the driver with input from file test1.txt.
Using notepad or something, open file text1.txt and compare with the results on the
screen.
4. CLASS EXERCISES

4.1 Open text2.txt and point out the difference between two files.

Now insert this line into lex.mll, after line 8

| ['+' '-'] as x { ADD x}

Run lexgen.bat, compile.bat then driver test2.txt

Write the results!

4.2 Now insert this line into lex.mll, after recent added line

| ('*' | '/') as y { MUL y}

Run lexgen.bat, compile.bat then driver test3.txt

Look at the result, what do you think about square brackets (“[]”) and parentheses
(“(|)”)?

Das könnte Ihnen auch gefallen