You are on page 1of 5

LAB SESSION 1

INTRODUCTION TO ANTLRWORKS

1. OBJECTIVE
The objectives of Lab 1 are (1) to play with the ANTLRWorks tool; (2) to construct a
simple lexer with few regular expressions on ANTLRWorks;

2. INTRODUCTION

ANTLRWorks (lastest released version of the legendary ANTLR tool) is a powerful tool
for those who want to develop a language compiler quickly and stably. To download via
https://www.antlr3.org/works/
JRE: https://www.java.com/download/manual.jsp
Download and run ANTLRWorks, example: java -jar antlrworks-1.5.2-complete.jar

3. EXPERIMENT

3.1 Run ANTLR

According to the lab setting, tutor will guide student how to run the pre-installed
ANTLRWorks. Once launched, an interface will show up as illustrated in Figure 1. For
convenient referring, the four sub-panels of the interface are labeled as Panel-1, Panel-2,
Panel-3 and Panel-4 as indicated by the red numbers.
Figure 1 – ANTLRWorks interface.

3.2 The first regular expression

Students will perform the following steps:

- Select Interpreter tab in Panel-3

- Type the following regular expression (RE) in Panel-2:

A : 'a'|'bc';

(do not forget the semicolon)

When done correctly, the drop-box associated with the left-arrow symbol in Panel-3 will
display the first available item (‘A”) as depicted in Figure 2.

Figure 2 – Regular Expression.


You may want to test your RE by typing an input string in Panel-3 and then press the left-
arrow to observe the result. A red message will inform you that there is an error
occurring. Switch to Console tab in Panel-3 to observe the error. It is likely that a lexer
is missing.

3.2 The first lexer

You must define a grammar before generating a lexer.

In order to define a grammar, just simply type the following command in the beginning
of Panel-2:

grammar <grammarName>;

(feel free to assign any value for the variable grammarName)

After defining a grammar, the next step is to declare your lexer, meaning that you will
type the command as follows:

@lexer::header {package test;};

(I still do not fully understand the meaning of header directive, but at least it works for
now. We will together investigate its meaning later.)

When done, your Panel-2 should look like that in Figure 3.

Figure 3 – The first lexer.

3.3 Test your lexer

You can input your test string in Panel-3 to check if your lexer works normally. If your
inputted string matches with the RE (e.g. “a” or “bc”), Panel-4 will display a tree, which
basically indicates that a pattern of A has been successfully recognized. Figure 4
illustrated a case of recognizing the input string of “bc”. Otherwise, an “error tree” will
be shown instead, as presented in Figure 5.
Figure 4 – A pattern successfully recognized.

Figure 5 – Oops, something wrong lah!

3.4 Lexer with multiple REs

You can generate a lexer with multiple REs defined, as depicted in Figure 6. Just to
remember to choose a desired pattern rule before testing a given string.

Figure 6 – Multi-RE lexer.


4. CLASS EXERCISES

4.1. Play with the following REs, check them with some right and wrong patterns.

a) A: ('a'|'bc')*;

b) B: '1' ('1'|'0 ')* '0 ';

c)

C: '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9';

D: C+;

4.2 Construct REs recognizing the following patterns:

a). A year in the 17th century (e.g. 1760)

b) A Vietnam mobile phone number, which is of the following forms: 09XXXXXXXX


or 849XXXXXXXX or +849XXXXXXXX

5. SUBMISSIONS

Students are required to submit in writing to the tutor-in-charge the following materials:

5.1 The trees generated by ANTLRWorks with the following REs and inputs

RE input

4.1a abcaabc

4.1b 11010

4.1c (D selected) 167340

5.2 The REs for exercises 4.2

You might also like