Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You <em>could</em> do the checking of valid super-parks (<code>extends ...</code>) inside the parser grammar, but a tree grammar is less cluttered version of your (combined) grammar: so I like to do such stuff in there.</p> <p>What you could do is create a <code>Set&lt;String&gt; parks</code> as a member of your tree walker:</p> <pre><code>tree grammar CarParkWalker; options { tokenVocab=CarPark; // assuming your combined grammar is called CarPark.g ASTLabelType=CommonTree; } @members { private Set&lt;String&gt; parks = new HashSet&lt;String&gt;(); } // rules here </code></pre> <p>and then add <code>String</code>s to it while the AST is traversed. Then when you stumble upon an <code>extends VALUE</code>, you add some custom code where you check if <code>VALUE.text</code> is present in your <code>Set&lt;String&gt; parks</code>. If not, throw an exception.</p> <p>About mandatory an optional attributes of cars (or car-parks), simply accept zero or more parameters in your (combined) grammar and let your tree grammar rules return a <code>Vehicle</code> instance:</p> <pre><code>/* tree grammar rules! */ vehicle returns [Vehicle v] : car {$v = $car.v;} | motorcycle {$v = $motorcycle.v;} ; car returns [Vehicle v] @init{$v = new Car();} : ^(Car ... ) ; motorcycle returns [Vehicle v] @init{$v = new Motorcycle();} : ^(Motorcycle ... ) ; </code></pre> <p>where a <code>Vehicle</code> may look like:</p> <pre><code>abstract class Vehicle { protected String make; protected String color; protected int age; protected int wheels; // ... } </code></pre> <p>And then check if for each vehicle if all mandatory attributes have been set.</p> <p>If, after trying yourself, you're having trouble implementing all this, I'm willing to post a little demo of it. Just add a comment in that case.</p> <p>Good luck!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload