Discussion:
ANTLR4 (beta 3 bug report) . . 1) problem with imported @actions . . 2) @after action and import . . 3) strange error message
Bernard Kaiflin
2012-11-20 22:20:48 UTC
Permalink
[ Bugs button not working yet :) In the meantime, I post a bug report here.
]

To parse the Ruby parser, consisting of a Bison grammar and C code, I have
written a C grammar which parses only C code, and a Bison grammar which
parses only the Bison tags and meta-language, and rely upon the C grammar
to parse C embedded code.

So I need to import the C grammar into the Bison grammar.

*Problem 1)* The Bison grammar contains neither @header, nor @members, nor
@after actions, but ANTLR complains :

$ alias
alias antlr4='java -jar /usr/local/lib/antlr-4.0b3-complete.jar'
$ java -version
java version "1.6.0_37"
Java(TM) SE Runtime Environment (build 1.6.0_37-b06-434-11M3909)
Java HotSpot(TM) 64-Bit Server VM (build 20.12-b01-434, mixed mode)
$ antlr4 C.g4 [used previously]
$ antlr4 Bison.g4
error(94): /.../C.g4:23:9: redefinition of members action
error(94): /.../C.g4:349:1: redefinition of after action
error(94): /.../C.g4:361:1: redefinition of after action
error(94): /.../C.g4:579:1: redefinition of after action
[... *100 % CPU, heating* ...]
*^C*$

Have also tried this :

grammar B;
import BisonOnly; [without import C;]
import C;

$ antlr4 B.g4
[ ... Java task 100 % ... ]
^C$

I have simplified both grammars to a minimum. Bison_test.g4 compiles only
after removing the last @after action remaining in the imported grammar
C_test.g4.

grammar Bison_test;
import C_test;

grammar C_test;
program : declaration ;
declaration
//@after {System.out.println("...... exiting declaration " +
$declaration.text);}
: 'auto' | 'register' | 'static' | 'extern'
;
ID : [a-zA-Z] ;


The message

$ antlr4 Bison_test.g4
error(94): /.../C_test.g4:9:9: redefinition of members action

appears again only if adding both @lexer and @parser actions in C_test.g4 :

grammar C_test;
@lexer::members {
}

@parser::members {
}



*Problem (or just strange behavior ) 2)* Before simplifying C and Bison, I
have tried to reproduce the problem with code/reference/ELang.g4 +
MyELang.g4 :

grammar ELang;

stat : (expr ';')+ ;
expr
@after {System.out.println("expr found " );} // + $expr.text);}
: ID ;
WS : [ \r\t\n]+ -> skip ;
ID : [a-z]+ ;

The grammar MyELang.g4 is not changed :

grammar MyELang;
import ELang;
expr : INT | ID ;
INT : [0-9]+ ;

$ antlr4 MyELang.g4
$ javac MyELang*.java
$ grun MyELang stat
34;
➾EOF [ctrl-D]
$

There is no output from expr. OK, that's because exp in MyELang overwrites
expr in ELang. But adding a print statement in MyELang makes them both
active.

grammar MyELang;
import ELang;
expr
@after {System.out.println("expr found " + $expr.text);}
: INT | ID ;

$ grun MyELang stat
34;
➾EOF [ctrl-D]
expr found 34
expr found




*Problem 3)* With no @after action in MyELang, one with the full print
statement in ELang.g4 using $expr.text in the *parser* rule expr

grammar MyELang;
import ELang;
expr
//@after {System.out.println("expr found " + $expr.text);}
: INT | ID ;
INT : [0-9]+ ;

grammar ELang;

stat : (expr ';')+ ;
expr
@after {System.out.println("expr found " + $expr.text);}
: ID ;
WS : [ \r\t\n]+ -> skip ;
ID : [a-z]+ ;

there is a strange error message (because expr is a parser, not lexer rule)
:

$ antlr4 MyELang.g4
error(128): MyELang.g4:5:44: attribute references not allowed in
*lexer*actions: $expr.text
Exception in thread "main" java.lang.NullPointerException
at
org.antlr.v4.codegen.ActionTranslator.qualifiedAttr(ActionTranslator.java:210)
....

Kind regards
Bernard

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: http://www.antlr.org/mailman/options/antlr-i
Terence Parr
2012-11-22 23:56:22 UTC
Permalink
THANKS! Fixed all 3 issues you found. please grab from github and rebuild.
Ter
Post by Bernard Kaiflin
[ Bugs button not working yet :) In the meantime, I post a bug report here.
]
To parse the Ruby parser, consisting of a Bison grammar and C code, I have
written a C grammar which parses only C code, and a Bison grammar which
parses only the Bison tags and meta-language, and rely upon the C grammar
to parse C embedded code.
So I need to import the C grammar into the Bison grammar.
$ alias
alias antlr4='java -jar /usr/local/lib/antlr-4.0b3-complete.jar'
$ java -version
java version "1.6.0_37"
Java(TM) SE Runtime Environment (build 1.6.0_37-b06-434-11M3909)
Java HotSpot(TM) 64-Bit Server VM (build 20.12-b01-434, mixed mode)
$ antlr4 C.g4 [used previously]
$ antlr4 Bison.g4
error(94): /.../C.g4:23:9: redefinition of members action
error(94): /.../C.g4:349:1: redefinition of after action
error(94): /.../C.g4:361:1: redefinition of after action
error(94): /.../C.g4:579:1: redefinition of after action
[... *100 % CPU, heating* ...]
*^C*$
grammar B;
import BisonOnly; [without import C;]
import C;
$ antlr4 B.g4
[ ... Java task 100 % ... ]
^C$
I have simplified both grammars to a minimum. Bison_test.g4 compiles only
C_test.g4.
grammar Bison_test;
import C_test;
grammar C_test;
program : declaration ;
declaration
$declaration.text);}
: 'auto' | 'register' | 'static' | 'extern'
;
ID : [a-zA-Z] ;
The message
$ antlr4 Bison_test.g4
error(94): /.../C_test.g4:9:9: redefinition of members action
grammar C_test;
@lexer::members {
}
@parser::members {
}
*Problem (or just strange behavior ) 2)* Before simplifying C and Bison, I
have tried to reproduce the problem with code/reference/ELang.g4 +
grammar ELang;
stat : (expr ';')+ ;
expr
@after {System.out.println("expr found " );} // + $expr.text);}
: ID ;
WS : [ \r\t\n]+ -> skip ;
ID : [a-z]+ ;
grammar MyELang;
import ELang;
expr : INT | ID ;
INT : [0-9]+ ;
$ antlr4 MyELang.g4
$ javac MyELang*.java
$ grun MyELang stat
34;
➾EOF [ctrl-D]
$
There is no output from expr. OK, that's because exp in MyELang overwrites
expr in ELang. But adding a print statement in MyELang makes them both
active.
grammar MyELang;
import ELang;
expr
@after {System.out.println("expr found " + $expr.text);}
: INT | ID ;
$ grun MyELang stat
34;
➾EOF [ctrl-D]
expr found 34
expr found
statement in ELang.g4 using $expr.text in the *parser* rule expr
grammar MyELang;
import ELang;
expr
: INT | ID ;
INT : [0-9]+ ;
grammar ELang;
stat : (expr ';')+ ;
expr
@after {System.out.println("expr found " + $expr.text);}
: ID ;
WS : [ \r\t\n]+ -> skip ;
ID : [a-z]+ ;
there is a strange error message (because expr is a parser, not lexer rule)
$ antlr4 MyELang.g4
error(128): MyELang.g4:5:44: attribute references not allowed in
*lexer*actions: $expr.text
Exception in thread "main" java.lang.NullPointerException
at
org.antlr.v4.codegen.ActionTranslator.qualifiedAttr(ActionTranslator.java:210)
....
Kind regards
Bernard
List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: http://www.antlr.org/mailman/options/antl

Loading...