/* * PUBLIC DOMAIN PCCTS-BASED C++ GRAMMAR (cplusplus.g, stat.g, expr.g) * * Authors: Sumana Srinivasan, NeXT Inc.; sumana_srinivasan@next.com * Terence Parr, Parr Research Corporation; parrt@parr-research.com * Russell Quong, Purdue University; quong@ecn.purdue.edu * * VERSION 1.1 * * SOFTWARE RIGHTS * * This file is a part of the ANTLR-based C++ grammar and is free * software. We do not reserve any LEGAL rights to its use or * distribution, but you may NOT claim ownership or authorship of this * grammar or support code. An individual or company may otherwise do * whatever they wish with the grammar distributed herewith including the * incorporation of the grammar or the output generated by ANTLR into * commerical software. You may redistribute in source or binary form * without payment of royalties to us as long as this header remains * in all source distributions. * * We encourage users to develop parsers/tools using this grammar. * In return, we ask that credit is given to us for developing this * grammar. By "credit", we mean that if you incorporate our grammar or * the generated code into one of your programs (commercial product, * research project, or otherwise) that you acknowledge this fact in the * documentation, research report, etc.... In addition, you should say nice * things about us at every opportunity. * * As long as these guidelines are kept, we expect to continue enhancing * this grammar. Feel free to send us enhancements, fixes, bug reports, * suggestions, or general words of encouragement at parrt@parr-research.com. * * NeXT Computer Inc. * 900 Chesapeake Dr. * Redwood City, CA 94555 * 12/02/1994 * * Restructured for public consumption by Terence Parr late February, 1995. * * Requires PCCTS 1.32b4 or higher to get past ANTLR. * * DISCLAIMER: we make no guarantees that this grammar works, makes sense, * or can be used to do anything useful. */ class CPPParser { statement_list : (statement)+ ; statement : ( declaration )? | asm_statement | labeled_statement | expression SEMICOLON | compound_statement | selection_statement | iteration_statement | jump_statement | ";" | try_block | throw_statement ; labeled_statement : ID COLON statement | CASE constant_expression COLON statement | DEFAULT COLON statement ; compound_statement : LCURLYBRACE <> { generic_bloc } RCURLYBRACE <> ; generic_bloc : <<;>> <<;>> ( ~CURLY_TOKCLASS | LCURLYBRACE generic_bloc RCURLYBRACE )* ; argument_statement : LPARENTHESIS <> { generic_argument } RPARENTHESIS <> ; generic_argument : <<;>> <<;>> ( ~PARENTHESIS_TOKCLASS | LPARENTHESIS generic_argument RPARENTHESIS )* ; templ_arg_statement : "<" <> { generic_bloc_templ } ">" <> ; generic_bloc_templ : <<;>> <<;>> ( ~INF_SUP_TOKCLASS | "<" generic_bloc_templ ">" )* ; /* NOTE: cannot remove ELSE ambiguity, but it parses correctly. * The warning is removed with the #pragma */ selection_statement : IF LPARENTHESIS expression RPARENTHESIS statement #pragma approx { ELSE statement } | SWITCH LPARENTHESIS expression RPARENTHESIS statement ; iteration_statement : WHILE LPARENTHESIS expression RPARENTHESIS statement | DO statement WHILE LPARENTHESIS expression RPARENTHESIS SEMICOLON | "for" "\(" ( ( declaration )? | expression ";" |parameter_declaration ";" | ";" ) //parameter_declaration ";" { expression } ";" { expression } "\)" statement ; jump_statement : GOTO ID SEMICOLON | CONTINUE SEMICOLON | BREAK SEMICOLON | RETURN { expression } SEMICOLON//*** ; try_block : "try" compound_statement (handler)* ; handler : "catch" <> <> "\(" exception_declaration "\)" <> compound_statement <> ; exception_declaration : parameter_declaration_list | "..." ; /* This is an expression of type void according to the ARM, which * to me means "statement"; it removes some ambiguity to put it in * as a statement also. */ throw_statement : "throw" {assignment_expression} ";" ; asm_statement : // the difficulty of this case is // there are many way to declare an asm code // __asm { code on severals lines } or only { __asm code on 1 line } // ("__asm" | "asm") #pragma approx ( LCURLYBRACE (~RCURLYBRACE)* RCURLYBRACE | (~RCURLYBRACE)* ) ; }