如何在Yacc中制定特定规则以先于其他规则先减少?

me目

我有一个关于Yacc的问题。这是我的Yacc代码,用于解析C语言语法。请注意,下面的代码只是完整代码的一些相关部分:

%token  IDENTIFIER I_CONSTANT F_CONSTANT STRING_LITERAL FUNC_NAME SIZEOF
%token  PTR_OP INC_OP DEC_OP LEFT_OP RIGHT_OP LE_OP GE_OP EQ_OP NE_OP
%token  AND_OP OR_OP MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN ADD_ASSIGN
%token  SUB_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN
%token  XOR_ASSIGN OR_ASSIGN
%token  TYPEDEF_NAME ENUMERATION_CONSTANT
%token  TYPEDEF EXTERN STATIC AUTO REGISTER INLINE
%token  CONST RESTRICT VOLATILE
%token  BOOL CHAR SHORT INT LONG SIGNED UNSIGNED FLOAT DOUBLE VOID
%token  COMPLEX IMAGINARY
%token  STRUCT UNION ENUM ELLIPSIS
%token  CASE DEFAULT IF ELSE SWITCH WHILE DO FOR GOTO CONTINUE BREAK RETURN
%token  ALIGNAS ALIGNOF ATOMIC GENERIC NORETURN STATIC_ASSERT THREAD_LOCAL

%union{
    char* a;
    char* b;
}

%type <a>IDENTIFIER
%type <a>SIGNED UNSIGNED
%type <a>INT CHAR DOUBLE FLOAT LONG SHORT
%type <b>EXTERN STATIC AUTO REGISTER

%%
declaration
: declaration_specifiers ';'  
| declaration_specifiers init_declarator_list ';' { printf("semicolon:%s\n", $<a>3);} 
| static_assert_declaration
;

declaration_specifiers
: storage_class_specifier declaration_specifiers 
| storage_class_specifier                    
| type_specifier declaration_specifiers      
| type_specifier                             
| type_qualifier declaration_specifiers      
| type_qualifier                             
| function_specifier declaration_specifiers
| function_specifier
| alignment_specifier declaration_specifiers
| alignment_specifier
;

init_declarator_list
: init_declarator                           
| init_declarator_list ',' init_declarator  {printf("The comma:%s\n",$<a>2);} 
;

type_specifier
: VOID                                          {printf("type_specifier:%s\n",$<a>1);}
| CHAR                                          {printf("type_specifier:%s\n",$<a>1);}
| SHORT                                         {printf("type_specifier:%s\n",$<a>1);}
| INT                                           {printf("type_specifier:%s\n",$<a>1);}
| LONG                                          {printf("type_specifier:%s\n",$<a>1);}
| FLOAT                                         {printf("type_specifier:%s\n",$<a>1);}
| DOUBLE                                        {printf("type_specifier:%s\n",$<a>1);}
| SIGNED                                        {printf("type_specifier:%s\n",$<a>1);}
| UNSIGNED                                      {printf("type_specifier:%s\n",$<a>1);}
| BOOL                                          {printf("type_specifier:%s\n",$<a>1);}
| COMPLEX                                       {printf("type_specifier:%s\n",$<a>1);}
| IMAGINARY     /* non-mandated extension */    {printf("type_specifier:%s\n",$<a>1);}
| atomic_type_specifier                         {printf("type_specifier:%s\n",$<a>1);}
| struct_or_union_specifier                     {printf("type_specifier:%s\n",$<a>1);}
| enum_specifier                                {printf("type_specifier:%s\n",$<a>1);}
| TYPEDEF_NAME  {printf("type_specifier:%s\n",$<a>1);}
;

declarator
: pointer direct_declarator  
| direct_declarator  
;

direct_declarator
: IDENTIFIER {printf("The identifier: %s\n", $<a>1);}   
| '(' declarator ')'   
| direct_declarator '[' ']'     
| direct_declarator '[' '*' ']' 
| direct_declarator '[' STATIC type_qualifier_list assignment_expression ']' 
| direct_declarator '[' STATIC assignment_expression ']' 
| direct_declarator '[' type_qualifier_list '*' ']' 
| direct_declarator '[' type_qualifier_list STATIC assignment_expression ']' 
| direct_declarator '[' type_qualifier_list assignment_expression ']' 
| direct_declarator '[' type_qualifier_list ']'
| direct_declarator '[' assignment_expression ']' 
| direct_declarator '(' parameter_type_list ')'
| direct_declarator '(' ')' 
| direct_declarator '(' identifier_list ')' 
;

这是我的Lex代码,与Yacc代码相对应(再次,这些只是相关的部分):

"int"                   { yylval.a=strdup(yytext); return(INT); } /* Data Type*/
"long"                  { yylval.a=strdup(yytext); return(LONG); }
"char"                  { yylval.a=strdup(yytext); return(CHAR); }
"short"                 { yylval.a=strdup(yytext); return(SHORT); }
"signed"                { yylval.a=strdup(yytext); return(SIGNED); }
"double"                { yylval.a=strdup(yytext); return(DOUBLE); }
"unsigned"              { yylval.a=strdup(yytext); return(UNSIGNED); }
"float"                 { yylval.a=strdup(yytext); return(FLOAT); }
";"                     { yylval.a=strdup(yytext); return ';'; }
","                     { yylval.a=strdup(yytext); return ','; }

我的问题是,当我在以下命令中输入输入时:

int a,b;

它应该打印:

type_specifier:int
The identifier: a
The comma:,
The identifier:b
semicolon:;

但我的输出是

type_specifier:int
The identifier: a
The identifier: b
The comma:,
semicolon:;

我该怎么做才能使例行程序:{printf("The comma:%s\n",$<a>2);}在两个之间激活IDENTIFIER {printf("The identifier: %s\n", $<a>1);}

参考 (原始版本)

https://www.lysator.liu.se/c/ANSI-C-grammar-y.html

https://www.lysator.liu.se/c/ANSI-C-grammar-l.html

大卫·格斯琳

printf必须先完成对的解析,然后才能执行init_declarator尝试以下方法:

| init_declarator_list ',' {printf("The comma:%s\n",$<a>2);}  init_declarator  

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在SonarQube中仅运行一个特定规则?

如何从iptables中删除特定规则?

制定规则谁可以计数之后,如何将事实布尔值放入插槽中?

为什么使用议程过滤器激发特定规则时,Drools会验证所有其他规则?

设置.htaccess规则以指定其他文件夹

如何禁用FSharpLint中的特定规则?

如何制定Firebase规则以确保您只能在字段中输入某些内容?

其他规则中的makefile规则

当不满足其他任何规则时,如何创建htaccess规则?如何在php中捕获该规则?

如何制定规则“模板”

给定特定规则,如何将字符串中的特定字母大写

如何避免tslint特定规则?

您如何在Bazel规则中运行其他规则的可执行文件?

为什么在Bison中为每个操作制定规则

当日期高于Google Data Studio中的特定日期时,如何制定规则?

我如何在Java中制定杰西规则

如何在numpy中使用特定规则从数组中获取值

如何定义“其他”解析器规则以捕获非法额外内容

如何在Yii中给出验证规则以上传所有其他文件(.php,aspx,.cs等)

如何在yii中制定字母数字规则

如何在Apache中制定负面规则?

PHP Laravel 5制定验证器规则以检查是否存在具有特定类型的帐户

如何在makefile中为目标编写特定规则?

Excel-制定规则以强制显示三个数字,将“ null”替换为0

按特定规则为其他列选择行值对

为字符串 PHP 制定特定规则

请问如何通过sql来制定规则?

如何在特定规则上出错而不是警告

在excel中制定规则时遇到问题