File: iio_math_parser.y

package info (click to toggle)
gr-iio 0.3-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,368 kB
  • sloc: cpp: 3,155; xml: 1,468; lex: 107; yacc: 104; python: 60; makefile: 11; ansic: 9
file content (152 lines) | stat: -rw-r--r-- 3,618 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
%{
/* 
 * Copyright 2016 Analog Devices Inc.
 * Author: Paul Cercueil <paul.cercueil@analog.com>
 * 
 * This 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 3, or (at your option)
 * any later version.
 * 
 * This software 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 software; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

	#include "parser.h"

	#include <math.h>
	#include <stdio.h>

	int yylex(YYSTYPE *type, yyscan_t scanner);
	void yyerror(yyscan_t scanner, const char *msg);
%}

%code requires {
#ifndef YY_TYPEDEF_YY_SCANNER_T
#define YY_TYPEDEF_YY_SCANNER_T
typedef void *yyscan_t;
#endif

int yylex_init_extra(void *pdata, yyscan_t *scanner);
int yylex_destroy(yyscan_t yyscanner);

void * yyget_extra(yyscan_t scanner);

void * src_block(void *pdata, unsigned int input);
void * const_block(void *pdata, double value);
void * add_block(void *pdata, void *left, void *right);
void * sub_block(void *pdata, void *left, void *right);
void * mult_block(void *pdata, void *left, void *right);
void * div_block(void *pdata, void *left, void *right);
void * pow_block(void *pdata, void *left, void *right);
void * mod_block(void *pdata, void *left, void *right);
void * func_block(void *pdata, void *input, const char *name);
void * neg_block(void *pdata, void *input);

void connect_to_output(void *pdata, void *input);
void delete_block(void *pdata, void *block);
}

/* Bison declarations.  */
%define api.pure
%lex-param { yyscan_t scanner }
%parse-param { yyscan_t scanner }

%start Line

%union {
	double val;
	unsigned int ival;
	char *fname;
	void *block;
}

%token<fname> FNAME;
%token<val> VALUE;
%token<ival> IN_PORT;
%type<block> Element;
%type<block> Factor;
%type<block> Term;
%token POWER;

%left '-' '+' '*' '/' '%'
%precedence PREFIX
%right POWER

%destructor { free($$); } <fname>
%destructor { delete_block(yyget_extra(scanner), $$); } <block>

%%

Element:
	IN_PORT[t] {
		$$ = src_block(yyget_extra(scanner), $t);
	}
	| VALUE[t] {
		$$ = const_block(yyget_extra(scanner), $t);
	}
	| FNAME[n] '(' Term[t] ')' {
		$$ = func_block(yyget_extra(scanner), $t, $n);
	}
	| '(' Term[t] ')' {
		$$ = $t;
	}
	| '-' Element[t] %prec PREFIX {
		$$ = neg_block(yyget_extra(scanner), $t);
	}
	| '+' Element[t] %prec PREFIX {
		$$ = $t;
	}
	;

Factor:
	Element
	| Factor[f1] '*' Factor[f2] {
		$$ = mult_block(yyget_extra(scanner), $f1, $f2);
	}
	| Factor[f1] '/' Factor[f2] {
		$$ = div_block(yyget_extra(scanner), $f1, $f2);
	}
	| Factor[f1] POWER Factor[f2] {
		$$ = pow_block(yyget_extra(scanner), $f1, $f2);
	}
	| Factor[f1] '%' Factor[f2] {
		$$ = mod_block(yyget_extra(scanner), $f1, $f2);
		/* TODO(pcercuei): Implement modulo */
	}
	;

Term:
	Factor
	/* TODO(pcercuei): Implement const operations
	| Term '+' VALUE { }
	| Term '-' VALUE { }
	| VALUE '+' Term { }
	| VALUE '-' Term { }
	*/
	| Term[t1] '+' Term[t2] {
		$$ = add_block(yyget_extra(scanner), $t1, $t2);
	}
	| Term[t1] '-' Term[t2] {
		$$ = sub_block(yyget_extra(scanner), $t1, $t2);
	}
	;

Line:
	Term[t] {
		connect_to_output(yyget_extra(scanner), $[t]);
	}

%%

void yyerror(yyscan_t scanner, const char *msg)
{
	printf("ERROR: %s\n", msg);
}