-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScannerByRegularExpression
More file actions
91 lines (68 loc) · 1.04 KB
/
ScannerByRegularExpression
File metadata and controls
91 lines (68 loc) · 1.04 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
------------
test.y:
%{
#include <stdio.h>
int yylex();
int yyerror(char *s);
%}
%token STRING NUM OTHER SEMICOLON
%type <name> STRING
%type <number> NUM
%union{
char name[20];
int number;
}
%%
prog:
stmts
;
stmts:
| stmt SEMICOLON stmts
stmt:
STRING {
printf("Your entered a string - %s", $1);
}
| NUM {
printf("The number you entered is - %d", $1);
}
| OTHER
;
%%
int yyerror(char *s)
{
printf("Syntax Error on line %s\n", s);
return 0;
}
int main()
{
yyparse();
return 0;
}
------------
test.l:
%{
#include <stdio.h>
#include <string.h>
#include "test.tab.h"
void showError();
%}
numbers ([0-9])+
alpha ([a-zA-Z])+
%%
{alpha} {sscanf(yytext, "%s", yylval.name); return (STRING);}
{numbers} {yylval.number = atoi(yytext); return (NUM);}
";" {return (SEMICOLON);}
. {showError(); return(OTHER);}
%%
void showError(){
printf("Other input");
}
int yywrap(){
return 1;
}
------------
TO RUN:
win_flex test.l
win_bison -d test.y
gcc lex.yy.c test.tab.c
a