저는 ANTLR에 새로운 새소식입니다. ANTLR을 사용하여 2 개의 JAVA 파일을 컴파일 할 때도 동일한 오류가 발생했습니다. 나무는 결점없이 잘 보입니다. 이 오류의 근본 원인에 대한 단서를 찾을 수 없습니다. 사용 된 ANTLR 버전은 'ANTLR 4'입니다. 아무도 그것을 고칠 방법을 알고 있습니까? 미리 감사드립니다!ANTLR 오류 : '<EOF>'입력시 실행 가능한 대안이 없습니다. 그것을 고치는 방법?
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) 자바 파일 2 아래 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, 문법>
다운로드 - 자바 갈 준비가있다는 from the Antlr site 은 문법 'string'이'type' 규칙에서 빠져 있기 때문에 26 행 int countChars (String str, char c) {'을 파싱 할 수 없습니다. 또한'=='을'LOGICAL OPERATOR'에서'COMPOPERATOR'로 이동하십시오. – BernardK
감사합니다. Bernard! 그것은 파일 1에 대해 작동합니다. 그리고 당신에 의해 계몽 된, 나는 파일 2에 대해서도 그것을 해결했습니다. –