Professional Documents
Culture Documents
Lab1 PPL
Lab1 PPL
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
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.
A : 'a'|'bc';
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.
In order to define a grammar, just simply type the following command in the beginning
of Panel-2:
grammar <grammarName>;
After defining a grammar, the next step is to declare your lexer, meaning that you will
type the command as follows:
(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.)
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.
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.
4.1. Play with the following REs, check them with some right and wrong patterns.
a) A: ('a'|'bc')*;
c)
C: '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9';
D: C+;
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