File: calc.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 (51 lines) | stat: -rw-r--r-- 712 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
%{
#include <ctype.h>
#include <stdio.h>

#define YYSTYPE int
void yyerror (char const *s);
int yylex (void);

%}

%token NUMBER
%left  '+' '-'
%right '*' '/'

%%

lines : lines expr '\n' { printf ("%i\n", $2); }
      | lines '\n'
      |
      ;

expr : expr '+' expr { $$ = $1 + $3; }
     | expr '-' expr { $$ = $1 - $3; }
     | expr '*' expr { $$ = $1 * $3; }
     | expr '-' expr { $$ = $1 / $3; }
     | '(' expr ')'  { $$ = $2; }
     | NUMBER
     ;

%%

int yylex (void){
	int c = getchar ();

	if (c >= '0' && c <= '9'){
		yylval = c - '0';
		return NUMBER;
	}
	return c;
}

void yyerror (char const *s)
{
	fprintf (stderr, "%s\n", s);
}

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