]> mj.ucw.cz Git - umpf.git/blob - cond.y
0155cbd353b24091ca7189972af03819652d23ee
[umpf.git] / cond.y
1 %{
2
3 #include <stdio.h>
4 #include <string.h>
5
6 int yylex (void);
7 void yyerror (char const *);
8
9 %}
10
11 %union {
12         int b;  
13         char* str;
14 }
15
16 %token <str> CONST
17 %left EQ
18 %left '|' '&' '^'
19 %left '!'
20 %type <b> boo
21
22 %%
23 input:  /* empty */
24         | input line
25 ;
26
27 line:   '\n'
28         | boo '\n'              { printf("%s\n",$1?"true":"false"); }
29         | error '\n'            { yyerrok; }
30 ;
31
32 boo:     CONST EQ CONST         { $$ = ! strcmp($1, $3); } 
33         | boo EQ boo            { $$ = $1 == $3 } 
34         | boo '|' boo           { $$ = $1 || $3 }
35         | boo '&' boo           { $$ = $1 && $3 }
36         | boo '^' boo           { $$ = ($1 || $3) && !($1 && $3) }
37         | '!' boo               { $$ = ! $2 }
38 ;
39
40 ;
41 %%
42
43 #include <ctype.h>
44 #include <stdlib.h>
45
46 #define BUFSIZE 4096
47
48 int
49 yylex(void)
50 {
51
52         int i, c, last;
53         
54         while ((c = getchar ()) == ' ' || c == '\t');
55         
56         if (c == '"'){
57                 last = '"';
58                 i = 0;
59                 if (!(yylval.str = malloc(BUFSIZE))){
60                         puts("Low memory");
61                         exit(0);
62                 }
63                 while ((c = getchar()) != '"' || last == '\\'){
64                         yylval.str[i] = c;
65                         last = c;
66                         i++;
67                         if (i >= BUFSIZE-1)
68                                 break;
69                 }       
70                 yylval.str[i] = '\0';
71         
72                 return CONST;   
73         }
74
75         if (c == '='){
76                 if ((c = getchar ()) == '=')
77                         return EQ;
78         }
79         
80         if (c == EOF)
81                 return 0;
82         
83         return c;
84 }
85
86 void
87 yyerror (char const *s)
88 {
89         fprintf (stderr, "%s\n", s);
90 }
91
92 int
93 main(void)
94 {
95         return yyparse ();
96 }