{ /* Java syntax transcribed from The Java Language Specification, Gosling, Joy and Steele, Copyright 1996, Sun Microsystems, Inc. Transcription and instrumentation, Copyright 1996 Parsifal Software Disclaimer: This syntax may not accurately represent The Java Language Specification. Use of this syntax may be subject to the license agreement printed therein. Not included in this file: Unicode prescan Lexer Class definitions for Abstract Syntax Tree "Java" is a trademark of Sun Microsystems, Inc. */ #include "javadefs.h" #include "tfile.h" } [ event driven input values context type = location ~declare pcb line numbers ~allow macros error trace parser stack size = 100 parser name = Parser parser file name = "parser.cpp" header file name = "parser.h" default input type = JavaNode * default token type = JavaNode * enum { eof = 0, BYTE, BOOLEAN, SHORT, INT, LONG, CHAR, FLOAT, DOUBLE, PACKAGE, IMPORT, PUBLIC, PROTECTED, PRIVATE, STATIC, FINAL, TRANSIENT, VOLATILE, ABSTRACT, NATIVE, SYNCHRONIZED, CLASS, EXTENDS, IMPLEMENTS, THROWS, VOID, THIS, SUPER, INTERFACE, IF = 64, ELSE, SWITCH, CASE, DEFAULT, WHILE, DO, FOR, BREAK, CONTINUE, RETURN, THROW, TRY, CATCH, FINALLY, NEW, INSTANCEOF, IDENTIFIER, INTEGER_LITERAL, FLOATING_POINT_LITERAL, BOOLEAN_LITERAL, CHARACTER_LITERAL, STRING_LITERAL, NULL_LITERAL, EQEQ = 96, LE, GE, NE, ANDAND, OROR, PLUSPLUS, MINUSMINUS, LTLT, GTGT, GTGTGT, PLUSEQ, MINUSEQ, STAREQ, SLASHEQ, ANDEQ, IOREQ, XOREQ, REMEQ, LSEQ, RSEQ, LRSEQ, } ] LEFTPAREN = '(' RIGHTPAREN = ')' LEFTBRACKET = '[' RIGHTBRACKET = ']' LEFTBRACE = '{' RIGHTBRACE = '}' SEMICOLON = ';' COMMA = ',' DOT = '.' EQ = '=' GT = '>' LT = '<' BANG = '!' TILDE = '~' QM = '?' COLON = ':' PLUS = '+' MINUS = '-' STAR = '*' SLASH = '/' AND = '&' IOR = '|' XOR = '^' REM = '%' literal -> INTEGER_LITERAL -> FLOATING_POINT_LITERAL -> BOOLEAN_LITERAL -> CHARACTER_LITERAL -> STRING_LITERAL -> NULL_LITERAL type -> primitive type:t =new PrimitiveType(t); -> reference type (TypeEnum) primitive type -> numeric type -> BOOLEAN =BooleanType; (TypeEnum) numeric type -> integral type -> floating point type (TypeEnum) integral type -> BYTE =ByteType; -> SHORT =ShortType; -> INT =IntType; -> LONG =LongType; -> CHAR =CharType; (TypeEnum) floating point type -> FLOAT =FloatType; -> DOUBLE =DoubleType; reference type -> class or interface type -> array type class or interface type -> name:n =new ClassOrInterfaceType(n); class type -> class or interface type:t =new ClassType(t); interface type -> class or interface type:t =new InterfaceType(t); array type -> primitive type:t, LEFTBRACKET, RIGHTBRACKET =new ArrayTypeA(t); -> name:n, LEFTBRACKET, RIGHTBRACKET =new ArrayTypeB(n); -> array type:t, LEFTBRACKET, RIGHTBRACKET =new ArrayTypeC(t); name -> simple name -> qualified name simple name -> IDENTIFIER:i =new SimpleName(i); qualified name -> name:n, DOT, IDENTIFIER:i =new QualifiedName(n,i); compilation unit $ -> package declaration:p, import declarations:i, type declarations:t, eof =new CompilationUnit(p,i,t); import declarations -> =new ImportDeclarationsA; -> import declarations:l, import declaration:s =new ImportDeclarationsB(l,s); type declarations -> =new TypeDeclarationsA; -> type declarations:t, type declaration:d =new TypeDeclarationsB(t,d); package declaration -> =new PackageA; -> PACKAGE, name:n, SEMICOLON =new PackageB(n); import declaration -> IMPORT, name:n, SEMICOLON =new ImportDeclarationA(n); -> IMPORT, name:n, DOT, STAR, SEMICOLON =new ImportDeclarationB(n); type declaration -> class declaration -> interface declaration -> SEMICOLON =new EmptyTypeDeclaration; (int) modifiers -> modifier:m =m; -> modifiers:ms, modifier:m =ms|m; (ModifierEnum) modifier -> PUBLIC =PublicModifier; -> PROTECTED =ProtectedModifier; -> PRIVATE =PrivateModifier; -> STATIC =StaticModifier; -> FINAL =FinalModifier; -> TRANSIENT =TransientModifier; -> VOLATILE =VolatileModifier; -> ABSTRACT =AbstractModifier; -> NATIVE =NativeModifier; -> SYNCHRONIZED =SynchronizedModifier; class declaration -> CLASS, IDENTIFIER:n, super:s, interfaces:i, class body:b =new ClassDeclarationA(n,s,i,b); -> modifiers:ms, CLASS, IDENTIFIER:n, super:s, interfaces:i, class body:b =new ClassDeclarationB(ms,n,s,i,b); super -> =new SuperA; -> EXTENDS, class type:n =new SuperB(n); interfaces -> =new InterfacesA; -> IMPLEMENTS, interface type list:l =l; interface type list -> interface type:t =new InterfacesB(t); -> interface type list:l, interface type:t =new InterfacesC(l,t); class body -> LEFTBRACE, class body declarations:f, RIGHTBRACE =new ClassBody(f); class body declarations -> =new ClassBodyDeclarationsA; -> class body declarations:l, class body declaration:d =new ClassBodyDeclarationsB(l,d); class body declaration -> class member declaration -> static initializer -> constructor declaration class member declaration -> field declaration -> method declaration field declaration -> type:t, variable declarators:d, SEMICOLON =new FieldDeclarationA(t,d); -> modifiers:m, type:t, variable declarators:d, SEMICOLON =new FieldDeclarationB(m,t,d); variable declarators -> variable declarator:v =new VariableDeclaratorsA(v); -> variable declarators:l, COMMA, variable declarator:v =new VariableDeclaratorsB(l,v); variable declarator -> variable declarator id:n =new VariableDeclaratorA(n); -> variable declarator id:n, EQ, variable initializer:i =new VariableDeclaratorB(n,i); variable declarator id -> IDENTIFIER:i =new VariableDeclaratorIDA(i); -> variable declarator id:n, LEFTBRACKET, RIGHTBRACKET =new VariableDeclaratorIDB(n); variable initializer -> expression:x =new VariableInitializerA(x); -> array initializer:x =new VariableInitializerB(x); method declaration -> method header:h, method body:b =new MethodDeclaration(h,b); method header -> type:t, method declarator:d, throws:th =new MethodHeaderA(t,d,th); -> modifiers:m, type:t, method declarator:d, throws:th =new MethodHeaderB(m,t,d,th); -> VOID, method declarator:d, throws:th =new MethodHeaderC(d,th); -> modifiers:m, VOID, method declarator:d, throws:th =new MethodHeaderD(m,d,th); method declarator -> IDENTIFIER:n, LEFTPAREN, formal parameters:p, RIGHTPAREN =new MethodDeclaratorA(n, p); -> method declarator:m, LEFTBRACKET, RIGHTBRACKET =new MethodDeclaratorB(m); formal parameters -> =new FormalParametersA; -> formal parameter list formal parameter list -> formal parameter:p =new FormalParametersB(p); -> formal parameter list:l, COMMA, formal parameter:p =new FormalParametersC(l,p); formal parameter -> type:t, variable declarator id:n =new FormalParameter(t,n); throws -> =new ThrowsA; -> THROWS, class type list:l =new ThrowsB(l); class type list -> class type:t =new ClassTypeListA(t); -> class type list:l, COMMA, class type:t =new ClassTypeListB(l,t); method body -> block -> SEMICOLON =new EmptyBlock; static initializer -> STATIC, block:b =new StaticInitializer(b); constructor declaration -> constructor declarator:d, throws:t, constructor body:b =new ConstructorDeclarationA(d,t,b); -> modifiers:m, constructor declarator:d, throws:t, constructor body:b =new ConstructorDeclarationB(m, d,t,b); constructor declarator -> simple name:n, LEFTPAREN, formal parameters:p, RIGHTPAREN =new ConstructorDeclarator(n,p); constructor body -> LEFTBRACE, explicit constructor invocation:c, optional block statements:b, RIGHTBRACE =new ConstructorBodyA(c,b); -> LEFTBRACE, optional block statements:b, RIGHTBRACE =new ConstructorBodyB(b); explicit constructor invocation -> THIS, LEFTPAREN, optional argument list:a, RIGHTPAREN, SEMICOLON =new ConstructorCallA(a); -> SUPER, LEFTPAREN, optional argument list:a, RIGHTPAREN, SEMICOLON =new ConstructorCallB(a); interface declaration -> INTERFACE, IDENTIFIER:i, extends interfaces:x, interface body:b =new InterfaceDeclarationA(i,x,b); -> modifiers:m, INTERFACE, IDENTIFIER:i, extends interfaces:x, interface body:b =new InterfaceDeclarationB(m,i,x,b); extends interfaces -> =new ExtendsInterfacesA; -> EXTENDS, extends interface list:l =l; extends interface list -> interface type:t =new ExtendsInterfacesB(t); -> extends interface list:l, interface type:t =new ExtendsInterfacesC(l,t); interface body -> LEFTBRACE, interface member declarations:f, RIGHTBRACE =new InterfaceBody(f); interface member declarations -> =new InterfaceMemberDeclarationsA; -> interface member declaration list interface member declaration list -> interface member declaration:d =new InterfaceMemberDeclarationsB(d); -> interface member declaration list:l, interface member declaration:d =new InterfaceMemberDeclarationsC(l,d); interface member declaration -> constant declaration -> abstract method declaration constant declaration -> field declaration:d =new ConstantDeclaration(d); abstract method declaration -> method header, SEMICOLON array initializer -> LEFTBRACE, variable initializers:i, RIGHTBRACE =new ArrayInitializer(i); variable initializers -> COMMA? =new VariableInitializersA; -> variable initializer list, COMMA? variable initializer list -> variable initializer:i =new VariableInitializersB(i); -> variable initializer list:l, COMMA, variable initializer:i =new VariableInitializersC(l,i); block -> LEFTBRACE, optional block statements:b, RIGHTBRACE =b; optional block statements -> =new StatementsA; -> block statements block statements -> block statement:s =new StatementsB(s); -> block statements:b, block statement:s =new StatementsC(b,s); block statement -> local variable declaration, SEMICOLON -> statement local variable declaration -> type:t, variable declarators:d =new LocalDeclaration(t,d); statement -> statement without trailing substatement -> labeled statement -> if then statement -> if then else statement -> while statement -> for statement statement no short if -> statement without trailing substatement -> labeled statement no short if -> if then else statement no short if -> while statement no short if -> for statement no short if statement without trailing substatement -> block -> empty statement -> expression statement -> switch statement -> do statement -> break statement -> continue statement -> return statement -> synchronized statement -> throw statement -> try statement empty statement -> SEMICOLON // empty statement labeled statement -> IDENTIFIER:i, COLON, statement:s =new LabeledStatement(i,s); labeled statement no short if -> IDENTIFIER:i, COLON, statement no short if:s =new LabeledStatement(i,s); expression statement -> statement expression:x, SEMICOLON =new ExpressionStatement(x); statement expression -> assignment -> preincrement expression -> predecrement expression -> postincrement expression -> postdecrement expression -> method invocation -> class instance creation expression if then statement -> IF, LEFTPAREN, expression:x, RIGHTPAREN, statement:s =new IfThenStatement(x,s); if then else statement -> IF, LEFTPAREN, expression:x, RIGHTPAREN, statement no short if:s1, ELSE, statement:s2 =new IfThenElseStatement(x,s1,s2); if then else statement no short if -> IF, LEFTPAREN, expression:x, RIGHTPAREN, statement no short if:s1, ELSE, statement no short if:s2 =new IfThenElseStatement(x,s1,s2); switch statement -> SWITCH, LEFTPAREN, expression:x, RIGHTPAREN, switch block:b =new SwitchStatement(x,b); switch block -> LEFTBRACE, switch block statement groups:g, optional switch labels:l, RIGHTBRACE =new SwitchBlock(g,l); switch block statement groups -> =new SwitchBlockGroupsA; -> switch block statement groups:l, switch block statement group:g =new SwitchBlockGroupsB(l,g); switch block statement group -> switch labels:l, block statements:s =new SwitchBlockGroup(l,s); optional switch labels -> =new SwitchLabelsA; -> switch labels switch labels -> switch label:l =new SwitchLabelsB(l); -> switch labels:s, switch label:l =new SwitchLabelsC(s,l); switch label -> CASE, constant expression:x, COLON =new SwitchLabelA(x); -> DEFAULT, COLON =new SwitchLabelB; while statement -> WHILE, LEFTPAREN, expression:x, RIGHTPAREN, statement:s =new WhileStatement(x,s); while statement no short if -> WHILE, LEFTPAREN, expression:x, RIGHTPAREN, statement no short if:s =new WhileStatement(x,s); do statement -> DO, statement:s, WHILE, LEFTPAREN, expression:x, RIGHTPAREN, SEMICOLON =new DoWhileStatement(s,x); for statement -> FOR, LEFTPAREN, for init:i, SEMICOLON, for condition:c, SEMICOLON, for update:u,RIGHTPAREN, statement:s =new ForStatement(i,c,u,s); for statement no short if -> FOR, LEFTPAREN, for init:i, SEMICOLON, for condition:c, SEMICOLON, for update:u,RIGHTPAREN, statement no short if:s =new ForStatement(i,c,u,s); for init -> =new ForInitA; -> statement expression list:x =new ForInitB(x); -> local variable declaration:d =new ForInitC(d); for condition -> =new ForConditionA; -> expression:x =new ForConditionB(x); for update -> =new ForUpdateA; -> statement expression list:s =new ForUpdateB(s); statement expression list -> statement expression:s =new ExpressionListA(s); -> statement expression list:l, COMMA, statement expression:s =new ExpressionListB(l,s); break statement -> BREAK, SEMICOLON =new BreakStatementA; -> BREAK, IDENTIFIER:i, SEMICOLON =new BreakStatementB(i); continue statement -> CONTINUE, SEMICOLON =new ContinueStatementA; -> CONTINUE, IDENTIFIER:i, SEMICOLON =new ContinueStatementB(i); return statement -> RETURN, SEMICOLON =new ReturnStatementA; -> RETURN, expression:x, SEMICOLON =new ReturnStatementB(x); throw statement -> THROW, SEMICOLON =new ThrowStatementA; -> THROW, expression:x, SEMICOLON =new ThrowStatementB(x); synchronized statement -> SYNCHRONIZED, LEFTPAREN, expression:x, RIGHTPAREN, block:b =new SynchronizedStatement(x,b); try statement -> TRY, block:b, catches:c =new TryStatementA(b,c); -> TRY, block:b, optional catches:c, finally:f =new TryStatementB(b,c,f); optional catches -> =new CatchesA; -> catches catches -> catch clause:c =new CatchesB(c); -> catches:l, catch clause:c =new CatchesC(l,c); catch clause -> CATCH, LEFTPAREN, formal parameter:p, RIGHTPAREN, block:b =new Catch(p,b); finally -> FINALLY, block:b =new Finally(b); primary -> primary no new array -> array creation expression primary no new array -> literal -> THIS =new This; -> LEFTPAREN, expression:x, RIGHTPAREN =x; -> class instance creation expression -> field access -> method invocation -> array access class instance creation expression -> NEW, class type:t, LEFTPAREN, optional argument list:l, RIGHTPAREN =new ClassInstance(t,l); optional argument list -> =new ArgumentsA; -> argument list argument list -> expression:x =new ArgumentsB(x); -> argument list:l, COMMA, expression:x =new ArgumentsC(l,x); array creation expression -> NEW, primitive type:t, dim exprs:x, optional dims:n =new ArrayExpressionA(t,x,n); -> NEW, class or interface type:t, dim exprs:x, optional dims:n =new ArrayExpressionB(t,x,n); dim exprs -> dim expr:x =new DimExprsA(x); -> dim exprs:l, dim expr:x =new DimExprsB(l,x); dim expr -> LEFTBRACKET, expression:x, RIGHTBRACKET =x; (int) optional dims -> =0; -> dims (int) dims -> LEFTBRACKET, RIGHTBRACKET =1; -> dims:d, LEFTBRACKET, RIGHTBRACKET =d+1; field access -> primary:x, DOT, IDENTIFIER:i =new FieldAccessA(x,i); -> SUPER,DOT, IDENTIFIER:i =new FieldAccessB(i); method invocation -> name:n, LEFTPAREN, optional argument list:l, RIGHTPAREN =new MethodInvocationA(n,l); -> primary:x, DOT, IDENTIFIER:i, LEFTPAREN, optional argument list:l, RIGHTPAREN =new MethodInvocationB(x,i,l); -> SUPER, DOT, IDENTIFIER, LEFTPAREN, optional argument list:l, RIGHTPAREN =new MethodInvocationC(l); array access -> name:n, LEFTBRACKET, expression:x, RIGHTBRACKET =new ArrayAccessA(n,x); -> primary no new array:p, LEFTBRACKET, expression:x, RIGHTBRACKET =new ArrayAccessB(p,x); postfix expression -> primary -> name -> postincrement expression -> postdecrement expression postincrement expression -> postfix expression:x, PLUSPLUS =new PostIncrement(x); postdecrement expression -> postfix expression:x, MINUSMINUS =new PostDecrement(x); unary expression -> preincrement expression -> predecrement expression -> PLUS, unary expression:x =new UnaryPlus(x); -> MINUS, unary expression:x =new UnaryMinus(x); -> unary expression not plus minus preincrement expression -> PLUSPLUS, unary expression:x =new PreIncrement(x); predecrement expression -> MINUSMINUS, unary expression:x =new PreDecrement(x); unary expression not plus minus -> postfix expression -> TILDE, unary expression:x =new Complement(x); -> BANG, unary expression:x =new Not(x); -> cast expression cast expression -> LEFTPAREN, primitive type:t, optional dims:n, RIGHTPAREN, unary expression:x =new CastA(t,n,x); -> LEFTPAREN, expression:n, RIGHTPAREN, unary expression not plus minus:x =new CastB(n,x); -> LEFTPAREN, name:n, dims:k, RIGHTPAREN, unary expression not plus minus:x =new CastC(n,k,x); multiplicative expression -> unary expression -> multiplicative expression:x, STAR, unary expression:y =new Multiply(x,y); -> multiplicative expression:x, SLASH, unary expression:y =new Divide(x,y); -> multiplicative expression:x, REM, unary expression:y =new Remainder(x,y); additive expression -> multiplicative expression -> additive expression:x, PLUS, multiplicative expression:y =new Add(x,y); -> additive expression:x, MINUS, multiplicative expression:y =new Sub(x,y); shift expression -> additive expression -> shift expression:x, LTLT, additive expression:y =new LeftShift(x,y); -> shift expression:x, GTGT, additive expression:y =new RightShift(x,y); -> shift expression:x, GTGTGT, additive expression:y =new LogRightShift(x,y); relational expression -> shift expression -> relational expression:x, LT, shift expression:y =new LessThan(x,y); -> relational expression:x, GT, shift expression:y =new GreaterThan(x,y); -> relational expression:x, LE, shift expression:y =new LessEq(x,y); -> relational expression:x, GE, shift expression:y =new GreaterEq(x,y); -> relational expression:x, INSTANCEOF, reference type:r =new InstanceOf(x,r); equality expression -> relational expression -> equality expression:x, EQEQ, relational expression:y =new Equal(x,y); -> equality expression:x, NE, relational expression:y =new NotEqual(x,y); and expression -> equality expression -> and expression:x, AND, equality expression:y =new AndExpression(x,y); exclusive or expression -> and expression -> exclusive or expression:x, XOR, and expression:y =new XorExpression(x,y); inclusive or expression -> exclusive or expression -> inclusive or expression:x, IOR, exclusive or expression:y =new IorExpression(x,y); conditional and expression -> inclusive or expression -> conditional and expression:x, ANDAND, inclusive or expression:y =new CondAndExpression(x,y); conditional or expression -> conditional and expression -> conditional or expression:x, OROR, conditional and expression:y =new CondOrExpression(x,y); conditional expression -> conditional or expression -> conditional or expression:c, QM, expression:x, COLON, conditional expression:y =new ConditionalExpression(c,x,y); assignment expression -> conditional expression -> assignment assignment -> left hand side:x, assignment operator:op, assignment expression:y =op->operands(x,y); left hand side -> name -> field access -> array access (BinaryExpression *) assignment operator -> EQ =new Assign; -> STAREQ =new MulAssign; -> SLASHEQ =new DivAssign; -> REMEQ =new RemAssign; -> PLUSEQ =new AddAssign; -> MINUSEQ =new SubAssign; -> LSEQ =new LSAssign; -> RSEQ =new RSAssign; -> LRSEQ =new LRSAssign; -> ANDEQ =new AndAssign; -> XOREQ =new XorAssign; -> IOREQ =new IorAssign; expression -> assignment expression constant expression -> expression { #define PCB (*Parser_pcb) Parser_pcb_type *Parser_pcb; #define SYNTAX_ERROR fprintf(stderr,"%s, line %d, column %d\n", \ (PCB).error_message, PCB.input_context.line, PCB.input_context.column) JavaParser::JavaParser() { pcb = new Parser_pcb_type; reset(); } JavaParser::~JavaParser() { delete pcb; } void JavaParser::reset(void) { Parser_pcb = pcb; init_Parser(); } JavaParser &JavaParser::put(ParserInputCode c, JavaNode *n, location l) { Parser_pcb = pcb; PCB.input_code = c; PCB.input_value = n; PCB.input_context = l; Parser(); return *this; } JavaParser &JavaParser::put(ParserInputCode c, location l) { Parser_pcb = pcb; PCB.input_code = c; PCB.input_value = NULL; PCB.input_context = l; Parser(); return *this; } JavaParser &JavaParser::put(int c, location l) { Parser_pcb = pcb; PCB.input_code = c; PCB.input_value = NULL; PCB.input_context = l; Parser(); return *this; } JavaParser &JavaParser::put(UnicodeChar *, location) { return *this; } void JavaParser::close(void) { put(eof, PCB.input_context); } void JavaParser::error(void) { PCB.exit_flag = AG_SYNTAX_ERROR_CODE; } class Buffer : public JavaLexer::string_buffer_connector { UnicodeChar *data; int size, n; public: Buffer(int s) : size(s){ data = new UnicodeChar[s+1]; n = 0;} ~Buffer() { delete [] data;} virtual JavaLexer::string_buffer_connector &reset() {n = 0; return *this;} virtual JavaLexer::string_buffer_connector &append(UnicodeChar c) { if (n < size) data[n++] = c; return *this; } virtual UnicodeChar *string() { data[n] = 0; return data;} }; Buffer buffer(1000); JavaParser javaParser; JavaLexer javaLexer(javaParser, buffer); JavaPrescan javaPrescan(javaLexer); int main(int argc, char *argv[]) { if (argc < 2) { printf("Usage:\n"); exit(1); } text_file input(argv[1]); char *ptr = input.GetText(); while (PCB.exit_flag == AG_RUNNING_CODE && *ptr) { javaPrescan.put(*ptr++); } if (*ptr == 0) javaPrescan.close(); return 0; } }
Link to: Home page | Trial Copy | Syntax Directed Parsing | Glossary