-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyscanner.c
51 lines (43 loc) · 1.17 KB
/
myscanner.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
#include <stdio.h>
#include "myscanner.h"
extern int yylex();
extern int yylineno;
extern char* yytext;
char *names[] = {NULL, "db_type", "db_name", "db_table_prefix", "db_port"};
int main(void)
{
int ntoken, vtoken;
ntoken = yylex();
while(ntoken) {
printf("%d\n", ntoken);
//error
if(yylex() != COLON) {
printf("Syntax error in line %d, Expected a ':' but found %s\n", yylineno, yytext);
return 1;//exit
}
//value token
vtoken = yylex();
switch(ntoken) {
case TYPE:
case NAME:
case TABEL_PREFIX:
if(vtoken != IDENTIFIER) {
printf("Syntax error in line %d, Expected a identifier but found %s\n", yylineno, yytext);
return 1;//exit
}
printf("%s is set to %s\n", names[ntoken], yytext);
break;
case PORT:
if(vtoken != INTEGER) {
printf("Syntax error in line %d, Expected an integer but found %s\n", yylineno, yytext);
return 1;//exit
}
printf("%s is set to %s\n", names[ntoken], yytext);
break;
default:
printf("Syntax error in line %d\n", yylineno);
}
ntoken = yylex();
}
return 0;
}