[antlr-interest] parsing quoted string

Siddharth Karandikar siddharth.karandikar at gmail.com
Wed Nov 26 02:34:21 PST 2008


Hi All!

I am a newbie here and trying to test out few things with Antlr-v3.
Following is what I have written to parse quoted string and print that
parsed string on console.
For this I referred to StringLiteral definition in Java.g But somehow,
grammar written by me is not working. Am I doing something really
stupid?

Thanks,
Siddharth

Grammar -

grammar Test;

literal : q=quoted
  {
    System.out.println("q=>"+$q.text);
  };

quoted : '"' ( EscapeSequence | ~('\\'|'"') )* '"' ;

fragment
EscapeSequence : '\\' ('\"'|'\''|'\\') ;

WS  :  (' '|'\r'|'\t'|'\u000C'|'\n') {$channel=HIDDEN;}
    ;


Errors that I get -

$ java -classpath .:output/:../../antlr-3.1.jar Run
"abcd ABCD"
line 1:1 no viable alternative at character 'a'
line 1:2 no viable alternative at character 'b'
line 1:3 no viable alternative at character 'c'
line 1:4 no viable alternative at character 'd'
line 1:6 no viable alternative at character 'A'
line 1:7 no viable alternative at character 'B'
line 1:8 no viable alternative at character 'C'
line 1:9 no viable alternative at character 'D'
q=>" "


More information about the antlr-interest mailing list