Is there a way to determine if an optional (? Operator) tree grammar rule is implemented in an action?

path[Scope sc] returns [Path p] @init{ List<String> parts = new ArrayList<String>(); } : ^(PATH (id=IDENT{parts.add($id.text);})+ pathIndex? ) {// ACTION CODE // need to check if pathIndex has executed before running this code. if ($pathIndex.index >=0 ){ p = new Path($sc, parts, $pathIndex.index); }else if($pathIndex.pathKey != ""){ p = new Path($sc, parts, $pathIndex.pathKey); } ; 

Is there a way to determine if pathIndex was running? In my action code, I tried testing $ pathIndex == null, but ANTLR does not allow you to do this. ANTLRWorks gives a syntax error that says: "There is no access to attributes in the rules pane: pathIndex."

The reason I need to do this is because in my action code, I:

 $pathIndex.index 

which returns 0 if the variable $ pathIndex is set to zero. When you access the attribute, ANTLR generates pathIndex7!=null?pathIndex7.index:0 This causes a problem with the object because it changes the value I set for -1 as the error flag to 0.

+4
source share
2 answers

There are several options:

1

Put your code inside the optional pathIndex:

 rule : ^(PATH (id=IDENT{parts.add($id.text);})+ (pathIndex {/*pathIndex cannot be null here!*/} )? ) ; 

2

Use the boolean flag to indicate the presence (or absence) of pathIndex :

 rule @init{boolean flag = false;} : ^(PATH (id=IDENT{parts.add($id.text);})+ (pathIndex {flag = true;} )? ) { if(flag) { // ... } } ; 

EDIT

You can also make pathIndex match for nothing, so you don't need to make it optional inside path :

 path[Scope sc] returns [Path p] : ^(PATH (id=IDENT{parts.add($id.text);})+ pathIndex) { // code } ; pathIndex returns [int index, String pathKey] @init { $index = -1; $pathKey = ""; } : ( /* some rules here */ )? ; 

PS. Understand that the expression $pathIndex.pathKey != "" likely to evaluate to false . To compare the contents of strings in Java, use their equals(...) method:

 !$pathIndex.pathKey.equals("") 

or if $pathIndex.pathKey can be null , you can bypass NPE by doing:

 !"".equals($pathIndex.pathKey) 
+4
source

More information would be helpful. However, if I understand correctly, when the value for the index is missing from the input you want to check for $pathIndex.index == null . This code does this using the pathIndex rule to return Integer $ index in the path rule:

  path : ^(PATH IDENT+ pathIndex?) { if ($pathIndex.index == null) System.out.println("path index is null"); else System.out.println("path index = " + $pathIndex.index); } ; pathIndex returns [Integer index] : DIGIT { $index = Integer.parseInt($DIGIT.getText()); } ; 

For testing, I created these simple parser and lexer rules:

 path : 'path' IDENT+ pathIndex? -> ^(PATH IDENT+ pathIndex?) ; pathIndex : DIGIT ; /** lexer rules **/ DIGIT : '0'..'9' ; IDENT : LETTER+ ; fragment LETTER : ('a'..'z' | 'A'..'Z') ; 

When an index is present at the input, as in path abc 5 , the output is:

 Tree = (PATH abc 5) path index = 5 

When there is no index on the input, as in path abc , the output is:

 Tree = (PATH abc) path index is null 
0
source

All Articles