I'm using antlr 4 to write my grammar. I would like to see the gui three generated by my grammar. When I try to run the example on the antlr site (http://www.antlr.org/wiki/display/ANTLR4/Getting+Started+with+ANTLR+v4) it works, but if I try with my grammar it doesn't work. In particular I've tried also to run the grammar on NetBeans and AntlrWorks 2 with the command "Run in TestRig.." , but the result is the same error message ""Can't load Troubles as lexer or parser"".
This is my grammar:
script
: include*
systemDef
labelsDef
issueDef*
observationDef*
actionDef*
procedure*
;
include
: USE qualifiedName EOS;
systemDef
: SYSTEM definition;
labelsDef
: LABELS COLON '{' label* '}' EOS ;
label
: IDENTIFIER COLON literal EOS
;
issueDef
: ISSUES definition;
observationDef
: OBSERVATIONS definition;
actionDef
: ACTIONS definition;
definition
: IDENTIFIER? parameters? ( COLON block )? EOS
;
reference
: IDENTIFIER? parameters? (COLON)? (atClause ( COMMA atClause )*)? EOS
;
parameters
: '[' param (COMMA param)* ']'
;
param
: literal
| IDENTIFIER
;
literal
: STRINGLITERAL
| INTEGET_LITERAL
| PERCENTAGELITERAL
| FLOATLITERAL
| TRUE
| FALSE
;
block
: '{' ( (marker)? (definition | reference) )+ '}'
;
marker
: PLUS
| MINUS
| MULTIPLY
| SHARP
| QUESTION_MARK
;
atClause
: '@' qualifiedName parameters?
;
qualifiedName
: (DOT)? IDENTIFIER (DOT IDENTIFIER)*
;
procedure
: PROCEDURE IDENTIFIER params? COLON body
;
params
: '[' IDENTIFIER (COMMA IDENTIFIER)* ']'
;
body
: statement
| '{' statement* '}'
;
statement
: assignment
| expression // .str???
| callExpression
| ifStatement
| whileStatement
| repeatStatement
| forStatement
;
assignment
: qualifiedName ASSIGN expression (EOS)?
;
expression
: conditionalOrExpression (EOS)?;
conditionalOrExpression
: conditionalAndExpression ( OR conditionalAndExpression )*
;
conditionalAndExpression
: equalityExpression ( AND equalityExpression )*
;
equalityExpression
: stringExpression (equalityOp stringExpression)?
;
equalityOp
: EQUAL
| NE
;
stringExpression
: relationalExpression ('§' stringExpression)?
;
relationalExpression
: additiveExpression (relationalOp additiveExpression)*
;
relationalOp
: '<='
| '>='
| '<'
| '>'
;
additiveExpression
: multiplicativeExpression (( PLUS | MINUS ) multiplicativeExpression )*
;
multiplicativeExpression
: unaryExpression (( MULTIPLY | DIVIDE ) unaryExpression )*
;
unaryExpression
: ( PLUS | MINUS | NOT )? basicExpression
;
basicExpression
: rangeExpression
| callExpression
| qualifiedName // .str >
| percentageExpression
| '(' expression ')'
| literal
;
percentageExpression
: PERCENTAGELITERAL 'of' additiveExpression
;
rangeExpression
: qualifiedName '(' ThreeDigits '..' ThreeDigits ')' (EOS)?
;
callExpression
: qualifiedName arguments (EOS)?
;
arguments
: '(' (expression (COMMA expression)*)? ')'
;
ifStatement // (ELSE body)?
: IF '(' expression ')' body
(ELSE ifStatement | body )?
;
whileStatement
: WHILE '(' expression ')' body
;
repeatStatement
: REPEAT body UNTIL '(' expression ')' EOS
;
forStatement
: FOR .qualifiedName ASSIGN value TO value STEP value body
;
value
: FLOATLITERAL
;
and this is my file build.xml:
<target name="-pre-compile" depends="antlr">
</target>
<target name="init-antlr">
<!-- Full path to Antlr jar -->
<property name="antlr-4.0-complete.jar" location="C:/Users/Mary/Documents/TesiMagistrale/lib/antlr-4.0-complete.jar"/>
<!-- Grammar path -->
<property name="antlr.grammar" location="src/troubles/lang/Troubles.g4"/>
<!--<property name="antlr.lexer" location="src/trouble/lang/TroublesL.g4"/>-->
</target>
<target name="antlr" depends="init-antlr" unless="up-to-date">
<!-- Compiling grammar -->
<java classname="org.antlr.v4.Tool" fork="true">
<arg value="${antlr.grammar}"/>
<!--<arg value="${antlr.lexer}"/>-->
<arg value="-visitor"/>
<classpath path="${antlr-4.0-complete.jar}"/>
</java>
</target>
I'm rewriting my answer so that it is more accurate.
The most likely reason you can't successfully run TestRig in NetBeans/antlrworks2 is because you set your grammar to be in a package other than the default package. If you have something like this in your grammar,
@header {
package org.whatever.troubles;
}
try commenting it out and running "Run in TestRig...". It should work this way.
Unfortunately, the "Run in TestRig..." feature does not allow you to specify a package. You'll have to either keep your grammar in the default package, or use "grun" at the command prompt.
I recommend running grun manually until this is fixed. A nice way to handle it is to create a batch file (run.bat) in your source folder (where your .g4 is). It will show up in NetBeans in the Projects pane where you can right-click on it and select "run".
And, still, your CLASSPATH must be accurate...
Example run.bat contents:
rem grun org.whatever.troubles.Troubles script -tokens -tree -gui test.txt
java -cp .;C:\workdir\proj\dist\troubles.jar;C:\antlr-4.0-complete.jar;"C:\Program Files\Java\jre7\lib" org.whatever.troubles.Troubles script -tokens -tree -gui test.txt