Search code examples
c#.netparsingyaccirony

What's Irony's equivalent of Yacc's optional operator ("?")?


I have a fragment of grammar in Yacc notation:

stylesheet
: [ CHARSET_SYM STRING ';' ]?
  [S|CDO|CDC]* [ import [ CDO S* | CDC S* ]* ]*
  [ [ ruleset | media | page ] [ CDO S* | CDC S* ]* ]*
;

How do I implement this fragment in Irony? I can't find any equivalent of ?, which means 0 or 1 occurrence in Yacc.


Solution

  • You can use the BnfTerm.Q method to represent '?' (0 or 1 occurrences). This was a reasonable design decision since C# does not let you write a custom implementation of the ? operator, unlike + and *.

    From the Non Terminals page on the Irony Wikibook:

    In traditional BNF notation, the "?", "+", and "*" characters are used to indicate "0 or 1 time", "1 or more times" and "0 or more times", respectively. In Irony, it's done slightly differently. You use the MakePlusRule and MakeStarRule methods from the base Grammar class for "+" and "*" or you can use the Q(), Plus(), and Star() methods directly on the term within the rule.