2014-04-16 4 views
-2

ANTLR에 공급 될 때 두 파일이 CPPParser 및 CPPLexer처럼 생성되는 C++ 문법 파일 (EX : CPP_grammar.g)이 ANTLR의 작동 방식입니다.하지만 내 질문은 - ---- C++ 문법의 변경을 통해 Java 특정 JAVA 파서 및 JAVA Lexer 클래스를 얻을 수있는 방법이 있습니까?ANTLR 문법 변환

감사

@zakaza :이 바보 의심의 여지하지만 난 그냥 날에 알아요 고마워요 할 수있는 절차 지루한 process.If가있어 문법 ++은 C를 변경해야하는 경우 나, 해결책을 찾고 알고 전진.


grammar CPP_grammar_; 

options 
{ 
    language = C; 
} 

tokens 
{ 
    OPERATOR = 'operator'; 
} 

@parser::header 
{ 
    extern "C++" 
    { 
    #include "header_file.h" 
    } 
    //typedef CPP_grammar_Parser_function_specifier_return FS_ret_t; 
    //typedef CPP_grammar_Parser_type_specifier_return TS_ret_t; 
} 

@parser::members 
{ 
    extern "C++" 
    { 
    #include "members_file.h" 
    } 
} 

@lexer::header 
{ 
    extern "C++" 
    { 
    #include "header_file.h" 
    } 
} 

@lexer::members 
{ 
    //#include "members_file.h" // already in parser source code 
} 


//translation_unit 
translation_unit 
    @init { 
     CPPParser__init(); 
    } 
    : {enterExternalScope();} 
     (external_declaration)* EOF 
     {exitExternalScope();} 
    ; 

//external_declaration Note: These comment lines are provided to assist searching for productions 
external_declaration  
    scope { 
     char *pScopeOverrideBuf; 
    } 
    @declarations { 
     //FunctionSpecifier fs = fsInvalid; // inline,virtual,explicit 
     char bufScopeOverrideScope[CPPParser_MaxQualifiedItemSize+1] = {0}; 
    } 
    @init { 
     lineNo = LT(1)->line; 
     boolean K_and_R; 
     K_and_R = FALSE; 
     //FunctionSpecifier 
     fs = fsInvalid; // inline,virtual,explicit 
     //in_user_file = in_user_file_deferred; 
     $external_declaration::pScopeOverrideBuf = bufScopeOverrideScope; 
    } 
    : 
    (
     // Template explicit specialisation 
     ('template' LESSTHAN GREATERTHAN)=> 
     {if(statementTrace>=1) 
      printf("\%d external_declaration template explicit-specialisation\n",LT(1)->line); 
     } 
     'template' LESSTHAN GREATERTHAN external_declaration 

    | 
     // All typedefs 
     ('typedef')=> 
     (
      ('typedef' 'enum')=> 
      {if(statementTrace>=1) 
       printf("\%d external_declaration Typedef enum type\n",LT(1)->line); 
      } 
      'typedef' enum_specifier {_td = TRUE;} (init_declarator_list)? SEMICOLON {end_of_stmt();} 
     | 
      (declaration_specifiers function_declarator[0] SEMICOLON)=> // DW 11/02/05 This may not be possible 
      {if(statementTrace>=1) 
       printf("\%d external_declaration Typedef function type\n",LT(1)->line); 
      } 
      declaration 
     | 
      (declaration_specifiers (init_declarator_list)? SEMICOLON)=> 
      {if(statementTrace>=1) 
       printf("\%d external_declaration Typedef variable type\n",LT(1)->line); 
      } 
      declaration 
     | 
      ('typedef' class_specifier)=> 
      {if(statementTrace>=1) 
       printf("\%d external_declaration Typedef class type\n",LT(1)->line); 
      } 
      'typedef' class_decl_or_def[bufScopeOverrideScope, fs] {_td = TRUE;} (init_declarator_list)? SEMICOLON {end_of_stmt();} 
     ) 

    | 
     // Class template declaration or definition 
     (template_head (fs = function_specifier)* class_specifier)=> 
     {if (statementTrace>=1) 
      printf("\%d external_declaration Templated class decl or def\n",LT(1)->line); 
     } 
     template_head (fs = function_specifier)* class_decl_or_def[bufScopeOverrideScope, fs] (init_declarator_list)? SEMICOLON {end_of_stmt();} // declaration 
    | 
     // Templated functions and constructors matched here. 
     {beginTemplateDeclaration();} 
     template_head 
     ( 
      // templated forward class decl, init/decl of static member in template 
      (declaration_specifiers (init_declarator_list)? SEMICOLON {end_of_stmt();})=> 
      {if (statementTrace>=1) 
       printf("\%d external_declaration Templated class forward declaration\n",LT(1)->line); 
      } 
      declaration_specifiers (init_declarator_list)? SEMICOLON {end_of_stmt();} 
     | 
      // Templated function declaration 
      (declaration_specifiers function_declarator[0] SEMICOLON)=> 
      {if (statementTrace>=1) 
       printf("\%d external_declaration Templated function declaration\n",LT(1)->line); 
      } 
      declaration 
     | 
      // Templated function definition 
      (declaration_specifiers function_declarator[1] LCURLY)=> 
      {if (statementTrace>=1) 
       printf("\%d external_declaration Templated function definition\n",LT(1)->line); 
      } 
      function_definition 
     | 
      // Templated constructor definition 
        // JEL 4/3/96 Added predicate that works once the 
        // restriction is added that ctor cannot be virtual 
      ( ctor_decl_spec 
       {qualifiedItemIsOneOf(qiCtor,0)}? 
      )=> 
      {if (statementTrace>=1) 
       printf("\%d external_declaration Templated constructor definition\n",LT(1)->line); 
      } 
      ctor_definition 
     ) 
     {endTemplateDeclaration();} 
    | 
     // Enum definition (don't want to backtrack over this in other alts) 
     ('enum' (ID)? LCURLY)=> 
     {if (statementTrace>=1) 
      printf("\%d external_declaration Enum definition\n",LT(1)->line); 
     } 
     enum_specifier (init_declarator_list)? SEMICOLON {end_of_stmt();} 
    | 
     // Destructor definition (templated or non-templated) 
     ((template_head)? dtor_head[1] LCURLY)=> 
     {if (statementTrace>=1) 
      printf("\%d external_declaration Destructor definition\n",LT(1)->line); 
     } 
     (template_head)? dtor_head[1] dtor_body 
    | 
     // Constructor definition (non-templated) 
     // JEL 4/3/96 Added predicate that works, once the 
     // restriction is added that ctor cannot be virtual 
     // and ctor_declarator uses a more restrictive id 
     ( (//{dummyVar}? //added by V3-Author //options {warnWhenFollowAmbig = false;}: 
      ctor_decl_spec)? 
      {qualifiedItemIsOneOf(qiCtor,0)}? 
     )=> 
     {if (statementTrace>=1) 
      printf("\%d external_declaration Constructor definition\n",LT(1)->line); 
     } 
     ctor_definition 
    | 
     // User-defined type cast 
     (('inline')? scope_override[$external_declaration::pScopeOverrideBuf] conversion_function_decl_or_def)=> 
     {if (statementTrace>=1) 
      printf("\%d external_declaration Operator function\n",LT(1)->line); 
     } 
     ('inline')? s1 = scope_override[bufScopeOverrideScope] conversion_function_decl_or_def 
    | 
     // Function declaration 
     (declaration_specifiers function_declarator[0] SEMICOLON)=> 
     {if (statementTrace>=1) 
      printf("\%d external_declaration Function declaration\n",LT(1)->line); 
     } 
     declaration_specifiers function_declarator[0] SEMICOLON {end_of_stmt();} 
    | 
     // Function definition 
     (declaration_specifiers function_declarator[1] LCURLY)=> 
     {if (statementTrace>=1) 
      printf("\%d external_declaration Function definition\n",LT(1)->line); 
     } 
     function_definition 
    | 
     // Function definition with int return assumed 
     (function_declarator[1] LCURLY)=> 
     {if (statementTrace>=1) 
      printf("\%d external_declaration Function definition without return type\n",LT(1)->line); 
     } 
     function_definition 
    | 
     // K & R Function definition 
     (declaration_specifiers function_declarator[1] declaration)=> 
     {K_and_R = TRUE; 
     if (statementTrace>=1) 
      printf("\%d external_declaration K & R function definition\n",LT(1)->line); 
     } 
     function_definition 
    | 
     // K & R Function definition with int return assumed 
     (function_declarator[1] declaration)=> 
     {K_and_R = TRUE; 
     if (statementTrace>=1) 
      printf("\%d external_declaration K & R function definition without return type\n",LT(1)->line); 
     } 
     function_definition 
    | 
     // Class declaration or definition 
     (('extern')? (fs = function_specifier)* class_specifier)=> 
     {if (statementTrace>=1) 
      printf("\%d external_declaration Class decl or def\n",LT(1)->line); 
     } 
     ('extern')? (fs = function_specifier)* class_decl_or_def[bufScopeOverrideScope, fs] (init_declarator_list)? SEMICOLON {end_of_stmt();} 


    // by the V3-Author, concepts, copied from the above class def/decl 
    //| 
     // Concept declaration or definition 
     //('concept' ID LCURLY)=> 
     //{if (statementTrace>=1) 
     // fprintf(stderr,"\%d external_declaration Concept decl or def\n\t begining at pos \%d\n",LT(1)->line, LT(1)->charPosition+1); 
     // } 
     //concept_decl_or_def 
     //{ 
     // if (statementTrace>=1) 
     // fprintf(stderr,"\t ending in line \%d at pos \%d\n",LT(1)->line, LT(1)->charPosition+1); 
     //} 

     // SEMICOLON 
     // { 
     // end_of_stmt();} 


    | 
     // Copied from member_declaration 31/05/07 
     (declaration_specifiers (init_declarator_list)? SEMICOLON)=> 
     {if (statementTrace>=1) 
      printf("\%d external_declaration Declaration\n",LT(1)->line); 
     } 
     declaration 

    | 
     // Namespace definition 
     {if (statementTrace>=1) 
      printf("\%d external_declaration Namespace definition\n",LT(1)->line); 
     } 
     'namespace' namespace_definition 
    | 
     // Semicolon 
     {if (statementTrace>=1) 
      printf("\%d external_declaration Semicolon\n",LT(1)->line); 
     } 
     SEMICOLON {end_of_stmt();} 
    | 
     // Anything else 
     {if (statementTrace>=1) 
      printf("\%d external_declaration Other Declaration\n",LT(1)->line); 
     } 
     declaration 

    | 
     // The next two entries may be used for debugging 
     // Use this statement in the source code to turn antlr trace on (See note above) 
     'antlrTrace_on' {antlrTrace(TRUE);} 
    | 
     // Use this statement in the source code to turn antlr trace off (See note above) 
     'antlrTrace_off' {antlrTrace(FALSE);} 

    ) 
    ; 

//member_declaration 
member_declaration 
    @declarations { 
     char bufQualifiedIdScope[CPPParser_MaxQualifiedItemSize+1] = {0}; 
    } 
    //options{backtrack=true;} 
    @init { 
     lineNo = LT(1)->line; 
     //FunctionSpecifier fs = fsInvalid; // inline,virtual,explicit 
    } 
    : 
    (
     // Template explicit specialisation 
     ('template' LESSTHAN GREATERTHAN)=> 
     {if(statementTrace>=1) 
      printf("\%d member_declaration Template explicit-specialisation\n",LT(1)->line); 
     } 
     'template' LESSTHAN GREATERTHAN member_declaration 
    | 

     // All typedefs 
    //(options{backtrack=true;}: 
    // (
     ('typedef')=> 
     (
      ('typedef' 'enum')=> 
      {if(statementTrace>=1) 
       printf("\%d member_declaration Typedef enum type\n",LT(1)->line); 
      } 
      'typedef' enum_specifier {_td = TRUE;} (init_declarator_list)? SEMICOLON {end_of_stmt();} 
     | 
      (declaration_specifiers function_declarator[0] SEMICOLON)=> // DW 11/02/05 This may not be possible member declaration 
      {if(statementTrace>=1) 
       printf("\%d member_declaration Typedef function type\n",LT(1)->line); 
      } 
      declaration 
      //SEMICOLON 
     | 
      (declaration_specifiers (init_declarator_list)? SEMICOLON)=> 
      {if(statementTrace>=1) 
       printf("\%d member_declaration Typedef variable type\n",LT(1)->line); 
      } 
      declaration 
      //SEMICOLON 
     | 
      ('typedef' class_specifier)=> 
      {if(statementTrace>=1) 
       printf("\%d member_declaration Typedef class type\n",LT(1)->line); 
      } 
      'typedef' class_decl_or_def[bufQualifiedIdScope, fs] {_td = TRUE;} (init_declarator_list)? SEMICOLON {end_of_stmt();} 
     ) 
    //) 
    //) 
    | 
     // Templated class declaration or definition 
     (template_head (fs = function_specifier)* class_specifier)=> 
     {if (statementTrace>=1) 
      printf("\%d member_declaration Templated class decl or def\n",LT(1)->line); 
     } 
     template_head (fs = function_specifier)* class_decl_or_def[bufQualifiedIdScope, fs] (init_declarator_list)? SEMICOLON {end_of_stmt();} // declaration 
    | 
     // Templated functions and constructors matched here. 
     {beginTemplateDeclaration();} 
     template_head 
     ( 
      // templated forward class decl, init/decl of static member in template 
      (declaration_specifiers (init_declarator_list)? SEMICOLON)=> 
      {if (statementTrace>=1) 
       printf("\%d member_declaration Templated forward declaration\n",LT(1)->line); 
       // DW 15/11/10 The above line amd other 6 were changed from antlr2. Insertion of backslash and replace getLine() with line 
      } 
      declaration_specifiers (init_declarator_list)? SEMICOLON {end_of_stmt();} 
     | 
      // Templated function declaration 
      (declaration_specifiers function_declarator[0] SEMICOLON)=> 
      {if (statementTrace>=1) 
       printf("\%d member_declaration Templated function declaration\n",LT(1)->line); 
      } 
      declaration 
     | 
      // Templated function definition 
      (declaration_specifiers function_declarator[1] LCURLY)=> 
      {if (statementTrace>=1) 
       printf("\%d member_declaration Templated function definition\n",LT(1)->line); 
      } 
      function_definition 
     | 
      // Templated constructor declarator 
      ( ctor_decl_spec 
       {qualifiedItemIsOneOf(qiCtor,0)}? // DW 15/11/10 Second argument - lookahead offset - inserted 
       ctor_declarator[0] SEMICOLON 
      )=> 
      {if (statementTrace>=1) 
       printf("\%d member_declaration Templated constructor declarator\n",LT(1)->line); 
      } 
      ctor_decl_spec ctor_declarator[0] SEMICOLON {end_of_stmt();} 
     | 
      // Templated constructor definition 
      // JEL 4/3/96 Added predicate that works once the 
      // restriction is added that ctor cannot be virtual 
      (ctor_decl_spec {qualifiedItemIsOneOf(qiCtor,0)}? // DW 15/11/10 Second argument - lookahead offset - inserted 
      )=> 
      {if (statementTrace>=1) 
       printf("\%d member_declaration Templated constructor definition\n",LT(1)->line); 
      } 
      ctor_definition 
     | 
      // Templated operator function 
      {if (statementTrace>=1) 
       printf("\%d member_declaration Templated operator function\n",LT(1)->line); 
      } 
      conversion_function_decl_or_def 
     | 
      // Templated class definition 
      {if (statementTrace>=1) 
       printf("\%d member_declaration Templated class definition\n",LT(1)->line); 
      } 
      class_head declaration_specifiers (init_declarator_list)? SEMICOLON {end_of_stmt();} 
     ) 
     {endTemplateDeclaration();} 
    | 
     // Enum definition (don't want to backtrack over this in other alts) 
     ('enum' (ID)? LCURLY)=> 
     {if (statementTrace>=1) 
      printf("\%d member_declaration Enum definition\n",LT(1)->line); 
     } 
     enum_specifier (init_declarator_list)? SEMICOLON {end_of_stmt();} 
    | 
     // Constructor declarator 
     ( ctor_decl_spec 
      {qualifiedItemIsOneOf(qiCtor,0)}? 
      ctor_declarator[0] SEMICOLON 
     )=> 
     {if (statementTrace>=1) 
      printf("\%d member_declaration Constructor declarator\n",LT(1)->line); 
     } 
     ctor_decl_spec ctor_declarator[0] SEMICOLON {end_of_stmt();} 
    | 
     // JEL Predicate to distinguish ctor from function 
     // This works now that ctor cannot have VIRTUAL 
     // It unfortunately matches A::A where A is not enclosing 
     // class -- this will have to be checked semantically 
     // Constructor definition 
     ( ctor_decl_spec 
      {qualifiedItemIsOneOf(qiCtor,0)}? 
      ctor_declarator[1] 
      (COLON  // DEFINITION :ctor_initializer 
      |LCURLY  // DEFINITION (compound Statement) ? 
      ) 
     )=> 
     {if (statementTrace>=1) 
      printf("\%d member_declaration Constructor definition\n",LT(1)->line); 
     } 
     ctor_definition 
    | 
     // No template_head allowed for dtor member 
     // Backtrack if not a dtor (no TILDE) 
     // Destructor declaration 
     (dtor_head[0] SEMICOLON)=> 
     {if (statementTrace>=1) 
      printf("\%d member_declaration Destructor declaration\n",LT(1)->line); 
     } 
     dtor_head[0] SEMICOLON {end_of_stmt();} 
    |// 
     // No template_head allowed for dtor member 
     // Backtrack if not a dtor (no TILDE) 
     // Destructor definition 
     (dtor_head[1] LCURLY)=> 
     {if (statementTrace>=1) 
      printf("\%d member_declaration Destructor definition\n",LT(1)->line); 
     } 
     dtor_head[1] dtor_body 

    | 
     // Function declaration 
     (declaration_specifiers function_declarator[0] SEMICOLON)=> 
     {if (statementTrace>=1) 
      printf("\%d member_declaration Function declaration\n",LT(1)->line); 
     } 
     declaration_specifiers function_declarator[0] SEMICOLON {end_of_stmt();} 

    | 
     // Function definition 
     (declaration_specifiers function_declarator[1] LCURLY)=> 
     {if (statementTrace>=1) 
      printf("\%d member_declaration Function definition\n",LT(1)->line); 
     } 
     function_definition 
    ////  
    | 
     // Class declaration or definition 
     (('friend')? (fs = function_specifier)* class_specifier)=> 
     {if (statementTrace>=1) 
      printf("\%d member_declaration Class decl or def\n",LT(1)->line); 
     } 
     ('friend')? (fs = function_specifier)* class_decl_or_def[bufQualifiedIdScope, fs] (init_declarator_list)? SEMICOLON {end_of_stmt();} 
    | 
     (declaration_specifiers (init_declarator_list)? SEMICOLON)=> 
     {if (statementTrace>=1) 
      printf("\%d member_declaration Declaration\n",LT(1)->line); 
     } 
     declaration //HERE// 
    | 
     // Member without a type (I guess it can only be a function declaration or definition) 
     ((fs = function_specifier)* function_declarator[0] SEMICOLON)=> 
     {fprintf(stderr,"\%d warning Function declaration found without return type\n",LT(1)->line); 
     if (statementTrace>=1) 
      printf("\%d member_declaration Function declaration\n",LT(1)->line); 
     } 
     (fs = function_specifier)* function_declarator[0] SEMICOLON {end_of_stmt();} 
    | 
     // Member without a type (I guess it can only be a function definition) 
     ((fs = function_specifier)* function_declarator[1] LCURLY)=> 
     {fprintf(stderr,"\%d warning Function definition found without return type\n",LT(1)->line); 
     if (statementTrace>=1) 
      printf("\%d member_declaration Function definition without return type\n",LT(1)->line); 
     } 
     (fs = function_specifier)* function_declarator[1] compound_statement {endFunctionDefinition();} 
    | 
     // User-defined type cast 
     (('inline')? conversion_function_decl_or_def)=> 
     {if (statementTrace>=1) 
      printf("\%d member_declaration Operator function\n",LT(1)->line); 
     } 
     ('inline')? conversion_function_decl_or_def 
    | 
     // Hack to handle decls like "superclass::member", 
     // to redefine access to private base class public members 
     // Qualified identifier 
     (qualified_id[NULL] SEMICOLON)=> 
     {if (statementTrace>=1) 
      printf("\%d member_declaration Qualified ID\n",LT(1)->line); 
     } 
     q2 = qualified_id[bufQualifiedIdScope] SEMICOLON {end_of_stmt();} 
    ////  
    | 
     // Access specifier 
     {if (statementTrace>=1) 
      printf("\%d member_declaration Access specifier\n",LT(1)->line); 
     } 
     access_specifier COLON 
    | 
     // Semicolon 
     {if (statementTrace>=1) 
      printf("\%d member_declaration Semicolon\n",LT(1)->line); 
     } 
     SEMICOLON {end_of_stmt();} 
    | 
     // The next two entries may be used for debugging 
     // Use this statement in the source code to turn antlr trace on (See note above) 
     'antlrTrace_on' {antlrTrace(TRUE);} 
    | 
     // Use this statement in the source code to turn antlr trace off (See note above) 
     'antlrTrace_off' {antlrTrace(FALSE);} 
    ) 
    ; 



//namespace_definition 
namespace_definition 
    : 
     (ns=ID{declaratorID(($ns.text->chars),qiType);})? 
     LCURLY 
     {enterNewLocalScope();} 
     (external_declaration)* 
     {exitLocalScope();} 
     RCURLY 
    ; 

//namespace_alias_definition 
namespace_alias_definition 
    @declarations { 
     char bufQualifiedIdScope[CPPParser_MaxQualifiedItemSize+1] = {0}; 
    } 
    : 
     'namespace' 
     ns2=ID {declaratorID($ns2.text->chars,qiType);} 
     ASSIGNEQUAL qid2 = qualified_id[bufQualifiedIdScope] SEMICOLON {end_of_stmt();} 
    ; 

//function_definition 
function_definition 
    @init { 
     lineNo = LT(1)->line; 
    } 
    : 
    ( // Next line is equivalent to guarded predicate in PCCTS 
     // (SCOPE | ID)? => <<qualifiedItemIsOneOf(qiType|qiCtor)>>? 
     {(!(LA(1)==SCOPE||LA(1)==ID) || qualifiedItemIsOneOf(qiType|qiCtor,0))}? 
     declaration_specifiers function_declarator[1] 
     ( //{dummyVar}? //added by V3-Author //options{warnWhenFollowAmbig = FALSE;}: 
      (declaration)=> 
      ({lineNo = LT(1)->line;} declaration)* // Possible for K & R definition 
      {in_parameter_list = FALSE;} 
     )? 
     compound_statement 
    | 
     function_declarator[1] 
     ( //{dummyVar}? //added by V3-Author //options{warnWhenFollowAmbig = FALSE;}: 
      (declaration)=> 
      ({lineNo = LT(1)->line;} declaration)* // Possible for K & R definition 
      {in_parameter_list = FALSE;} 
     )? 
     compound_statement 
    ) 
    {endFunctionDefinition();} 
    ; 

//declaration 
declaration 
    : 
     ('extern' StringLiteral)=> 
     linkage_specification 
    | 
     simple_declaration 
    | 
     using_statement 
    ; 


//linkage_specification 
linkage_specification 
    : 
     'extern' 
     StringLiteral 
     (LCURLY (external_declaration)* RCURLY 
     |declaration 
     ) 
    ; 

//class_head 
class_head 
    : // Used only by predicates 
     ('struct' 
     |'union' 
     |'class' 
     ) 
     (ID 
      (LESSTHAN template_argument_list GREATERTHAN)? 
      (base_clause)? 
     )? 
     LCURLY 
    ; 

//declaration_specifiers 
declaration_specifiers 
    @declarations { 
     StorageClass sc = scInvalid; // auto,register,static,extern,mutable 
     TypeQualifier tq = tqInvalid; // const,volatile // aka cv_qualifier See type_qualifier 
     TypeSpecifier ts = tsInvalid; // char,int,double, etc., class,struct,union 
     FunctionSpecifier fs = fsInvalid; // inline,virtual,explicit 
    } 
    @init { 
     // Locals 
     boolean td = FALSE; // For typedef 
     boolean fd = FALSE; // For friend 

     // Global flags to allow for nested declarations 
     _td = FALSE;  // For typedef 
     _fd = FALSE;  // For friend 
     _sc = scInvalid; // For StorageClass  // auto,register,static,extern,mutable 
     _tq = tqInvalid; // For TypeQualifier // aka cv_qualifier See type_qualifier 
     _ts = tsInvalid; // For TypeSpecifier 
     _fs = fsInvalid; // For FunctionSpecifier // inline,virtual,explicit 
    } 
    : 
    ( 
     (//{dummyVar}? //added by V3-Author //options {warnWhenFollowAmbig = false;}: 
      'typedef' {td=TRUE;}   
     | 'friend' {fd=TRUE;} 
     | sc1 = storage_class_specifier {sc = $sc1.scReturn;} // auto,register,static,extern,mutable 
     | tq1 = type_qualifier   {tq = $tq1.tqReturn;} // const,volatile // aka cv_qualifier See type_qualifier 
     | fs1 = function_specifier  {fs = $fs1.fsReturn;} // inline,virtual,explicit 
     | ('_declspec'|'__declspec') LPAREN ID RPAREN 
     )* 
     ts1 = type_specifier {ts = $ts1.tsReturn;} 
     (tq1 = type_qualifier {tq = $tq1.tqReturn;})* // const,volatile // aka cv_qualifier See type_qualifier 
    ) 
    {_td=td; declarationSpecifier(td,fd,sc,tq,ts,fs);} 
    ; 


//storage_class_specifier 
storage_class_specifier returns [StorageClass scReturn] 
    @init { 
     $scReturn = scInvalid; 
    } 
    : 'auto'  {$scReturn = scAUTO;} 
    | 'register' {$scReturn = scREGISTER;} 
    | 'static' {$scReturn = scSTATIC;} 
    | 'extern' {$scReturn = scEXTERN;} 
    | 'mutable' {$scReturn = scMUTABLE;} 
    ; 


//function_specifier 
function_specifier returns [FunctionSpecifier fsReturn] 
    @init { 
     $fsReturn = fsInvalid; 
    } 
    : ('inline'|'_inline'|'__inline') {$fsReturn = fsINLINE;} 
    | 'virtual'      {$fsReturn = fsVIRTUAL;} 
    | 'explicit'      {$fsReturn = fsEXPLICIT;} 
    ; 

//type_specifier 
type_specifier returns [TypeSpecifier tsReturn] 
    @init { 
     TypeQualifier tq = tqInvalid; 
     $tsReturn = tsInvalid; 
    } 
    : 
     ts = simple_type_specifier {$tsReturn = $ts.tsReturn ;} 
    ; 

답변

1

예,없고.

아니요, Java는 다른 언어이므로 처음부터 다시 작성하거나 Java 코드 구문 분석에 사용할 수있는 다른 문법 중 하나를 사용하는 것이 좋습니다.

그렇습니다. 두 파일 모두 ANTLR과 동일한 구문의 텍스트 파일이므로 기술적으로 C++ 및 Java 문법을 diff에서 열고 차이점을 관찰 할 수 있습니다. 이것은 크게 무의미한 운동이 되겠지만. 당신이 당신의 문법 파일에 특정 CPP 코드가없는 경우

0

, 단지 변경 :

options 
{ 
    language = Cpp; 
} 

options 
{ 
    language = Java; 
} 

에 당신이 행동, 헤더가 또는 CPP 코드를 사용하여 포함 된 경우 많은 것을 바꿔야합니다. 이 경우 문법을 게시하면 어떻게 보이는지 알 수 있습니까?

+0

문법 파일이 추가되었습니다.보세요 – Abhid

+0

정확하게 요점을 얻지 못했습니다. 자바 문법을 원한다면 [java.g] (http://openjdk.java.net/projects/compiler-grammar/antlrworks/Java.g) 또는 [java.g4] (https : // github. com/antlr/grammars-v4/blob/master/java/Java.g4). 그렇지 않으면 Java 코드에서이 cpp 구문 분석기를 사용하려면 JavaCPP –