开发者

variable not passed to predicate method in ANTLR

The java code generated from ANTLR is one rule, one method in most times. But for the following rule:

switchBlockLabels[ITdcsEntity _entity,TdcsMethod _method,List<IStmt> _preStmts]
    :   ^(SWITCH_BLOCK_LABEL_LIST switchCaseLabel[_entity, _method, _preStmts]* switchDefaultLabel? switchCaseLabel*)
    ;

it generates a submethod named synpred125_TreeParserStage3_fragment(), in which mehod switchCaseLabel(_entity, _method, _preStmts) is called:

synpred125_TreeParserStage3_fragment(){
    ......
    switchCaseLabel(_entity, _meth开发者_运维百科od, _preStmts);//variable not found error        
    ......
}

switchBlockLabels(ITdcsEntity _entity,TdcsMethod _method,List<IStmt> _preStmts){
    ......
    synpred125_TreeParserStage3_fragment();
    ......
}

The problem is switchCaseLabel has parameters and the parameters come from the parameters of switchBlockLabels() method, so "variable not found error" occurs.

How can I solve this problem?


My guess is that you've enabled global backtracking in your grammar like this:

options {
  backtrack=true;
}

in which case you can't pass parameters to ambiguous rules. In order to communicate between ambiguous rules when you have enabled global backtracking, you must use rule scopes. The "predicate-methods" do have access to rule scopes variables.


A demo

Let's say we have this ambiguous grammar:

grammar Scope;

options { 
  backtrack=true;
}

parse
  :  atom+ EOF
  ;

atom
  :  numberOrName+
  ;

numberOrName
  :  Number
  |  Name
  ;

Number : '0'..'9'+;
Name   : ('a'..'z' | 'A'..'Z')+;
Space  : ' ' {skip();};

(for the record, the atom+ and numberOrName+ make it ambiguous)

If you now want to pass information between the parse and numberOrName rule, say an integer n, something like this will fail (which is the way you tried it):

grammar Scope;

options { 
  backtrack=true;
}

parse
@init{int n = 0;}
  :  (atom[++n])+ EOF
  ;

atom[int n]
  :  (numberOrName[n])+
  ;

numberOrName[int n]
  :  Number {System.out.println(n + " = " + $Number.text);}
  |  Name   {System.out.println(n + " = " + $Name.text);}
  ;

Number : '0'..'9'+;
Name   : ('a'..'z' | 'A'..'Z')+;
Space  : ' ' {skip();};

In order to do this using rule scopes, you could do it like this:

grammar Scope;

options { 
  backtrack=true;
}

parse
scope{int n;         /* define the scoped variable          */ }
@init{$parse::n = 0; /* important: initialize the variable! */ }
  :  atom+ EOF
  ;

atom
  :  numberOrName+
  ;

numberOrName /* increment and print the scoped variable from the parse rule */
  :  Number {System.out.println(++$parse::n + " = " + $Number.text);} 
  |  Name   {System.out.println(++$parse::n + " = " + $Name.text);}
  ;

Number : '0'..'9'+;
Name   : ('a'..'z' | 'A'..'Z')+;
Space  : ' ' {skip();};

Test

If you now run the following class:

import org.antlr.runtime.*;

public class Main {
  public static void main(String[] args) throws Exception {
    String src = "foo 42 Bar 666";
    ScopeLexer lexer = new ScopeLexer(new ANTLRStringStream(src));
    ScopeParser parser = new ScopeParser(new CommonTokenStream(lexer));
    parser.parse();
  }
}

you will see the following being printed to the console:

1 = foo
2 = 42
3 = Bar
4 = 666

P.S.

I don't know what language you're parsing, but enabling global backtracking is usually overkill and can have quite an impact on the performance of your parser. Computer languages often are ambiguous in just a few cases. Instead of enabling global backtracking, you really should look into adding syntactic predicates, or enabling backtracking on those rules that are ambiguous. See The Definitive ANTLR Reference for more info.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜