[antlr-interest] Simple Grammar Question
John Gardener
John.Gardener at carrotgarden.com
Sat Jan 17 06:22:34 PST 2009
*Gerald*, hi;
Thank you very much for your response;
YES, you are right, I can see that: " Your NAME rule is consuming
all tokens. "
What I can not see is how to *correct this properly*?
Thanks again,
John
-------- Original Message --------
Subject: Re: [antlr-interest] Simple Grammar Question
From: Gerald Rosenberg <gerald at certiv.net>
To: John Gardener <John.Gardener at carrotgarden.com>, antlr-interest at antlr.org
Date: Fri 16 Jan 2009 10:31:10 PM CST
> Your NAME rule is consuming all tokens.
>
> The record rule fails to find a digit, recovers and finds a name,
> succeeds yet reports the exception.
>
>
> At 07:08 PM 1/16/2009, John Gardener wrote:
>>
>> Hello;
>>
>> I am stuck with simple grammar; any help is much welcomed;
>>
>> I want to parse 2 term sentenses, such as:
>> <1: single digit > <2: name containing letters and digits > EOF
>>
>> Below comes:
>> 1) grammar
>> 2) test rig
>> 3) output
>>
>> PROBLEM:
>> Second term (name) seems to greedily consume whole input;
>>
>> Please let me know what is the proper way to deal with this?
>>
>> 1) GRAMMAR
>>
>> grammar Simple;
>>
>> options {
>> language = Java;
>> }
>>
>> @parser::header {
>> package simple;
>> import static java.lang.System.out;
>> }
>>
>> @lexer::header{
>> package simple;
>> import static java.lang.System.out;
>> }
>>
>> // PARSER
>>
>> record :
>> digit name EOF
>> { out.println( "+record: " + $text ); };
>>
>> digit : DIGIT
>> { out.println( "+digit: " + $text ); };
>>
>> name : NAME
>> { out.println( "+name: " + $text ); };
>>
>>
>> // LEXER
>>
>> DIGIT : '0'..'9'
>> { out.println("+DIGIT: " + $text ); } ;
>>
>> LETTER : 'A'..'Z'
>> { out.println("+LETTER: " + $text ); } ;
>>
>> NAME : ( LETTER | DIGIT ) +
>> { out.println("+NAME: " + $text ); } ;
>>
>>
>> 2) TEST RIG
>>
>> package simple;
>>
>> import java.io.ByteArrayInputStream;
>>
>> import org.antlr.runtime.ANTLRInputStream;
>> import org.antlr.runtime.CommonTokenStream;
>>
>> import static java.lang.System.out;
>>
>> public class SimpleTest {
>>
>> public static void main(String[] args) throws Exception {
>>
>> String record = "3B5A";
>>
>> ByteArrayInputStream stream = new ByteArrayInputStream(record
>> .getBytes());
>>
>> ANTLRInputStream input = new ANTLRInputStream(stream);
>>
>> SimpleLexer lexer = new SimpleLexer(input);
>>
>> CommonTokenStream tokens = new CommonTokenStream(lexer);
>>
>> SimpleParser parser = new SimpleParser(tokens);
>>
>> parser.record();
>>
>> out.println(record);
>>
>> }
>>
>> }
>>
>>
>> 3) TEST OUTPUT
>>
>> +DIGIT: 3
>> +LETTER: 3B
>> +DIGIT: 3B5
>> +LETTER: 3B5A
>> +NAME: 3B5A
>> line 1:0 missing DIGIT at '3B5A'
>> +digit: null
>> +name: 3B5A
>> +record: 3B5A
>> 3B5A
>>
>>
>> Thank you,
>>
>> John
>>
>>
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> Unsubscribe:
>> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090117/16a1327f/attachment.html
More information about the antlr-interest
mailing list