]> mj.ucw.cz Git - umpf.git/blob - cond.y
uaaa!
[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
21 %%
22 input:  /* empty */
23         | input line
24 ;
25
26 line:   '\n'
27         | boo '\n'              { printf("%s\n",$1?"true":"false"); }
28 ;
29
30 boo:
31         CONST EQ CONST  { $$ = ! strcmp($1, $2); } 
32         | boo EQ boo            { $$ = $1 == $2 } 
33         | boo '|' boo           { $$ = $1 || $2 }
34         | boo '&' boo           { $$ = $1 && $2 }
35         | boo '^' boo           { $$ = ($1 || $2) && !($1 && $2) }
36         | '!' boo               { $$ = ! $1 }
37 ;
38
39 ;
40 %%
41
42 #include <ctype.h>
43
44 #define BUFSIZE 4096
45
46 int
47 yylex(void)
48 {
49
50         int c, last;
51         char temp[BUFSIZE];
52         char* p=temp;
53         
54         while ((c = getchar ()) == ' ' || c == '\t');
55         
56         if (c == '"'){
57                 last = '"';
58                 while ((c = getchar()) != '"' || last == '\\'){
59                         *p = c;
60                         last = c;
61                         p++;
62                         if (p-temp >= BUFSIZE-1)
63                                 break;
64                 }       
65                 *p = '\0';
66         
67                 strcpy(&yylval,temp);
68                 return CONST;   
69         }
70
71         if (c == '='){
72                 if ((c = getchar ()) == '=')
73                         return EQ;
74         }
75         
76         if (c == EOF)
77                 return 0;
78         
79         return c;
80 }
81
82 void
83 yyerror (char const *s)
84 {
85         fprintf (stderr, "%s\n", s);
86 }
87
88 int
89 main(void)
90 {
91         return yyparse ();
92 }