如何将ANTLR C语法中的postfix_expression转换为AST?

发布于 2024-12-08 15:06:31 字数 689 浏览 0 评论 0原文

我正在通过修改C语法并尝试一些自己感兴趣的东西来学习ANTLR。我开始使用的C语法来自: http://www.antlr.org/grammar/1153358328744/ Cg

现在我想将 postfix_expression 转换为其相应的 AST,但还不知道与表单转换相关的任何内容xx (aa|bb|cc)* yy

...
unary_expression
  : postfix_expression
  | unary_operator^ unary_expression
  ;

postfix_expression
  : primary_expression
  ( '[' expression ']'
  | '(' ')'
  | '(' argument_expression_list ')'
  | '.' ID
  )*
  ;

unary_operator
  : '+'
  | '-'
  | '~'
  | '!'
  ;
...

你能帮我解决这个问题吗?您只需在语法中的 postfix_expression 部分添加一些 ^ 和/或 ! 符号即可。

I'm learning ANTLR by modifying the C grammar and trying something interests myself. The C grammar I started with is from: http://www.antlr.org/grammar/1153358328744/C.g

Now I want to transform postfix_expression to its corresponding AST but haven't known anything related to the transformation of the form xx (aa|bb|cc)* yy

...
unary_expression
  : postfix_expression
  | unary_operator^ unary_expression
  ;

postfix_expression
  : primary_expression
  ( '[' expression ']'
  | '(' ')'
  | '(' argument_expression_list ')'
  | '.' ID
  )*
  ;

unary_operator
  : '+'
  | '-'
  | '~'
  | '!'
  ;
...

Can you help me with this problem? You may just add some ^ and/or ! notations to the postfix_expression part in the grammar.

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

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

发布评论

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

评论(1

浮光之海 2024-12-15 15:06:31

我会选择这样的东西:

grammar T;

options {
  output=AST;
}

tokens {
  ROOT;
  MEMBER;
  INDEX;
  CALL;
}

parse
  :  unary_expression EOF -> ^(ROOT unary_expression)
  ;

unary_expression
  :  postfix_expression
  |  unary_operator unary_expression -> ^(unary_operator unary_expression)
  ;

postfix_expression
  :  primary_expression tail* -> ^(primary_expression tail*)
  ;

tail
  :  '[' expression ']'                -> ^(INDEX expression)
  |  '(' argument_expression_list? ')' -> ^(CALL argument_expression_list?)
  |  '.' ID                            -> ^(MEMBER ID)
  ;

primary_expression
  :  ID
  |  '(' expression ')' -> expression
  ;

argument_expression_list
  :  expression (',' expression)* -> expression+
  ;

unary_operator
  :  '+'
  |  '-'
  |  '~'
  |  '!'
  ;

expression
  :  NUMBER
  |  ID
  ;

NUMBER : '0'..'9'+;
ID     : ('a'..'z' | 'A'..'Z')+;

它将把输入解析

a.b.c(foo,42)[123]

为以下 AST:

图像描述">

Id'd go for something like this:

grammar T;

options {
  output=AST;
}

tokens {
  ROOT;
  MEMBER;
  INDEX;
  CALL;
}

parse
  :  unary_expression EOF -> ^(ROOT unary_expression)
  ;

unary_expression
  :  postfix_expression
  |  unary_operator unary_expression -> ^(unary_operator unary_expression)
  ;

postfix_expression
  :  primary_expression tail* -> ^(primary_expression tail*)
  ;

tail
  :  '[' expression ']'                -> ^(INDEX expression)
  |  '(' argument_expression_list? ')' -> ^(CALL argument_expression_list?)
  |  '.' ID                            -> ^(MEMBER ID)
  ;

primary_expression
  :  ID
  |  '(' expression ')' -> expression
  ;

argument_expression_list
  :  expression (',' expression)* -> expression+
  ;

unary_operator
  :  '+'
  |  '-'
  |  '~'
  |  '!'
  ;

expression
  :  NUMBER
  |  ID
  ;

NUMBER : '0'..'9'+;
ID     : ('a'..'z' | 'A'..'Z')+;

which will parse the input:

a.b.c(foo,42)[123]

into the following AST:

enter image description here

making it easy to evaluate the expression left to right.

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