/* * The file "calc.y": Parser definitions * Contains the grammar rules and semantic actions. * Used by bison as an input file. */ /*========== 1. The Definition Section ==============================*/ %{ #include #include #include #include /* The file "parser.h" contains the definition of YYSTYPE: */ /* the type of values in the semantic stack of parser */ #include "parser.h" /* Any C++ code may be added here... */ %} %term INT_CONST DOUBLE_CONST NAME %term ENDL LPAR RPAR ILLEGAL LBRACKET RBRACKET SM %term INT DOUBLE %left PLUS MINUS %left MULT DIV %start defs %% /*========== 2. The Grammar Section =========================================*/ /* Program is a sequence of lines containing expressions */ defs : def | defs def ; def : basetype decl SM { if ($1.int_value == 0) printf("int\n"); else printf("double\n"); } | error SM | SM { exit(0); } | ENDL { } ; basetype : INT { $$.int_value = 0; } | DOUBLE { $$.int_value = 1; } ; decl : array | MULT decl { printf("pointer to\n"); } ; array : NAME { printf("%s is\n", $1.name); } | array LBRACKET RBRACKET { printf("array of undefined size of type\n"); } | array LBRACKET INT_CONST RBRACKET { printf( "array of size %d of type\n", $3.int_value ); } | LPAR decl RPAR ; %% /*================ 3. The Program Section ================================*/ int main() { printf("Enter the definition of variable:\n"); yyparse(); return 0; } /* Parse error diagnostics */ int yyerror(const char *s) { printf("%s\n", s); return 0; }