[antlr-interest] Ambiguities or resolving the rules
John B. Brodie
jbb at acm.org
Wed May 13 07:42:18 PDT 2009
Greetings!
On Wednesday 13 May 2009 01:43:06 am Bharath R wrote:
> Hi All ,, Good morning.
> I m very new to ANTLR , I m facing a below problme like the second rule is
> getting matched with the first rule and its not able to be parsed.
>
> This is my grammar
>
> grammar tel;
>
> TEL : T E L COLON ;
> COLON : ':';
> fragment T : ('t'|'T');
> fragment E : ('e'|'E');
> fragment L : ('l'|'L');
> URI : TEL (parameter)*;
> parameter : pname '=' pvalue ;
> pname:ALPHA;
> pvalue=ALPHA;
>
>
> Now when I pass the value as "tel:airtel=provider" then the parser
> validation fails,
>
> The exception got in " Mismatch character "=" expected ";" " , this is
> like for the input "tel:airtel=provider" the stream "tel" is taken and
> validated for the rule "TEL"
> But its expected to alphabets.
>
>
> Pls give ur suggestion on this.
>
need to fix these 3 errors in your above grammar:
1) ALPHA is undefined
2) pvalue : ALPHA; (not pvalue=....)
3) downcase URI to uri because it is a parser rule not a lexer rule.
with those 3 fixes, the grammar parses your sample input without any error(s).
i have attached the grammar i actually used to test this.
I assume you have extracted the above grammar from some larger grammar,
because, in particular, the error message you give cites the expectation of a
";", yet the ";" does not appear anywhere in your sample grammar.
Could you please post the smallest yet COMPLETE (e.g. please test it before
posting) grammar that shows the issue you are having?
----------
-jbb
-------------- next part --------------
grammar tel;
options {
output = AST;
ASTLabelType = CommonTree;
}
@members {
private static final String [] x = new String[]{
"tel:airtel=provider"
};
public static void main(String [] args) {
for( int i = 0; i < x.length; ++i ) {
try {
System.out.println("about to parse:`"+x[i]+"`");
telLexer lexer = new telLexer(new ANTLRStringStream(x[i]));
CommonTokenStream tokens = new CommonTokenStream(lexer);
telParser parser = new telParser(tokens);
telParser.start_return p_result = parser.start();
CommonTree ast = p_result.tree;
if( ast == null ) {
System.out.println("resultant tree: is NULL");
} else {
System.out.println("resultant tree: " + ast.toStringTree());
}
System.out.println();
} catch(Exception e) {
e.printStackTrace();
}
}
}
}
start : uri EOF!;
TEL : T E L COLON ;
COLON : ':';
fragment T : ('t'|'T');
fragment E : ('e'|'E');
fragment L : ('l'|'L');
uri : TEL (parameter)*;
parameter : pname '=' pvalue ;
pname:ALPHA;
pvalue:ALPHA;
ALPHA : ('a'..'z'|'A'..'Z')+ ;
WS : ' '+ { $channel = HIDDEN; } ;
More information about the antlr-interest
mailing list