How to add custom node to AST in Pharo SmaCC?

217 views Asked by At

I am creating CST with universal custom nodes for Java language in SmaCC with Pharo. I found grammar (parser and scanner) and I tested it with few examples, creating Abstract Syntax Tree works perfect.

But, I need to create Concrete Syntax Tree with custom universal Nodes, e.g. on each while, for, do nodes I need to add parent node LOOP_STATEMENT. I can't find how to do it? Is it possible?

I read all about SmaCC and Pharo, but can't find example or solution here http://books.pharo.org/booklet-Smacc/html/Chapters/Smacc/SmaccAST.html

I tried for example to add custom CST nodes on sample Calculator code.

This is AST grammar

<whitespace> : \s+;

%left "+" "-";
%left "*" "/";
%right "^";

%annotate_tokens;
%root Expression;
%prefix AST;
%suffix Node;

Expression 
    : Expression 'left' "+" 'operator' Expression 'right' {{Binary}}
    | Expression 'left' "-" 'operator' Expression 'right' {{Binary}}
    | Expression 'left' "*" 'operator' Expression 'right' {{Binary}}
    | Expression 'left' "/" 'operator' Expression 'right' {{Binary}}
    | Expression 'left' "^" 'operator' Expression 'right' {{Binary}}
    | "(" Expression ")" {{}}
    | Number
    ;
Number 
    : <number> {{Number}}
    ;

For example, I need to add node SEPARATOR as a parent of each bracket. By knowing how to do that, that would solve my problem with Java grammar and adding LOOP_STATEMENT as a parent of while node.

0

There are 0 answers