我正在尝试为SRT格式创建语法:
以下是srt文件的示例:
1
00:00:02,218 --> 00:00:04,209
[SHELDON SPEAKING IN MANDARIN]
2
00:00:04,721 --> 00:00:05,745
No, it's:
3
00:00:05,922 --> 00:00:07,913
[SPEAKING IN MANDARIN]
4
00:00:09,392 --> 00:00:11,383
[SPEAKING IN MANDARIN]
5
00:00:13,430 --> 00:00:15,193
What's this?
6
00:00:16,266 --> 00:00:18,029
That's what you did.
7
00:00:18,201 --> 00:00:22,467
I assumed, as in a number of languages,
that the gesture was part of the phrase.
8
00:00:22,639 --> 00:00:25,233
- Well, it's not.
- Why am I supposed to know that?
9
00:00:25,408 --> 00:00:28,900
As teacher, it's your obligation
to separate your personal idiosyncrasies...
10
00:00:29,079 --> 00:00:30,512
...from the subject matter.
11
00:00:31,081 --> 00:00:33,845
- I'm glad you decided to learn Mandarin.
- Why?
326
00:18:56,818 --> 00:19:00,720
Actually, I've heard
far too much about Schrödinger's cat.
327
00:19:01,623 --> 00:19:03,022
Good.
328
00:19:09,131 --> 00:19:11,895
All right, the cat's alive.
Let's go to dinner.
329
00:19:12,000 --> 00:19:15,072
Download Movie Subtitles Searcher from www.OpenSubtitles.org
这是我的antlr语法(v.3.4)。
grammar Exp;
parse
: (SUBTITLE)+
;
SUBTITLE
: i=ID NL
t1=Timestamp SPACE ARROW SPACE t2=Timestamp NL
txt1 = TEXT
{
System.out.println("id="+$i);
System.out.println("t1= "+$t1);
System.out.println("t2= "+$t2);
System.out.println("txt1= "+$txt1);
}
;
TEXT
: ((TextLine NL NL)|(TextLine NL TextLine NL NL))
;
ID
: DIG+
;
ARROW
: '-->'
;
Timestamp
: DIG DIG ':' DIG DIG ':' DIG DIG ',' DIG DIG DIG
;
TextLine
: ~('\r' | '\n')*
;
NL
: '\r'? '\n'
| '\r'
;
fragment
DIG
: '0'..'9'
;
fragment
SPACE
: ' ' | '\t'
;
我的简单代码:
String input = IOUtils.toString(Test.class.getResourceAsStream("/subtitles.srt"));
ExpLexer lexer = new ExpLexer(new ANTLRStringStream(input));
CommonTokenStream stream = new CommonTokenStream(lexer);
ExpParser parser = new ExpParser(stream);
parser.parse();
如果在文件末尾我有两条新线,几乎所有东西都能完美运行。如果不是,我收到了这个错误:
line 1484:0 no viable alternative at character '<EOF>'
任何建议如何更改我的语法更灵活?接受最后将是一个新线,两个新线或更多。
答案 0 :(得分:2)
原因是TEXT
最后需要2个新行。
您可以尝试从TEXT
删除一个尾随NL,而不是SUBTITLE
之间的分隔符。
类似的东西:
parse
: SUBTITLE (NL SUBTITLE)*
;
是的,TEXT是否只能有一两行?
答案 1 :(得分:2)
你正在使用过多的词法规则。
尝试这样的事情:
grammar T;
options {
output=AST;
}
tokens {
BLOCKS;
BLOCK;
TIME_RANGE;
LINES;
LINE;
WORD;
}
parse
: LineBreak* blocks LineBreak* EOF -> blocks
;
blocks
: block (LineBreak LineBreak+ block)* -> ^(BLOCKS block+)
;
block
: Number Spaces? LineBreak time_range LineBreak text_lines -> ^(BLOCK Number time_range text_lines)
;
time_range
: Time Spaces? Arrow Spaces? Time Spaces? -> ^(TIME_RANGE Time Time)
;
text_lines
: line (LineBreak line)* -> ^(LINES line+)
;
line
: Spaces? word (Spaces word)* Spaces? -> ^(LINE word+)
;
word
: (Other | Number | Dashes | Arrow)+ -> WORD[$text]
;
Time : Number ':' Number ':' Number ',' Number;
Arrow : '-->';
Dashes : '-'+;
Number : '0'..'9'+;
LineBreak : '\r'? '\n' | '\r';
Spaces : (' ' | '\t')+;
Other : . ;
将解析输入:
1 00:00:02,218 --> 00:00:04,209 [A B C] 2 00:00:04,721 --> 00:00:05,745 -- Line 1 -- Line 2 3 00:00:05,922 --> 00:00:07,913 mu --> MU
进入以下AST:
(点击图片查看大图)
我在文本中有数字和冒号时有问题。 '第1季第15集:'或'“我会在11:00给你打电话。维多利亚。” '试图修改你的例子,但没有成功。
未经测试,但我认为这应该可行:只需在{/ 1}}规则中的第一个冒号之后将所有设为可选。在规则的最后,检查Time
中的最后Number
是否匹配。如果没有,请将令牌类型更改为Time
。
Other