File: parser.y

package info (click to toggle)
mk-configure 0.37.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 4,112 kB
  • sloc: ansic: 5,441; makefile: 1,412; sh: 1,086; cpp: 200; perl: 101; yacc: 85; lex: 21
file content (57 lines) | stat: -rw-r--r-- 846 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

%{
#include <stdio.h>
#include <mkc_libm.h>

int yyerror (char *s);
int yylex (void);

%}

%union {
	double value;
	char* name;
}

%token <value> NUMBER

%type  <value>  expr

%left '+' '-'
%right SQRT
%left '*' '/'
%right '^'
%right UMINUS

%%
lines:	lines expr '\n'		{ printf("%.10g\n", $2); }
	|	lines '\n'
	|	error '\n' 		{ printf("Please re-enter last line: ");
	                	  yyerrok; }
	|
	;

expr:	expr '+' expr	{ $$ = $1 + $3; }
	|	expr '-' expr	{ $$ = $1 - $3; }
	|	expr '*' expr	{ $$ = $1 * $3; }
	|	expr '/' expr	{ $$ = $1 / $3; }
	|	expr '^' expr	{ $$ = pow($1, $3); }
	|	'(' expr ')'	{ $$ = $2; }
	|	'-' expr  %prec UMINUS { $$ = -$2; }
	|	NUMBER
	;

%%
#include <ctype.h>
#include <stdio.h>

int main (int argc, char **argv)
{
	return yyparse ();
}

int yyerror (char* errstr)
{
	printf ("Error: %s\n", errstr);
	return 1;
}