[antlr-interest] (Antlr C Target) Need help converting uint8_t to string

Jim Idle jimi at temporal-wave.com
Sun Sep 5 14:24:57 PDT 2010


It is just suing that type because I cannot know what the real type is, so
just cast it. However, as per the many times I have answered this in
antlr.markmail.org, the $text stuff is just a convenience, which should not
be used if you are using a parser that will parse large amounts of input.
This is because it autocreates pANTLR_STRING structures, which are not
released until the parser is freed. 

So, just use the token itself to create your C++ strings and not $text. In
the token you will find a pointer to the first byte that starts the string
and the end of the string. You must know what the encoding is, so use these
to find the length or otherwise construct the string directly. The pointer
types are u_int8 *but perhaps I should have made them void (and might do so
yet as people are already casting them anyway). 

Jim


> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Jay T
> Sent: Sunday, September 05, 2010 1:37 PM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] (Antlr C Target) Need help converting uint8_t to
> string
> 
> Hi everyone:
> 
> I am very new to Antlr so please be patient with me.
> I am writing a grammar that will be generated for a C target, compiled
using
> C++.
> 
> The problem I have is I am trying to convert a token text into a string
text
> which will be used for my internal data structures written in C++.
> One error that I keep getting is " ToyParser.c:415: error: invalid
conversion
> from 'uint8_t*' to 'char' ".
> I have searched exhaustively on Google trying to figure out how to convert
a
> uint8_t to a string and unfortunately could not find any resolution -- so
> hopefully an Antlr C programmer guru can give me some advice on how to a
> solution to this problem.
> 
> Below is a "Toy" Grammar file. I am simply trying to convert any received
> token text into a string, and use this as a basis to convert certain
tokens to
> int's for my own custom data structure.
> 
> If someone could please kindly respond, it would be greatly appreciated.
> 
> Thanks
> 
> 
> 
> 
> 
> 
> 
> grammar Toy;
> 
> options {
>   language = C;
> }
> 
> @Scope
> {
>     string typeName;
> }
> 
> @header
> {
>     #include <iostream>
>     #include <string>
>     #define ANTLR3_INLINE_INPUT_ASCII
> 
>     using namespace std;
> }
> 
> 
> @members
> {
> 
> #include <iostream>
> #include <string>
> #include "ToyLexer.h"
> 
> using namespace std;
> 
> int main(int argc, char * argv[])
> {
> 
>   pANTLR3_INPUT_STREAM           input;               //Create the input
> stream
>   pToyLexer               lex;
> //Lexer supplied by pANTLR3_INPUT_STREAM (Translate to tokens)
>   pANTLR3_COMMON_TOKEN_STREAM    tokens; //Token stream supplied
> by parser
> (Store tokens)
>   pToyParser              parser;
//Parser
> gets the tokens and sementally understands the tokens in order
> 
> 
> 
>   input  = antlr3AsciiFileStreamNew          ((pANTLR3_UINT8)argv[1]);
>             //Provide file input
>   lex    = ToyLexerNew                (input);
>                               //Create instance of lexer and set it to the
file name input
>   tokens = antlr3CommonTokenStreamSourceNew  (ANTLR3_SIZE_HINT,
> TOKENSOURCE(lex));    //Create token stream from the lexer
>   parser = ToyParserNew               (tokens);
>                                                       //Feed in tokens
into the parser
> 
>   string Name;
> 
>   parser  ->declarator(parser, Name);
>                         //Call the parser rule
> 
>   parser ->free(parser);
>   tokens ->free(tokens);
>   lex    ->free(lex);
>   input  ->close(input);
> 
>   cout << "Received name: " << Name << endl;
> 
>   return 0;
>  }
> 
> }
> 
> 
> 
> 
> //----- PARSER RULES -----//
> 
> declarator [string &typeName]
>     :    ID     {
>                 typeName.append(1,$ID.text->chars);
>             }
>     ;
> 
> //----- LEXER RULES -----//
> 
> ID
>     :    ('A'..'Z' | 'a'..'z')+
>     ;
> 
> DIGIT
>     :    ('0'..'9')+
>     ;
> 
> WS
>     :    (' ' | '\t' | '\r' | '\f' | '\n')+ {$channel=HIDDEN;}
>     ;
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address



More information about the antlr-interest mailing list