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