计算YACC中特定类型变量的数量

发布于 2025-01-31 16:07:19 字数 1160 浏览 2 评论 0原文

我刚刚开始学习如何使用LEX和YACC。我正在使用这些网站中描述的语法( http:http:/ /www.quut.com/c/ansi-c-grammar-l-2011.html http://www.quut.com/c/ansi-c/ansi-c-grammar-y-2011.html )。

我目前正在尝试将YACC语法应用于我正在处理的代码:计数特定类型的变量(例如计数INT变量的数量)或计数已声明了多少个数组。

在定义部分中,我还以这样的方式写下:

%{
#include<stdio.h>
int num;
%}

YACC识别INT这样的变量,我想在规则部分中更改以下代码:

declaration 
    : declaration_specifiers ';'
    | declaration_specifiers init_declarator_list ';'
    ; 

像这样:

declaration
    : type_specifier init_declarator_list ';'
    | type_specifier IDENTIFIER '[' primary_expression ']' ';'
    ;

或者是特定的,如果我要计算INT变量或数组由INT组成,代码可能是这样写的:

declaration
    : INT init_declarator_list ';' {num++;}
    | INT IDENTIFIER '[' primary_expression ']' ';' {num++;}
    ;

显然,如果我只是这样编辑,则该代码会导致语法错误。此外,如果C代码是这样,它将无法计算变量:

int a,b;

仅计数一次。为了使它起作用,我应该采取什么措施?先感谢您。

I've just started to learn how to use Lex and Yacc. I'm using the grammar described in these sites(http://www.quut.com/c/ANSI-C-grammar-l-2011.html, http://www.quut.com/c/ANSI-C-grammar-y-2011.html).

I'm currently trying to apply the Yacc grammar to the code I'm working on: Counting specific type of variables(e.g. counting number of int variables) or count how many arrays have been declared.

In definition section, I additionally wrote like this:

%{
#include<stdio.h>
int num;
%}

For yacc to recognize such variable as int, I want to change the code below in rule section:

declaration 
    : declaration_specifiers ';'
    | declaration_specifiers init_declarator_list ';'
    ; 

like this:

declaration
    : type_specifier init_declarator_list ';'
    | type_specifier IDENTIFIER '[' primary_expression ']' ';'
    ;

or to be specific, if I were to count INT variables or array consisted of int, the code might be written like this:

declaration
    : INT init_declarator_list ';' {num++;}
    | INT IDENTIFIER '[' primary_expression ']' ';' {num++;}
    ;

and apparently this code will cause syntax error if I just edit like that. Moreover, it would not be able to count variables if C code was like this:

int a,b;

Only to count once. What move should I make in order to make it work? Thank you in advance.

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

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

发布评论

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

评论(1

另类 2025-02-07 16:07:19

您不能仅使用解析器(正确)执行此操作,因为以下是声明int variable的一种完全合法的方法:(

typedef int MyType;
MyType x;

C中的TypeDefs是 a是 Aliases ,而不是新类型

。对于int,在特定平台上。因此,这不仅仅是理论上。

即使将其抛在一边,尝试修改语法可能比使用语法要多得多。我的建议是,您可以创建某种类型的表示(例如AST),然后将语义操作用于变量声明,以查看声明的类型是否为简单int或其他内容。

You can't do this (correctly) with just a parser, because the following is a completely legal way of declaring an int variable:

typedef int MyType;
MyType x;

(Typedefs in C are aliases, not new types. So there is no difference between MyType and int after the above.)

It may well be the case that int32_t is a type alias for int, on a particular platform. So that's not just theoretical.

Even leaving that aside, trying to modify the grammar is probably a lot more work than using the grammar. My suggestion is that you create some kind of representation of types (something like an AST) and then use the semantic action for the variable declaration to see if the type being declared is a simple int or something else.

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