home *** CD-ROM | disk | FTP | other *** search
Lex Description | 1991-12-14 | 4.2 KB | 202 lines |
- %{
- #include <stdlib.h>
- #include <ctype.h>
- #include <stdio.h>
- #include "pico.h"
-
- static int yylex(void);
- static void yyerror(char *);
-
- static lastnum;
- static char lastname[1024];
- %}
-
- %union {
- Tree *node;
- int num;
- }
-
- %term ANDAND OROR EQ NE LE GE LL RR ICON NAME
- %type <node> cmd expr ss or xor and eq rel shift
- %type <node> plus times factor coord
- %type <num> file
-
- %%
-
- pico : /* empty */ { tree = NULL; YYACCEPT; }
- | '\n' { tree = NULL; YYACCEPT; }
- | error '\n' { tree = NULL; YYABORT; }
- | cmd '\n' { tree = $1; YYACCEPT; }
-
- cmd : expr
- | 'w' NAME { $$ = mkun(Write,lastname); }
- | 'r' NAME { $$ = mkun(Read,lastname); }
- | 'f' { $$ = mkempty(Files); }
- | 'u' { $$ = mkempty(Undo); }
- | 'q' { $$ = mkempty(Quit); }
-
- expr : ss
- | ss '?' expr ':' expr { $$ = mkCond($1,$3,$5); }
-
- ss : or
- | ss ANDAND or { $$ = mk(Andalso,$1,$3); }
- | ss OROR or { $$ = mk(Orelse,$1,$3); }
-
- or : xor
- | or '|' xor { $$ = mk(Or,$1,$3); }
-
- xor : and
- | xor '^' and { $$ = mk(Xor,$1,$3); }
-
- and : eq
- | and '&' eq { $$ = mk(And,$1,$3); }
-
- eq : rel
- | eq EQ rel { $$ = mk(Eq,$1,$3); }
- | eq NE rel { $$ = mk(Ne,$1,$3); }
-
- rel : shift
- | rel '<' shift { $$ = mk(Lt,$1,$3); }
- | rel '>' shift { $$ = mk(Gt,$1,$3); }
- | rel LE shift { $$ = mk(Ge,$1,$3); }
- | rel GE shift { $$ = mk(Le,$1,$3); }
-
- shift : plus
- | shift LL plus { $$ = mk(Ll,$1,$3); }
- | shift RR plus { $$ = mk(Rr,$1,$3); }
-
-
- plus : times
- | plus '+' times { $$ = mk(Add,$1,$3); }
- | plus '-' times { $$ = mk(Sub,$1,$3); }
-
- times : factor
- | times '*' factor { $$ = mk(Mult,$1,$3); }
- | times '/' factor { $$ = mk(Div,$1,$3); }
- | times '%' factor { $$ = mk(Mod,$1,$3); }
-
- factor : 'x' { $$ = mkempty(Xcoord); }
- | 'y' { $$ = mkempty(Ycoord); }
- | ICON { $$ = mkNum(lastnum); }
- | '+' factor { $$ = $2; }
- | '-' factor { $$ = mkun(Neg,$2); }
- | '!' factor { $$ = mkun(Bang,$2); }
- | '~' factor { $$ = mkun(Not,$2); }
- | file coord { $$ = mkCoord($1,$2); }
- | '(' expr ')' { $$ = $2; }
-
- file : NAME { $$ = findfile(lastname); }
- | '$' ICON { $$ = lastnum + 1; }
-
- coord : /* nada */ { $$ = NULL; }
- | '[' expr ',' expr ']' { $$ = mk(Add,$2,mk(Mult,$4,mkNum(DEF_Y))); }
- %%
-
- static int yylex() {
- char *l;
- int c;
- while ((c = getchar()) == ' ' || c == '\t')
- ;
- switch (c) {
- case EOF:
- saw_eof = 1;
- return 0;
- case '\n':
- case '?': case ':':
- case '-': case '+':
- case '*': case '/': case '%':
- case '~': case '^':
- case '(': case ')':
- case '[': case ']':
- case ',': case '$':
- return c;
- case '&':
- if ((c = getchar()) == '&')
- return ANDAND;
- ungetc(c,stdin);
- return '&';
- case '|':
- if ((c = getchar()) == '|')
- return OROR;
- ungetc(c,stdin);
- return '|';
- case '>':
- if ((c = getchar()) == '>')
- return RR;
- else if (c == '=')
- return GE;
- ungetc(c,stdin);
- return '>';
- case '<':
- if ((c = getchar()) == '<')
- return LL;
- else if (c == '=')
- return LE;
- ungetc(c,stdin);
- return '<';
- case '=':
- if ((c = getchar()) == '=')
- return EQ;
- ungetc(c,stdin);
- return '=';
- case '!':
- if ((c = getchar()) == '=')
- return NE;
- ungetc(c,stdin);
- return '!';
- case '\"':
- l = lastname;
- while ((c = getchar()) != '\"' && c != '\n' && c != EOF)
- *l++ = c;
- if (c != '\"')
- ungetc(c,stdin);
- *l = '\0';
- return NAME;
- default:
- if (isdigit(c)) {
- lastnum = c - '0';
- while (isdigit(c = getchar()))
- lastnum = (lastnum * 10) + (c - '0');
- ungetc(c,stdin);
- return ICON;
- } else {
- l = lastname;
- while (1) {
- *l++ = c;
- switch (c = getchar()) {
- case EOF: case ' ': case '\t': case '\n':
- case '?': case ':': case '-': case '+':
- case '*': case '/': case '%':
- case '~': case '^': case '(': case ')':
- case '<': case '>': case '[': case ']':
- case '&': case '|': case '=': case '!':
- case '\"': case ',':
- ungetc(c,stdin);
- if (l == lastname + 1)
- switch (*lastname) {
- case 'x': case 'y': case 'u':
- case 'f': case 'q': case 'w':
- case 'r':
- return *lastname;
- case 'X':
- lastnum = DEF_X;
- return ICON;
- case 'Y':
- lastnum = DEF_Y;
- return ICON;
- case 'Z':
- lastnum = 255;
- return ICON;
- }
- *l = '\0';
- return NAME;
- }
- }
- }
- }
- }
-
- static void yyerror(char *err) {
- fprintf(stderr, "%s\n", err);
- }
-