Parse DOT files the easy way

Skip to end of metadata
Go to start of metadata

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:

Labels:
None
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.