parser/calc/parser.y
author Markus Bröker <mbroeker@largo.localnet>
Thu, 16 Apr 2009 12:49:11 +0200
changeset 38 48f6f3918b82
parent 37 0fbbe329c3a2
child 39 46d7ec9d63bd
permissions -rw-r--r--
Project Split finished committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
     1
/**
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
     2
 * test/demos/parser/parser.yy
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
     3
 * Copyright (C) 2008 Markus Broeker
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
     4
 */
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
     5
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
     6
%{
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
     7
   #include <stdio.h>
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
     8
   #include <ctype.h>
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
     9
38
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    10
   int regs[26];
37
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
    11
   int base;
38
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    12
37
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
    13
   int yyerror();
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
    14
   int yylex();
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
    15
%}
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
    16
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
    17
%start list
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
    18
%token DIGIT LETTER
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
    19
%left MINUS PLUS MUL DIV
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
    20
%left UMINUS
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
    21
%%
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
    22
38
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    23
list:
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    24
	/* empty */
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    25
	| list stat '\n'
37
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
    26
	| list error '\n' { yyerrok; };
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
    27
38
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    28
stat:
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    29
	expr { (void) printf( "RESULT: %d\n", $1 ); }
37
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
    30
	| LETTER '=' expr { regs[$1] = $3; }
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
    31
	;
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
    32
38
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    33
expr:
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    34
	'(' expr ')'
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    35
	{
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    36
		$$ = $2;
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    37
	}
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    38
	| expr PLUS expr
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    39
	{
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    40
		$$ = $1 + $3;
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    41
	}
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    42
	| expr MINUS expr
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    43
	{
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    44
		$$ = $1 - $3;
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    45
	}
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    46
	| expr MUL expr
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    47
	{
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    48
		$$ = $1 * $3;
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    49
	}
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    50
	| expr DIV expr
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    51
	{
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    52
			$$ = $1 / $3;
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    53
	}
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    54
	| MINUS expr  %prec UMINUS
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    55
	{
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    56
		$$ = -$2;
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    57
	}
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    58
	| LETTER
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    59
	{
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    60
		$$ = regs[$1];
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    61
	}
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    62
	|  number
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    63
	;
37
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
    64
38
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    65
number:
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    66
	DIGIT
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    67
	{
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    68
		$$ = $1; base = ($1==0) ? 8 : 10;
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    69
	}
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    70
	| number DIGIT
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    71
	{
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    72
		$$ = base * $1 + $2;
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    73
	}
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    74
	;
37
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
    75
%%
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
    76
38
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    77
int yyerror()
37
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
    78
{
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
    79
	printf("ERROR\n");
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
    80
	return 0;
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
    81
}
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
    82
38
48f6f3918b82 Project Split finished
Markus Bröker <mbroeker@largo.localnet>
parents: 37
diff changeset
    83
int yywrap()
37
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
    84
{
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
    85
	return 1;
0fbbe329c3a2 demos/parser
Markus Bröker <mbroeker@largo.localnet>
parents:
diff changeset
    86
}