]> mj.ucw.cz Git - umpf.git/blob - cond.y
uh
[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
45 #define BUFSIZE 4096
46
47 int
48 yylex(void)
49 {
50
51         int c, last;
52         char temp[BUFSIZE];
53         char* p=temp;
54         
55         while ((c = getchar ()) == ' ' || c == '\t');
56         
57         if (c == '"'){
58                 last = '"';
59                 while ((c = getchar()) != '"' || last == '\\'){
60                         *p = c;
61                         last = c;
62                         p++;
63                         if (p-temp >= BUFSIZE-1)
64                                 break;
65                 }       
66                 *p = '\0';
67         
68                 strcpy(&yylval,temp);
69                 return CONST;   
70         }
71
72         if (c == '='){
73                 if ((c = getchar ()) == '=')
74                         return EQ;
75         }
76         
77         if (c == EOF)
78                 return 0;
79         
80         return c;
81 }
82
83 void
84 yyerror (char const *s)
85 {
86         fprintf (stderr, "%s\n", s);
87 }
88
89 int
90 main(void)
91 {
92         return yyparse ();
93 }