[antlr-interest] line 0:0: expecting
karl wettin
kalle at snigel.net
Fri Mar 31 16:48:50 PST 2006
30 mar 2006 kl. 01.14 skrev karl wettin:
>> line 0:0: expecting EN_NATURAL_SEASON, found ''
>
> Have I misunderstood how to combine a parser and a lexer? Too me
> that error sounds like there is no input.
This is an error you get when you are calling protected Lexer rules
from the Parser.
It should really be mentioned somewhere. It is not. I'm actually
suprised that Antlr accepts the code!
And now I got some new problems, of course :)
header {
package se.snigel.tindex.analysis.tvserie;
import java.util.*;
import java.io.*;
import antlr.CommonToken;
import antlr.Token;
}
class TVSerieEpisodeParser extends Parser;
options {
k=1;
}
{
public class SeasonAndEpisode {
private int season;
private int episode;
private SeasonAndEpisode(int season, int episode) {
this.season = season;
this.episode = episode;
}
public int getSeason() {
return season;
}
public int getEpisode() {
return episode;
}
public String toString() {
return season + "x" + episode;
}
}
private LinkedList<SeasonAndEpisode> episodes = new
LinkedList<SeasonAndEpisode>();
private LinkedList<Integer> seasons = new LinkedList<Integer>();
/** for sanity checks */
private Long fileSize;
private void addSequence(boolean skipFirstEpisode, Integer
fromSeason, Integer fromEpisode, Integer toSeason, Integer toEpisode) {
int startEpisode = fromEpisode;
int endEpisode;
if (fromSeason == toSeason) {
endEpisode = toEpisode;
} else {
throw new RuntimeException("Need to figure out the last
episode of this season.");
}
LinkedList<SeasonAndEpisode> saes = new
LinkedList<SeasonAndEpisode>();
int size = 0;
for (int season = fromSeason; season <= toSeason; season++) {
for (int episode = startEpisode; episode <= endEpisode;
episode++) {
saes.add(new SeasonAndEpisode(season, episode));
size++;
}
startEpisode = 1;
}
if (skipFirstEpisode) {
saes.removeFirst();
}
// how big is the file, and how many episodes are there?
if (fileSize != null) {
if (120 * 1024 * size <= fileSize) {
episodes.addAll(saes);
} else {
// todo could it be S1 - 7 as in s1e7?
}
} else {
episodes.addAll(saes);
}
}
public String toString() {
StringBuffer buf = new StringBuffer();
for (SeasonAndEpisode sae : episodes) {
buf.append(sae);
buf.append('\n');
}
for (Integer s : seasons) {
buf.append(s);
buf.append('\n');
}
return buf.toString();
}
public static void main(String[] args) throws Exception {
String[] testText = new String[]{
"season1",
"s1e1",
"s1",
"s 1",
"season 1",
"season 1 episode 3",
"s1,s2,s3",
"s1-s3",
"s1e3-4",
"s1 e4, 5, 6",
"season one will fail. how do I handle the EN_NATURAL_NUMBER int?",
"s1 - 4 is this really four seasons? sanity check. is it s1e4?",
"s1e19-s2e12 what is the last episode of season 1?",
};
for (String text : testText) {
System.out.println(text);
TVSerieEpisodeLexer lexer = new TVSerieEpisodeLexer(new
StringReader(text));
TVSerieEpisodeParser parser = new TVSerieEpisodeParser(lexer);
parser.en_NATURAL();
System.out.println(parser);
}
}
}
en_NATURAL
{
int startSeason;
StringBuffer prefix = new StringBuffer();
StringBuffer suffix = new StringBuffer();
}
:
NATURAL_SEASON
(WHITESPACE)?
startSeason = en_NATURAL_NUMBER
(WHITESPACE)?
(
//en_NATURAL_EPISODE[startSeason]
(
{
System.out.println("specific episode(s)");
int startEpisode;
}
NATURAL_EPISODE (WHITESPACE)?
startEpisode = en_NATURAL_NUMBER
{
// add inital episode
episodes.add(new SeasonAndEpisode(startSeason, startEpisode));
}
( en_NATURAL_EPISODE_SEQUENCE [startSeason, startEpisode] |
en_NATURAL_EPISODE_VECTOR [startSeason, startEpisode])?
)
| //en_NATURAL_SEASON[startSeason]
(
{
System.out.println("full season(s)");
// add inital season
seasons.add(startSeason);
// todo sanity check file size
}
( en_NATURAL_SEASON_SEQUENCE [startSeason] |
en_NATURAL_SEASON_VECTOR [startSeason])?
)
)
;
en_NATURAL_NUMBER returns [int n=-1;]
: v:NUMBER { n = Integer.valueOf(v.getText()); }
| "one" { n=1; }
| "two" { n=2; }
| "three" { n=3; }
| "four" { n=4; }
| "five" { n=5; }
| "six" { n=6; }
| "seven" { n=7; }
| "eight" { n=8; }
| "nine" { n=9; }
| "ten" { n=10;}
;
//en_NATURAL_EPISODE [int startSeason]
en_NATURAL_EPISODE_SEQUENCE [int startSeason, int startEpisode]
{
System.out.println("episode sequence");
Integer episodeSequenceEndSeason = null;
int episodeSequenceEndEpisode;
}
:
// sequence
NATURAL_SEQUENCE (WHITESPACE)?
(
NATURAL_SEASON (WHITESPACE)?
episodeSequenceEndSeason = en_NATURAL_NUMBER (WHITESPACE)?
)?
(NATURAL_EPISODE (WHITESPACE))?
episodeSequenceEndEpisode = en_NATURAL_NUMBER
{
// true cuts of the first episode in the list. it is already added.
addSequence(true,
startSeason,
startEpisode,
episodeSequenceEndSeason == null ? startSeason :
episodeSequenceEndSeason,
episodeSequenceEndEpisode
);
}
;
en_NATURAL_EPISODE_VECTOR [int startSeason, int startEpisode]
{
int episodeVectorAndSeason;
int episodeVectorAndEpisode;
}
:
{System.out.println("episode vector");}
// vector
{
Integer lastSeenSeason = startSeason;
// this is already done. episodes.add(new SeasonAndEpisode
(startSeason, startEpisode));
}
(
NATURAL_VECTOR (WHITESPACE)?
(
NATURAL_SEASON (WHITESPACE)?
episodeVectorAndSeason = en_NATURAL_NUMBER (WHITESPACE)?
{
lastSeenSeason = episodeVectorAndSeason;
}
)?
NATURAL_EPISODE (WHITESPACE)?
episodeVectorAndEpisode = en_NATURAL_NUMBER (WHITESPACE)?
{
episodes.add(new SeasonAndEpisode(lastSeenSeason,
episodeVectorAndEpisode));
}
)+
;
//en_NATURAL_SEASON [int startSeason]
en_NATURAL_SEASON_SEQUENCE [int startSeason]
{
System.out.println("sequence of seasons.");
int seasonSequenceEndSeason;
}
:
NATURAL_SEQUENCE (WHITESPACE)?
(NATURAL_SEASON (WHITESPACE)?)?
seasonSequenceEndSeason = en_NATURAL_NUMBER
{
for (int i= startSeason + 1; i <= seasonSequenceEndSeason; i++) {
seasons.add(i);
// todo sanity check file size
}
}
;
en_NATURAL_SEASON_VECTOR [int startSeason]
{
System.out.println("vector of seasons");
int seasonVectorAndSeason;
}
:
(
NATURAL_VECTOR
(NATURAL_SEASON (WHITESPACE)?)?
seasonVectorAndSeason = en_NATURAL_NUMBER (WHITESPACE)?
{
seasons.add(seasonVectorAndSeason);
}
)+
;
class TVSerieEpisodeLexer extends Lexer;
options {
k=2;
testLiterals=false;
}
WHITESPACE
: ('.' | " ")+
;
NATURAL_SEASON
: 's' ("eason" ('s')?)?
;
NATURAL_EPISODE
: 'e'('p'(("isode"('s')?) | 's')?)?
;
NATURAL_VECTOR
: "and" | ","
;
NATURAL_SEQUENCE
: "to" | "through" | "-"
;
NUMBER
: ('0'..'9')+
;
PUNCTSPACE
: ('!'
| '"'
| '#'
| '$'
| '%'
| '&'
| '('
| ')'
| '*'
| '+'
| "|"
| '-'
| '.'
| '/'
| ':'
| ';'
| '<'
| '='
| '>'
| '?'
| '@'
| '['
| '\\'
| ']'
| '^'
| '_'
| '`'
| '{'
| '}'
| '~'
| ' ')+
;
More information about the antlr-interest
mailing list