简体   繁体   English

我的语法中的函数定义问题

[英]issue with the function definition in my grammar

I have an issue with the function definition in my C grammar wich can be found here http://www.archive-host.com/files/1959635/24fe084677d7655eb57ba66e1864081450017dd9/cAST.txt , it does not define correctly and I can't multiply it by something. 我的C语法中的函数定义存在问题,可以在这里找到http://www.archive-host.com/files/1959635/24fe084677d7655eb57ba66e1864081450017dd9/cAST.txt ,它没有正确定义,我无法繁殖它靠某种东西。 The code I am tring to input is this one : 我要输入的代码是这样的:

int factorielle(int n)
  { int x;
   if ( n == 0)
  return 1;
   else return n*factorielle(n-1);
  }

The function definition is this one : 函数定义是这样的:

function_definition
    : declaration_specifiers declarator compound_statement
    | declarator compound_statement
    ;

declaration_specifiers should be linked to int and declarator to factorielle(int n), to do this I replaced this : declaration_specifiers应该链接到int和declarator到factorielle(int n),为此我替换了这个:

direct_declarator
: ID ((direct_declarator '[' ']') | (direct_declarator '(' parameter_type_list ')') | (direct_declarator '(' identifier_list ')') | (direct_declarator '(' ')') )*

with

direct_declarator
: ID ((direct_declarator '[' ']') | (direct_declarator '(' parameter_type_list ')') | (direct_declarator '(' identifier_list ')') | (direct_declarator '(' ')')  | '(' parameter_type_list ')' )*

But it does not help much. 但它没有多大帮助。

As for the multiplication I don't know how to do without bringing conflict. 至于乘法,我不知道怎么做而不会带来冲突。 is there a way to fix this please ? 有没有办法解决这个问题?

You're likely to have an difficult time parsing real C code using a pure grammar with pure ANTLR. 使用纯语法和纯ANTLR解析真正的C代码可能会很困难。

The reason is that certain declarations look like legitimate executable statements . 原因是某些声明看起来像合法的可执行语句 (While the referenced answer seems to be about LR(1) parsers, it is really about parsers that cannot handle ambiguity; ANTLR cannot). (虽然引用的答案似乎是关于LR(1)解析器,但它实际上是关于无法处理歧义的解析器; ANTLR不能)。

The only way to tell them apart is to use context information available from earlier symbol declarations. 区分它们的唯一方法是使用早期符号声明中提供的上下文信息。 So you will have to collect symbol types as you parse, and inspect that information in the grammar rule reductions to decide whether such instances are statements or declarations. 因此,您必须在解析时收集符号类型,并在语法规则缩减中检查该信息,以确定此类实例是语句还是声明。 (I don't know how one implements this in ANTLR, although I believe it to be possible). (我不知道如何在ANTLR中实现这一点,尽管我相信它是可能的)。

I may have found a solution to the first part of the issue by remplacing 我可能通过重新启动找到了问题第一部分的解决方案

compound_statement
  : '{' '}'
  | '{' statement_list '}'
  ;

with

compound_statement
  : '{' '}'
  | '{' statement_list '}'
  | '{' external_declaration+ '}'
  ;

and adding this to direct_declarator: 并将其添加到direct_declarator:

| ID '(' parameter_type_list ')'

But I don't know if it will bring some conflicts. 但我不知道它是否会带来一些冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM