📄 parse-scan.y
字号:
/* Parser grammar for quick source code scan of Java(TM) language programs. Copyright (C) 1998, 1999 Free Software Foundation, Inc. Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)This file is part of GNU CC.GNU CC is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU CC is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU CC; see the file COPYING. If not, write tothe Free Software Foundation, 59 Temple Place - Suite 330,Boston, MA 02111-1307, USA.Java and all Java-based marks are trademarks or registered trademarksof Sun Microsystems, Inc. in the United States and other countries.The Free Software Foundation is independent of Sun Microsystems, Inc. *//* This file parses Java source code. Action can be further completedto achieve a desired behavior. This file isn't part of the Javalanguage gcc front end.The grammar conforms to the Java grammar described in "The Java(TM)Language Specification. J. Gosling, B. Joy, G. Steele. Addison Wesley1996, ISBN 0-201-63451-1"Some rules have been modified to support JDK1.1 inner classesdefinitions and other extensions. */%{#define JC1_LITE#include "config.h"#include "system.h"#include "obstack.h"#include "toplev.h"extern char *input_filename;extern FILE *finput, *out;/* Obstack for the lexer. */struct obstack temporary_obstack;/* The current parser context. */static struct parser_ctxt *ctxp;/* Error and warning counts, current line number, because they're used elsewhere */int java_error_count;int java_warning_count;int lineno;/* Tweak default rules when necessary. */static int absorber;#define USE_ABSORBER absorber = 0/* Keep track of the current class name and package name. */static char *current_class;static char *package_name;/* Keep track of whether things have be listed before. */static int previous_output;/* Record modifier uses */static int modifier_value;/* Keep track of number of bracket pairs after a variable declarator id. */static int bracket_count; /* Record a method declaration */struct method_declarator { char *method_name; char *args;};#define NEW_METHOD_DECLARATOR(D,N,A) \{ \ (D) = \ (struct method_declarator *)xmalloc (sizeof (struct method_declarator)); \ (D)->method_name = (N); \ (D)->args = (A); \}/* Two actions for this grammar */static void report_class_declaration PROTO ((char *));static void report_main_declaration PROTO ((struct method_declarator *));#include "lex.h"#include "parse.h"%}%union { char *node; struct method_declarator *declarator; int value; /* For modifiers */}%pure_parser/* Things defined here have to match the order of what's in the binop_lookup table. */%token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK%token LS_TK SRS_TK ZRS_TK%token AND_TK XOR_TK OR_TK%token BOOL_AND_TK BOOL_OR_TK %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK/* This maps to the same binop_lookup entry than the token above */%token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK%token REM_ASSIGN_TK %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK%token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK/* Modifier TOKEN have to be kept in this order. Don't scramble it */%token PUBLIC_TK PRIVATE_TK PROTECTED_TK%token STATIC_TK FINAL_TK SYNCHRONIZED_TK%token VOLATILE_TK TRANSIENT_TK NATIVE_TK%token PAD_TK ABSTRACT_TK MODIFIER_TK/* Keep those two in order, too */%token DECR_TK INCR_TK/* From now one, things can be in any order */%token DEFAULT_TK IF_TK THROW_TK%token BOOLEAN_TK DO_TK IMPLEMENTS_TK%token THROWS_TK BREAK_TK IMPORT_TK %token ELSE_TK INSTANCEOF_TK RETURN_TK%token VOID_TK CATCH_TK INTERFACE_TK%token CASE_TK EXTENDS_TK FINALLY_TK%token SUPER_TK WHILE_TK CLASS_TK%token SWITCH_TK CONST_TK TRY_TK%token FOR_TK NEW_TK CONTINUE_TK%token GOTO_TK PACKAGE_TK THIS_TK%token BYTE_TK SHORT_TK INT_TK LONG_TK%token CHAR_TK INTEGRAL_TK%token FLOAT_TK DOUBLE_TK FP_TK%token ID_TK%token REL_QM_TK REL_CL_TK NOT_TK NEG_TK%token ASSIGN_ANY_TK ASSIGN_TK%token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK%token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK%token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK%type <node> ID_TK identifier name simple_name qualified_name type primitive_type reference_type array_type formal_parameter_list formal_parameter class_or_interface_type class_type interface_type%type <declarator> method_declarator%type <value> MODIFIER_TK%%/* 19.2 Production from 2.3: The Syntactic Grammar */goal: compilation_unit;/* 19.3 Productions from 3: Lexical structure */literal: INT_LIT_TK| FP_LIT_TK| BOOL_LIT_TK| CHAR_LIT_TK| STRING_LIT_TK| NULL_TK;/* 19.4 Productions from 4: Types, Values and Variables */type: primitive_type| reference_type;primitive_type: INTEGRAL_TK { /* use preset global here. FIXME */ $$ = xstrdup ("int"); }| FP_TK { /* use preset global here. FIXME */ $$ = xstrdup ("double"); }| BOOLEAN_TK { /* use preset global here. FIXME */ $$ = xstrdup ("boolean"); };reference_type: class_or_interface_type| array_type;class_or_interface_type: name;class_type: class_or_interface_type /* Default rule */;interface_type: class_or_interface_type;array_type: primitive_type OSB_TK CSB_TK| name OSB_TK CSB_TK { char *n = xmalloc (strlen ($1)+2); n [0] = '['; strcpy (n+1, $1); $$ = n; }| array_type OSB_TK CSB_TK { char *n = xmalloc (strlen ($1)+2); n [0] = '['; strcpy (n+1, $1); $$ = n; };/* 19.5 Productions from 6: Names */name: simple_name /* Default rule */| qualified_name /* Default rule */;simple_name: identifier /* Default rule */;qualified_name: name DOT_TK identifier { char *n = xmalloc (strlen ($1)+strlen ($3)+2); sprintf (n, "%s.%s", $1, $3); $$ = n; };identifier: ID_TK;/* 19.6: Production from 7: Packages */compilation_unit:| package_declaration| import_declarations| type_declarations| package_declaration import_declarations| package_declaration type_declarations| import_declarations type_declarations| package_declaration import_declarations type_declarations;import_declarations: import_declaration| import_declarations import_declaration;type_declarations: type_declaration| type_declarations type_declaration;package_declaration: PACKAGE_TK name SC_TK { package_name = $2; };import_declaration: single_type_import_declaration| type_import_on_demand_declaration;single_type_import_declaration: IMPORT_TK name SC_TK;type_import_on_demand_declaration: IMPORT_TK name DOT_TK MULT_TK SC_TK;type_declaration: class_declaration| interface_declaration| SC_TK;/* 19.7 Shortened from the original: modifiers: modifier | modifiers modifier modifier: any of public... */modifiers: MODIFIER_TK { if ($1 == PUBLIC_TK) modifier_value++; if ($1 == STATIC_TK) modifier_value++; USE_ABSORBER; } | modifiers MODIFIER_TK { if ($2 == PUBLIC_TK) modifier_value++; if ($2 == STATIC_TK) modifier_value++; USE_ABSORBER; } ;/* 19.8.1 Production from $8.1: Class Declaration */class_declaration: modifiers CLASS_TK identifier super interfaces { report_class_declaration($3); modifier_value = 0; } class_body| CLASS_TK identifier super interfaces { report_class_declaration($2); } class_body;super:| EXTENDS_TK class_type;interfaces:| IMPLEMENTS_TK interface_type_list;interface_type_list: interface_type { USE_ABSORBER; }| interface_type_list C_TK interface_type { USE_ABSORBER; };class_body: OCB_TK CCB_TK| OCB_TK class_body_declarations CCB_TK;class_body_declarations: class_body_declaration| class_body_declarations class_body_declaration;class_body_declaration: class_member_declaration| static_initializer| constructor_declaration| block /* Added, JDK1.1, instance initializer */;class_member_declaration: field_declaration| method_declaration| class_declaration /* Added, JDK1.1 inner classes */| interface_declaration /* Added, JDK1.1 inner classes */;/* 19.8.2 Productions from 8.3: Field Declarations */field_declaration: type variable_declarators SC_TK { USE_ABSORBER; }| modifiers type variable_declarators SC_TK { modifier_value = 0; };variable_declarators: /* Should we use build_decl_list () instead ? FIXME */ variable_declarator /* Default rule */| variable_declarators C_TK variable_declarator;variable_declarator: variable_declarator_id| variable_declarator_id ASSIGN_TK variable_initializer;variable_declarator_id: identifier { bracket_count = 0; USE_ABSORBER; }| variable_declarator_id OSB_TK CSB_TK { ++bracket_count; };variable_initializer: expression| array_initializer;/* 19.8.3 Productions from 8.4: Method Declarations */method_declaration: method_header method_body;method_header: type method_declarator throws { USE_ABSORBER; }| VOID_TK method_declarator throws| modifiers type method_declarator throws { modifier_value = 0; }| modifiers VOID_TK method_declarator throws { report_main_declaration ($3); modifier_value = 0; };method_declarator: identifier OP_TK CP_TK { struct method_declarator *d; NEW_METHOD_DECLARATOR (d, $1, NULL); $$ = d; }| identifier OP_TK formal_parameter_list CP_TK { struct method_declarator *d; NEW_METHOD_DECLARATOR (d, $1, $3); $$ = d; }| method_declarator OSB_TK CSB_TK;formal_parameter_list: formal_parameter| formal_parameter_list C_TK formal_parameter { char *n = xmalloc (strlen ($1)+strlen($3)+2); sprintf (n, "%s,%s", $1, $3); $$ = n; };formal_parameter: type variable_declarator_id { USE_ABSORBER; if (bracket_count) { int i; char *n = xmalloc (bracket_count + 1 + strlen ($$)); for (i = 0; i < bracket_count; ++i) n[i] = '['; strcpy (n + bracket_count, $$); $$ = n; } else $$ = $1; }| modifiers type variable_declarator_id /* Added, JDK1.1 final locals */ { if (bracket_count) { int i; char *n = xmalloc (bracket_count + 1 + strlen ($$)); for (i = 0; i < bracket_count; ++i) n[i] = '['; strcpy (n + bracket_count, $$); $$ = n; } else $$ = $2; };throws:| THROWS_TK class_type_list;class_type_list: class_type { USE_ABSORBER; }| class_type_list C_TK class_type { USE_ABSORBER; };method_body: block| block SC_TK| SC_TK;/* 19.8.4 Productions from 8.5: Static Initializers */static_initializer: static block| static block SC_TK /* Shouldn't be here. FIXME */;static: /* Test lval.sub_token here */ MODIFIER_TK { USE_ABSORBER; };/* 19.8.5 Productions from 8.6: Constructor Declarations *//* NOTE FOR FURTHER WORK ON CONSTRUCTORS: - If a forbidded modifier is found, the the error is either the use of a forbidded modifier for a constructor OR bogus attempt to declare a method without having specified the return type. FIXME */constructor_declaration: constructor_declarator throws constructor_body| modifiers constructor_declarator throws constructor_body { modifier_value = 0; }/* extra SC_TK, FIXME */| constructor_declarator throws constructor_body SC_TK/* extra SC_TK, FIXME */| modifiers constructor_declarator throws constructor_body SC_TK { modifier_value = 0; }/* I'm not happy with the SC_TK addition. It isn't in the grammer and should probably be matched by and empty statement. But it doesn't work. FIXME */;constructor_declarator: simple_name OP_TK CP_TK { USE_ABSORBER; }| simple_name OP_TK formal_parameter_list CP_TK { USE_ABSORBER; };constructor_body: OCB_TK CCB_TK| OCB_TK explicit_constructor_invocation CCB_TK| OCB_TK block_statements CCB_TK| OCB_TK explicit_constructor_invocation block_statements CCB_TK;/* Error recovery for that rule moved down expression_statement: rule. */explicit_constructor_invocation: this_or_super OP_TK CP_TK SC_TK| this_or_super OP_TK argument_list CP_TK SC_TK /* Added, JDK1.1 inner classes. Modified because the rule 'primary' couldn't work. */| name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK { USE_ABSORBER; }| name DOT_TK SUPER_TK OP_TK CP_TK SC_TK { USE_ABSORBER; };this_or_super: /* Added, simplifies error diagnostics */ THIS_TK| SUPER_TK;/* 19.9 Productions from 9: Interfaces *//* 19.9.1 Productions from 9.1: Interfaces Declarations */interface_declaration: INTERFACE_TK identifier interface_body { report_class_declaration ($2); modifier_value = 0; }| modifiers INTERFACE_TK identifier interface_body { report_class_declaration ($3); modifier_value = 0; }| INTERFACE_TK identifier extends_interfaces interface_body { report_class_declaration ($2); modifier_value = 0; }| modifiers INTERFACE_TK identifier extends_interfaces interface_body { report_class_declaration ($3); modifier_value = 0; };extends_interfaces: EXTENDS_TK interface_type| extends_interfaces C_TK interface_type;interface_body: OCB_TK CCB_TK| OCB_TK interface_member_declarations CCB_TK;interface_member_declarations: interface_member_declaration| interface_member_declarations interface_member_declaration;interface_member_declaration: constant_declaration
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -