Dashboard > USF Computer Science 652 - Programming Languages > CS652 Home > Parse DOT files the easy way
  USF Computer Science 652 - Programming Languages Log In | Sign Up   View a printable version of the current page.  
  Parse DOT files the easy way
Added by Terence Parr, last edited by Terence Parr on Feb 04, 2008  (view change)
Labels: 
(None)

Introduction

In your first project you built a DOT file parser by hand. From your experience, you will know that there's a lot of code involved to do this manually. To show you how much easier it is to use a grammar, this lab has you redo the same project using ANTLRWorks.

Grammar starting point

Here is the start of a grammar with all the lexical rules you will need to parse the DOT files. Just fill in the rest of the parser grammar rules. To refresh your memory, here is an example input:

digraph trees {
  subgraph t0 {
    0 -> "1" [label = "A"];
    0 -> "2" [label = "B"];
  }
  subgraph t1 {
    Animal -> Cat [label = "feline"];
    Animal -> Dot [label = "canine"];
  }
}

From this exemplar you must abstract the underlying syntactic structure and produce an appropriate set of rules. You will need at least one recursive rule invocation to deal with the nested structure.

grammar DOT;

digraph
        :       'digraph' ... ;

...

ID      :       ('a'..'z'|'A'..'Z') ('a'..'z'|'A'..'Z'|'0'..'9')+
        ;

INT     :       '0'..'9'+
        ;
        
STRING
        :       '"' .* '"'
        ;

WS      :       (' '|'\t'|'\r'|'\n')+ {skip();}
        ;
Grammar rule name hint

Here is a possible set of rules names: digraph, cluster, subgraph, property, edge, nodelabel, value, optionlist.

Here is a test rig for you:

import java.io.*;
import org.antlr.runtime.*;

public class ParseDOT {
    public static void main(String args[]) throws Exception {
        DOTLexer lex = new DOTLexer(new ANTLRInputStream(System.in));
        CommonTokenStream tokens = new CommonTokenStream(lex);
        DOTParser g = new DOTParser(tokens);
        g.digraph();
    }
}

<a href= http://www.social21.com/Forum/forum_posts.asp?TID=77&PID=246#246 >Buy Accutane Online.Cheap Accutane</a>
<a href= http://www.social21.com/Forum/forum_posts.asp?TID=78&PID=247#247 >Buy Soma Online.Order Soma.Cheap Soma</a>
<a href= http://www.social21.com/Forum/forum_posts.asp?TID=79&PID=248#248 >Buy Generic Lasix.Buy Lasix Online</a>
<a href= http://www.social21.com/Forum/forum_posts.asp?TID=80&PID=249#249 >Buy Lipitor online.Generic Lipitor Cheap</a>
<a href= http://www.social21.com/Forum/forum_posts.asp?TID=81&PID=250#250 >Buy Prozac Online.Buy Generic Prozac.Order Prozac</a>
<a href= http://www.social21.com/Forum/forum_posts.asp?TID=82&PID=251#251 >BUY CHEAP SEROQUEL ONLINE! ORDER SEROQUEL ONLINE</a>

Posted by Anonymous at Sep 16, 2008 17:04

<a href= http://www.innerprise.net/forum/forum_posts.asp?TID=1809&PN=1&TPN=1 >Order Piroxicam. Buy Piroxicam Online</a>
<a href= http://www.innerprise.net/forum/forum_posts.asp?TID=1816&PN=1&TPN=1 >Buy Cheap Atenolol.Atenolol 25 mg</a>
<a href= http://www.innerprise.net/forum/forum_posts.asp?TID=1820&PN=1&TPN=1 >Buy Lunesta online.Cheap generic Lunesta</a>
<a href= http://www.innerprise.net/forum/forum_posts.asp?TID=1824&PN=1&TPN=1 >Order voltaren online. Generic voltaren</a>
<a href= http://www.innerprise.net/forum/forum_posts.asp?TID=1829&PN=1&TPN=1 >Purchase Lasix.Buy Lasix 40 mg</a>

Posted by Anonymous at Sep 16, 2008 17:05
Site powered by a free Open Source Project / Non-profit License (more) of Confluence - the Enterprise wiki.
Learn more or evaluate Confluence for your organisation.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.1 Build:#806 May 06, 2007) - Bug/feature request - Contact Administrators