Get many instances of the grammer text
1 posts in topic
Flat View  Flat View

Posted By:   Alon_Tamir
Posted On:   Saturday, February 19, 2005 10:42 PM

I have the following grammer:

word: "a" ("b")?;

and I want to get all instances of "word" rule from a text file. My text file contains only "aa", so I expect to get two instances of the word each one has the text "a".

The problem is that when I run the grammer on the "aa" text file I get error:

line 1:2: unexpected token: a

How can I make antlr not to get error, and to accept only the first "a" as "word"?

Re: Get many instances of the grammer text

Posted By:   valentin_tihomirov  
Posted On:   Saturday, February 26, 2005 04:50 AM

Define your rules in the lexer. The parser retreives tokens from there. The lexer combines a series of letters into a token. It also converts the token type into an unique literal token (reserved keyword) if the text of the token is located in the list of reserved words. In your case, the keywords are "a" and "b". So, despite of the ANTLR's parser and lexer tight integration, you'll have to define a lex rule like
LETTER: ('a'..'z');

The most intriguing question I have here to ask from the Information Theory scientists and experts: what is the need for parser/lexer grammar separation? As you see, the barrier disturbs otherwise correct grammar.

About | Sitemap | Contact