0

I'm new to ANTLR and having trouble with printing tokens using actions. I have the following grammar file:

grammar SimpleGrammar;

options {
language = C;
}

prog : string { printf("hello\n"); printf($string.text); };

string : LETTER+;

LETTER : ('a'..'b' | 'A'..'B')+;

The test program is as follows:

#include "SimpleGrammarLexer.h"
#include "SimpleGrammarParser.h"

int main(int argc, char * argv[]) {
    pANTLR3_INPUT_STREAM input;
    pSimpleGrammarLexer lex;
    pANTLR3_COMMON_TOKEN_STREAM tokens;
    pSimpleGrammarParser parser;

    input = antlr3FileStreamNew((pANTLR3_UINT8)argv[1], 0);
    lex = SimpleGrammarLexerNew(input);
    tokens = antlr3CommonTokenStreamSourceNew(ANTLR3_SIZE_HINT, TOKENSOURCE(lex));
    parser = SimpleGrammarParserNew(tokens);

    parser -> prog(parser);

    parser -> free(parser);
    tokens -> free(tokens);
    lex -> free(lex);
    input -> close(input);

    return 0;
}

The input file contains a simple string: "AAAA". I'm expecting the output to be "hello" followed by "AAAA" on a newline, however I'm only getting "hello" followed by an empty string.

What is it that I'm doing wrong?

Thanks in advance.

P.S. I'm using ANTLRv3.

Khutsi
  • 73
  • 1
  • 1
  • 6

1 Answers1

1

Try this instead:

prog
 : string 
   { 
     printf("hello\n"); 
     printf($string.text->chars); 
   }
 ;
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288