jeudi 25 avril 2019

How do I get the original text that an antlr4 C++ rule matched?

Am trying to port the antlr java project to C++. In java I was able to get the original text by getting help from How do I get the original text that an antlr4 rule matched? It was awesome!!

My attempt in C++,

    CharStream *input = ctx->start->getInputStream();
    int a = ctx->start->getStartIndex();
    int b = ctx->start->getStopIndex();
    IntervalSet interval = IntervalSet(a,b);
    string text = input->getText(interval.getIntervals()[2]);

This "getIntervals()" is helping me just like it worked in Java.

Not working properly in C++:

    CharStream *input = ctx->start->getInputStream();
    int a = ctx->start->getStartIndex();
    int b = ctx->start->getStopIndex();
    IntervalSet interval = IntervalSet(a,b);
    string text = input->getText(interval.getIntervals()[2]);

When tried like this, Iam not getting spaces,

string text = ctx->getText();

getting text without spaces.

intmain(){cout<<"Hello, World!";strncpy(pStr,pStart,len);for(i=0;i<10;i++){j=i*i;i=j/5;}return0;}

Aucun commentaire:

Enregistrer un commentaire