解析C声明[savestruct] [英] Parsing C declarations [savestruct]

查看:105
本文介绍了解析C声明[savestruct]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



好​​吧,我正在尝试写几个星期前请求的程序

a,可以采用结构定义的那个

并创建便携式函数来读写那些

结构。因此,主题行中的savetruct。


我不能为我的生活弄清楚如何正确解析C

声明!我想出了一个中间的

格式,如果可能的话,我真的想保留,

,它涉及制作一个链表(或树) ,在这个看起来像这样的函数中,这是一个类似于:b $ b $(* p)[5];变成


is_a:指向

下来: - 。

|

` - > is_a:数组

down: - 。

|

` - > is_a:signed int

down:(NULL)


我遇到的问题是使用

解析声明尽可能地展望;有一个令牌是

前瞻,我已设法解析*


int(* p)[5];正确的,*或*


int p [3] [4];正确地说,


但无论我尝试什么,最终会弄乱其中一个

两个案例。


有没有人这里已经有了一个很好的解析器,

会生成这种树格式,或者认为它是非常琐碎的,可以在这里向我解释

或离线?


我目前的工作完成在
http://www.contrib.andrew.cmu.edu/~ajo/struct.html


TIA,

-Arthur


Well, I''m trying to write that program that was requested
a few weeks back, the one that could take struct definitions
and create portable functions to read and write those
structs. Hence the ''savestruct'' in the subject line.

I cannot for the life of me figure out how to parse C
declarations correctly! I''ve come up with an intermediate
format that I''d really like to keep, if at all possible,
which involves making a linked list (or tree, in the case
of functions) that looks something like this:

int (*p)[5]; becomes

is_a: pointer to
down: --.
|
`--> is_a: array of
down: --.
|
`--> is_a: signed int
down: (NULL)

The problem I''m having is parsing declarations using
as little look-ahead as possible; with one token of
look-ahead, I have managed to parse *either*

int (*p)[5]; correctly, *or*

int p[3][4]; correctly,

but whatever I try ends up messing up one of those
two cases.

Does anyone here already have a nice parser that
generates this kind of tree format, or think it''s
trivial enough to kindly explain to me either here
or offline?

My current effort is posted complete at
http://www.contrib.andrew.cmu.edu/~ajo/struct.html

TIA,
-Arthur

推荐答案

>
我不能为生活我弄清楚如何正确解析C
声明!我想出了一个中级
I cannot for the life of me figure out how to parse C
declarations correctly! I''ve come up with an intermediate



参见K& R(Kernighan& Ritchie的C编程语言,第2版),

第5.12节

HTH,

K



See K&R (The C Programming Language by Kernighan & Ritchie, 2nd Edition),
section 5.12

HTH,
K




在2003年10月1日星期四,M Kumar写道:

On Thu, 1 Oct 2003, M Kumar wrote:
我不能为我的生活弄清楚如何正确解析C
声明!我想出了一个中级
I cannot for the life of me figure out how to parse C
declarations correctly! I''ve come up with an intermediate



参见K& R(Kernighan& Ritchie的C编程语言,第2版),第5.12节



See K&R (The C Programming Language by Kernighan & Ritchie,
2nd Edition), section 5.12




该程序使用字符串函数创建一条文本消息

对应一个简单的声明。我想创建一个对应于声明的

树数据结构,最好是

,而不必每次都要遍历树时

它的东西(即只在根部添加新元素)。


例如,目前我可以解析

int * i [5];


as


(读取int)''signed int''

(读*)++ ptr_count

(读i)''我是 - >签署int''

--ptr_count''我是 - >指向 - >签名int''

(读[5])''我是 - >数组[5] - >指向 - >签名int''


然而,这种方法在阅读时会崩溃


int j [4] [2];


as


(读取int)''signed int''

(读j)''j是a - >签名int''

(读[4])''j是a - >数组[4] - > signed int''

(读[2])''j是a - >数组[2] - >数组[4] - >签名int''


你看,目前数组出来了倒退。

我*可以*实现一个堆栈,然后做疯了操纵

迫使一切顺利出来,但肯定会有更好的方式吗?


我会的喜欢保持代码尽可能干净。


-Arthur



That program uses string functions to create a text message
corresponding to a simple declaration. I want to create a
tree data structure corresponding to a declaration, preferably
without having to traverse the tree each time I want to add
something to it (i.e., add new elements only at the root).

For example, currently I can parse
int *i[5];

as

(read int) ''signed int''
(read *) ++ptr_count
(read i) ''i is a -> signed int''
--ptr_count ''i is a -> pointer to -> signed int''
(read [5]) ''i is a -> array[5] of -> pointer to -> signed int''

However, this method breaks down when reading

int j[4][2];

as

(read int) ''signed int''
(read j) ''j is a -> signed int''
(read [4]) ''j is a -> array[4] of -> signed int''
(read [2]) ''j is a -> array[2] of -> array[4] of -> signed int''

You see, at the moment arrays come out "backwards."
I *could* implement a stack, and do crazy manipulations
to force everything to come out right, but surely there''s
a better way?

I''d like to keep the code as clean as possible.

-Arthur


2003年10月1日星期三18 :23:44 -0400(美国东部时间),

Arthur J. O''Dwyer< aj*@nospam.andrew.cmu.edu>在Msg写了

。 < Pi *********************************** @ unix46.andr ew.cmu.edu>
On Wed, 1 Oct 2003 18:23:44 -0400 (EDT),
Arthur J. O''Dwyer <aj*@nospam.andrew.cmu.edu> wrote
in Msg. <Pi***********************************@unix46.andr ew.cmu.edu>
好吧,我正在尝试编写几周前请求的程序,这个程序可以采用结构定义
并创建便携式函数来读取并写下那些结构。因此,主题行中的savetruct。
Well, I''m trying to write that program that was requested
a few weeks back, the one that could take struct definitions
and create portable functions to read and write those
structs. Hence the ''savestruct'' in the subject line.




您是否看过lex和yacc(在GNU中称为flex和bison

世界)?学习曲线非常陡峭,但最后写这样的

a解析器变成了小菜一碟 - 当喂了几个小描述

文件时(一个用于解析器)这些工具

基本上吐出完成的C代码。


请问完成的程序应该对什么有益?


- 丹尼尔


-

和我一起没有错!和你在一起? (来自r.a.m.p)



Have you taken a look at lex and yacc (called flex and bison in the GNU
world)? The learning curve is extremely steep, but at the end writing such
a parser becomes a piece of cake -- when fed a couple of small description
files (one for the parser, and one for the grammar) these tools
essentially spit out the finished C code.

May I ask what the finished program is supposed to be good for?

--Daniel

--
"With me is nothing wrong! And with you?" (from r.a.m.p)


这篇关于解析C声明[savestruct]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆