由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 怎样才能用perl等东西知道c macro中的数值
相关主题
A problem on string parsing (using either grep or perl)python gc question
java string streamPerl插入MySQL中双引号的问题
help on string parse诚心请教Perl:简单的Variable Match in Regular expression
这个有更好的算法吗?这个PERL表达式干啥的?
诡异的异常处理python能检查出space是一个还是两个吗?
如何用Python或者Perl抓取文本?问一个python的string split问题
最高大上的 atoiistream_iterator问题
大牛 in Perl and SED?how to read a sentence into a vector of string?
相关话题的讨论汇总
话题: string话题: tempstring话题: pos话题: delimiter话题: sum
进入Programming版参与讨论
1 (共1页)
a****l
发帖数: 8211
1
我现在有一堆的C程序,需要知道其中宏定义的数值.比如:
file1: #define pig 1
file2: #define sheep 2
file3: #define dog 5
file4: #define total dog+sheep-pig
需要用一个比如perl的脚本程序扫描过所有的文件,然后能输出total=6.问题是显然这
个计算的方法不是固定的,所以脚本程序基本上需要按照C的规定展开宏定义,找到各词
语对应的数值,再计算出最终的数字.
有谁知道perl能不能做这样的工作?如果不是perl,有什么东西能做这样的事?谢谢!
N***m
发帖数: 4460
2
how about in java? tested a few cases and it seems to work.
Map map = new HashMap();
map.put("dog",1);
map.put("sheep",5);
map.put("cat",3);

String s = "dog-cat+dog-sheep";
List list = new LinkedList();
StringTokenizer st = new StringTokenizer(s, "+-");
while(st.hasMoreTokens()) {
String tempString = st.nextToken();
//System.out.println();
list.add(tempString);
}
Iterator it = list.iterator();
List newList = new LinkedList();
int pos = -1;
while(it.hasNext()) {
String tempString = it.next();
pos = s.indexOf(tempString,pos);
newList.add(tempString);
int delimiter = 0;
if(pos+tempString.length() delimiter = pos+tempString.length();
newList.add(s.substring(delimiter,delimiter+
1));
}
}

System.out.print("The expression after analysis is:");
it = newList.iterator();
while(it.hasNext()) {
System.out.print(it.next());
}
System.out.println();

it = newList.iterator();
int value = 0;
int sum = 0;

if(it.hasNext()) {
String ps = it.next();
sum = map.get(ps);
}
while(it.hasNext()) {
String d = it.next();
String ps = it.next();
value = map.get(ps);
if(d.equals("+"))
sum += value;
else
sum -= value;

}

System.out.println(sum);

【在 a****l 的大作中提到】
: 我现在有一堆的C程序,需要知道其中宏定义的数值.比如:
: file1: #define pig 1
: file2: #define sheep 2
: file3: #define dog 5
: file4: #define total dog+sheep-pig
: 需要用一个比如perl的脚本程序扫描过所有的文件,然后能输出total=6.问题是显然这
: 个计算的方法不是固定的,所以脚本程序基本上需要按照C的规定展开宏定义,找到各词
: 语对应的数值,再计算出最终的数字.
: 有谁知道perl能不能做这样的工作?如果不是perl,有什么东西能做这样的事?谢谢!

N***m
发帖数: 4460
3
forget to trim the string if it has blank spaces between +/- and variables.
i.e. change to "map.get(ps.trim())".

【在 N***m 的大作中提到】
: how about in java? tested a few cases and it seems to work.
: Map map = new HashMap();
: map.put("dog",1);
: map.put("sheep",5);
: map.put("cat",3);
:
: String s = "dog-cat+dog-sheep";
: List list = new LinkedList();
: StringTokenizer st = new StringTokenizer(s, "+-");
: while(st.hasMoreTokens()) {

t****t
发帖数: 6806
4
first of all, the value of macro is dependent on the place that macro is
expanded. for example:
#define A A1
#define A1 1
/* here A expands to 1 */
#undef A1
#define A1 2
/* here A expands to 2 */
therefore your requirement is not very strict.
second, assume that you want only the final value of every macro. you may
use gcc to do that:
gcc -dM -E xxxxxxxx.c > xxxxxx_processed.c
/* it outputs all the (final) macro definitions */
then you use a perl to process it: for every macro name in $_, (each line is
a macro definition), append a line to *_processed.c like
printf(#$_"=%d\n", $_);
add some necessary header (int main(), etc), and compile the program and run.
CPP is better processed with CPP. using perl for it is a little bit painful.

【在 a****l 的大作中提到】
: 我现在有一堆的C程序,需要知道其中宏定义的数值.比如:
: file1: #define pig 1
: file2: #define sheep 2
: file3: #define dog 5
: file4: #define total dog+sheep-pig
: 需要用一个比如perl的脚本程序扫描过所有的文件,然后能输出total=6.问题是显然这
: 个计算的方法不是固定的,所以脚本程序基本上需要按照C的规定展开宏定义,找到各词
: 语对应的数值,再计算出最终的数字.
: 有谁知道perl能不能做这样的工作?如果不是perl,有什么东西能做这样的事?谢谢!

t****t
发帖数: 6806
5
you can only process + and -?
just don't use java to process text. it's not for that.

【在 N***m 的大作中提到】
: how about in java? tested a few cases and it seems to work.
: Map map = new HashMap();
: map.put("dog",1);
: map.put("sheep",5);
: map.put("cat",3);
:
: String s = "dog-cat+dog-sheep";
: List list = new LinkedList();
: StringTokenizer st = new StringTokenizer(s, "+-");
: while(st.hasMoreTokens()) {

N***m
发帖数: 4460
6
agree:) If there are more operators, I have to do more heavy work on that
part.
Sometimes it is a bad practice of trying to put everything into java. hehe.

【在 t****t 的大作中提到】
: you can only process + and -?
: just don't use java to process text. it's not for that.

t****t
发帖数: 6806
7
as you said, every language has its purpose. there is no point arguing which
language is better without context.
this become clearer and clearer when you learned more than 3-4 languages.

【在 N***m 的大作中提到】
: agree:) If there are more operators, I have to do more heavy work on that
: part.
: Sometimes it is a bad practice of trying to put everything into java. hehe.

N***m
发帖数: 4460
8
嗯哪。主要是今天股票大跌,心情不佳,所以多灌了两下水。
权当我没说:)

which

【在 t****t 的大作中提到】
: as you said, every language has its purpose. there is no point arguing which
: language is better without context.
: this become clearer and clearer when you learned more than 3-4 languages.

a****l
发帖数: 8211
9
你说的完全正确,我就是顾忌到这种和其他的类似的复杂情况.我去试一下.

【在 t****t 的大作中提到】
: first of all, the value of macro is dependent on the place that macro is
: expanded. for example:
: #define A A1
: #define A1 1
: /* here A expands to 1 */
: #undef A1
: #define A1 2
: /* here A expands to 2 */
: therefore your requirement is not very strict.
: second, assume that you want only the final value of every macro. you may

b********e
发帖数: 58
10
To solve your problem, there are two ways:
1) Use some C/C++ preprocessor directly, such as gcc/g++. If I remember
correctly -E option will output all the macros. Mcrosoft CL tool has similar
options.
2) Write your own preprocessor, which perhaps is not that hard either since
you only need to deal with the macro definition and expansion. There are lex
/yacc files you can obtained by google, perhaps even source code directly
does this.
If you are using Ruby, rex/racc gems are your friend.
If you are using Python, Ply (Python Lex/Yacc) is the choice.
Sorry not an expert of perl any more. Stopped using it years ago.
1 (共1页)
进入Programming版参与讨论
相关主题
how to read a sentence into a vector of string?诡异的异常处理
(char **)返回值怎么用SWIG包成Python list (of strings如何用Python或者Perl抓取文本?
问一段C++ iostringstream的代码最高大上的 atoi
有人用SWIG吗?如何返回c-string ?大牛 in Perl and SED?
A problem on string parsing (using either grep or perl)python gc question
java string streamPerl插入MySQL中双引号的问题
help on string parse诚心请教Perl:简单的Variable Match in Regular expression
这个有更好的算法吗?这个PERL表达式干啥的?
相关话题的讨论汇总
话题: string话题: tempstring话题: pos话题: delimiter话题: sum