[antlr-interest] line 0:0: expecting
karl wettin
kalle at snigel.net
Wed Mar 29 15:14:23 PST 2006
Ok, I think the grammar is working now. No nondeterminism or anything.
Running main in the parser below now produce this:
> season 1 jo men visst
> line 0:0: expecting EN_NATURAL_SEASON, found ''
>
> s1e1
> 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.
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>();
for (int season = fromSeason; season <= toSeason; season++) {
for (int episode = startEpisode; episode <= endEpisode;
episode++) {
saes.add(new SeasonAndEpisode(season, episode));
}
startEpisode = 1;
}
if (skipFirstEpisode) {
saes.removeFirst();
}
// how big is the file, and how many episodes are there?
if (fileSize != null) {
if (120 * 1024 * saes.size() <= fileSize) {
episodes.addAll(saes);
} else {
// todo could it be S1 - 7 as in s1e7?
}
}
}
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[]{
"season 1 jo men visst",
"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;
}
: EN_NATURAL_SEASON (WHITESPACE)?
startSeason = en_NATURAL_NUMBER
(
en_NATURAL_EPISODE[startSeason]
| en_NATURAL_SEASON[startSeason]
)
;
en_NATURAL_NUMBER returns [int n=-1;]
: NUMBER {n=1;}
| "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]
{
System.out.println("specific episode(s)");
int startEpisode;
}
:
EN_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_EPISODE_SEQUENCE [int startSeason, int startEpisode]
{
System.out.println("episode sequence");
}
:
// sequence
EN_NATURAL_SEQUENCE (WHITESPACE)?
(
EN_NATURAL_SEASON (WHITESPACE)?
episodeSequenceEndSeason:EN_NATURAL_NUMBER (WHITESPACE)?
)?
EN_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 : new Integer
(episodeSequenceEndSeason.getText()),
new Integer(episodeSequenceEndEpisode.getText())
);
}
;
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));
}
(
EN_NATURAL_VECTOR (WHITESPACE)?
(
EN_NATURAL_SEASON (WHITESPACE)?
episodeVectorAndSeason = en_NATURAL_NUMBER (WHITESPACE)?
{
lastSeenSeason = episodeVectorAndSeason;
}
)?
EN_NATURAL_EPISODE (WHITESPACE)?
episodeVectorAndEpisode = en_NATURAL_NUMBER (WHITESPACE)?
{
episodes.add(new SeasonAndEpisode(lastSeenSeason,
episodeVectorAndEpisode));
}
)+
;
en_NATURAL_SEASON [int 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_SEASON_SEQUENCE [int startSeason]
{
System.out.println("sequence of seasons.");
int seasonSequenceEndSeason;
}
:
EN_NATURAL_SEQUENCE (WHITESPACE)?
(EN_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;
}
:
(
EN_NATURAL_VECTOR (WHITESPACE)?
(
(EN_NATURAL_SEASON (WHITESPACE)?)?
seasonVectorAndSeason = en_NATURAL_NUMBER (WHITESPACE)?
{
seasons.add(seasonVectorAndSeason);
}
)
)+
;
class TVSerieEpisodeLexer extends Lexer;
options {
k=2;
testLiterals=false;
}
protected WHITESPACE
: ('.' | ' ')+
;
protected EN_NATURAL_SEASON
: 's' ("eason" ('s')?)?
;
protected EN_NATURAL_EPISODE
: 'e'('p'(("isode"('s')?) | 's')?)?
;
protected EN_NATURAL_VECTOR
: "and" | ","
;
protected EN_NATURAL_SEQUENCE
: "to" | "through" | "-"
;
protected NUMBER returns [int v]
: ('0'..'9')+ { v=Integer.valueOf($getText); }
;
More information about the antlr-interest
mailing list