-
Notifications
You must be signed in to change notification settings - Fork 0
/
scannersyn
68 lines (61 loc) · 1.26 KB
/
scannersyn
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
63
64
65
66
67
68
/* Code: */
#include <stdio.h>
#include <stdlib.h>
/* A couple of globals */
int lineno = 0;
int number;
char string[100];
#define NUMBER 1
#define STRING 2
#define ASSIGN 3
#define DIV 4
int main(int argc, char **argv){
int rc;
while ((rc=yylex())){
switch (rc){
case NUMBER:
printf("We recognized a number of value %d\n", number);
break;
case STRING:
printf("We recognized a string of value %s\n", string);
break;
case '+':
printf ("+\n");
break;
}
}
return 0;
}
int yylex(){
int c;
while ((c=getchar()) == ' ' || c == '\t')
; /* Do nothing */
if (c == EOF)
return 0;
if (isdigit(c)){
ungetc(c,stdin);
scanf("%d", &number);
return NUMBER;
} else if (c=='+'||c=='-'||c=='/'||c=='*'||c=='('||c==')'){
return c;
} else if (c==':'){
c=getchar();
if (c=='=')
return ASSIGN;
} else if (c=='/'){
c=getchar();
if (c=='*'||c=='/')
c=getchar();
if (c=='*/'||'newline');
return 0;
else
return DIV;
} else {
ungetc(c,stdin);
scanf("%s", string);
return STRING;
}
}
/* scanner-simplest.c ends here */
/* make ./scanner-simplest in the directory
add new assignments to scanner-lex.c */