Next: , Up: Grammar styles   [Contents][Index]

4.1.1 Iterative style

The iterative style is the preferred style to use with Semantic. It relies on an iterative parser back-end mechanism which parses start nonterminals one at a time and automagically skips unexpected lexical tokens in input.

Compared to rule-based iterative functions (see Bison style), iterative parsers are better in that they can handle obscure errors more cleanly.

Each start nonterminal must produces a raw tag by calling a TAG-like grammar macro with appropriate parameters. See also Start nonterminals.

Then, each parsing iteration automatically translates a raw tag into expanded tags, updating the raw tag structure with internal properties and buffer related data.

After parsing completes, it results in a tree of expanded tags.

The following example is a snippet of the iterative style Java grammar provided in the Semantic distribution in the file semantic/wisent/java-tags.wy.

;; Alternate entry points
;;    - Needed by partial re-parse
%start formal_parameter
;;    - Needed by EXPANDFULL clauses
%start formal_parameters

    (EXPANDFULL $1 formal_parameters)

  | formal_parameter COMMA
  | formal_parameter RPAREN

  : formal_parameter_modifier_opt type variable_declarator_id
    (VARIABLE-TAG $3 $2 nil :typemodifiers $1)

It shows the use of the EXPANDFULL grammar macro to parse a ‘PAREN_BLOCK’ which contains a ‘formal_parameter_list’. EXPANDFULL tells to recursively parse ‘formal_parameters’ inside ‘PAREN_BLOCK’. The parser iterates until it digested all available input data inside the ‘PAREN_BLOCK’, trying to match any of the ‘formal_parameters’ rules:

At each iteration it will return a ‘formal_parameter’ raw tag, or nil to skip unwanted (single ‘LPAREN’ or ‘RPAREN’ for example) or unexpected input data. Those raw tags will be automatically expanded by the iterative back-end parser.

Next: , Up: Grammar styles   [Contents][Index]