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

Jay T dr.jaymahdi at gmail.com
Sun Sep 5 13:37:23 PDT 2010


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;}
    ;


More information about the antlr-interest mailing list