-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.c
62 lines (54 loc) · 1.37 KB
/
token.c
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
/*************************************************************************
* File Name: token.c
* Author: Li Peng
* Mail: [email protected]
* Created Time: 2013-11-06 17:08:20
* Description:
************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "include/token.h"
#include "include/tag.h"
token Token(char c){
token t = (token)malloc(sizeof(struct _token));
t->tag = c;
t->detail.dummy = c;
return t;
}
token Num(int v){
token t = (token)malloc(sizeof(struct _token));
t->tag = NUM;
t->detail.num = v;
return t;
}
token Real(float x){
token t = (token)malloc(sizeof(struct _token));
t->tag = REAL;
t->detail.real = x;
return t;
}
token Word(char* lexme,int tag){
token t = (token)malloc(sizeof(struct _token));
t->tag = tag;
t->detail.word = (char*)malloc(sizeof(char) * strlen(lexme) + 1);
strcpy(t->detail.word,lexme);
return t;
}
void print_token(token t){
printf("TAG:%d\n",t->tag);
if(t->tag == ID){
printf("lexme:%s\n",t->detail.word);
}
}
int token_equal(token t1,token t2){
if(t1->tag != t2->tag)
return 0;
if(t1->tag == NUM)
return t1->detail.num == t2->detail.num;
if(t1->tag == REAL)
return t1->detail.real == t2->detail.real;
if(t1->tag == ID)
return !strcmp(t1->detail.word,t2->detail.word);
return t1->detail.dummy == t2->detail.dummy;
}