📄 java.pars
字号:
/* Java Grammar ============ conforming to: The Java Language Specification - Version 1.1- builds an abstract syntax tree- the tree can be visualized if tcl + tk are available- The cpp variable SHARE in the parser controls, whether for end of list nodes (node types with the prefix 'no') only one node per node type is created or whether always separate nodes are allocated. '# define SHARE' selects "sharing" of nodes. This implies that instead of a tree a simple graph is constructed. This is more efficient in terms of runtime and memory consumption and it should work as long as only good-natured attribute computations are performed. If the syntax tree is to be visualized with the procedure DrawTree it is recommended not to define SHARE because pure trees are more pretty than simple graphs.*//* Ich, Doktor Josef Grosch, Informatiker, Apr. 1997 */PARSEREXPORT {# include <stdio.h># include "Idents.h"# define s_anonymous "<anonymous>"extern FILE * yyin;extern FILE * hlfp;extern int hl;extern char * current_file;extern tIdent i_anonymous;extern void start_parser ARGS ((char *fname, int parse_cplpl, FILE *highl_fp, int highlight));}GLOBAL {# include "stdlib.h"# include "StringM.h"# include "Tree.h"# include "Eval.h"# include "Trafo.h"# include "deftab.h"# include "paf.h"# define NIL (char *) NULLextern FILE * cross_ref_fp; FILE * yyin; FILE * hlfp; int hl; char * current_file; tIdent i_anonymous;static tString current_ident, current_class;static char current_string [256];static char ret_buffer [1024];static char arg_types_buffer [1024];static char args_buffer [1024];static unsigned long current_attr;static tTree current_type;# define SHARE# ifdef SHAREstatic tTree snocatch , snodecl , snoexpression , snoexpression_l , snofield , snoimport , snostatement , snoswitch , snotype , snotype_name ;# define dnocatch snocatch# define dnodecl snodecl# define dnoexpression snoexpression# define dnoexpression_l snoexpression_l# define dnofield snofield# define dnoimport snoimport# define dnostatement snostatement# define dnoswitch snoswitch# define dnotype snotype# define dnotype_name snotype_name# else# define dnocatch mnocatch ()# define dnodecl mnodecl ()# define dnoexpression mnoexpression (NoPosition)# define dnoexpression_l mnoexpression_l ()# define dnofield mnofield ()# define dnoimport mnoimport ()# define dnostatement mnostatement ()# define dnoswitch mnoswitch ()# define dnotype mnotype ()# define dnotype_name mnotype_name ()# endifstatic rbool is_initialized = rfalse;static rbool report = rfalse;static rbool graphic = rfalse;static void get_options ARGS ((void)){ char * arg = getenv ("PAF_JAVA"); if (arg == NULL) return; while (* arg) { if (* arg == '-') { switch (* ++ arg) { case 'f': report = rtrue; break; case 'g': graphic = rtrue; break; case 'h': (void) printf ("\nsyntax of PAF_JAVA:\n\n"); (void) printf (" [-f] [-h]\n\n"); (void) printf (" f : report error messages (default: do not report)\n"); (void) printf (" h : help\n\n"); break; } } arg ++; }}void start_parser (fname, parse_cplpl, highl_fp, highlight) char * fname; int parse_cplpl; FILE * highl_fp; int highlight;{ current_file = fname; hlfp = highl_fp; hl = highlight; if (! is_initialized) { get_options (); is_initialized = rtrue; } StoreMessages (rtrue); InitStringMemory (); InitIdents (); init_deftab (); if (fname) Attribute.Position.FileName = MakeIdent (fname, strlen (fname)); else Attribute.Position.FileName = NoIdent; BeginFile (""); (void) Parser (); /* (void) CheckTree (TreeRoot); */ if (cross_ref_fp != NULL) { Eval (TreeRoot); Traverse (TreeRoot); }# ifdef DRAWTREE if (graphic) DrawTree (TreeRoot);# endif ReleaseTreeModule (); CloseStringMemory (); if (report) WriteMessages (stderr);}static int class_stack_ptr = 0;static char * class_stack [32] = { NIL };# define push(ident) { class_stack [++ class_stack_ptr] = current_class; \ current_class = GetCStr (ident); }# define pop() { current_class = class_stack [class_stack_ptr --]; }# define top() class_stack [class_stack_ptr]static void get_bpos (tree, l, c) tTree tree; int * l; int * c;{ while (tree->Kind == kqualification) tree = tree->qualification.qualified_symbol; * l = tree->ident.pos.Line; * c = tree->ident.pos.Column - 1;}static void put_extends (tree) tTree tree;{ while (tree->Kind == ktype_name) { tTree symbol = tree->type_name.named_type->named_type.qualified_symbol; to_types (symbol, current_string); if (symbol->Kind == kident) put_symbol (PAF_CLASS_INHERIT, current_class, current_string, current_file, (int) symbol->ident.pos.Line, (int) symbol->ident.pos.Column - 1, (int) symbol->ident.pos.Line, (int) (symbol->ident.pos.Column + LengthSt (GetStringRef (symbol->ident.ident)) - 1), (unsigned long) PAF_PUBLIC, NIL, NIL, NIL, NIL, 0, 0, 0, 0); else { int l, c; get_bpos (symbol->qualification.qualified_symbol, & l, & c); put_symbol (PAF_CLASS_INHERIT, current_class, current_string, current_file, l, c, (int) symbol->qualification.pos.Line, (int) (symbol->qualification.pos.Column + LengthSt (GetStringRef (symbol->qualification.ident)) - 1), (unsigned long) PAF_PUBLIC, NIL, NIL, NIL, NIL, 0, 0, 0, 0); } tree = tree->type_name.next; }}}BEGIN { i_anonymous = MakeIdent (s_anonymous, strlen (s_anonymous));# ifdef SHARE snocatch = mnocatch (); snodecl = mnodecl (); snoexpression = mnoexpression (NoPosition); snoexpression_l = mnoexpression_l (); snofield = mnofield (); snoimport = mnoimport (); snostatement = mnostatement (); snoswitch = mnoswitch (); snotype = mnotype (); snotype_name = mnotype_name ();# endif}PREC RIGHT '>>=' '>>>=' '<<=' '+=' '-=' '*=' '/=' '%=' '&=' '^=' '|=' '=' RIGHT '?' ':' LEFT '||' LEFT '&&' LEFT '|' LEFT '^' LEFT '&' LEFT '==' '!=' LEFT '<=' '>=' instanceof '<' '>' LEFT '>>' '>>>' '<<' LEFT '+' '-' LEFT '*' '/' '%' NONE CAST NONE '++' '--' '!' '~' UNARY_OP NONE POST_OP NONE ')'PROPERTY INPUTRULEGoal = CompilationUnit .Type = < = PrimitiveType . = ReferenceType . = void { tree := msimple_type (t_void ); } .> .PrimitiveType = < NumericType = < IntegralType = < = byte { tree := msimple_type (t_byte ); } . = short { tree := msimple_type (t_short ); } . = int { tree := msimple_type (t_int ); } . = long { tree := msimple_type (t_long ); } . = char { tree := msimple_type (t_char ); } . > . FloatingPointType = < = float { tree := msimple_type (t_float ); } . = double { tree := msimple_type (t_double); } . > . > . = boolean { tree := msimple_type (t_boolean); } .> .ReferenceType = < = ClassOrInterfaceType . = ArrayType .> .ClassOrInterfaceType = Name { tree := mnamed_type (Name:tree); } .ClassType = ClassOrInterfaceType .InterfaceType = ClassOrInterfaceType .ArrayType = < = PrimitiveType '[' ']' { tree := marray_type (PrimitiveType:tree); } . = Name '[' ']' { tree := marray_type (mnamed_type (Name:tree)); } . = ArrayType '[' ']' { tree := marray_type (ArrayType:tree); } .> .Name = < SimpleName = Identifier { tree := mident (Identifier:Position, Identifier:ident); bpos := Identifier:Position; epos := { epos.Line = bpos.Line; epos.Column = bpos.Column + LengthSt (GetStringRef (Identifier:ident)) - 1; }; } . QualifiedName= Name '.' Identifier { tree := mqualification (Identifier:Position, Name:tree, Identifier:ident); bpos := Name:bpos; epos := { epos.Line = Identifier:Position.Line; epos.Column = Identifier:Position.Column + LengthSt (GetStringRef (Identifier:ident)) - 1; }; } .> .CompilationUnit = PackageDeclaration ImportDeclarations TypeDeclarations { => { TreeRoot = mcompilation_unit (PackageDeclaration:tree, ReverseTree (ImportDeclarations:tree), ReverseTree (TypeDeclarations:tree)); }; } .ImportDeclarations = < = { tree := dnoimport; } . = ImportDeclarations ImportDeclaration { tree := { tree = ImportDeclaration:tree; tree->import.next = ImportDeclarations:tree; }; } .> .TypeDeclarations = < = { tree := dnofield; } . = TypeDeclarations TypeDeclaration { tree := { tree = TypeDeclaration:tree; tree->field.next = TypeDeclarations:tree; }; } . = TypeDeclarations ';' .> .PackageDeclaration = < = { tree := dnoexpression; } . = package Name ';' .> .ImportDeclaration = < SingleTypeImportDeclaration = import Name ';' { => { if (hl >= 0) { to_files (Name:tree, current_string); put_symbol (PAF_INCLUDE_DEF, NIL, current_string, current_file, (int) Name:bpos.Line, (int) Name:bpos.Column - 1, (int) Name:epos.Line, (int) Name:epos.Column, 0L, NIL, NIL, NIL, NIL, 0, 0, 0, 0); } }; tree := mimport (NoTree, Name:tree); } . TypeImportOnDemandDeclaration = import Name '.' '*' ';' { => { if (hl >= 0) { to_files (Name:tree, current_string); (void) strcat (current_string, "/*"); put_symbol (PAF_INCLUDE_DEF, NIL, current_string, current_file, (int) Name:bpos.Line, (int) Name:bpos.Column - 1, (int) Name:epos.Line, (int) Name:epos.Column, 0L, NIL, NIL, NIL, NIL, 0, 0, 0, 0); } }; tree := mimport_asterisk (NoTree, Name:tree); } .> .TypeDeclaration = < = ClassDeclaration . = InterfaceDeclaration . /* = ';' . */> .Modifiers = < = Modifier . = Modifiers Modifier { attr := Modifiers:attr | Modifier:attr; } .> .Modifier = < = public { attr := PAF_PUBLIC ; } . = protected { attr := PAF_PROTECTED ; } . = private { attr := PAF_PRIVATE ; } . = static { attr := PAF_STATIC ; } . = abstract { attr := PAF_ABSTRACT ; } . = final { attr := PAF_FINAL ; } . = native { attr := PAF_NATIVE ; } . = synchronized { attr := PAF_SYNCHRONIZED ; } . = transient { attr := PAF_TRANSIENT ; } . = volatile { attr := PAF_VOLATILE ; } . /* = threadsafe { attr := PAF_THREADSAFE ; } . = const { attr := PAF_CONST ; } . */> .ClassDeclaration = < = class Identifier { => if (hl >= 0) push (Identifier:ident); } Super Interfaces ClassBody { => { if (hl >= 0) { current_attr = PAF_PRIVATE; put_symbol (PAF_CLASS_DEF, top (), current_class, current_file, (int) Identifier:Position.Line, (int) Identifier:Position.Column - 1, (int) ClassBody:epos.Line, (int) ClassBody:epos.Column, current_attr, NIL, NIL, NIL, NIL, 0, 0, 0, 0); pop (); } }; tree := mclass (NoTree, 0L, Identifier:ident, Identifier:Position, ClassBody:tree, Super:tree, Interfaces:tree); } . = Modifiers class Identifier { => if (hl >= 0) push (Identifier:ident); } Super Interfaces ClassBody { => { if (hl >= 0) { current_attr = Modifiers:attr; if ((current_attr & (PAF_PRIVATE | PAF_PROTECTED | PAF_PUBLIC)) == 0L) current_attr |= PAF_PRIVATE; put_symbol (PAF_CLASS_DEF, top (), current_class, current_file, (int) Identifier:Position.Line, (int) Identifier:Position.Column - 1, (int) ClassBody:epos.Line, (int) ClassBody:epos.Column, current_attr, NIL, NIL, NIL, NIL, 0, 0, 0, 0); pop (); } }; tree := mclass (NoTree, Modifiers:attr, Identifier:ident, Identifier:Position, ClassBody:tree, Super:tree, Interfaces:tree); } .> .Super = < = { tree := dnotype; } . = extends ClassType { => { if (hl >= 0) { to_types (ClassType:tree, current_string); put_symbol (PAF_CLASS_INHERIT, current_class, current_string, current_file, (int) ClassType:bpos.Line, (int) ClassType:bpos.Column - 1, (int) ClassType:epos.Line, (int) ClassType:epos.Column, (unsigned long) PAF_PUBLIC, NIL, NIL, NIL, NIL, 0, 0, 0, 0); } }; } .> .Interfaces = < = { tree := dnotype_name; } . = implements InterfaceTypeList { tree := ReverseTree (InterfaceTypeList:tree); } .> .InterfaceTypeList = < = InterfaceType { tree := mtype_name (dnotype_name, InterfaceType:tree); } . = InterfaceTypeList ',' InterfaceType { tree := mtype_name (InterfaceTypeList:tree, InterfaceType:tree); } .> .ClassBody = '{' ClassBodyDeclarations '}' { tree := ReverseTree (ClassBodyDeclarations:tree); epos := '}':Position; } .ClassBodyDeclarations = < = { tree := mnofield (); } . = ClassBodyDeclarations ClassBodyDeclaration
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -