考虑下面的词法语法:
lexer grammar CodeTableLexer;
CodeTabHeader : '[code table 1.0]';
Code : 'code';
Table : 'table';
End : 'end';
Row : 'row';
Naming : 'naming';
Dfltlang : 'dfltlang';
Language : 'english' | 'german' | 'french' | 'italian' | 'spanish';
Null : 'null';
Number
: Int ('.' Digit*)?
;
Identifier
: ('a'..'z' | 'A'..'Z' | '_') ('a'..'z' | 'A'..'Z' | '_' | '$' | '.' | Digit)*
;
String
@after {
setText(getText().substring(1, getText().length() - 1).replaceAll("\\\\(.)", "$1"));
}
: '"' (~('"'))* '"'
;
Comment
: '--' ~('\r' | '\n')* { skip(); }
| '/*' .* '*/' { skip(); }
;
Space
: (' ' | '\t' | '\r' | '\n' | '\u000C') { skip(); }
;
fragment Int
: '1'..'9'
| '0'
;
fragment Digit
: '0'..'9'
;
...以下语法解析器:
parser grammar CodeTableParser;
parse
: header^ begin (row)* end EOF
;
header
: CodeTabHeader
;
begin
: Code Table Identifier row
;
row
: Row (Number | Identifier) value
;
value
: (Identifier (Number | Identifier | String))+
(Naming Dfltlang String (Language String)*)?
;
end
: End Code Table
;
......终于下面的源解析:
[code table 1.0]
code table my_table
row 1
id "my_id_1"
name "my_name_1"
descn "my_description_1"
naming
dfltlang "My description 1"
english "My description 1"
german "Meine Beschreibung 1"
row 2
id "my_id_2"
name "my_name_2"
descn "my_description_2"
naming
dfltlang "My description 2"
english "My description 2"
german "Meine Beschreibung 2"
end code table
我总是得到错误信息
line 1:0 extraneous input '[code table 1.0]' expecting CodeTabHeader
我很困惑有点...令牌CodeTabHeader被定义为我的词法分析“[代码表1.0]”; 我这么想吗? 任何帮助将非常感激。
谢谢,J3D