UniCC v1.3 is out

UniCC v1.3 has just been released.

It contains many new, internal revisions, but also some new features. The most important one is the new AST notation feature.

Abstract syntrax tree notations can be specified within a grammar. These notations instruct the resulting parser to automatically construct an abstract syntrax tree (AST) during the full parse tree construction. The feature of constructing abstract syntrax trees rather than performing semantics during the parse process is a new approach which will become more important in a next UniCC major release (see https://github.com/phorward/unicc/wiki/UniCC-v2).

For AST notations, the grammar definition language has been slightly modified to allow for some more distinctive syntax. The following example implements the well-known expressional grammar without any semantics. The benefit is, that such kinds of grammars can be implemented in any UniCC target without any modification.

#whitespaces    ' \t';

@integer        '0-9'+                       = int;

#left           '+' '-';
#left           '*' '/';

//Defining the grammar
calculator$     : expression
                ;

expression      : expression '+' expression  = add
                | expression '-' expression  = sub
                | expression '*' expression  = mul
                | expression '/' expression  = div
                | '(' expression ')'
                | @integer

That's all. This example already uses the new-style syntax, where -> is replaced by : and the new operators := and = take place.

The equal-operator (=) can be used on productions and token definitions to define a generated AST node, while the defines-equal-operator (:=) is used to specify the defined nonterminal as name for the AST node. In case of multiple nonterminal definitions, the first will be used.

Compile and run with C:

$ unicc expr.ast.par
$ cc -o expr.ast expr.ast.c
$ ./expr.ast -l

ok
1+2*3+4
add
 add
  int (1)
  mul
   int (2)
   int (3)
 int (4)

Compile and run with Python:

$ unicc -l python expr.ast.par
$ python expr.ast.py
>1+2*3+4
add
 add
  int (1)
  mul
   int (2)
   int (3)
 int (4)

Please note, that this feature is still in beta, and will become useful in an upcoming UniCC version.