views:

116

answers:

1

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?

A: 

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.

ahe
Thanks for the quick reply $INT.text->chars returns a char pointer and solves the problem.
Bernhard Schenkenfelder