[antlr-interest] non-determinism warnings again
Tarun Khanna
tarunkhanna at gmail.com
Fri Jul 8 13:36:38 PDT 2005
Hey thanks for changing the code....I'll try to see why antlr does not
generate warnings now..
I ran the code and it does not parse correctly. I get a "invalid token null"
error.
try giving this as an input to the parser "a.b + b.g". Theoratically this
should work.
You can save the following code in a file called Main.java and use it for
testing.
Thanks and regards,
Tarun
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
try {
//testLexer lexer = new testLexer(new FileInputStream(args[0]));
testLexer lexer = new testLexer(new StringReader("a.b + b + x"));
testParser parser = new testParser(lexer);
parser.exp();
}
catch(Exception e)
{
System.err.println("exception: "+e);
e.printStackTrace(System.err); // so we can get stack trace
}
}
}
On 7/8/05, John B. Brodie <jbb at acm.org> wrote:
>
> Hello :-
>
> Attached please find an updated grammar which removes your ambiguity (no
> longer need to set any k=x option at all).
>
> I can not explain why, but I changed your factor rule to utilize an
> optional
> `qualification` rule (e.g. the DOT (ID | TAB) stuff) and this did the
> trick.
> Note I have not actually tried to execute the generated parser, I have
> only
> run it through the antlr.Tool and got no complaints.
>
> Again do not know why this fixed the problem...
>
> I also made some gratuitous changes to your lexer:
> 1) put the "mod" keyword into a tokens {} section; and
> 2) removed the need for k=2 in the lexer by updating the NL rule.
>
> Hope this helps...
> -jbb
>
> /*-------------------------begin test.g-------------------------*/
>
> class testParser extends Parser;
>
> //options { k = 2; }
>
> exp : term ( (PLUS | MINUS) term )* ;
>
> term : factor ( (TIMES | DIV | MOD) factor )* ;
>
> factor :
> NUM
> | ( ( LPAREN exp RPAREN ) | IDENT ) ( qualification )?
> | TAB
> ;
>
> qualification :
> DOT
> ( ( ID ( qualification )? )
> | TAB )
> ;
>
> class testLexer extends Lexer;
>
> options {
> charVocabulary = '\0'..'\377';
> testLiterals = true;
> //k=2;
> }
>
> tokens {
> MOD = "mod";
> }
>
> LPAREN : '(' ;
> RPAREN : ')' ;
> PLUS : '+' ;
> MINUS : '-' ;
> TIMES : '*' ;
> DIV : '/' ;
> DOT : '.' ;
> COMMA : ',' ;
> LBRACK : '{' ;
> RBRACK : '}' ;
> SEMI : ';' ;
>
> NUM : ('0'..'9')+ ;
>
> IDENT : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')* ;
>
> TAB : '\t' ;
>
> protected NL :
> ( '\r' // '\r' used on Macintosh
> ( '\n' )? // "\r\n" used on DOS/Windows
> | '\n' ) // '\n' used on Unix
> { newline(); }
> ;
>
> WS : ( ' ' | '\f' | NL ) { $setType(Token.SKIP); } ;
>
> /*------------------------- end test.g -------------------------*/
>
--
Tarun Khanna
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20050708/ddd4d245/attachment.html
More information about the antlr-interest
mailing list