我正在使用 antlr 4 来编写我的语法。我想看看我的语法生成的gui三。当我尝试在 antlr 站点(http://www.antlr.org/wiki/display/ANTLR4/Getting+Started+with+ANTLR+v4)上运行该示例时,它可以工作,但如果我尝试使用我的语法它不会不行。特别是,我还尝试使用“在 TestRig 中运行..”命令在 NetBeans 和 AntlrWorks 2 上运行语法,但结果是相同的错误消息““无法将故障加载为词法分析器或解析器”。
这是我的语法:
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
;
这是我的文件 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>