Warning, /kdevelop/kdevelop-pg-qt/examples/cc/cc.g is written in an unsupported language. File is not indexed.
0001 -- Copyright (C) 2005 Roberto Raggi <roberto@kdevelop.org> 0002 -- Copyright (C) 2009 Jonathan Schmidt-Dominé <devel@the-user.org> 0003 -- 0004 -- This library is free software; you can redistribute it and/or 0005 -- modify it under the terms of the GNU Library General Public 0006 -- License as published by the Free Software Foundation; either 0007 -- version 2 of the License, or (at your option) any later version. 0008 -- 0009 -- This library is distributed in the hope that it will be useful, 0010 -- but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 -- Library General Public License for more details. 0013 -- 0014 -- You should have received a copy of the GNU Library General Public License 0015 -- along with this library; see the file COPYING.LIB. If not, write to 0016 -- the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 0017 -- Boston, MA 02111-1307, USA. 0018 0019 [: 0020 #include <QString> 0021 #include <QDebug> 0022 :] 0023 0024 %parserclass (protected declaration) 0025 [: 0026 void expectedSymbol(cc::AstNode::AstNodeKind kind, const QString& name) { qWarning() << "In AstNode " << kind << ": Expected symbol " << name; } 0027 void expectedToken(int kind, enum TokenType token, const QString& name) { qWarning() << "In AstNode " << kind << ": Expected token " << name << " (" << token << ")";} 0028 struct ParserState { 0029 int ltCounter; 0030 }; 0031 ParserState m_state; 0032 :] 0033 0034 %parser_declaration_header "ccast.h" 0035 0036 ------------------------------------------------------------ 0037 -- T O K E N L I S T 0038 ------------------------------------------------------------ 0039 0040 -- keywords: 0041 %token CASE ("case"), DEFAULT ("default"), IF ("if"), ELSE ("else"), 0042 SWITCH ("switch"), WHILE ("while"), DO ("do"), FOR ("for"), 0043 BREAK ("break"), CONTINUE ("continue"), GOTO ("goto"), 0044 RETURN ("return"), TYPEDEF ("typedef"), EXTERN ("extern"), 0045 STATIC ("static"), AUTO ("auto"), REGISTER ("register"), VOID ("void"), 0046 CHAR ("char"), SHORT ("short"), INT ("int"), LONG ("long"), 0047 FLOAT ("float"), DOUBLE ("double"), SIGNED ("signed"), 0048 UNSIGNED ("unsigned"), TYPEDEF_NAME ("pre-defined type specification"), 0049 STRUCT ("struct"), UNION ("union"), ENUM ("enum"), CONST ("const"), 0050 VOLATILE ("volatile") ;; 0051 0052 -- seperators: 0053 %token LPAREN ("("), RPAREN (")"), LBRACE ("{"), RBRACE ("}"), LBRACKET ("["), 0054 RBRACKET ("]"), DOT ("."), ARROW ("->"), COLON (":"), COMMA (","), 0055 SEMICOLON (";") ;; 0056 0057 -- operators: 0058 %token PLUS ("+"), MINUS ("-"), STAR ("*"), DIVIDE ("/"), REMAINDER ("%"), 0059 TILDE ("~"), AND ("&"), OR ("|"), XOR ("^"), NOT ("!"), 0060 SIZEOF ("sizeof"), PLUS_PLUS ("++"), MINUS_MINUS ("--"), LSHIFT ("<<"), 0061 RSHIFT (">>"), AND_AND ("&&"), OR_OR ("||"), QUESTION ("?"), 0062 EQUAL ("="), PLUS_EQUAL ("+="), MINUS_EQUAL ("-="), STAR_EQUAL ("*="), 0063 DIVIDE_EQUAL ("/="), REMAINDER_EQUAL ("%="), AND_EQUAL ("&="), 0064 OR_EQUAL ("|="), XOR_EQUAL ("^="), LSHIFT_EQUAL ("<<="), 0065 RSHIFT_EQUAL (">>="), EQUAL_EQUAL ("=="), NOT_EQUAL ("!="), 0066 LESS ("<"), GREATER (">"), LESS_EQUAL ("<="), GREATER_EQUAL (">="), 0067 ELLIPSIS ("...") ;; 0068 0069 -- identifiers and literals: 0070 %token IDENTIFIER ("identifier"), STRING_LITERAL ("string literal"), 0071 X_CONSTANT ;; 0072 0073 -- GCC extensions 0074 %token INLINE ("inline"), EXTENSION ("__extension__"), ASM ("asm") ;; 0075 0076 0077 0078 0079 ------------------------------------------------------------ 0080 -- E X T E R N A L D E C L A R A T I O N S 0081 ------------------------------------------------------------ 0082 (#ddeclaration=ddeclaration)* 0083 -> document ;; 0084 0085 enum_specifier=enum_specifier SEMICOLON 0086 | struct_or_union_specifier=struct_or_union_specifier SEMICOLON 0087 | TYPEDEF typedef_d=typedef_d SEMICOLON 0088 | try/rollback(external_block=external_block) catch( 0089 value_declaration=value_declaration 0090 ) 0091 -> ddeclaration ;; 0092 0093 typed_identifier ( SEMICOLON 0094 | try/rollback(function_definition=function_definition) catch( 0095 function_declaration=function_declaration 0096 )) 0097 -> value_declaration ;; 0098 0099 EXTERN STRING_LITERAL LBRACE (#ddeclaration=ddeclaration)* RBRACE 0100 -> external_block ;; 0101 0102 -- VOID 0103 -- | CHAR 0104 -- | SHORT 0105 -- | INT 0106 -- | LONG 0107 -- | FLOAT 0108 -- | DOUBLE 0109 ---> type_name_d ;; 0110 0111 LPAREN type_attribute_identifier=type_attribute_identifier RPAREN (LPAREN (0 | (#function_pointer_parameter=function_pointer_parameter @ COMMA)) RPAREN | 0) 0112 | STAR type_attribute_identifier=type_attribute_identifier 0113 | type_attribute_identifier=type_attribute_identifier LBRACKET (0 | X_CONSTANT) RBRACKET 0114 | IDENTIFIER 0115 -> type_attribute_identifier ;; 0116 0117 type_name=type_name type_attribute_identifier=type_attribute_identifier 0118 -> typed_identifier ;; 0119 0120 typed_identifier=typed_identifier SEMICOLON 0121 -> variable_declaration ;; 0122 0123 try/rollback(typed_identifier=typed_identifier) catch( 0124 type_name=type_name 0125 ) 0126 -> parameter ;; 0127 0128 struct_or_union_specifier=struct_or_union_specifier 0129 | enum_specifier=enum_specifier 0130 | typed_identifier=typed_identifier 0131 -> typedef_d ;; 0132 0133 (#statement = statement)* 0134 -> execution_block ;; 0135 0136 LPAREN (0|#declaration_parameter=declaration_parameter @ COMMA) RPAREN (0|asm_against_mangling=asm_against_mangling) SEMICOLON 0137 -> function_declaration ;; 0138 0139 typed_identifier=typed_identifier (0 | EQUAL constant_expression=constant_expression) 0140 | ELLIPSIS 0141 -> named_parameter ;; 0142 0143 try/rollback(typed_identifier=typed_identifier (0 | EQUAL constant_expression=constant_expression)) catch( 0144 type_name=type_name 0145 ) 0146 | ELLIPSIS 0147 -> declaration_parameter ;; 0148 0149 try/rollback(typed_identifier=typed_identifier) catch( 0150 type_name=type_name 0151 ) 0152 | ELLIPSIS 0153 -> function_pointer_parameter ;; 0154 0155 LPAREN (#named_parameter=named_parameter @ COMMA) RPAREN LBRACE execution_block=execution_block RBRACE 0156 -> function_definition ;; 0157 0158 -- (#external_declaration=external_declaration)* 0159 ---> translation_unit ;; 0160 0161 #declaration_specifier=declaration_specifier (#declaration_specifier=declaration_specifier)* 0162 -> declaration_header ;; 0163 0164 -- declaration_header=declaration_header variable_or_function=variable_or_function 0165 ---> external_declaration ;; 0166 0167 -- declarator=declarator (COMMA #init_declarator=init_declarator @ COMMA SEMICOLON 0168 -- | SEMICOLON 0169 -- | ?[:is_fun_definition:] declaration* compound_statement 0170 -- | (#declaration=declaration)* compound_statement=compound_statement 0171 -- | initializer=initializer (COMMA #init_declarator=init_declarator)* SEMICOLON) 0172 ---> variable_or_function ;; 0173 0174 ------------------------------------------------------------ 0175 -- GCC-STUFF 0176 ------------------------------------------------------------ 0177 STRING_LITERAL LPAREN IDENTIFIER 0178 -> asm_specifier ;; 0179 0180 ASM (0 | VOLATILE) LPAREN (STRING_LITERAL*) (0 | COLON (#output_operands=asm_specifier @ COMMA) (0 | COLON (#input_operands=asm_specifier @ COMMA) (0 | COLON (STRING_LITERAL @ COMMA)))) 0181 -> inline_asm ;; 0182 0183 EXTENSION LPAREN LBRACE execution_block RBRACE RPAREN 0184 -> ext_expression ;; 0185 0186 ASM LPAREN STRING_LITERAL RPAREN 0187 -> asm_against_mangling ;; 0188 0189 ------------------------------------------------------------ 0190 -- E X P R E S S I O N S 0191 ------------------------------------------------------------ 0192 0193 identifier=IDENTIFIER 0194 | constant=constant 0195 | string_literal=STRING_LITERAL 0196 | LPAREN expression=expression RPAREN 0197 -> primary_expression ;; 0198 0199 primary_expression=primary_expression (#postfix_expression_rest=postfix_expression_rest)* 0200 -> postfix_expression ;; 0201 0202 (DOT | ARROW) IDENTIFIER 0203 | PLUS_PLUS 0204 | MINUS_MINUS 0205 | LPAREN (argument_expression_list=argument_expression_list | 0) RPAREN 0206 | LBRACKET expression=expression RBRACKET 0207 -> postfix_expression_rest ;; 0208 0209 #assignment_expression=assignment_expression @ COMMA 0210 -> argument_expression_list ;; 0211 0212 postfix_expression=postfix_expression 0213 | PLUS_PLUS unary_expression=unary_expression 0214 | MINUS_MINUS unary_expression=unary_expression 0215 | unary_operator cast_expression=cast_expression 0216 | SIZEOF LPAREN type_name=type_name RPAREN 0217 -> unary_expression ;; 0218 0219 AND 0220 | STAR 0221 | PLUS 0222 | MINUS 0223 | TILDE 0224 | NOT 0225 -> unary_operator ;; 0226 0227 LPAREN type_name=type_name RPAREN cast_expression=cast_expression 0228 | unary_expression=unary_expression 0229 -> cast_expression ;; 0230 0231 #cast_expression=cast_expression @ (STAR | DIVIDE | REMAINDER) 0232 -> multiplicative_expression ;; 0233 0234 #multiplicative_expression=multiplicative_expression @ (PLUS | MINUS) 0235 -> additive_expression ;; 0236 0237 #additive_expression=additive_expression @ (LSHIFT | RSHIFT) 0238 -> shift_expression ;; 0239 0240 #shift_expression=shift_expression @ (LESS | GREATER | LESS_EQUAL | GREATER_EQUAL) 0241 -> relational_expression ;; 0242 0243 #relational_expression=relational_expression @ (EQUAL_EQUAL | NOT_EQUAL) 0244 -> equality_expression ;; 0245 0246 #equality_expression=equality_expression @ AND 0247 -> AND_expression ;; 0248 0249 #AND_expression=AND_expression @ XOR 0250 -> exclusive_OR_expression ;; 0251 0252 #exclusive_OR_expression=exclusive_OR_expression @ OR 0253 -> inclusive_OR_expression ;; 0254 0255 #inclusive_OR_expression=inclusive_OR_expression @ AND_AND 0256 -> logical_AND_expression ;; 0257 0258 #logical_AND_expression=logical_AND_expression @ OR_OR 0259 -> logical_OR_expression ;; 0260 0261 logical_OR_expression=logical_OR_expression (QUESTION expression COLON conditional_expression | 0) 0262 -> conditional_expression ;; 0263 0264 #conditional_expression=conditional_expression @ assignment_operator 0265 -> assignment_expression ;; 0266 0267 EQUAL 0268 | STAR_EQUAL 0269 | DIVIDE_EQUAL 0270 | REMAINDER_EQUAL 0271 | PLUS_EQUAL 0272 | MINUS_EQUAL 0273 | LSHIFT_EQUAL 0274 | RSHIFT_EQUAL 0275 | AND_EQUAL 0276 | XOR_EQUAL 0277 | OR_EQUAL 0278 -> assignment_operator ;; 0279 0280 #assignment_expression=assignment_expression @ COMMA 0281 -> expression ;; 0282 0283 conditional_expression=conditional_expression 0284 -> constant_expression ;; 0285 0286 X_CONSTANT 0287 -> constant ;; 0288 0289 ------------------------------------------------------------ 0290 -- S T A T E M E N T S 0291 ------------------------------------------------------------ 0292 IDENTIFIER COLON 0293 | labeled_statement=labeled_statement 0294 | compound_statement=compound_statement 0295 | expression_statement=expression_statement 0296 | selection_statement=selection_statement 0297 | iteration_statement=iteration_statement 0298 | jump_statement=jump_statement 0299 | inline_asm 0300 | SEMICOLON 0301 -> statement ;; 0302 0303 CASE constant_expression=constant_expression COLON statement=statement 0304 | DEFAULT COLON statement=statement 0305 -> labeled_statement ;; 0306 0307 LBRACE (#declaration=declaration)* (#statement=statement)* RBRACE 0308 -> compound_statement ;; 0309 0310 expression=expression SEMICOLON 0311 -> expression_statement ;; 0312 0313 IF LPAREN expression=expression RPAREN statement=statement (ELSE alternative_statement=statement | 0) 0314 | SWITCH LPAREN expression=expression RPAREN statement=statement 0315 -> selection_statement ;; 0316 0317 WHILE LPAREN (expression=expression | ext_expression=ext_expression) RPAREN statement=statement 0318 | DO statement WHILE LPAREN expression=expression RPAREN SEMICOLON 0319 | FOR LPAREN (for_1=expression|for1_ext=ext_expression|0) SEMICOLON (for_2=expression|for2_ext=ext_expression|0) SEMICOLON (for_3=expression|for3_ext=ext_expression|0) RPAREN statement=statement 0320 -> iteration_statement ;; 0321 0322 GOTO IDENTIFIER SEMICOLON 0323 | CONTINUE SEMICOLON 0324 | BREAK SEMICOLON 0325 | RETURN (expression=expression | 0) SEMICOLON 0326 -> jump_statement ;; 0327 0328 ------------------------------------------------------------ 0329 -- D E C L A R A T I O N S 0330 ------------------------------------------------------------ 0331 0332 #declaration_specifier=declaration_specifier (#declaration_specifier=declaration_specifier)* (#init_declarator=init_declarator @ COMMA | 0) SEMICOLON 0333 -> declaration ;; 0334 0335 storage_class_specifier=storage_class_specifier 0336 | type_specifier=type_specifier 0337 | type_qualifier=type_qualifier 0338 -> declaration_specifier ;; 0339 0340 declarator=declarator (EQUAL initializer=initializer | 0) 0341 -> init_declarator ;; 0342 0343 TYPEDEF 0344 | EXTERN 0345 | STATIC 0346 | AUTO 0347 | REGISTER 0348 -> storage_class_specifier ;; 0349 0350 VOID 0351 | CHAR 0352 | SHORT 0353 | INT 0354 | LONG 0355 | FLOAT 0356 | DOUBLE 0357 | SIGNED 0358 | UNSIGNED 0359 | struct_or_union_specifier=struct_or_union_specifier 0360 | enum_specifier=enum_specifier 0361 | TYPEDEF_NAME 0362 -> type_specifier ;; 0363 0364 -- EXTERN STRING_LITERAL LBRACE (#external_declaration=external_declaration)* RBRACE 0365 ---> external_block ;; 0366 0367 (STRUCT | UNION) (IDENTIFIER LBRACE (#struct_declaration=struct_declaration)* RBRACE 0368 | LBRACE (#struct_declaration=struct_declaration)* RBRACE) 0369 -> struct_or_union_specifier ;; 0370 0371 #specifier_qualifier=specifier_qualifier #specifier_qualifier=specifier_qualifier (#specifier_qualifier=specifier_qualifier*) (#struct_declarator=struct_declarator)* SEMICOLON 0372 -> struct_declaration ;; 0373 0374 type_specifier=type_specifier 0375 | type_qualifier=type_qualifier 0376 -> specifier_qualifier ;; 0377 0378 declarator=declarator (constant_expression=constant_expression | 0) 0379 | COLON constant_expression=constant_expression 0380 -> struct_declarator ;; 0381 0382 ENUM (IDENTIFIER LBRACE #enumerator=enumerator @ COMMA RBRACE | LBRACE #enumerator=enumerator @ COMMA RBRACE) 0383 -> enum_specifier ;; 0384 0385 IDENTIFIER (EQUAL constant_expression=constant_expression | 0) 0386 -> enumerator ;; 0387 0388 CONST 0389 | VOLATILE 0390 -> type_qualifier ;; 0391 0392 (pointer=pointer direct_declarator=direct_declarator | direct_declarator=direct_declarator) (#direct_declarator_rest=direct_declarator_rest)* 0393 | direct_declarator=direct_declarator 0394 -> declarator ;; 0395 0396 IDENTIFIER 0397 | LPAREN declarator=declarator RPAREN 0398 -> direct_declarator ;; 0399 0400 LBRACKET (constant_expression | 0) RBRACKET 0401 | LPAREN (IDENTIFIER @ COMMA | parameter_type_list=parameter_type_list | 0) RPAREN 0402 -> direct_declarator_rest ;; 0403 0404 STAR (#type_qualifier=type_qualifier | STAR)* 0405 -> pointer ;; 0406 0407 (#parameter_declaration=parameter_declaration | ELLIPSIS) @ COMMA 0408 | 0 0409 -> parameter_type_list ;; 0410 0411 #declaration_specifier=declaration_specifier (#declaration_specifier=declaration_specifier)* (declarator=declarator | abstract_declarator=abstract_declarator | 0) 0412 -> parameter_declaration ;; 0413 0414 #specifier_qualifier=specifier_qualifier (#specifier_qualifier=specifier_qualifier)* 0415 -> type_name ;; 0416 0417 (pointer=pointer #direct_abstract_declarator=direct_abstract_declarator | #direct_abstract_declarator=direct_abstract_declarator) (#direct_abstract_declarator=direct_abstract_declarator)* 0418 -> abstract_declarator ;; 0419 0420 LPAREN (abstract_declarator=abstract_declarator | parameter_type_list=parameter_type_list) RPAREN 0421 | LBRACKET (constant_expression | 0) RBRACKET 0422 -> direct_abstract_declarator ;; 0423 0424 assignment_expression=assignment_expression 0425 | LBRACE #initializer=initializer (COMMA (#initializer=initializer | 0))* RBRACE 0426 -> initializer ;; 0427 0428 [: 0429 namespace cc 0430 { 0431 Parser::ParserState *Parser::copyCurrentState() 0432 { 0433 ParserState *state = new ParserState(); 0434 state->ltCounter = m_state.ltCounter; 0435 return state; 0436 } 0437 0438 void Parser::restoreState( Parser::ParserState *state ) 0439 { 0440 m_state.ltCounter = state->ltCounter; 0441 } 0442 }; 0443 :]