Ask a questionAsk a question
 

AnswerChanging default values in MGrammar

  • Thursday, June 25, 2009 8:38 PMjustncase80 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I have a syntax something like this:

    syntax Foo 
         =   b:Bar z:Baz?
         => Foo { Bar => b, Baz => z }

    But if Baz doesn't match then the graph projects to be:

    Foo { Bar => Bar { ... }, Baz => null }

    What I'd really like is for Baz to not project to null but something else instead (such as Baz { }). The only way I could get it to work so far is to completely duplicate the syntax one with Baz required and one with Baz totally missing and change the greater projection.



    Here is a more complete example

    Given the input text "bar", modify this language:

    module test
    {
        language test
        {
            syntax Main = Foo;
            
            syntax Foo 
                =  b:Bar z:Baz?
                => Foo { Bar => b, Baz => z };
            
            token Bar = "bar" => true;
            token Baz = "baz" => true;
        }
    }

    To produce the graph:

    Main[
      Foo{
        Bar => true,
        Baz => false
      }
    ]

    • Edited byjustncase80 Thursday, June 25, 2009 8:38 PMformatting
    •  

Answers

  • Thursday, June 25, 2009 10:04 PMjustncase80 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Rocky Lhotka helped me solve this perfectly by pointing out the "empty" keyword.

    b : (b : Bar => b | empty => false)
    z : (b : Baz => b | empty => false)
    • Marked As Answer byjustncase80 Thursday, June 25, 2009 10:04 PM
    •  

All Replies

  • Thursday, June 25, 2009 10:04 PMjustncase80 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Rocky Lhotka helped me solve this perfectly by pointing out the "empty" keyword.

    b : (b : Bar => b | empty => false)
    z : (b : Baz => b | empty => false)
    • Marked As Answer byjustncase80 Thursday, June 25, 2009 10:04 PM
    •