[antlr-interest] Bug Report: Composite Grammar does not delegate Lexer methods (like mLETTER)

George S. Cowan cowang at comcast.net
Fri Jan 2 09:41:23 PST 2009


On Tue Dec 30, 2008, at 10:56 PST, Terrence Parr wrote
> Hi. you'll have to call the lexer methods directly on the generated  
> lexer not using delegates I think
 
As it turns out, I can insert the delegates myself as a workaround.
 
--------- begin C.g ---------
grammar C ;
import L, P2 ;

@lexer::members {
  public final void mLETTER() throws RecognitionException { gL.mLETTER(); }
}

stuff : ( letters spaces )+ ;
--------- end C.g ------------
 
This is a pain for a large grammar, but it will only have to be done once
(assuming all my new development is happening at the top level, and there
are no lexicon updates coming in at the bottom levels). I can find the name
of the delegates, e.g., "gL", in my CLexer.java file. I can find the method
signature lines, e.g. "public final void mLETTER() throws
RecognitionException", in my C_L.java file. Of course, if I override a lexer
rule, I have to remove the delegator, but if I forget, I get a reminder from
the Java compiler:
------------- begin Java compile error message ----------
CLexer.java:52: mLETTER() is already defined in CLexer
    public final void mLETTER() throws RecognitionException {
                      ^
CLexer.java:14: cannot find symbol
symbol  : method mLETTER()
location: class C_L
      public final void mLETTER() throws RecognitionException {
gL.mLETTER(); }
------------ end Java compile error message ----------
 
This should work. But I hope the next release automates the delegation for
the lexer.
 
George
 
--------------------------- begin original message
----------------------------------------
Hi. you'll have to call the lexer methods directly on the generated  
lexer not using delegates I think
Ter
On Dec 30, 2008, at 7:16 AM, George S. Cowan wrote:

> The generated lexer for composite grammars does not generate the  
> delegation methods for lexer rules in the topmost grammar. This is  
> only a problem if one is directly calling the lexer methods, for  
> instance, in unit testing and, in particular, testing lexer rules in  
> gUnit.
>
> Here is the example grammar from the Composite Grammar page
(http://www.antlr.org/wiki/display/ANTLR3/Composite+Grammars 
> ) with slight modifications so it will work:
>
> -------------------- begin L.g --------------
> lexer grammar L ;
> LETTER : 'a'..'z' ;
> ---- begin P1.g  ---
> parser grammar P1 ;
> letter : LETTER ;
> spaces : ' '+ ;
> --- begin P2.g ---
> parser grammar P2 ;
> import P1 ;
> letters : letter+ ;
> --------- begin C.g ---------
> grammar C ;
> import L, P2 ;
> stuff : ( letters spaces )+ ;
> --------- end grammar files ------------
>
> Here is the gUnit test file and the exception:
> ------ CTest.testsuite -----
> gunit C;
> LETTER
> : "a"   OK
>   "A"   FAIL
> ------------------------ runtime exception  
> -------------------------------
> java.lang.NoSuchMethodException: CLexer.mLETTER()
>         at java.lang.Class.getMethod(Unknown Source)
>         at org.antlr.gunit.gUnitExecuter.runLexer(gUnitExecuter.java: 
> 211)
>         at  
> org.antlr.gunit.gUnitExecuter.runCorrectParser(gUnitExecuter.java:131)
>         at  
> org.antlr.gunit.gUnitExecuter.executeTests(gUnitExecuter.java:149)
>         at org.antlr.gunit.gUnitExecuter.execTest(gUnitExecuter.java: 
> 97)
>         at org.antlr.gunit.Interp.main(Interp.java:62)
> ------------------------ END  
> --------------------------------------------------
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090102/74e5634a/attachment.html 


More information about the antlr-interest mailing list