1 %{ 2 #include "test2ast.h" 3 %} 4 %token COMMENT 5 %token IDENT 6 %token LITERAL 7 %token LPAREN 8 %token OPT 9 %token OR 10 %token PLUS 11 %token RPAREN 12 %token SEPARATOR 13 %token STAR 14 %token TERMINATOR 15 16 %union { 17 grammar* pgrammar; 18 production* pproduction; 19 lhs* plhs; 20 std::list<expressionList*>* pexpressionListList; 21 std::list<*>* pexpressionList; 22 expression* pexpression; 23 base* pbase; 24 alternation* palternation; 25 std::string* pstring; 26 } 27 28 %type <pgrammar> grammar 29 %type <pproduction> production 30 %type <plhs> lhs 31 %type <pexpressionListList> expressionListList 32 %type <pexpressionList> expressionList 33 %type <pexpression> expression 34 %type <pbase> base 35 %type <palternation> alternation 36 %type <pstring> COMMENT IDENT LITERAL LPAREN OPT OR PLUS RPAREN SEPARATOR STAR TERMINATOR 37 38 %% 39 40 41 42 grammar: 43 production 44 { $$ = new grammar_production($1) } 45 | grammar production 46 { $$ = new grammar_grammar_production($1, $2) } 47 | grammar COMMENT 48 { $$ = new grammar_grammar_COMMENT($1, $2) } 49 50 51 production: 52 lhs expressionListList TERMINATOR 53 { $$ = new production($1, $2, $3) } 54 55 56 lhs: 57 IDENT SEPARATOR 58 { $$ = new lhs($1, $2) } 59 60 61 expressionListList: 62 expressionList 63 { $$ = new std::list<expressionList*>(1, $1) } 64 | expressionListList OR expressionList 65 { $1->push_back($3); delete $2; $$ = $1; } 66 67 68 expressionList: 69 /* EMPTY */ 70 { $$ = new std::list<*>() } 71 | expressionList expression 72 { $1->push_back($2); $$ = $1; } 73 74 75 expression: 76 base 77 { $$ = new expression_base($1) } 78 | base OPT 79 { $$ = new expression_base_OPT($1, $2) } 80 | base STAR 81 { $$ = new expression_base_STAR($1, $2) } 82 | base PLUS 83 { $$ = new expression_base_PLUS($1, $2) } 84 | COMMENT 85 { $$ = new expression_COMMENT($1) } 86 87 88 base: 89 LITERAL 90 { $$ = new base_LITERAL($1) } 91 | IDENT 92 { $$ = new base_IDENT($1) } 93 | LPAREN expressionList RPAREN 94 { $$ = new base_LPAREN_expressionList_RPAREN($1, $2, $3) } 95 | LPAREN alternation RPAREN 96 { $$ = new base_LPAREN_alternation_RPAREN($1, $2, $3) } 97 98 99 alternation: 100 expression OR expression 101 { $$ = new alternation_expression_OR_expression($1, $2, $3) } 102 | alternation OR expression 103 { $$ = new alternation_alternation_OR_expression($1, $2, $3) } 104