-2

I have C++ grammar file(EX:CPP_grammar.g), which when fed to ANTLR------two files will generate like CPPParser and CPPLexer,this is how ANTLR works.But my question is----- Is there any way that I can do changes in C++ grammar so that I get Java specific JAVA Parser and JAVA Lexer classes.

@zakaza: I know this is silly doubt but I am looking for the solution, If I have to change the C++ grammar it's tedious process. If there are any procedures just let me know.


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 ;}
    ;
Cœur
  • 37,241
  • 25
  • 195
  • 267
Abhid
  • 1
  • 2

2 Answers2

1

Yes, and no.

No, Java is a different language, so you'll want to start over and either create your own or use one of the other grammars available for parsing Java code.

Yes, both files will be text files in the same syntax (ANTLR), so you could technically open up the C++ and Java grammars in a diff and observe how they differ. Though this would be a largely pointless exercise.

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
0

If you don't have any specific cpp code in your grammar file, just change:

options
{ 
  language = Cpp;
}

to

options
{ 
  language = Java;
}

If you have actions, headers or includes using cpp code, you must change a lot of things. In this case, can you post your grammar that I see how does it look like?

  • added grammar file.Take a look – Abhid Apr 22 '14 at 09:46
  • I didn't get exactly the point. If you want a java grammar, look at [java.g](http://openjdk.java.net/projects/compiler-grammar/antlrworks/Java.g) or [java.g4](https://github.com/antlr/grammars-v4/blob/master/java/Java.g4). Otherwise, if you want to use this cpp parser in a java code, look at JavaCPP – Zakaria Jaiathe Apr 23 '14 at 14:33