SmaCCRefactoring / SmaCC

Smalltalk Compiler Compiler : a parser generator
Other
33 stars 15 forks source link

Type Inference and Hierarchy #22

Open ThierryGoubier opened 7 years ago

ThierryGoubier commented 7 years ago

SmaCC correctly infer types if the right superclass is created, fail inference if there is no common superclass, and never creates labelled node (i.e. a superclass instance).

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

This segment only resolve properly if the following is added:

%root Expression;

Or

%hierarchy Expression (Binary Number);

Otherwise SmaCC signal the following error:

Unnamed symbol in production. Without a variable name the value will be dropped from the parsed AST.

Expression : "(" >>>Expression<<< ")" 
ThierryGoubier commented 7 years ago

To explain: with either the %root or %hierarchy directive, SmaCC will generate three classes around Expression, Binary and Number.

With %prefix AST; and %suffix Node;, we will have:

SmaCCParseNode subclass: #ASTExpressionNode
    instanceVariableNames: 'leftParenTokens rightParenTokens'
    classVariableNames: ''
    poolDictionaries: ''
    category: 'SmaCC-Tutorial'

And:

ASTExpressionNode subclass: #ASTBinaryNode
    instanceVariableNames: 'left operator right'
    classVariableNames: ''
    poolDictionaries: ''
    category: 'SmaCC-Tutorial'.
ASTExpressionNode subclass: #ASTNumberNode
    instanceVariableNames: 'numberToken'
    classVariableNames: ''
    poolDictionaries: ''
    category: 'SmaCC-Tutorial'

Parsing the following: ((4)) will then create an ASTNumberNode with the parenthesis inside the node:

image

That is, even with the parenthesis, no ASTExpressionNode will be created. The same with (3+4), it will create an instance of ASTBinaryNode, with the parenthesis inside.

ThierryGoubier commented 7 years ago

Evaluation: