1

I have a rule which expects a STRING from the user. Currently when the user gives a keyword that I have defined in my grammar, parser gives a segmentation fault.

For ex :

sampleClause: calc! strName {##->setType(SAMPLE_CLAUSE);};
strName : STRING;
calc: "CALC" | "calc";

If the user enters calc/CALC as strName, parser throws Seg Fault. I am not sure how to avoid this.

EDIT: I also want to know how to avoid the parser giving a segmentation fault. This crashes my application which I do not want. I want to exit gracefully in these situations instead of seg fault.

P.S: I need the solution in ANTLR 2, as there is a dependency.

user2761431
  • 925
  • 2
  • 11
  • 26

1 Answers1

0

The usual solution here is to modify strName to askew all the keywords

strName : STRING | calc ;

Assuming you have a bunch of keywords, you want to define each of them as a lexer token and create a keywords rule.

calc: CALC;
CALC: "Calc" | "calc";
FOO: "foo";
BAR: "bar";

keywords: CALC| FOO | BAR;

strName: STRING | keywords;

You probably also want to change to token type to string when keywords matches. I forget how to do that in ANTLR 2, but it is fairly straightforward.

Troy Daniels
  • 3,270
  • 2
  • 25
  • 57