-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.y
85 lines (70 loc) · 1.58 KB
/
example.y
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
/* SPDX-License-Identifier: GPL-3.0-or-later
* Author: bluewww */
%{
#include <stdio.h>
#include <readline/history.h>
#include <readline/readline.h>
#include "example.tab.h"
#include "example.lex.h"
/* Pass the argument to yyparse through to yylex. */
extern int yylex (YYSTYPE * yylval_param, YYLTYPE * yylloc_param , yyscan_t yyscanner);
extern void yyerror (YYLTYPE* yyllocp, yyscan_t unused, const char* msg);
%}
%code requires {
/* avoid circular dependency */
typedef void* yyscan_t;
}
%define api.pure full
%define parse.error detailed
%define parse.trace
%locations
%param { yyscan_t scanner }
%token INT
%left '+'
%left '*'
%%
prog: expr { printf("-> %d\n", $1); } ;
expr: INT
| '(' expr ')' { $$ = $2; }
| expr '+' expr { $$ = $1 + $3; }
| expr '*' expr { $$ = $1 * $3; } ;
%%
void
yyerror (YYLTYPE* yyllocp, __attribute__((unused)) yyscan_t unused,
const char* msg)
{
fprintf (stderr, "%s\n", msg);
}
static char *line_read = NULL;
/* Read a string and return a pointer to it. Returns NULL on EOF. */
char *
gra_gets(void)
{
if (line_read) {
free(line_read);
line_read = (char *)NULL;
}
line_read = readline("$ ");
if (line_read && *line_read)
add_history(line_read);
return (line_read);
}
char *gra_line = NULL;
int
main(void)
{
yyscan_t scanner;
yylex_init(&scanner);
yyset_debug(1, scanner);
for (;;) {
while(!(gra_line = gra_gets())) {
printf("\n");
return EXIT_SUCCESS;
}
YY_BUFFER_STATE buffer = yy_scan_string(gra_line, scanner);
yyparse(scanner);
yy_delete_buffer(buffer, scanner);
}
yylex_destroy(scanner);
return 0;
}