-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.y
More file actions
75 lines (58 loc) · 1.01 KB
/
Copy pathparser.y
File metadata and controls
75 lines (58 loc) · 1.01 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
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void yyerror(const char *s);
int yylex(void);
typedef struct {
int ival;
char *str;
} YYSTYPE;
#define YYSTYPE_IS_DECLARED 1
%}
%union {
int ival;
char *str;
}
%token <ival> NUMBER
%token <str> IDENTIFIER
%token INT FLOAT IF ELSE WHILE RETURN
%token LBRACE RBRACE LPAREN RPAREN SEMICOLON COMMA ASSIGN PLUS MINUS TIMES DIVIDE
%start program
%%
program:
program statement
| statement
;
statement:
declaration
| assignment
| function
;
declaration:
type IDENTIFIER SEMICOLON
;
assignment:
IDENTIFIER ASSIGN expression SEMICOLON
;
type:
INT
| FLOAT
;
expression:
NUMBER
| IDENTIFIER
| expression PLUS expression
| expression MINUS expression
;
function:
type IDENTIFIER LPAREN RPAREN LBRACE statement_list RBRACE
;
statement_list:
statement_list statement
| /* empty */
;
%%
void yyerror(const char *s) {
fprintf(stderr, "Parse error: %s\n", s);
}