给定以下 Lexer 语法:
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 在我的词法分析器中被定义为“[code table 1.0]”;我错过了什么吗?任何帮助将非常感激。
谢谢, J3d