开发者

ANTLR - accessing token values in c/c++

开发者 https://www.devze.com 2022-12-23 19:28 出处:网络
I am trying to parse integers and to access their value in antlr 3.2. I already found out how to do this in Java:

I am trying to parse integers and to access their value in antlr 3.2.

I already found out how to do this in Java:

//token definition

INT :  '0'..'9'+;

//rule to access token value:

start : val=INT           {Integer x = Integer.valueOf( $val.text ).intValue(); }

;

..开发者_开发问答. but I couldn't find a solution for this in C/C++. Does someone know how to do this?


According to examples-v3/C/C.g from http://www.antlr.org/download/examples-v3.tar.gz, $INT.text->chars should work for C but I didn't test it.

If you traverse an AST from outside of the parser in a C program and you have a node named "node" of type ANTLR3_BASE_TREE you can access it with node->getText(node)->chars (tested since I use this myself).

Remember that the C and the C++ target are two completely different things. I only use the C target so I can't say much about the one for C++.

Nevertheless the examples are a great resource to learn about such details that unfortunately aren't documented very well.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号