我有C ++语法文件(EX:CPP_grammar.g),当它被送到ANTLR ------两个文件会像CPPParser和CPPLexer一样生成,这就是ANTLR的工作方式。但我的问题是---- - 有什么方法可以对C ++语法进行更改,以便我获得Java特定的JAVA Parser和JAVA Lexer类。
@zakaza:我知道这是一个愚蠢的疑问,但我正在寻找解决方案,如果我必须改变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 ;}
;
答案 0 :(得分:1)
是的,没有。
不,Java是一种不同的语言,因此您需要重新开始,创建自己的语言或使用其他可用于解析Java代码的语法之一。
是的,两个文件都是相同语法的文本文件(ANTLR),因此您可以在技术上打开差异中的C ++和Java语法,并观察它们的区别。虽然这在很大程度上是毫无意义的。
答案 1 :(得分:0)
如果您的语法文件中没有任何特定的cpp代码,只需更改:
options
{
language = Cpp;
}
到
options
{
language = Java;
}
如果你有动作,标题或包括使用cpp代码,你必须改变很多东西。在这种情况下,您可以发布我看到它的外观吗?