1 2 3 4 5 6 7 | /* generate include-file with symbols and types */ %defines /* a more advanced semantic type */ %union { double value; char *string; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /* terminal symbols */ %token <string> IDENTIFIER %token <value> VALUE %type <value> expression /* operator-precedence * top-0: - * 1: * / * 2: + - */ %left ADD SUB %left MULT DIV %left NEG %start program |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | program : statement SEMICOLON program | statement SEMICOLON | statement error SEMICOLON program ; statement : IDENTIFIER ASSIGN expression | expression ; expression : LBRACE expression RBRACE | SUB expression %prec NEG | expression ADD expression | expression SUB expression | expression MULT expression | expression DIV expression | VALUE | IDENTIFIER ; |
1 2 3 4 | | expression DIV expression { $$ = ReduceDiv($1, $3); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | [ \t\r\n]+ { /* eat up whitespace */ } {DIGIT}+ { yylval.value = atof(yytext); return VALUE; } {DIGIT}+"."{DIGIT}* { yylval.value = atof(yytext); return VALUE; } {DIGIT}+[eE]["+""-"]?{DIGIT}* { yylval.value = atof(yytext); return VALUE; } {DIGIT}+"."{DIGIT}*[eE]["+""-"]?{DIGIT}* { yylval.value = atof(yytext); return VALUE; } {ID} { yylval.string = malloc(strlen(yytext)+1); strcpy(yylval.string, yytext); return IDENTIFIER; } "+" { return ADD; } "-" { return SUB; } "*" { return MULT; } "/" { return DIV; } "(" { return LBRACE; } ")" { return RBRACE; } ";" { return SEMICOLON; } "=" { return ASSIGN; } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |