grammar Language;
program : (varDecl)* (funcDecl)* EOF;
varDecl : type IDENTIFIER ('=' expression)? ';';
funcDecl : type IDENTIFIER '(' ')' statementBlock;
type : 'int' # IntType
;
statementBlock : '{' (statement)* '}';
statement : varDecl ;
expression : IDENTIFIER '(' (expression (',' expression)*)? ')' # FuncCallExpression
;
IDENTIFIER : ('a'..'z')+;
WHITE_SPACE : [ \t\u000C\n\r]+ -> skip;
int b() {
}
i nt a() {
int x = b();
}
public class DummyListener extends LanguageBaseListener {
@Override
public void exitFuncDecl(LanguageParser.FuncDeclContext ctx) {
super.exitFuncDecl(ctx);
if (ctx.statementBlock() == null) {
System.out.println("Oh, no :(");
}
}
}
funcDecl : type IDENTIFIER '(' ')' statementBlock { System.out.println("ID: " + $IDENTIFIER.text + ", text is: " + $statementBlock.text); };
System.out.println("Listener: id " + ctx.IDENTIFIER().getText());
if (ctx.statementBlock() == null) {
System.out.println("Oh, no :(");
} else {
System.out.println("content is " + ctx.statementBlock().getText());
}
line 3:0 extraneous input 'i' expecting {<EOF>, 'int'}
ID: b, text is: {}
line 4:7 mismatched input '=' expecting '('
Listener: id b
content is {}
Listener: id x
Oh, no :(
Thanks for your answer Eric,
But then why isn't ANTLR calling the action rule? It's only calling the listener method.
Also, if the input turned to be invalid, why does it make sense to create a node for a rule it didn't match?.
I was wondering what would be the right way to handle this kind of cases. Checking for null attributes in every method if the listener doesn't look quite right to me
Thank you very much
--
You received this message because you are subscribed to a topic in the Google Groups "antlr-discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/antlr-discussion/-FocT-iVNoc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to antlr-discussi...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
program : (varDecl|funcDecl)* EOF;
With the following input, it produces the same result:
int b() {
}
i nt a() {
int x = ;
}
Now, if it doesn't have parenthesis (x) nor braces (not even added during error correction), hows does it know that it should be a FuncDeclContext and not a VarDeclContext?
Anyway, would it be safe to assume that a null exception inside the context implies I have a complete rule?
Thank you again