开发者

How to change token type in parser rule in ANTLR using CSharp3 language

I am using ANTLR 3.3 with langage CSharp3.

In my token types, I have a tokens called WORD, UTTAR, PRADESH, UPABBR among others. I also have a rule as follows

specialSynonym5     : (UTTAR PRADESH)=>a=UTTAR b=PRADESH -> ^(SpecialSynonymNode ^(SynonymNode $a $b) ^(SynonymNode UPABBR))
      | (UPABBR) =>开发者_StackOverflow中文版; a=UPABBR -> ^(SpecialSynonymNode^(SynonymNode $a) ^(SynonymNode UTTAR PRADESH))
      | UTTAR;
      | PRADESH;

What I would like to do is to change the token types to WORD so that after this rules is processed, the token types are WORD. I have tried a few options but none has succeeded.

Thanks.


I found out that the solution is to do the following :

specialSynonym5     : (UTTAR PRADESH)=>a=UTTAR b=PRADESH { a.Type = WORD; } -> ^(SpecialSynonymNode ^(SynonymNode $a $b) ^(SynonymNode UPABBR))      
| (UPABBR) => a=UPABBR { a.Type = WORD; } -> ^(SpecialSynonymNode^(SynonymNode $a) ^(SynonymNode UTTAR PRADESH))      
| a=UTTAR { a.Type = WORD; };      
| a=PRADESH { a.Type = WORD; };

Adding this here just in case someone else faces the same question.

However changing the type for the token that is not part of the check still eludes me. Will update this answer once I know how to do this.

Got that too. Create the token using {(object)adaptor.Create(WORD, "")}. As such, the rule becomes

specialSynonym5     : (UTTAR PRADESH)=>a=UTTAR b=PRADESH { a.Type = WORD; } -> ^(SpecialSynonymNode ^(SynonymNode $a $b) ^(SynonymNode {(object)adaptor.Create(WORD, "u.p.")} ))      
| (UPABBR) => a=UPABBR { a.Type = WORD; } -> ^(SpecialSynonymNode^(SynonymNode $a) ^(SynonymNode {(object)adaptor.Create(WORD, "uttar")} {(object)adaptor.Create(WORD, "pradesh")}))      
| a=UTTAR { a.Type = WORD; };      
| a=PRADESH { a.Type = WORD; };

There may be a better solution than this, but right now this is what I have.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜