문제

sorry for my english! I have problem, faced here with such a problem, give the decision:

line 1:0 required (...)+ loop did not match anything at input < E O F >

This my file, calc.g

grammar calc;

options {
  language = Java;
}

rule: statement+;


statement
    : expr NEWLINE
    | ID '=' expr NEWLINE
    | NEWLINE
    ;

NEWLINE : '\r'? '\n'
    ;

expr
    : multExpression ('+' multExpression |'-' multExpression)*
    ; 

multExpression
    : a1=atom ('*' a2=atom | '/' a2=atom)*
    ;

atom
    : ID
    | INT 
    | '(' expr ')' 
    ;

ID  :   ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
    ;

INT :   ('1'..'9') ('0'..'9')*
    ;

this is my main:

ANTLRReaderStream input = new ANTLRReaderStream(); 
calcLexer lexer = new calcLexer(input); 
CommonTokenStream tokens = new CommonTokenStream(lexer); 
calcParser parser = new calcParser(tokens); 
parser.rule();      
도움이 되었습니까?

해결책

It looks like your input is empty: the parser immediately encounters the EOF (end-of-file) where it expects at least one statement.

Try something like this:

ANTLRStringStream input = new ANTLRStringStream("2*42\n"); 
calcLexer lexer = new calcLexer(input); 
CommonTokenStream tokens = new CommonTokenStream(lexer); 
calcParser parser = new calcParser(tokens); 
parser.rule(); 

As 280Z28 mentioned in the comment beneath my answer, you should probably force the parser to consume the entire input by adding EOF at the end of your parser's entry point:

rule: statement+ EOF;

and if you want to allow an empty string to be valid input too, change the + to a *;

rule: statement* EOF;

다른 팁

I didn't test it but think you have to add the default EOF token to your grammar:

rule: statement+ EOF!;

Your parser seems to recognize the whole input but at the end there is an EOF but you did not add the corresponding rule to your grammar.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top