|
|
||
|
|
Java Parser Example Click here to download the Java ProGrammar project (Java.GDP), which contains the Java grammar and several parsing examples. /************************************************************************* * * * THIS EXAMPLE IS INTENDED FOR DEMONSTRATION PURPOSES ONLY AND * * IS PROVIDED "AS IS" WITHOUT WARRANTY EITHER EXPRESS OR IMPLIED. * * * * Copyright (c) 1999 - 2002 NorKen Technologies, Inc. * * All rights reserved. * * * *************************************************************************/ // Java.GDL - a grammar for parsing the Java (version 1.1) language. // // Start symbol(s): // // Java.compilation_unit // // NOTES // ----- // The source code for a working application that uses this grammar // is located in subdirectory "JavaBrowser". It's developed in // Visual C++/MFC. // // REVISION DATE: 03/27/02 // grammar Java <SPACE = space_symbol, HIDELITERALS, HIDEREPEATERS> { compilation_unit ::= {[package_statement] [{import_statement}] [{type_declaration}]} ; package_statement ::= "package" package_name ";" ; import_statement ::= "import" (package_name "." asterisk | qualified_name) ";" ; asterisk ::= "*" ; type_declaration ::= ( class_declaration | interface_declaration) [";"] ; class_declaration ::= [modifiers] "class" #anchor class_name [class_extends_clause] [class_implements_clause] "{" [field_declarations] "}" ; interface_declaration ::= [modifiers] "interface" #anchor interface_name [interface_extends_clause] "{" [field_declarations] "}" ; class_extends_clause ::= "extends" qualified_name; class_implements_clause ::= "implements" {qualified_name, ","}; interface_extends_clause ::= ["extends" {qualified_name, ","}] ; field_declarations ::= {field_declaration} ; field_declaration ::= variable_declaration | method_declaration | constructor_declaration | type_declaration | static_initializer | ";" ; method_declaration ::= [modifiers] type method_name "(" [parameter_list] ")" [{empty_subscripts}] [throws_clause] (statement_block | ";") ; constructor_declaration ::= [modifiers] ident "(" [parameter_list] ")" [throws_clause] statement_block ; statement_block ::= "{" [{statement}] "}" ; variable_declaration ::= [modifiers] type variable_declarators ";" ; variable_declarators ::= {variable_declarator, ","} ; variable_declarator ::= ident [{empty_subscripts}] ["=" variable_initializer] ; variable_initializer ::= expression | "{" [{variable_initializer, ","} [","]] "}" ; static_initializer ::= "static" statement_block ; throws_clause ::= "throws" {exception_name, ","} ; parameter_list ::= {parameter, ","} ; parameter ::= type ident [{empty_subscripts}] ; statement ::= statement_block | if_statement | do_statement | while_statement | for_statement | try_statement | switch_statement | "return" [expression] ";" | "break" [ident] ";" | "continue" [ident] ";" | "synchronized" "(" expression ")" statement | "throw" expression ";" | type_declaration | variable_declaration | [expression] ";" | ident ":" statement ; if_statement ::= "if" "(" expression ")" statement ["else" statement] ; do_statement ::= "do" statement "while" "(" expression ")" ";" ; while_statement ::= "while" "(" expression ")" statement ; for_statement ::= "for" "(" for_init for_expr [for_incr] ")" statement ; for_init ::= variable_declaration | expression_list ";" | ";" ; for_expr ::= [expression_list] ";" ; for_incr ::= expression_list ; try_statement ::= "try" statement [{"catch" "(" parameter ")" statement}] ["finally" statement] ; switch_statement ::= "switch" "(" expression ")" "{" {("case" expression | "default") ":" [{statement}]} "}" ; type ::= type_specifier [{empty_subscripts}] ; primitive_type ::= "boolean" | "byte" | "char" | "short" | "int" | "float" | "long" | "double" | "void" ; type_specifier<TERMINAL> ::= primitive_type | qualified_name ; modifiers ::= {modifier} ; modifier<TERMINAL> ::= "abstract" | "final" | "native" | "private" | "protected" | "public" | "static" | "synchronized" | "threadsafe" | "transient" | "volatile" ; qualified_name <SPACE=""> ::= {ident, "."}; package_name ::= qualified_name; exception_name ::= qualified_name; method_name<TERMINAL> ::= ident; class_name<TERMINAL> ::= ident; interface_name<TERMINAL> ::= ident; empty_subscripts ::= "[""]"; // ------------------------------------------------------------------- // // EXPRESSIONS // // ------------------------------------------------------------------- // The following rules establish operator precedence // (increasing precedence for each successive rule) // Assignment (assign_term) is lowest precedence expression<SHOWDELIMITERS> ::= assign_term; assign_term ::= { ternary_term, assign_level_op }; ternary_term ::= or_term ["?" expression ":" expression]; or_term ::= { and_term, or_level_op }; and_term ::= { bit_term, and_level_op }; bit_term ::= { cmp_term, bit_level_op }; cmp_term ::= { shift_term, cmp_level_op }; shift_term ::= { add_term, shift_level_op }; add_term ::= { mult_term, add_level_op }; mult_term ::= { factor, mult_level_op }; factor ::= literal_expression | creating_expression | casting_expression | "!" expression | "(" expression ")" ["." reference] | unary_op expression | [prefix_op] reference [postfix_op | instanceof_op] ; // operators assign_level_op ::= ("=" | "*=" | "/=" | "%=" | "+=" | "-=" | "<<=" | ">>=" | ">>>=" | "&=" | "^=" | "|="); or_level_op ::= "||"; and_level_op ::= "&&" ; bit_level_op ::= "|" | "^" | "&"; cmp_level_op ::= "==" | "!=" | "<" | ">" | ">=" | "<="; shift_level_op ::= ">>>" | ">>" | "<<"; add_level_op ::= "+" | "-"; mult_level_op ::= "/" | "*" | "%"; unary_op ::= "-" | "~" | "+"; prefix_op ::= "++" | "--"; postfix_op ::= "++" | "--"; instanceof_op ::= "instanceof" qualified_name; expression_list ::= {expression, ","}; reference ::= {reference_term, "."}; reference_term ::= ident [{ "[" expression "]" | "(" [argument_list] ")" }] | "class"; argument_list ::= {expression, ","}; creating_expression ::= "new" ( qualified_name "(" [argument_list] ")" ["." reference] // method invocation [anonymous_decl] // anonymous class declaration | type_specifier [{"[" expression "]"}] [empty_subscripts] [variable_initializer] | "(" expression ")" ) ; anonymous_decl ::= "{" [field_declarations] "}" ; casting_expression ::= "(" type ")" expression; literal_expression ::= numeric_literal | string | character ; ident<TERMINAL>::= identifier (? #value !::= keyword; ); keyword ::= "abstract" | "boolean" | "break" | "byte" | "case" | "catch" | "char" | "class" | "continue" | "default" | "do" | "double" | "else" | "extends" | "final" | "finally" | "float" | "for" | "if" | "implements" | "import" | "instanceof" | "int" | "interface" | "long" | "native" | "new" | "package" | "private" | "protected" | "public" | "return" | "short" | "static" | "switch" | "synchronized" | "threadsafe" | "throw" | "throws" | "transient" | "try" | "void" | "volatile" | "while" ; numeric_literal<TERMINAL> ::= '0[xX][0-9a-fA-F]+' // hex constant | (numeric ["." numeric] | "." numeric) [exponent_part] [float_type_suffix | long_type_suffix] ; exponent_part ::= "e" ("+" | "-") numeric; float_type_suffix ::= "f" | "d"; long_type_suffix ::= "l" | "L"; string ::= quotedstring; character<TERMINAL,SPACE=""> ::= "\'" ["\\"] ( numeric | '.' ) "\'"; // this example uses the SPACE attribute to skip comments in // the Java file space_symbol <TERMINAL,HIDDEN> ::= { ( "\32" | "\r" |"\n"|"\t") | "//" *("\n") | "/*" *("*/") "*/",0 }; }; // Java Grammar |
|
|
For comments or questions about this site, please contact webmaster@programmar.com Copyright © 1998-2008 NorKen Technologies, Inc. All rights reserved. |
||