Adding Regular Expressions to Graphical Syntax Viewer

 

Submitted To:

Dr. Sergio Antoy

Portland State University

Submitted By:

Shobha Ranganathan

Date:

Winter 1999

 

 

Introduction

This project is an extension of prior work done by four other graduate students, Hazel Morgan, summer 1997, Vijayasree Krishnaprasad, fall 1997, Phillip R. Neff, winter 1998 and Sushma Kumar, spring 1998. Hazel's project, written in Java 1.0, implemented a diagrammatic representation of the syntax of a language. Each diagram was created by first constructing primitive diagram objects (i.e., TerminalDiagram, NonTerminalDiagram and EmptyDiagram). These primitive diagram objects were composed into another diagram using the SequenceDiagram, DecisionDiagram and LoopDiagram constructors. This composite diagram represented the production of a non-terminal of the target language. In the init() function of the applet class CurryDiagram, the setup() method built all the diagrams of the target language by calling a diagram-specific method for each of the non-terminals of the target language. All such diagrams were put in a hash table for easy access. These made the code dependent on the syntax of the target language.

In order to remove the bottleneck described above, the diagram-constructing functions were moved into a separate class called Grammar class with all the diagrams declared as public fields in the class. This Grammar class created all the diagrams needed for representing the syntax of the target language. Using the reflection methods available in java.lang.reflect class in JDK 1.1, the applet class CurryDiagram adds all the diagrams of the target language into the hash table.

The init() function displays the syntax of the target language as diagrams. As in any language grammar, non-terminals can be further expanded in terms of terminals and other non-terminals. Hence, the non-terminals displayed on the applet would respond to mouse clicks. On sensing a mouseDown event on a non-terminal, the appropriate diagram was located in the hash table using the appropriate key and displayed on the applet.

The prior work displayed the grammar of Curry language only as diagrams. These diagrams did not define certain symbols such as INTEGER or INFIXOP in the grammar. Such undefined symbols actually stand for regular expressions in the grammar of Curry language. In the prior work, clicking on both a nonterminal and an undefined symbol, which stands for a regular expression, gave the same result of displaying a diagram on the applet. These undefined symbols were displayed as terminals on the applet i.e. green colored uppercased strings inside ovals. This representation is incorrect since real terminals are always represented in lower case letters. So it was necessary to investigate a declarative representation for regular expression and link that representation to the graphical syntax viewer.

The graphical syntax viewer was implemented as an applet so that it can be embedded in a web page and viewed by standard browsers. It was limiting in the sense that it could not be executed as a standalone application.

 

Goals

The goals of this project are

      1. Find a notation for regular expressions in the grammar of the target language and represent and integrate them with the graphical syntax viewer.
      2. Be able to run the CurryDiagram program both as an applet and as an application.

Current Work

To represent and display the regular expressions present in the Curry language, it was necessary to extend GrammarConverter.java to accept regular expressions and make the syntax viewer present them on the applet. As a first step, a new keyword regularexp was introduced to signal the start of the regular expressions in the human readable representation of the diagram definitions contained in an ASCII text file.

 

The following shows an example of how a regular expression is written in the ASCII file. Consider the non-terminal ListConstr, which in turn contains the non-terminal Nil, where Nil stands for a regular expression.

(diagram ListConstr

(sequence

(choice

(nonterminal Nil)

(terminal ":"))))

Regular expression for Nil is

"["(["\", "\n", "\r"])*"]"

Nil would be expanded as follows in the ASCII file.

(regularexp Nil ("\"[\" ([ \" \", \"\\n\", \"\\r\"])* \"]\""))

 

Nil is written as above with the inclusion of backslashes so that Java recognizes the regular expression string as a string containing double quotes (i.e., a quoted string).

A major step towards achieving my first goal was to modify GrammarConverter.java so that it recognizes the start of regular expressions in the text file containing diagram definitions and interprets the regular expression specification that follows it. Hence, a separate routine called parseReg() was added in GrammarConverter.java to extract the regular expression, in this case, "["(["\", "\n", "\r"])*"]", from the text file. To add the above functionality in GrammarConverter.java, the following lines of code were added.

 

if (expecting_reg_name) { // Expecting Regular expressions

latestName = tokens.sval;

diagNames.addElement(latestName);

DiagramParser parser = new DiagramParser();

tempString = parser.parseReg(tokens);

finalStr = transformString(tempString);

sequenceDecl = latestName + " = " + finalStr;

expecting_name = false;

expecting_reg_name = false;

expecting_end = true;

}

 

 

To handle the graphical display of regular expressions, a separate class called Regular was defined. The Java code for building a regular expression to be written into Grammar.java was an output of parseReg() routine. Hence parseReg() routine returns an instantiation code in string form for each of the regular expression encountered in the text file. As an example, return value of parseReg() for Nil would be new Regular("["(["\", "\n", "\r"])*"]"). As regular expressions often include quotes, they have to be displayed on the applet in the same way. So the function transformString() was added to do the conversion of "["(["\", "\n", "\r"])*"]" into "\"[\" ([ \" \", \"\\n\", \"\\r\"])* \"]\"".

The following example shows how the regular expression for Nil would be written in Grammar.java.

Nil = new Regular("\"[\" ([ \" \", \"\\n\", \"\\r\"])* \"]\"");

Class Regular is a subclass of abstract class Diagram. Class Regular determined whether the regular expression would be displayed on the applet in a single line or in multiple lines depending upon the length of the regular expression string. Suppose it were n numbers of lines of display, then the regular expression string would be split into n numbers of substrings. All these substrings were stored in an array with the first element as the leftmost substring of regular expression string and last element being the rightmost substring of the regular expression string. Hence, the paint()method in class Regular was overridden to use this array for displaying the regular expression string. It was also modified to dictate the font size and the color of the regular expression string and the color of the region surrounding the regular expression string. So the following lines of code were added to the paint() method.

public void paint (Graphics g){

Color terminalColor = new Color(255,0,255);

startString = 5;

g.setFont(t_font);

setBackground(Color.white);

terminalColor = ((terminalColor.brighter()).darker()).darker();

terminalColor = (terminalColor.brighter()).darker();

g.setColor (terminalColor);

for(int i =0; i <= len; i++)

{

g.drawString(sub[i], startString,dest[i]);

}

}

Hence, the regular expressions were displayed in purple on a white background, relative to their size, on a gray colored applet. To make all those non-terminals that expand into regular expressions look distinct and sharp in amidst of other non-terminals, these non-terminals were displayed in pink edged rectangles with white color interior. Hence regular expressions have a representation and a colorful display as opposed to being treated as terminals in capital letters.

To fulfill the second goal, one of the approaches taken was to center the Applet within a Frame. As a first step, the main() method was added in the applet class CurryDiagram. Putting the main() method in the applet class enables the program to be executed with same arguments whether it is going to be run as an applet or as an application. The following lines of code were added to the applet class CurryDiagram.

 

public static void main (String [] args) {

(new CurryFrame ("Syntax Diagrams")).show(); }

 

Since applications have to create their own frames (Applets run in the frame provided by the Java enabled web browsers or appletviewer), the class CurryFrame was defined as a subclass of Frame with the following definition.

class CurryFrame extends Frame {

/** Construct the frame and add the MainPanel to it. */

 

CurryFrame (String frameTitle) {

super(frameTitle);

add("North", new MainPanel (null));

pack ();

}

}

A new class called MainPanel was defined as a subclass of Panel. Class MainPanel took over the responsibility (from setup()method in the init() function of the applet class CurryDiagram) of pulling out the diagrams from the object Grammar and adding the diagrams to the panel. Hence, all the functionality of the setup() method existing in the prior code was removed from the applet class and moved to the constructor function of class MainPanel. The earlier init() method of the applet class, CurryDiagram, was replaced by the following lines of code.

public void init() {

add(new MainPanel (this) );}

 

Class MainPanel handled all the mouse events except closing the program and frame, which were handled by CurryFrame class. In the init() method of the applet class CuryyDiagram, MainPanel was added to it and the CurryFrame displayed it when its show() method was invoked.

Future Work

The display and the layout for regular expressions can be made more interesting. Currently, they are shown as a plain purple colored string in white background relative to their length. This may not work well with some complicated regular expressions that occur in the syntax of Curry language. For accommodating complex regular expressions, new classes will have to be defined for an effective graphic representation.

Conclusions

The changes made to the graphical syntax viewer display regular expressions in classic way as opposed to being treated merely as terminals. By adding regular expressions to graphical syntax viewer, an attempt has been made to complete the graphical display of the syntax of Curry language.

The CurryDiagram program can now be executed both from within a browser and as a standalone application, thereby giving flexibility of execution.