flex

 1 %{
 2 #include <stdio.h>
 3 #include "mycalc.tab.h"
 4 
 5 int yywrap(void) {return 1;}
 6 %}
 7 
 8 %%
 9 
10 "+"    return ADD;
11 "-"    return SUB;
12 "*"    return MUL;
13 "/"    return DIV;
14 "("    return LP;
15 ")"    return RP;
16 "\n"   return CR;
17 ([1-9][0-9]*)|0|([0-9]+\.[0-9]+) {
18     double temp;
19     sscanf(yytext, "%lf", &temp);
20     yylval.double_value = temp;
21     return DOUBLE_LITERAL;
22 }
23 [ \t] ;
24 . {
25     fprintf(stderr, "lexical error.\n");
26     exit(1);
27 }
28 
29 %%

bison

 1 %{
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #define YYDEBUG 1
 5 %}
 6 %union {
 7     int int_value;
 8     double double_value;
 9 }
10 %token <double_value> DOUBLE_LITERAL
11 %token ADD SUB MUL DIV CR LP RP
12 %type <double_value> expression term primary_expression
13 
14 %%
15 
16 line_list
17     : line
18     | line_list line
19     ;
20 line
21     : expression CR
22     {
23         printf(">>%lf\n", $1);
24     }
25 expression
26     : term
27     | expression ADD term
28     {
29         $$ = $1 + $3;
30     }
31     | expression SUB term
32     {
33         $$ = $1 - $3;
34     }
35     ;
36 term
37     : primary_expression
38     | term MUL primary_expression
39     {
40         $$ = $1 * $3;
41     }
42     | term DIV primary_expression
43     {
44         $$ = $1 / $3;
45     }
46     ;
47 primary_expression
48     : DOUBLE_LITERAL
49     | SUB primary_expression
50     {
51         $$ = -$2;
52     }
53     | LP expression RP
54     {
55         $$ = $2;
56     }
57    ;
58 
59 %%
60 
61 int yyerror(char const *str) 
62 {
63     extern char *yytext;
64     fprintf(stderr, "parser error near %s\n", yytext);
65     return 0;
66 }
67 
68 int main()
69 {
70     extern int yyparse(void);
71     extern FILE *yyin;
72     yyin = stdin;
73     if (yyparse()) 
74     {
75         fprintf(stderr, "Error ! Error! Error!\n");
76         exit(1);
77     }
78     return 0;
79 }

bison -dv mycalc.y

flex mycalc.l

cc -o mycalc mycalc.tab.c lex.yy.c

 

 1 /* A Bison parser, made by GNU Bison 3.0.2.  */
 2 
 3 /* Bison interface for Yacc-like parsers in C
 4 
 5    Copyright (C) 1984, 1989-1990, 2000-2013 Free Software Foundation, Inc.
 6 
 7    This program is free software: you can redistribute it and/or modify
 8    it under the terms of the GNU General Public License as published by
 9    the Free Software Foundation, either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19 
20 /* As a special exception, you may create a larger work that contains
21    part or all of the Bison parser skeleton and distribute that work
22    under terms of your choice, so long as that work isn't itself a
23    parser generator using the skeleton or a modified version thereof
24    as a parser skeleton.  Alternatively, if you modify or redistribute
25    the parser skeleton itself, you may (at your option) remove this
26    special exception, which will cause the skeleton and the resulting
27    Bison output files to be licensed under the GNU General Public
28    License without this special exception.
29 
30    This special exception was added by the Free Software Foundation in
31    version 2.2 of Bison.  */
32 
33 #ifndef YY_YY_MYCALC_TAB_H_INCLUDED
34 # define YY_YY_MYCALC_TAB_H_INCLUDED
35 /* Debug traces.  */
36 #ifndef YYDEBUG
37 # define YYDEBUG 0
38 #endif
39 #if YYDEBUG
40 extern int yydebug;
41 #endif
42 
43 /* Token type.  */
44 #ifndef YYTOKENTYPE
45 # define YYTOKENTYPE
46   enum yytokentype
47   {
48     DOUBLE_LITERAL = 258,
49     ADD = 259,
50     SUB = 260,
51     MUL = 261,
52     DIV = 262,
53     CR = 263,
54     LP = 264,
55     RP = 265
56   };
57 #endif
58 
59 /* Value type.  */
60 #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
61 typedef union YYSTYPE YYSTYPE;
62 union YYSTYPE
63 {
64 #line 6 "mycalc.y" /* yacc.c:1909  */
65 
66     int int_value;
67     double double_value;
68 
69 #line 70 "mycalc.tab.h" /* yacc.c:1909  */
70 };
71 # define YYSTYPE_IS_TRIVIAL 1
72 # define YYSTYPE_IS_DECLARED 1
73 #endif
74 
75 
76 extern YYSTYPE yylval;
77 
78 int yyparse (void);
79 
80 #endif /* !YY_YY_MYCALC_TAB_H_INCLUDED  */
mycalc.tab.h

相关文章: