📄 cppparser.java
字号:
/* Generated By:JavaCC: Do not edit this line. CPPParser.java */package net.sourceforge.pmd.cpd.cppast;public final class CPPParser implements CPPParserConstants { private static String vers = "0.1"; private static String id = "C++ Parser"; private static void msg(String s) { System.out.println(id + " Version " + vers +": " + s); } public static void main(String args[]) { CPPParser parser; java.io.InputStream input; int ai = 0; if (ai == (args.length-1)) { msg("Reading from file " + args[ai] + " . . ."); try { input = new java.io.FileInputStream(args[ai]); } catch (java.io.FileNotFoundException e) { msg("File " + args[0] + " not found."); return; } } else if (ai >= args.length) { msg("Reading from standard input . . ."); input = System.in; } else { msg("Usage: java " + id + " [-d] [inputfile]"); return; } try { parser = new CPPParser(input); parser.translation_unit(); msg("Program parsed successfully."); } catch (ParseException e) { msg("Encountered errors during parse."); } } /** * A symbol table manager object. Currently only types are recorded for * doing semantic predicates for parsing. */ static SymtabManager sym; /* * Methods used in semantics predicates. */ /** * Reads a fully qualified name (since it is used during lookahead, we * cannot use token. We have to explicitly use getToken). */ static String GetFullyScopedName() throws ParseException { Token t = getToken(1); if (t.kind != ID && t.kind != SCOPE) return null; StringBuffer s = new StringBuffer(); int i; if (t.kind != SCOPE) { s.append(t.image); t = getToken(2); i = 3; } else i = 2; while (t.kind == SCOPE) { s.append(t.image); s.append((t = getToken(i++)).image); t = getToken(i++); } return s.toString(); } /** * This method first tries to read a sequence of tokens of the form * ("::")? <ID> ("::" <ID>)* * and if it succeeds then asks the symbol table manager if this is * the name of a constructor. */ static boolean IsCtor() throws ParseException { return sym.IsCtor(GetFullyScopedName()); } static final public void translation_unit() throws ParseException { sym.OpenScope(null, false); label_1: while (true) { if (jj_2_1(2)) { ; } else { break label_1; } external_declaration(); } jj_consume_token(0); sym.CloseScope(); } static final public void external_declaration() throws ParseException { boolean isTypedef = false; if (jj_2_5(2147483647)) { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case TEMPLATE: template_head(); break; default: jj_la1[0] = jj_gen; ; } declaration(); } else if (jj_2_6(2147483647)) { enum_specifier(); switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case LPARENTHESIS: case SCOPE: case AMPERSAND: case STAR: case TILDE: case OPERATOR: case ID: init_declarator_list(false); break; default: jj_la1[1] = jj_gen; ; } jj_consume_token(SEMICOLON); } else if (jj_2_7(2147483647)) { dtor_definition(); } else if (jj_2_8(2147483647)) { ctor_definition(); } else if (jj_2_9(2147483647)) { function_definition(); } else if (jj_2_10(2147483647)) { conversion_function_decl_or_def(); } else { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case TEMPLATE: template_head(); if (jj_2_2(2147483647)) { ctor_definition(); } else if (jj_2_3(2147483647)) { function_definition(); } else if (jj_2_4(1)) { isTypedef = declaration_specifiers(); switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case LPARENTHESIS: case SCOPE: case AMPERSAND: case STAR: case TILDE: case OPERATOR: case ID: init_declarator_list(isTypedef); break; default: jj_la1[2] = jj_gen; ; } jj_consume_token(SEMICOLON); } else { jj_consume_token(-1); throw new ParseException(); } break; default: jj_la1[3] = jj_gen; if (jj_2_11(1)) { declaration(); } else { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case SEMICOLON: jj_consume_token(SEMICOLON); break; default: jj_la1[4] = jj_gen; jj_consume_token(-1); throw new ParseException(); } } } } } static final public void function_definition() throws ParseException { Scope sc = null; boolean isTypedef; if (jj_2_12(3)) { isTypedef = declaration_specifiers(); sc = function_declarator(isTypedef); func_decl_def(sc); } else { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case SCOPE: case AMPERSAND: case STAR: case OPERATOR: case ID: sc = function_declarator(false); func_decl_def(sc); break; default: jj_la1[5] = jj_gen; jj_consume_token(-1); throw new ParseException(); } } } static final public void func_decl_def(Scope sc) throws ParseException { boolean closeReqd = false; if (closeReqd = (sc != null && sc != sym.GetCurScope())) sym.OpenScope(sc); switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case SEMICOLON: jj_consume_token(SEMICOLON); break; case LCURLYBRACE: compound_statement(); break; default: jj_la1[6] = jj_gen; jj_consume_token(-1); throw new ParseException(); } if (closeReqd) sym.CloseScope(); } static final public void linkage_specification() throws ParseException { jj_consume_token(EXTERN); jj_consume_token(STRING); switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case LCURLYBRACE: jj_consume_token(LCURLYBRACE); label_2: while (true) { if (jj_2_13(1)) { ; } else { break label_2; } external_declaration(); } jj_consume_token(RCURLYBRACE); if (jj_2_14(2147483647)) { jj_consume_token(SEMICOLON); } else { ; } break; default: jj_la1[7] = jj_gen; if (jj_2_15(1)) { declaration(); } else { jj_consume_token(-1); throw new ParseException(); } } } static final public void declaration() throws ParseException { boolean isTypedef = false; if (jj_2_16(2)) { isTypedef = declaration_specifiers(); switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case LPARENTHESIS: case SCOPE: case AMPERSAND: case STAR: case TILDE: case OPERATOR: case ID: init_declarator_list(isTypedef); break; default: jj_la1[8] = jj_gen; ; } jj_consume_token(SEMICOLON); } else { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case EXTERN: linkage_specification(); break; default: jj_la1[9] = jj_gen; jj_consume_token(-1); throw new ParseException(); } } }/** * Very temporary. Just returns true if it sees a typedef. Finally, we will * need a structure that stores all the attributes. */ static final public boolean type_modifiers() throws ParseException { boolean isTypedef = false; switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case AUTO: case EXTERN: case REGISTER: case STATIC: case TYPEDEF: isTypedef = storage_class_specifier(); break; case CONST: case VOLATILE: type_qualifier(); break; case INLINE: jj_consume_token(INLINE); break; case VIRTUAL: jj_consume_token(VIRTUAL); break; case FRIEND: jj_consume_token(FRIEND); break; default: jj_la1[10] = jj_gen; jj_consume_token(-1); throw new ParseException(); } {if (true) return isTypedef;} throw new Error("Missing return statement in function"); }/** * Very temporary. Just returns true if it sees a typedef. Finally, we will * need a structure that stores all the attributes. */ static final public boolean declaration_specifiers() throws ParseException { Token t; boolean isTypedef = false, tmp; switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case AUTO: case CONST: case EXTERN: case FRIEND: case INLINE: case REGISTER: case STATIC: case TYPEDEF: case VIRTUAL: case VOLATILE: label_3: while (true) { tmp = type_modifiers(); isTypedef |= tmp; if (jj_2_17(2147483647)) { ; } else { break label_3; } } if (jj_2_25(2)) { if (jj_2_23(2147483647)) { builtin_type_specifier(); label_4: while (true) { if (jj_2_18(2)) { ; } else { break label_4; } if (jj_2_19(2147483647)) { builtin_type_specifier(); } else if (jj_2_20(2147483647)) { tmp = type_modifiers(); } else { jj_consume_token(-1); throw new ParseException(); } isTypedef |= tmp; } } else if (jj_2_24(1)) { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case STRUCT: case CLASS: case UNION: class_specifier(); break; case ENUM: enum_specifier(); break; default: jj_la1[11] = jj_gen; if (jj_2_21(1)) { qualified_type(); } else { jj_consume_token(-1); throw new ParseException(); } } label_5: while (true) { if (jj_2_22(2)) { ; } else { break label_5; } tmp = type_modifiers(); isTypedef |= tmp; } } else { jj_consume_token(-1); throw new ParseException(); } } else { ; } break; default: jj_la1[14] = jj_gen; if (jj_2_30(2147483647)) { builtin_type_specifier(); label_6: while (true) { if (jj_2_26(2)) { ; } else { break label_6; } if (jj_2_27(2147483647)) { builtin_type_specifier(); } else { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case AUTO: case CONST: case EXTERN: case FRIEND: case INLINE: case REGISTER: case STATIC: case TYPEDEF: case VIRTUAL: case VOLATILE: tmp = type_modifiers(); isTypedef |= tmp; break; default: jj_la1[12] = jj_gen; jj_consume_token(-1); throw new ParseException(); } } } } else if (jj_2_31(1)) { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case STRUCT: case CLASS: case UNION: class_specifier(); break; case ENUM: enum_specifier(); break; default: jj_la1[13] = jj_gen; if (jj_2_28(1)) { qualified_type(); } else { jj_consume_token(-1); throw new ParseException(); } } label_7: while (true) { if (jj_2_29(2)) { ; } else { break label_7; } tmp = type_modifiers(); isTypedef |= tmp; } } else { jj_consume_token(-1); throw new ParseException(); } } {if (true) return isTypedef;} throw new Error("Missing return statement in function"); }/*void type_specifier() :{}{ simple_type_specifier() | class_specifier() | enum_specifier()}*/ static final public void simple_type_specifier() throws ParseException { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case CHAR: case DOUBLE: case FLOAT: case INT: case LONG: case SHORT: case SIGNED: case UNSIGNED: case VOID: builtin_type_specifier(); break; default: jj_la1[15] = jj_gen; if (jj_2_32(1)) { qualified_type(); } else { jj_consume_token(-1); throw new ParseException(); } } } static final public void scope_override_lookahead() throws ParseException { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case SCOPE: jj_consume_token(SCOPE); break; case ID: jj_consume_token(ID); switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case LESSTHAN: jj_consume_token(LESSTHAN); template_argument_list(); jj_consume_token(GREATERTHAN); break; default: jj_la1[16] = jj_gen; ; } jj_consume_token(SCOPE); break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -