带初始化和不带初始化的变量定义树

发布于 2024-12-01 21:23:48 字数 1066 浏览 3 评论 0原文

我要解析的语言包含类似

public var a, b = 42, c;

I.e. 的 语句.g 文件看起来像这样:

statements
    : (introduction | expression ';'! | ... )+
    ;
introduction
    : head single+ -> ^(head single)+
    ;
single
    : Name ('='^ expression)?
    ;
head
    : modifiers* v='var' -> ^(VARIABLE[$v] modifiers*)
    ;

生成这样的树很容易,但大多没用(对我来说):

         ----------statements----------
        /               |              \
    variable         variable       variable
    /      \         /      \       /      \
'public'   'a'   'public'  '='   'public'  'c'
                          /   \
                         'b'  expr

我希望将 '=' 放在中间节点的顶部:

         ----------statements----------
        /               |              \
    variable           '='          variable
    /      \          /   \         /      \
'public'   'a'  variable  expr   'public'  'c'
                 /     \
             'public'  'b'

但是我找不到执行此操作的重写规则。

my language to parse contains statements like

public var a, b = 42, c;

I.e. the .g file looks something like:

statements
    : (introduction | expression ';'! | ... )+
    ;
introduction
    : head single+ -> ^(head single)+
    ;
single
    : Name ('='^ expression)?
    ;
head
    : modifiers* v='var' -> ^(VARIABLE[$v] modifiers*)
    ;

Generating a tree like that would be easy, but mostly useless (for me):

         ----------statements----------
        /               |              \
    variable         variable       variable
    /      \         /      \       /      \
'public'   'a'   'public'  '='   'public'  'c'
                          /   \
                         'b'  expr

I would like to have the the '=' on top of the middle node:

         ----------statements----------
        /               |              \
    variable           '='          variable
    /      \          /   \         /      \
'public'   'a'  variable  expr   'public'  'c'
                 /     \
             'public'  'b'

but I can't find the rewrite rule to do that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乞讨 2024-12-08 21:23:48

以你制定规则的方式来说,这并不容易做到。

这是一种可能的方式:

grammar T;

options { 
  output=AST;
  ASTLabelType=CommonTree; 
}

tokens {
  STATEMENTS;
  VARIABLE;
  DEFAULT_MODIFIER;
}

declaration
  :  modifier 'var' name[$modifier.tree] (',' name[$modifier.tree])* ';' -> ^(STATEMENTS name+)
  ;

modifier
  :  'public'
  |  'private'
  |  /* nothing */ -> DEFAULT_MODIFIER
  ;

name [CommonTree mod]
  :  ID '=' expression -> ^('=' ^(VARIABLE {new CommonTree(mod)} ID) expression)
  |  ID                -> ^(VARIABLE {new CommonTree(mod)} ID)
  ;

// other parser & lexer rules

它会生成以下 AST:

>

public var a, b = 42, c;

并生成:

在此处输入图像描述

输入:

var a, b = 42, c;

That is not easly done with the way you've set up your rules.

Here's a way it is possible:

grammar T;

options { 
  output=AST;
  ASTLabelType=CommonTree; 
}

tokens {
  STATEMENTS;
  VARIABLE;
  DEFAULT_MODIFIER;
}

declaration
  :  modifier 'var' name[$modifier.tree] (',' name[$modifier.tree])* ';' -> ^(STATEMENTS name+)
  ;

modifier
  :  'public'
  |  'private'
  |  /* nothing */ -> DEFAULT_MODIFIER
  ;

name [CommonTree mod]
  :  ID '=' expression -> ^('=' ^(VARIABLE {new CommonTree(mod)} ID) expression)
  |  ID                -> ^(VARIABLE {new CommonTree(mod)} ID)
  ;

// other parser & lexer rules

which produces the following AST:

enter image description here

for the input:

public var a, b = 42, c;

And produces:

enter image description here

for the input:

var a, b = 42, c;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文