[go: up one dir, main page]

File: expr_gram.y

package info (click to toggle)
cook 2.10-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 6,856 kB
  • ctags: 3,842
  • sloc: ansic: 47,714; sh: 12,150; makefile: 4,317; yacc: 3,104; awk: 154
file content (174 lines) | stat: -rw-r--r-- 3,408 bytes parent folder | download | duplicates (2)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 *	cook - file construction tool
 *	Copyright (C) 1997, 1998 Peter Miller;
 *	All rights reserved.
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 *
 * MANIFEST: functions to parse expression grammar
 */

%{

#include <ac/stdarg.h>
#include <ac/stdio.h>

#include <str.h>
#include <sub/expr_gram.h>
#include <sub/expr_lex.h>
#include <sub/private.h>
#include <trace.h>

#ifdef DEBUG
#define YYDEBUG 1
#endif

%}

%token DIV
%token JUNK
%token LP
%token MINUS
%token MUL
%token NUMBER
%token PLUS
%token RP

%union
{
	long	lv_number;
}

%type <lv_number> NUMBER expr

%left PLUS MINUS
%left MUL DIV
%right UNARY

%{

static	long		result;
static	sub_context_ty	*scp;


string_ty *
sub_expr_gram(p, s)
	sub_context_ty	*p;
	string_ty	*s;
{
	int		bad;
	extern int yyparse _((void));
#ifdef DEBUG
        extern int yydebug;
#endif

	trace(("sub_expr_gram()\n{\n"/*}*/));
	scp = p;
#ifdef DEBUG
	yydebug = trace_pretest_;
#endif
	sub_expr_lex_open(s);
	bad = yyparse();
	sub_expr_lex_close();
	trace(("bad = %d\n", bad));

	scp = 0;
	trace((/*{*/"}\n"));
	if (bad)
		return 0;
	return str_format("%ld", result);
}


void yyerror _((char *));

void
yyerror(s)
	char	*s;
{
	trace(("yyerror(\"%s\")\n{\n", s));
	sub_context_error_set(scp, s);
	trace((/*{*/"}\n"));
}

#ifdef DEBUG

/*
 * jiggery-pokery for yacc
 *
 *	Replace all calls to printf with a call to trace_printf.  The
 *	trace_where_ is needed to set the location, and is safe, because
 *	yacc only invokes the printf with an if (be careful, the printf
 *	is not in a compound statement).
 */
#define printf trace_where_, trace_printf

/*
 * jiggery-pokery for bison
 *
 *	Replace all calls to fprintf with a call to yydebugger.  Ignore
 *	the first argument, it will be ``stderr''.  The trace_where_ is
 *	needed to set the location, and is safe, because bison only
 *	invokes the printf with an if (be careful, the fprintf is not in
 *	a compound statement).
 */
#define fprintf trace_where_, yydebugger

void yydebugger _((void *, char *, ...));

void
yydebugger(junk, fmt sva_last)
	void		*junk;
	char		*fmt;
	sva_last_decl
{
	va_list		ap;
	string_ty	*s;

	sva_init(ap, fmt);
	s = str_vformat(fmt, ap);
	va_end(ap);
	trace_printf("%s", s->str_text);
	str_free(s);
}

#endif

%}

%%

grammar:
	expr
		{ result = $1; }
	;

expr
	: LP expr RP
		{ $$ = $2; trace(("$$ = %ld;\n", $$)); }
	| NUMBER
		{ $$ = $1; trace(("$$ = %ld;\n", $$)); }
	| MINUS expr
		%prec UNARY
		{ $$ = -$2; trace(("$$ = %ld;\n", $$)); }
	| expr PLUS expr
		{ $$ = $1 + $3; trace(("$$ = %ld;\n", $$)); }
	| expr MINUS expr
		{ $$ = $1 - $3; trace(("$$ = %ld;\n", $$)); }
	| expr MUL expr
		{ $$ = $1 * $3; trace(("$$ = %ld;\n", $$)); }
	| expr DIV expr
		{ $$ = $3 ? $1 / $3 : 0; trace(("$$ = %ld;\n", $$)); }
	;