antlr3 - Parsing ambiguous input with Antlr -


I'm trying to parse text with text and numbers for a few days (I have called it a sentence In my grammar)

  sentence option {greedy = false; }: (ANY_WORD | INT) +;  

I have a rule that needs to be parse the sentence ending with INT

  Sentence_arrow: Sentence INT;  

So if I had some input that "the number of shoes that were 14 sizes purchased was 3" then the sentence-sentence_finity would not match the sentence only. I'm sure there is a better way to do this, but I'm just learning the tools.

Thanks, Richard

Your grammar:


  grammar test; Sentence_Content: Sentence {System.out.println ("Pars: Sentence = '" $ sentence. Text + "'");} INT {System.out.println ("Pars: int = '" + $ INT.text + " '' ");}; Sentence: (ANY_WORD | INT) +; ANY_WORD: ('a' .. 'z' | 'A' .. 'Z') +; INT: ('0' .. '9') +; WS: ('' '' '\ t' | '\ r' | '' \ n ') {$ channel = HIDDEN;};  

In the same way it is a short test here:

  import org.antlr.runtime. *; Public square demo {public static zero main (string [] args throws exceptions {ANTLRStringStream = new ANTLRStringStream ("Size 14 boots were bought 3 were shoes"); TestLexer lexer = New TestLexer (in); Common Token Stream Token = New Common Token Stream (Lexer); Test parser parser = new test parser (token); Parser.sentence_with_int (); }}  

First generate a parser & amp; Lexer (assuming all your files and ANLR jar, are in the same directory):

 java-cp antlr-3.2.jor org.antlr.Tool Test.g 

and all .java compile source files:

 javac-cp antlr-3.2.jar * .java 

and end run the demo Class:

 Java-CP: Annaler-3.2.jpg Demo 

(, with : with a Change; )

which produces the following output:

 Parsed: Sentence = 'Buy 14 shoe sizes Mr. Number parsing ": Int = '3' 

Comments