-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha.l
More file actions
63 lines (55 loc) · 1.46 KB
/
a.l
File metadata and controls
63 lines (55 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
%{
#include "a.tab.h"
struct Node{
char* type;
int value;
char* cval;
struct Node* left;
struct Node* middle;
struct Node* right;
};
struct Node* createNodeLex(struct Node* pLeft, struct Node* pRight, char* node_type) {
struct Node *node = (struct Node *) malloc( sizeof(struct Node) );
node->type = node_type;
node->left = pLeft;
node->right = pRight;
node->middle = NULL;
node->value = 0;
node->cval = "";
return node;
}
%}
digit [0-9]
letter [a-z]
num 0|[1-9]{digit}*|-[1-9]{digit}*
id {letter}({letter}|{digit}|-)*
bTrue #t
bFalse #f
%%
"print-num" {return(PRINTNUM);}
"print-bool" {return(PRINTBOOL);}
"and" {return AND;}
"or" {return OR;}
"not" {return NOT;}
"define" {return DEF;}
"fun" {return FUN;}
"if" {return IF;}
"+" {return ADD;}
"-" {return SUB;}
"*" {return MUL;}
"/" {return DIV;}
"mod" {return MOD;}
">" {return GREATER;}
"<" {return SMALLER;}
"=" {return EQU;}
{bTrue} {yylval.ival=1; return(BOOL);}
{bFalse} {yylval.ival=0; return(BOOL);}
{num} {yylval.ival=atoi(yytext); return(NUMBER);}
{id} { /* IDENTIFIER */
yylval.nd = createNodeLex(NULL, NULL, "string");
yylval.nd->cval = strdup(yytext);
return ID;
}
"("|")" {return yytext[0];}
.|"\t"|"\n"|"\r"|" " {/* do nothing */}
%%