【发布时间】:2017-12-02 01:30:22
【问题描述】:
我是 ANTLR 的新手。并且在使用 ANTLR 编译 2 个 JAVA 文件时遇到了同样的错误。树看起来很好,没有缺陷的叶子。我找不到有关此错误根本原因的任何线索。 我使用的 ANTLR 版本是“ANTLR 4”。 有人知道如何解决吗?提前致谢!
1) ANTLR 文件为 test.g4 如下,
grammar test;
// Syntax Specification ==> Context-free Grammar
pa1:
mainClass aClass*;
mainClass:
classDeclaration '{' mainDeclaration ('{'body'}'|'{''{'body'}''}') '}';
mainDeclaration:
'public' 'static' 'void' 'main' '(' 'String''['']' ID ')';
aClass:
classDeclaration '{' body '}';
classDeclaration:
'class' ID;
aMethod:
methodDeclaration'{'body'}';
methodDeclaration:
type ID'('parameterList')';
body:
(varDeclaration|statement|expression|aMethod)*;
varDeclaration:
type ID ';' ;
statement:
(ID|arrayElement) '=' (NUM|ID|string|aChar|('new' type)? arrayElement|'new' (type|ID) '('')'|aCall|mathExpression)';'; //(ID|arrayElement) '=' (NUM|ID|string|aChar|arrayElement|aCall|mathExpression|booleanExpression)';';
string:
'"' .*? '"';
aChar:
'\''(.?|'+'|'-')'\'';
expression:
ID';'|whileExpression|ifExpression|sysPrintExpression|returnExpression;
ifExpression:
'if''('booleanExpression')' ((varDeclaration|statement|expression)*|'{'(varDeclaration|statement|expression)*'}')
('else''if''('booleanExpression')' ((varDeclaration|statement|expression)*|'{'(varDeclaration|statement|expression)*'}'))?
('else'((varDeclaration|statement|expression)*|'{'(varDeclaration|statement|expression)*'}'))?;
whileExpression:
'while''('booleanExpression')' '{'(varDeclaration|statement|expression)*'}';
sysPrintExpression:
'System''.''out''.''println''('(NUM|arrayElement|aCall)')'';';
returnExpression:
'return'(NUM|ID)';';
compExpression:
(ID|NUM|mathExpression) COMPOPERATOR (ID|NUM|'('mathExpression')'|'('ID')');
mathExpression:
(ID|NUM) (PLUS|MINUS|MULT|DIV)(ID|NUM|('('ID'.'ID'('parameterList')'')'));
singleBooleanExpression:
'!'?('('compExpression')'|compExpression|aCall|ID);//(LOGICALOPERATOR('!'?(compExpression|aCall|ID|string|aChar)))?;
doubleBooleanExpression:
'(''!'?('('compExpression')'|compExpression|aCall|ID)')'LOGICALOPERATOR('(''!'?(compExpression|aCall|ID|string|aChar)')');
booleanExpression:
singleBooleanExpression|doubleBooleanExpression;
aCall:
(ID|'new'? ID '('')')calling|'('(ID|ID'('')')calling')'calling;
calling:
'.'(ID('('parameterList')')?);
parameterList:
(NUM|type? ID|aChar|string|mathExpression|aCall)?(','(NUM|type? ID|aChar|mathExpression))*;
arrayElement:
ID?'['(ID|NUM)']';
type:
'int''['']'|'boolean'|'int'|'char'|ID;
// Lexer Specification ==> Regular Expressions
NUM: ('0' | [1-9][0-9]*);
ID: [a-zA-Z_][0-9a-zA-Z_]*;
PLUS : '+' ;
MINUS : '-' ;
MULT : '*' ;
DIV : '/';
COMPOPERATOR: '<'|'>';
LOGICALOPERATOR: '=='|'||'|'&&';
WHITESPACE: [ \t\r\n]+ -> skip;
COMMENT: ('/*'.*?'*/'|'//'~[\r\n|\r|\n]*) -> skip;
2) JAVA file1 是 MyChar.java 如下,
class MyChar{
public static void main(String[] a){
{
System.out.println(new CharEditor().whichIsSmaller('a', 'c'));
System.out.println(new CharEditor().whichIsSmaller('a', 'A'));
System.out.println(new CharEditor().whichIsSmaller('1', 'd'));
System.out.println(new CharEditor().whichIsSmaller('-', '+'));
// System.out.println("There are total " + new MyChar().countFromCharToChar('a', 'z', true) + " characters in between a and z");
System.out.println(new CharEditor().countChars("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", 'c'));
}
}
}
class CharEditor {
char whichIsSmaller(char firstChar, char secondChar){
char returnChar;
if(secondChar < firstChar) //the comparison is based on the character's ASCII code
returnChar = firstChar;
else
returnChar = secondChar;
return returnChar;
}
int countChars(String str, char c){
int n;
int sz;
char c1;
int counter;
counter = 0;
sz = str.length();
n = 0;
while ( n < sz ) {
c1 = str.charAt(n);
if ( c1 == c) {
counter = counter + 1;
}
n = n + 1;
}
return counter;
}
}
3)JAVA file2 为 MyString.java 如下,
class MyString{
public static void main(String[] a){
{
System.out.println(new StringEditor().removeSpace("Hello World And Happy Coding"));
System.out.println(new StringEditor().containsChar("Hello World And Happy Coding", 'd'));
System.out.println(new StringEditor().containsChar("Hello World And Happy Coding", 'b'));
}
}
}
class StringEditor {
String removeSpace(String str) {
String toReturn;
int n;
int sz;
char c;
toReturn = "";
sz = str.length();
n = 0;
while ( n < sz ) {
c = str.charAt(n);
if ( c == ' ') {
} else {
toReturn = toReturn + c;
}
n = n+1;
}
return toReturn;
}
boolean containsChar(String str, char c) {
int n;
int sz;
char c1;
boolean toReturn;
toReturn = false;
sz = str.length();
n = 0;
while ( n < sz ) {
c1 = str.charAt(n);
if ( c1 == c) {
toReturn = true;
break;
}
n = n+1;
}
return toReturn;
}
}
【问题讨论】:
-
关于文件 1,语法无法解析第 26 行
int countChars(String str, char c){,因为规则type中缺少String。同时将==从LOGICALOPERATOR移动到COMPOPERATOR。 -
谢谢伯纳德!它适用于文件 1。在你的启发下,我也为文件 2 解决了它。
标签: antlr