Conditional grammar structures

+1 vote
asked Jul 13, 2020 in Grammars by lapplandscohan (150 points)
I'm writing a grammar for MPLS files.

One of the elements is StreamType (1 byte numeric), and if StreamType is 1 it is followed by by the A structure only. If StreamType is 2 it is followed by the B, C, and A structures in that order, and if StreamType is 3 it is followed by the B and A structures in that order.

How do I accomplish this?

I don't have the pro-version, so no scripting as I understand it unfortunately.

1 Answer

+1 vote
answered Sep 9, 2020 by vaczijani (160 points)

I've just purchased the software and had a similar problem. I've tried to put it together for you quickly.

The method I used:

- create new grammar

- add structure

  name it MPLS file: this is going to be your root element which will contain all the others

- add structure:

  name it type or StreamType: this is going to contain the numeric value which you will use for your condition.

  Right-click on it and append it with a number, call it type and make sure its length is 1 byte.

- add structures: A, B, C. I don't know the actual content of these but you will use them in your particular structures.

- add structure:

  name it StreamType 1: on the right in Properties: Set Extends entry to StreamType (or type depending how you called your type structure)

 click on StreamType 1/type element and in Fixed values: press '+' icon to add a value, give it a name and set its value to 1. I think this will make sure that this structure will be used when type = 1. Right-click on StreamType 1 element and append a structure reference to 'A'.

- Go back to MPLS file element and right-click and append a structure reference to StreamType 1.

  I'm guessing your MPLS file can contain any number of structures in any order so set MPLS file Properties on the right to have Element order: Variable and Repeat: min: 0 and max: Unlimited.

Click on Stream Type 1 within MPLS file and in Properties set Repeat: min: 0 and max:1. I guess this sets it to be optional.

Repeat everything you've done for StreamType 1 for your StreamType 2 and StreamType 3.

I'm hoping it is going to be easy once you tried it on the interface.

Best regards,


Here is the result I've got:

<?xml version="1.0" encoding="UTF-8"?>
<ufwb version="1.17">
    <grammar name="New grammar" start="id:36" author="JanosVaczi">
        <structure name="MPLS file" id="36" repeatmin="0" repeatmax="-1" encoding="ISO_8859-1:1987" endian="big" signed="no" order="variable">
            <structref name="&lt;Stream Type 1&gt;" id="50" repeatmin="0" structure="id:47"/>
            <structref name="&lt;Stream Type 2&gt;" id="68" repeatmin="0" structure="id:58"/>
            <structref name="&lt;Stream Type 3&gt;" id="69" repeatmin="0" structure="id:63"/>
        <structure name="StreamType" id="44" encoding="ISO_8859-1:1987" endian="big" signed="no">
            <number name="type" id="46" type="integer" length="1"/>
        <structure name="Stream Type 1" id="47" extends="id:44">
            <number name="type" mustmatch="yes" id="49" type="integer">
                    <fixedvalue name="One" value="1"/>
            <structref name="&lt;A&gt;" id="57" structure="id:51"/>
        <structure name="Stream Type 2" id="58" extends="id:44">
            <number name="type" mustmatch="yes" id="60" type="integer">
                    <fixedvalue name="Two" value="2"/>
            <structref name="&lt;B&gt;" id="61" structure="id:53"/>
            <structref name="&lt;C&gt;" id="62" structure="id:55"/>
        <structure name="Stream Type 3" id="63" extends="id:44">
            <number name="type" mustmatch="yes" id="65" type="integer">
                    <fixedvalue name="Three" value="3"/>
            <structref name="&lt;B&gt;" id="66" structure="id:53"/>
            <structref name="&lt;A&gt;" id="67" structure="id:51"/>
        <structure name="A" id="51" encoding="ISO_8859-1:1987" endian="big" signed="no"/>
        <structure name="B" id="53" encoding="ISO_8859-1:1987" endian="big" signed="no"/>
        <structure name="C" id="55" encoding="ISO_8859-1:1987" endian="big" signed="no"/>