-
Notifications
You must be signed in to change notification settings - Fork 49
/
myscanner.c
46 lines (41 loc) · 1 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
#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);
if(yylex() != COLON) {
printf("Syntax error in line %d, Expected a ':' but found %s\n", yylineno, yytext);
return 1;
}
vtoken = yylex();
switch (ntoken) {
case TYPE:
case NAME:
case TABLE_PREFIX:
if(vtoken != IDENTIFIER) {
printf("Syntax error in line %d, Expected an identifier but found %s\n", yylineno, yytext);
return 1;
}
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;
}
printf("%s is set to %s\n", names[ntoken], yytext);
break;
default:
printf("Syntax error in line %d\n",yylineno);
}
ntoken = yylex();
}
return 0;
}