【背景】
之前花了很多精力,终于下载到Antlr的Eclipse插件并安装成功:
并且配置好antlr:
现在就可以用Eclipse去折腾Antlr的开发了。
1.参考:
去新建一个java项目:
就可以新建好此java的项目了:
2.选中该java项目后,
再去新建项目:
3.然后就可以新建出对应的antlr的语法文件了:
然后写入新的内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | grammar HartEddl; /* Notes: 1. HART EDDL Spec can find from hart 7.3 -> SPEC500.pdf, that is Device Description Language Specification */ options { output = AST; ASTLabelType = CommonTree; // type of $stat.tree ref etc... } COMMENT : '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;} | '/*' ( options {greedy=false;} : . )* '*/' {$channel=HIDDEN;} ; WS : ( ' ' | '\t' | '\r' | '\n')+ {$channel=HIDDEN;}; //fragment ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*; fragment DIGIT : '0'..'9'; fragment OCTAL_ESC : '\\' ('0'..'3') ('0'..'7') ('0'..'7') | '\\' ('0'..'7') ('0'..'7') | '\\' ('0'..'7') ; fragment UNICODE_ESC : '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT ; fragment ESC_SEQ : '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\') | UNICODE_ESC | OCTAL_ESC ; //fragment STRING : '"' ( ESC_SEQ | ~('\\'|'"') )* '"' ; fragment IMPORTED_VALUE : '[' ID ']'; /* normal ID with [] */ //fragment DEFINE_IMPORTED_VALUE : ID | IMPORTED_VALUE; fragment HEX_DIGIT : ('a'..'f'|'A'..'F' | DIGIT) ; //fragment HEX_VALUE : '0x' HEX_DIGIT+; //fragment DECIMAL_VALUE : DIGIT+; //fragment direct_value : (DECIMAL_VALUE | HEX_VALUE); string_value : (DEFINE_IMPORTED_VALUE | STRING); expression_or_direct_value : direct_value; //todo: add expression support expression_or_string : STRING;//todo:add string expression support /******************************************************************************* Main Entry Rule *******************************************************************************/ startParse : (singleInclude | idInfos | variable | command | collection | menu)+; /******************************************************************************* include *******************************************************************************/ singleInclude : '#include' STRING; /******************************************************************************* EDDL Identification Info *******************************************************************************/ idInfos : idinfo (',' idinfo)+; idinfo : idInfoKey idInfoValue; idInfoKey : 'MANUFACTURER' | 'DEVICE_TYPE' | 'DEVICE_REVISION' | 'DD_REVISION'; idInfoValue : direct_value | DEFINE_IMPORTED_VALUE; /******************************************************************************* Common definitions *******************************************************************************/ common_label : 'LABEL' string_value+ ';'; common_help : 'HELP' help_value ';'; /******************************************************************************* 7.27 VARIABLE Table 138 – VARIABLE attributes *******************************************************************************/ variable : 'VARIABLE' variable_identifier '{' variable_body '}'; variable_identifier : ID; variable_body : variable_field+; variable_field : variable_class | variable_label | variable_type | variable_help | constant_unit | variable_handling; variable_class : 'CLASS' class_value ';' ; class_value : single_class_value ('&' single_class_value)*; single_class_value : ('ALARM'| 'ANALOG_INPUT'| 'ANALOG_OUTPUT'| 'COMPUTATION'| 'CONTAINED'| 'CORRECTION'| 'DEVICE'| 'DIAGNOSTIC'| 'DIGITAL_INPUT'| 'DIGITAL_OUTPUT'| 'DISCRETE_INPUT'| 'DISCRETE_OUTPUT'| 'DYNAMIC'| 'FREQUENCY_INPUT'| 'FREQUENCY_OUTPUT'| 'HART'| 'INPUT'| 'LOCAL'| 'LOCAL_DISPLAY'| 'OPERATE'| 'OUTPUT'| 'SERVICE'| 'TUNE'); variable_label : common_label; variable_type : integer_type_value | float_type_value | enumarated_type_value | bitEmurated_type_value; //todo: add more values_display_format : 'DISPLAY_FORMAT' expression_or_string ';'; values_default_value : 'DEFAULT_VALUE' expression_or_direct_value ';'; enumarated_value: direct_value; enumarated_desc : string_value; enumarated_help : string_value; enumarated_desc_help : enumarated_desc_help_call | enumarated_desc_help_direct; enumarated_desc_help_direct : enumarated_desc (',' enumarated_help)?; enumarated_desc_help_call : ID '(' direct_value ')'; // call some variable // : variable_identifier '(' direct_value ')'; // call some variable // : 'units_code(57)'; // call some variable //Note: //7.27.2.2.3.2 ENUMERATED //[(description, value, help)<exp>]+, //should be //[(value, description, help)<exp>]+, values_enumarated : '{' enumarated_value ',' enumarated_desc_help '}' ','?; //TODO: add DEFAULT_VALUE,help,INITIAL_VALUE,size //7.27.2.2.3.2 ENUMERATED enumarated_type_value : 'TYPE' 'ENUMERATED' '{' values_enumarated+ '}'; values_attribute : values_default_value | values_display_format;//todo: add more EDIT_FORMAT, ... float_type_value : 'TYPE' 'FLOAT' '{' values_attribute+ '}'; integer_type_value : 'TYPE' 'INTEGER' '{' values_attribute+ '}'; bitEnumarated_action : ID ('&' ID)* ('(' ID '&' ID ')')?; bitEnumarated_value : direct_value; bitEnumarated_desc : string_value; values_bitEnumarated : '{' bitEnumarated_value ',' bitEnumarated_desc (',' bitEnumarated_action)?'}' ','?; bitEmurated_type_value : 'TYPE' 'BIT_ENUMERATED' '{' values_bitEnumarated+ '}'; constant_unit : 'CONSTANT_UNIT' string_value ';' ; variable_help : common_help; help_value : string_value; /* 7.27.2.5 HANDLING */ single_handling_value : ('READ' | 'READ_WRITE' | 'WRITE'); handling_value : single_handling_value ('&' single_handling_value)*; variable_handling : 'HANDLING' handling_value ';' ; /******************************************************************************* 7.7 COMMAND *******************************************************************************/ command : 'COMMAND' command_identifier '{' command_body '}'; command_body : command_field+;//command_number command_operation command_transaction+ command_responseCode*; command_field : command_number | command_operation | command_transaction | command_responseCode; command_identifier : ID; command_number : 'NUMBER' direct_value ';'; command_operation : 'OPERATION' command_operation_attr ';'; command_operation_attr : 'READ'|'WRITE'|'COMMAND'|'DATA_EXCHANGE'|string_value; command_transaction :'TRANSACTION' command_transaction_number? '{' command_transaction_body '}'; command_transaction_number : direct_value; command_transaction_body : command_transaction_request command_transaction_reply command_transaction_responseCode*; //for HART EDDL //9.1 Transactions command_transaction_request : 'REQUEST' '{' request_reply_dataItem? (',' request_reply_dataItem)* '}'; //TODO: add more for: Table 46 – REPLY and REQUEST attributes command_transaction_reply : 'REPLY' '{' request_reply_dataItem (',' request_reply_dataItem)* '}'; request_reply_dataItem : direct_value | request_reply_dataItem_reference; request_reply_dataItem_reference : ID; //TODO: add "9.1.1 Data Item Masks", "9.1.2 Data Item Qualifiers", "" /* //for Profibus EDDL command_transaction_request : 'REQUEST' '{' request_reply_attibutes '}'; //TODO: add more for: Table 46 – REPLY and REQUEST attributes command_transaction_reply : 'REPLY' '{' request_reply_attibutes '}'; request_reply_attibutes : (requestReply_attibute_reference (',' requestReply_attibute_itemMask (',' requestReply_attibute_info (',' requestReply_attibute_index)?)?)?)?; //here set to optional, which is not confrom to : //Table 46 – REPLY and REQUEST attributes //but can match our demo file, which REQUEST is empty requestReply_attibute_reference : ID; requestReply_attibute_itemMask : ID; requestReply_attibute_info : ID; requestReply_attibute_index : ID; */ command_transaction_responseCode : command_responseCode; command_responseCode : 'RESPONSE_CODES' '{' responseCode_body '}'; responseCode_body : responseCode_exp+; responseCode_exp: responseCode_exp_value ',' responseCode_exp_type ',' responseCode_exp_description (','? responseCode_exp_help)? ';'; responseCode_exp_value : direct_value; responseCode_exp_type : 'DATA_ENTRY_ERROR'|'DATA_ENTRY_WARNING'|'MISC_ERROR'|'MISC_WARNING'|'MODE_ERROR'|'PROCESS_ERROR'|'SUCCESS'; responseCode_exp_description : string_value; responseCode_exp_help : string_value; /******************************************************************************* 7.6 COLLECTION in Profibus EDDL 8.4 Collections in HART EDDL *******************************************************************************/ collection : 'COLLECTION' collection_name '{' collection_body '}'; collection_name : collection_of | ID; collection_of : 'OF' collection_attr_itemType ID; collection_body : collection_attribute+; collection_attribute : collection_attr_lable | collection_attr_help | collection_attr_members; //collection_attr_validity collection_attr_lable : common_label; collection_attr_help : common_help; collection_attr_itemType : 'AXIS' | 'BLOCK_A' | 'BLOCK_B' | 'CHART' | 'COLLECTION' | 'COMMAND' | 'CONNECTION' | 'DOMAIN' | 'EDIT_DISPLAY' | 'FILE' | 'GRAPH' | 'LIST' | 'MENU' | 'METHOD' | 'PROGRAM' | 'RECORD' | 'REFERENCE_ARRAY' | 'REFRESH' | 'RESPONSE_CODES' | 'SOURCE' | 'UNIT' | 'VALUE_ARRAY' | 'VARIABLE' | 'VARIABLE_LIST' | 'WAVEFORM' | 'WRITE_AS_ONE'; collection_attr_members : 'MEMBERS' '{' collection_attr_members_body '}'; collection_attr_members_body : collection_attr_members_item+; collection_attr_members_item : collection_attr_members_item_name ',' collection_attr_members_item_ref (',' collection_attr_members_item_desc ',' collection_attr_members_item_help)? ';'; collection_attr_members_item_name : ID; collection_attr_members_item_ref : ID; collection_attr_members_item_desc : DEFINE_IMPORTED_VALUE; collection_attr_members_item_help : DEFINE_IMPORTED_VALUE; /******************************************************************************* 7.18 MENU 10.1 Menus *******************************************************************************/ menu : 'MENU' menu_identifier '{' menu_body '}' ; menu_identifier : ID; menu_body : menu_attribute+; menu_attribute : menu_label | menu_items; //TODO: add help,... menu_label : common_label; menu_items : 'ITEMS' '{' menu_items_body '}'; menu_items_body : menu_item (',' menu_item)*; menu_item : ID menu_item_attributes?; menu_item_attributes : '(' menu_item_attribute (',' menu_item_attribute)* ')'; menu_item_attribute : 'DISPLAY_VALUE' | 'HIDDEN' | 'READ_ONLY' | 'NO_LABEL' | 'NO_UNIT' | 'REVIEW'; |
变成:
注意到,其.g的语法文件下面,除了Grammar,还有对应的Interpreter和Railroad View。
4.然后再去转换为Antlr项目:
然后什么变化好像都没有。
不过再去查看选项时,就没了Configure项了:
5.然后就可以去编译项目了:
对应的,编译成功后:
console中有对应的log输出:
ANTLR Parser Generator Version 3.5. Grammar: D:\DevRoot\android\android_root\HartEddl\HartEddl.g BUILD SUCCESSFUL Total time: 1 second |
然后也生成了对应的输出文件夹output和对应的lexer和parser的java文件,以及token文件。
至此,编译.g文件,算是OK了。
6.不过,此时注意到,当前环境下,还是无法识别antlr的库的:
鼠标移动上去,出现提示:
但是却看到检测出来的antlr的jar是3.4的:
不是我们之前配置antlr时所添加的3.5的。
7.所以,此处不用此办法。
而是自己去添加正确的路径去:
右击antlr项目->Properties->Java Build Path -> Libraries -> Add External JARs
添加对应的antlr的complete的jar包,此处为antlr-3.5-complete.jar,即可:
然后项目中的代码,就可以找到antlr的库,就可以消除错误了:
【总结】
至此,创建并编译antlr的项目,就完成了。
接着,就是去折腾如何调试antlr代码:
【记录】使用Eclipse开发Antlr:如何调试antlr项目
转载请注明:在路上 » 【记录】使用Eclipse开发Antlr:创建antlr项目并编译