Skip to content

Start nonterminals

When you write a grammar for Semantic, it is important to carefully indicate the start nonterminals. Each one defines an entry point in the grammar, and after parsing its semantic value is returned to the back-end iterative engine. Consequently:

The semantic value of a start nonterminal must be a produced by a TAG like grammar macro.

Start nonterminals are declared by %start statements. When nothing is specified the first nonterminal that appears in the grammar is the start nonterminal.

Generally, the following nonterminals must be declared as start symbols:

  • The main grammar entry point

    Of course!

  • nonterminals passed to EXPAND/EXPANDFULL

    These grammar macros recursively parse a part of input data, based on rules of the given nonterminal.

    For example, the following will parse ‘PAREN_BLOCK’ data using the ‘formal_parameters’ rules:

    bash
    formal_parameter_list
      : PAREN_BLOCK
        (EXPANDFULL $1 formal_parameters)
      ;

    The semantic value of ‘formal_parameters’ becomes the value of the EXPANDFULL expression. It is a list of Semantic tags spliced in the tags tree.

    Because the automaton must know that ‘formal_parameters’ is a start symbol, you must declare it like this:

    %start formal_parameters

The EXPANDFULL macro has a side effect it is important to know, related to the incremental re-parse mechanism of Semantic: the nonterminal symbol parameter passed to EXPANDFULL also becomes the reparse-symbol property of the tag returned by the EXPANDFULL expression.

When buffer’s data mapped by a tag is modified, Semantic schedules an incremental re-parse of that data, using the tag’s reparse-symbol property as start nonterminal.

The rules associated to such start symbols must be carefully reviewed to ensure that the incremental parser will work!

Things are a little bit different when the grammar is written in Bison style.

The reparse-symbol property is set to the nonterminal symbol the rule that explicitly uses EXPANDTAG belongs to.

For example:

bash
rule:
    rhs
    (let* ((rhs $1)
           name type comps prec action elt)
      ...
      (EXPANDTAG
       (TAG name 'rule :type type :value comps :prec prec :expr action)
       ))
  ;

Set the reparse-symbol property of the expanded tag to ‘rule’. An important consequence is that:

Every nonterminal having any rule that calls EXPANDTAG in a semantic action, should be declared as a start symbol!