目录
1、前言
2、标准C++词法文件(没有整理全,可以参考C的标准词法文件和C++的语法文件补充完整)
3、标准C++语法文件(C++11规范,未整理完全,部分可优化,请自行参考语言规范整理)
1、前言
鉴于这段时间美国总统特朗普先生对以华为为代表的中国企业围堵的全面升级,本人将平时收集整理的标准C++语言的Flex和Bison元文件发出来,供大家参考。算是为反围堵尽自己的一份绵薄之力。请注意文件只是标识了标准的词法和语法,并没有语法分析和语义分析部分,我一直没有来得及补写,现在放出来是给大家一个参考。
2、标准C++词法文件(没有整理全,可以参考C的标准词法文件和C++的语法文件补充完整)
%option noyywrap nodefault yylineno
%{
#include <stdio.h>
#include "Standard_CPlus.tab.h"
void yyerror(char *text);
void count(void);
void comment(void);
int check_type(void);
%}
D [0-9]
L [a-zA-Z_]
H [a-fA-F0-9]
E ([Ee][+-]?{D}+)
P ([Pp][+-]?{D}+)
FS (f|F|l|L)
IS ((u|U)|(u|U)?(l|L|ll|LL)|(l|L|ll|LL)(u|U))
%%
"/*" { comment(); }
"//"[^\n]* { /* consume //-comment */ }
"asm" { count(); return(ASM); }
"auto" { count(); return(AUTO); }
"_Bool" { count(); return(BOOL); }
"break" { count(); return(BREAK); }
"case" { count(); return(CASE); }
"catch" { count(); return(CATCH); }
"char" { count(); return(CHAR); }
"class" { count(); return(CLASS); }
"_Complex" { count(); return(COMPLEX); }
"const" { count(); return(CONST); }
"const_cast" { count(); return(CONST_CAST); }
"continue" { count(); return(CONTINUE); }
"default" { count(); return(DEFAULT); }
"do" { count(); return(DO); }
"double" { count(); return(DOUBLE); }
"dynamic_cast" { count(); return(DYNAMIC_CAST); }
"else" { count(); return(ELSE); }
"enum" { count(); return(ENUM); }
"explicit" { count(); return(EXPLICIT);}
"extern" { count(); return(EXTERN); }
"float" { count(); return(FLOAT); }
"for" { count(); return(FOR); }
"friend" { count(); return(FRIEND);}
"goto" { count(); return(GOTO); }
"if" { count(); return(IF); }
"_Imaginary" { count(); return(IMAGINARY); }
"inline" { count(); return(INLINE); }
"int" { count(); return(INT); }
"long" { count(); return(LONG); }
"mutable" { count(); return(MUTABLE); }
"namespace" { count(); return(NAMESPACE); }
"operator" { count(); return(OPERATOR); }
"private" { count(); return(PRIVATE); }
"protected" { count(); return(PROTECTED); }
"public" { count(); return(PUBLIC); }
"register" { count(); return(REGISTER); }
"reinterpret_cast" { count(); return(REINTERPRET_CAST); }
"restrict" { count(); return(RESTRICT); }
"return" { count(); return(RETURN); }
"short" { count(); return(SHORT); }
"signed" { count(); return(SIGNED); }
"sizeof" { count(); return(SIZEOF); }
"static" { count(); return(STATIC); }
"static_cast" { count(); return(STATIC_CAST); }
"struct" { count(); return(STRUCT); }
"switch" { count(); return(SWITCH); }
"template" { count(); return(TEMPLATE); }
"this" { count(); return(THIS); }
"throw" { count(); return(THROW); }
"try" { count(); return(TRY); }
"typedef" { count(); return(TYPEDEF); }
"typeid" { count(); return(TYPEID);}
"typename" { count(); return(TYPENAME); }
"union" { count(); return(UNION); }
"unsigned" { count(); return(UNSIGNED); }
"using" { count(); return(USING); }
"virtual" { count(); return(VIRTUAL);}
"void" { count(); return(VOID); }
"volatile" { count(); return(VOLATILE); }
"while" { count(); return(WHILE); }
"wchar_t" { count(); return(WCHAR_T); }
{L}({L}|{D})* { count(); return(check_type()); }
0[xX]{H}+{IS}? { count(); return(CONSTANT); }
0[0-7]*{IS}? { count(); return(CONSTANT); }
[1-9]{D}*{IS}? { count(); return(CONSTANT); }
L?'(\\.|[^\\'\n])+' { count(); return(CONSTANT); }
{D}+{E}{FS}? { count(); return(CONSTANT); }
{D}*"."{D}+{E}?{FS}? { count(); return(CONSTANT); }
{D}+"."{D}*{E}?{FS}? { count(); return(CONSTANT); }
0[xX]{H}+{P}{FS}? { count(); return(CONSTANT); }
0[xX]{H}*"."{H}+{P}?{FS}? { count(); return(CONSTANT); }
0[xX]{H}+"."{H}*{P}?{FS}? { count(); return(CONSTANT); }
L?\"(\\.|[^\\"\n])*\" { count(); return(STRING_LITERAL); }
"..." { count(); return(ELLIPSIS); }
">>=" { count(); return(RIGHT_ASSIGN); }
"<<=" { count(); return(LEFT_ASSIGN); }
"+=" { count(); return(ADD_ASSIGN); }
"-=" { count(); return(SUB_ASSIGN); }
"*=" { count(); return(MUL_ASSIGN); }
"/=" { count(); return(DIV_ASSIGN); }
"%=" { count(); return(MOD_ASSIGN); }
"&=" { count(); return(AND_ASSIGN); }
"^=" { count(); return(XOR_ASSIGN); }
"|=" { count(); return(OR_ASSIGN); }
">>" { count(); return(RIGHT_OP); }
"<<" { count(); return(LEFT_OP); }
"++" { count(); return(INC_OP); }
"--" { count(); return(DEC_OP); }
"->" { count(); return(PTR_OP); }
"&&" { count(); return(AND_OP); }
"||" { count(); return(OR_OP); }
"<=" { count(); return(LE_OP); }
">=" { count(); return(GE_OP); }
"==" { count(); return(EQ_OP); }
"!=" { count(); return(NE_OP); }
"::" { count(); return(DOMAIN_OP);}
";" { count(); return(';'); }
("{"|"<%") { count(); return('{'); }
("}"|"%>") { count(); return('}'); }
"," { count(); return(','); }
":" { count(); return(':'); }
"=" { count(); return('='); }
"(" { count(); return('('); }
")" { count(); return(')'); }
("["|"<:") { count(); return('['); }
("]"|":>") { count(); return(']'); }
"." { count(); return('.'); }
"&" { count(); return('&'); }
"!" { count(); return('!'); }
"~" { count(); return('~'); }
"-" { count(); return('-'); }
"+" { count(); return('+'); }
"*" { count(); return('*'); }
"/" { count(); return('/'); }
"%" { count(); return('%'); }
"<" { count(); return('<'); }
">" { count(); return('>'); }
"^" { count(); return('^'); }
"|" { count(); return('|'); }
"?" { count(); return('?'); }
[ \t\v\n\f] { count(); }
. { /* Add code to complain about unmatched characters */ }
%%
void comment(void)
{
char c, prev = 0;
while ((c = yyinput()) != 0) /* (EOF maps to 0) */
{
if (c == '/' && prev == '*')
return;
prev = c;
}
yyerror("unterminated comment");
}
int column = 0;
void count(void)
{
int i;
for (i = 0; yytext[i] != '\0'; i++)
if (yytext[i] == '\n')
column = 0;
else if (yytext[i] == '\t')
column += 8 - (column % 8);
else
column++;
ECHO;
}
int check_type(void)
{
/*
* pseudo code --- this is what it should check
*
* if (yytext == type_name)
* return TYPE_NAME;
*
* return IDENTIFIER;
*/
/*
* it actually will only return IDENTIFIER
*/
return IDENTIFIER;
}
3、标准C++语法文件(C++11规范,未整理完全,部分可优化,请自行参考语言规范整理,同标准C语言语法文件没有语法分析和语义分析部分代码,各位可自行完成,仅供参考)
%{
#include "stdio.h"
#ifndef YYSTYPE
#define YYSTYPE int
#endif
extern char yytext[];
extern int column;
void yyerror(char *text);
int yylex(void);
%}
/
//以下为C关键字
%token PTR_OP "->"
%token INC_OP "++"
%token DEC_OP "--"
%token LEFT_OP "<<"
%token RIGHT_OP ">>"
%token LE_OP "<="
%token GE_OP ">="
%token EQ_OP "=="
%token NE_OP "!="
%token AND_OP "&&"
%token OR_OP "||"
%token MUL_ASSIGN "*="
%token DIV_ASSIGN "/="
%token MOD_ASSIGN "%="
%token ADD_ASSIGN "+="
%token SUB_ASSIGN "-="
%token LEFT_ASSIGN "<<="
%token RIGHT_ASSIGN ">>="
%token AND_ASSIGN "&="
%token XOR_ASSIGN "^="
%token OR_ASSIGN "|="
%token ELLIPSIS "..."
%token IDENTIFIER CONSTANT STRING_LITERAL SIZEOF
%token TYPEDEF EXTERN STATIC AUTO REGISTER MUTABLE
%token CHAR SHORT INT LONG SIGNED UNSIGNED FLOAT DOUBLE CONST VOLATILE VOID WCHAR_T
%token BOOL COMPLEX IMAGINARY INLINE RESTRICT
%token STRUCT UNION ENUM
%token CASE DEFAULT IF ELSE SWITCH WHILE DO FOR GOTO CONTINUE BREAK RETURN
%token ASM
/
/
//以下为C++扩展的关键字
%token TYPENAME
%token TYPEID
%token THIS TEMPLATE NEW DELETE FRIEND VIRTUAL EXPLICIT
%token DOMAIN_OP "::"
%token DYNAMIC_CAST
%token STATIC_CAST
%token REINTERPRET_CAST
%token CONST_CAST
%token NAMESPACE
%token USING
%token CLASS
%token PRIVATE
%token PROTECTED
%token PUBLIC
%token OPERATOR
%token TRY
%token CATCH
%token THROW
/
%start translation_unit
%%
///
//Basic concepts
///
translation_unit
: declaration_seqopt
;
///
//Expressions
///
primary_expression
: CONSTANT
| THIS
| "::" IDENTIFIER
| "::" operator_function_id
| "::" qualified_id
| '(' expression ')'
| id_expression
;
id_expression
: unqualified_id
| qualified_id
;
unqualified_id
: IDENTIFIER
| operator_function_id
| conversion_function_id
| '~' class_name
| template_id
;
qualified_id
: nested_name_specifier templateopt unqualified_id
;
nested_name_specifier
: class_or_namespace_name "::" nested_name_specifieropt
;
nested_name_specifieropt
: nested_name_specifier
| /*empty*/
;
class_or_namespace_name
: class_name
| namespace_name
;
postfix_expression
: primary_expression
| postfix_expression '[' expression ']'
| postfix_expression '(' expression_listopt ')'
| simple_type_specifier '(' expression_listopt ')'
| postfix_expression '.' templateopt domainopt id_expression
| postfix_expression "->" templateopt domainopt id_expression
| postfix_expression '.' pseudo_destructor_name
| postfix_expression "->" pseudo_destructor_name
| postfix_expression "++"
| postfix_expression "--"
| DYNAMIC_CAST '<' type_id '>' '(' expression ')'
| STATIC_CAST '<' type_id '>' '(' expression ')'
| REINTERPRET_CAST '<' type_id '>' '(' expression ')'
| CONST_CAST '<' type_id '>' '(' expression ')'
| TYPEID '(' expression ')'
| TYPEID '(' type_id ')'
;
templateopt
: TEMPLATE
| /*empty*/
;
domainopt
: "::"
| /*empty*/
;
expression_listopt
: expression_list
| /*empty*/
;
expression_list
: assignment_expression
| expression_list ',' assignment_expression
;
pseudo_destructor_name
: domainopt nested_name_specifieropt type_name "::" '~' type_name
| domainopt nested_name_specifieropt '~' type_name
;
unary_expression
: postfix_expression
| "++" unary_expression
| "--" unary_expression
| unary_operator cast_expression
| SIZEOF unary_expression
| SIZEOF '(' type_id ')'
| new_expression
| delete_expression
;
unary_operator
: '&'
| '*'
| '+'
| '-'
| '~'
| '!'
;
new_expression
: domainopt NEW new_placementopt new_type_id new_initializeropt
| domainopt NEW new_placementopt '(' type_id ')' new_initializeropt
;
new_placementopt
: '(' expression_list ')'
| /*empty*/
;
new_type_id
: type_specifier_seq new_declaratoropt
;
new_declaratoropt
: new_declarator
| /*empty*/
;
new_declarator
: ptr_operator new_declaratoropt
| direct_new_declarator
;
direct_new_declarator
:'[' expression ']'
| direct_new_declarator '[' constant_expression ']'
;
new_initializeropt
: '(' expression_listopt ')'
| /*empty*/
;
delete_expression
: domainopt DELETE cast_expression
| domainopt DELETE '[' ']' cast_expression
;
cast_expression
: unary_expression
| '(' type_id ')' cast_expression
;
pm_expression
: cast_expression
| pm_expression '.' '*' cast_expression
| pm_expression "->" '*' cast_expression
;
multiplicative_expression
: pm_expression
| multiplicative_expression '*' pm_expression
| multiplicative_expression '/' pm_expression
| multiplicative_expression '%' pm_expression
;
additive_expression
: multiplicative_expression
| additive_expression '+' multiplicative_expression
| additive_expression '-' multiplicative_expression
;
shift_expression
: additive_expression
| shift_expression "<<" additive_expression
| shift_expression ">>" additive_expression
;
relational_expression
: shift_expression
| relational_expression '<' shift_expression
| relational_expression '>' shift_expression
| relational_expression "<=" shift_expression
| relational_expression ">=" shift_expression
;
equality_expression
: relational_expression
| equality_expression "==" relational_expression
| equality_expression "!=" relational_expression
;
and_expression
: equality_expression
| and_expression '&' equality_expression
;
exclusive_or_expression
: and_expression
| exclusive_or_expression '^' and_expression
;
inclusive_or_expression
: exclusive_or_expression
| inclusive_or_expression '|' exclusive_or_expression
;
logical_and_expression
: inclusive_or_expression
| logical_and_expression "&&" inclusive_or_expression
;
logical_or_expression
: logical_and_expression
| logical_or_expression "||" logical_and_expression
;
conditional_expression
: logical_or_expression
| logical_or_expression '?' expression ':' assignment_expression
;
assignment_expression
: conditional_expression
| logical_or_expression assignment_operator assignment_expression
| throw_expression
;
assignment_operator
: '='
| "*="
| "/="
| "%="
| "+="
| "-="
| ">>="
| "<<="
| "&="
| "^="
| "|="
;
expression
: assignment_expression
| expression ',' assignment_expression
;
constant_expression
: conditional_expression
;
///
// Statements
///
statement
: labeled_statement
| expression_statement
| compound_statement
| selection_statement
| iteration_statement
| jump_statement
| declaration_statement
| try_block
;
labeled_statement
: IDENTIFIER ':' statement
| CASE constant_expression ':' statement
| DEFAULT ':' statement
;
expression_statement
: expressionopt ';'
;
expressionopt
: expression
| /*empty*/
;
compound_statement
: '{' statement_seqopt '}'
;
statement_seqopt
: statement_seq
| /*empty*/
;
statement_seq
: statement
| statement_seq statement
;
selection_statement
: IF '(' condition ')' statement
| IF '(' condition ')' statement ELSE statement
| SWITCH '(' condition ')' statement
;
condition
: expression
| type_specifier_seq declarator '=' assignment_expression
;
iteration_statement
: WHILE '(' condition ')' statement
| DO statement WHILE '(' expression ')' ';'
| FOR '(' for_init_statement conditionopt ';' expressionopt ')' statement
;
conditionopt
: condition
| /*empty*/
;
for_init_statement
: expression_statement
| simple_declaration
;
jump_statement
: BREAK ';'
| CONTINUE ';'
| RETURN expressionopt ';'
| GOTO IDENTIFIER ';'
;
declaration_statement
: block_declaration
;
///
//Declarations
///
declaration_seq
: declaration
| declaration_seq declaration
;
/*
declaration:
block-declaration
function-definition
template-declaration
explicit-instantiation
explicit-specialization
linkage-specification
namespace-definition
*/
declaration
: block_declaration
| function_definition
| template_declaration
| explicit_specialization
| linkage_specification
| namespace_definition
;
/*
explicit_instantiation
: template_declaration
;
*/
block_declaration
: simple_declaration
| asm_definition
| namespace_alias_definition
| using_declaration
| using_directive
;
simple_declaration
: decl_specifier_seqopt init_declarator_listopt ';'
decl_specifier_seqopt
: decl_specifier_seq
| /*empty*/
;
init_declarator_listopt
: init_declarator_list
| /*empty*/
;
decl_specifier
: storage_class_specifier
| type_specifier
| function_specifier
| FRIEND
| TYPEDEF
;
decl_specifier_seq
: decl_specifier_seqopt decl_specifier
;
storage_class_specifier
: AUTO
| REGISTER
| STATIC
| EXTERN
| MUTABLE
;
function_specifier
: INLINE
| VIRTUAL
| EXPLICIT
;
/*
typedef_name
: IDENTIFIER
;
*/
type_specifier
: simple_type_specifier
| class_specifier
| enum_specifier
| elaborated_type_specifier
| cv_qualifier
;
simple_type_specifier
: domainopt nested_name_specifieropt type_name
| CHAR
| WCHAR_T
| BOOL
| SHORT
| INT
| LONG
| SIGNED
| UNSIGNED
| FLOAT
| DOUBLE
| VOID
;
/*
type_name
: class_name
| enum_name
| typedef_name
;
*/
type_name
: IDENTIFIER
;
elaborated_type_specifier
: class_key domainopt nested_name_specifieropt IDENTIFIER
| ENUM domainopt nested_name_specifieropt IDENTIFIER
| TYPENAME domainopt nested_name_specifier IDENTIFIER
| TYPENAME domainopt nested_name_specifier IDENTIFIER '<' template_argument_list '>'
;
/*
enum_name
: IDENTIFIER
;
*/
enum_specifier
: ENUM identifieropt '{' enumerator_listopt '}'
;
identifieropt
: IDENTIFIER
| /*empty*/
;
enumerator_listopt
: enumerator_list
| /*empty*/
;
enumerator_list
: enumerator_definition
| enumerator_list ',' enumerator_definition
;
enumerator_definition
: enumerator
| enumerator '=' constant_expression
;
enumerator
: IDENTIFIER
;
namespace_name
: original_namespace_name
/* | namespace_alias */
;
original_namespace_name
: IDENTIFIER
;
/*
namespace_alias
: IDENTIFIER
;
*/
namespace_definition
: named_namespace_definition
| unnamed_namespace_definition
;
named_namespace_definition
: original_namespace_definition
| extension_namespace_definition
;
original_namespace_definition
: NAMESPACE IDENTIFIER '{' namespace_body '}'
;
extension_namespace_definition
: NAMESPACE original_namespace_name '{' namespace_body '}'
;
unnamed_namespace_definition
: NAMESPACE '{' namespace_body '}'
;
namespace_body
: declaration_seqopt
;
declaration_seqopt
: declaration_seq
| /*empty*/
;
namespace_alias_definition
: NAMESPACE IDENTIFIER '=' qualified_namespace_specifier ';'
;
qualified_namespace_specifier
: domainopt nested_name_specifieropt namespace_name
;
using_declaration
: USING type_name domainopt nested_name_specifier unqualified_id ';'
| USING domainopt nested_name_specifier unqualified_id ';'
| USING "::" unqualified_id ';'
;
/*
typenameopt
: type_name
|
;
*/
using_directive
: USING NAMESPACE domainopt nested_name_specifieropt namespace_name ';'
;
asm_definition
: ASM '(' STRING_LITERAL ')' ';'
;
linkage_specification
: EXTERN STRING_LITERAL '{' declaration_seqopt '}'
| EXTERN STRING_LITERAL declaration
;
///
//Declarators
///
init_declarator_list
: init_declarator
| init_declarator_list ',' init_declarator
;
init_declarator
: declarator initializeropt
;
initializeropt
: initializer
| /*empty*/
;
declarator
: direct_declarator
| ptr_operator declarator
;
direct_declarator
: declarator_id
| direct_declarator '(' parameter_declaration_clause ')' cv_qualifier_seqopt exception_specificationopt
| direct_declarator '[' constant_expressionopt ']'
| '(' declarator ')'
;
cv_qualifier_seqopt
: cv_qualifier_seq
| /*empty*/
;
exception_specificationopt
: exception_specification
| /*empty*/
;
ptr_operator
: '*' cv_qualifier_seqopt
| '&'
| domainopt nested_name_specifier '*' cv_qualifier_seqopt
;
cv_qualifier_seq
: cv_qualifier cv_qualifier_seqopt
;
cv_qualifier
: CONST
| VOLATILE
;
declarator_id
: domainopt id_expression
| domainopt nested_name_specifieropt type_name
;
type_id
: type_specifier_seq abstract_declaratoropt
;
abstract_declaratoropt
: abstract_declarator
| /*empty*/
;
type_specifier_seq
: type_specifier type_specifier_seqopt
;
type_specifier_seqopt
: type_specifier_seq
| /*empty*/
;
abstract_declarator
: ptr_operator abstract_declaratoropt
| direct_abstract_declarator
;
direct_abstract_declarator
: direct_abstract_declaratoropt '(' parameter_declaration_clause ')' cv_qualifier_seqopt exception_specificationopt
| direct_abstract_declaratoropt '[' constant_expressionopt ']'
| '(' abstract_declarator ')'
;
direct_abstract_declaratoropt
: direct_abstract_declarator
| /*empty*/
;
constant_expressionopt
: constant_expression
| /*empty*/
;
parameter_declaration_clause
: parameter_declaration_listopt ellipsisopt
| parameter_declaration_list ',' "..."
;
parameter_declaration_listopt
: parameter_declaration_list
| /*empty*/
;
ellipsisopt
: "..."
| /*empty*/
;
parameter_declaration_list
: parameter_declaration
| parameter_declaration_list ',' parameter_declaration
;
parameter_declaration
: decl_specifier_seq declarator
| decl_specifier_seq declarator '=' assignment_expression
| decl_specifier_seq abstract_declaratoropt
| decl_specifier_seq abstract_declaratoropt '=' assignment_expression
;
function_definition
: decl_specifier_seqopt declarator ctor_initializeropt function_body
| decl_specifier_seqopt declarator function_try_block
;
ctor_initializeropt
: ctor_initializer
| /*empty*/
;
function_body
: compound_statement
;
initializer
: '=' initializer_clause
| '(' expression_list ')'
;
initializer_clause
: assignment_expression
| '{' initializer_list commaopt '}'
| '{' '}'
;
commaopt
: ','
| /*empty*/
;
initializer_list
: initializer_clause
| initializer_list ',' initializer_clause
;
///
//Classes
///
class_name
: IDENTIFIER
| template_id
;
class_specifier
: class_head '{' member_specificationopt '}'
;
class_head
: class_key identifieropt base_clauseopt
| class_key nested_name_specifier IDENTIFIER base_clauseopt
;
base_clauseopt
: base_clause
| /*empty*/
;
class_key
: CLASS
| STRUCT
| UNION
;
member_specification
: member_declaration member_specificationopt
| access_specifier ':' member_specificationopt
;
member_specificationopt
: member_specification
| /*empty*/
;
member_declaration
: decl_specifier_seqopt member_declarator_listopt ';'
| function_definition semicolonopt
| qualified_id ';'
| using_declaration
| template_declaration
;
semicolonopt
: ';'
| /*empty*/
;
member_declarator_listopt
: member_declarator_list
| /*empty*/
;
member_declarator_list
: member_declarator
| member_declarator_list ',' member_declarator
;
/*
member-declarator:
declarator pure-specifieropt
declarator constant-initializeropt
identifieropt : constant-expression
*/
member_declarator
: declarator '=' '0'
| declarator '=' constant_expression
| declarator
| identifieropt ':' constant_expression
;
/*
pure_specifieropt
: pure_specifier
|
;
pure_specifier
: '=' '0'
;
*/
///
//Derived classes
///
base_clause
: ':' base_specifier_list
;
base_specifier_list
: base_specifier
| base_specifier_list ',' base_specifier
;
base_specifier
: domainopt nested_name_specifieropt class_name
| VIRTUAL access_specifieropt domainopt nested_name_specifieropt class_name
| access_specifier virtualopt domainopt nested_name_specifieropt class_name
;
access_specifieropt
: access_specifier
| /*empty*/
;
virtualopt
: VIRTUAL
| /*empty*/
;
access_specifier
: PRIVATE
| PROTECTED
| PUBLIC
;
///
//Special member functions
///
conversion_function_id
: operator_name conversion_type_id
;
conversion_type_id
: type_specifier_seq conversion_declaratoropt
;
conversion_declaratoropt
: conversion_declarator
| /*empty*/
;
conversion_declarator
: ptr_operator conversion_declaratoropt
;
ctor_initializer
: ':' mem_initializer_list
;
mem_initializer_list
: mem_initializer
| mem_initializer ',' mem_initializer_list
;
mem_initializer
: mem_initializer_id '(' expression_listopt ')'
;
mem_initializer_id
: domainopt nested_name_specifieropt class_name
| IDENTIFIER
;
///
//Overloading
///
operator_function_id
: OPERATOR operator_name
;
operator_name
: NEW | DELETE | NEW '['']' | DELETE '[' ']'
| '+' | '-' | '*' | '/'
| '%' | '^' | '&' | '|'
| '~' | '!' | '=' | '<'
| '>' | "+=" | "-=" | "*="
| "/=" | "%=" | "^=" | "&="
| "|=" | "<<" | ">>" | ">>="
| "<<=" | "==" | "!=" | "<="
| ">=" | "&&" | "||" | "++"
| "--" | ',' | "->" '*' | "->"
| '(' ')' | '[' ']'
;
///
//Templates
///
template_declaration
: exportopt TEMPLATE '<' template_parameter_list '>' declaration
;
exportopt
: /*empty*/
;
template_parameter_list
: template_parameter
| template_parameter_list ',' template_parameter
;
template_parameter
: type_parameter
| parameter_declaration
;
type_parameter
: CLASS identifieropt
| CLASS identifieropt '=' type_id
| TYPENAME identifieropt
| TYPENAME identifieropt '=' type_id
| TEMPLATE '<' template_parameter_list '>' CLASS identifieropt
| TEMPLATE '<' template_parameter_list '>' CLASS identifieropt '=' template_name
;
template_id
: template_name '<' template_argument_list '>'
;
template_name
: IDENTIFIER
;
template_argument_list
: template_argument
| template_argument_list ',' template_argument
;
template_argument
: assignment_expression
| type_id
| template_name
;
explicit_specialization
: TEMPLATE '<' '>' declaration
;
///
//Exception handling
///
try_block
: TRY compound_statement handler_seq
;
function_try_block
: TRY ctor_initializeropt function_body handler_seq
;
handler_seqopt
: handler_seq
| /*empty*/
;
handler_seq
: handler handler_seqopt
;
handler
: CATCH '(' exception_declaration ')' compound_statement
;
exception_declaration
: type_specifier_seq declarator
| type_specifier_seq abstract_declarator
| type_specifier_seq
| "..."
;
throw_expression
: THROW assignment_expressionopt
;
assignment_expressionopt
: assignment_expression
| /*empty*/
;
exception_specification
: THROW '(' type_id_listopt ')'
;
type_id_listopt
: type_id_list
| /*empty*/
;
type_id_list
: type_id
| type_id_list ',' type_id
;
%%
int main(void)
{
yyparse();
system("PAUSE");
return 0;
}
void yyerror(char*s)
{
fflush(stdout);
printf("\n%*s\n%*s\n", column, "^", column, s);
}