-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.yy
79 lines (65 loc) · 2.81 KB
/
parser.yy
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
%{
// Jenner Higgins
// Bison Parser
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include "scanType.hpp" // TokenData type
extern int yylex();
extern FILE *yyin;
extern int line; // line number from the scanner
extern int numErrors; // error count
#define YYERROR_VERBOSE
void yyerror(const char *msg) {
printf("ERROR(%d): '%s'\n", line, msg);
numErrors++;
}
%}
// included in tab.h file
%union {
TokenData * tokenData;
double value;
}
%token <tokenData> NUMCONST STRINGCONST CHARCONST BOOLCONST KEYWORD OP ID INC DEC ASGN ADDASS SUBASS MULASS DIVASS LEQ GEQ NEQ EQ INVALID
%type <value> declList MULOP
%%
program : declList program
|
;
declList : NUMCONST { printf("Line %d Token: NUMCONST Value: %d Input: %s\n", $1->linenum, $1->nvalue, $1->tokenstr); }
| STRINGCONST { printf("Line %d Token: STRINGCONST Value: \"%s\" Len: %d Input: %s\n", $1->linenum, $1->svalue, $1->slen, $1->tokenstr); } // NEED TO PRINT LENGTH
| CHARCONST { printf("Line %d Token: CHARCONST Value: '%c' Input: %s\n", $1->linenum, $1->cvalue, $1->tokenstr); }
| BOOLCONST { printf("Line %d Token: BOOLCONST Value: %d Input: %s\n", $1->linenum, $1->nvalue, $1->tokenstr); }
| KEYWORD { printf("Line %d Token: %s\n", $1->linenum, $1->tokenstr); }
| OP { printf("Line %d Token: %s\n", $1->linenum, $1->tokenstr); }
| ID { printf("Line %d Token: ID Value: %s\n", $1->linenum, $1->tokenstr); }
| INVALID { printf("ERROR(%d): Invalid or misplaced input character: '%s'. Character Ignored.\n", $1->linenum, $1->tokenstr);}
| MULOP
;
MULOP : INC { printf("Line %d Token: INC\n", $1->linenum); }
| DEC { printf("Line %d Token: DEC\n", $1->linenum); }
| ASGN { printf("Line %d Token: ASGN\n", $1->linenum); }
| ADDASS { printf("Line %d Token: ADDASS\n", $1->linenum); }
| SUBASS { printf("Line %d Token: SUBASS\n", $1->linenum); }
| MULASS { printf("Line %d Token: MULASS\n", $1->linenum); }
| DIVASS { printf("Line %d Token: DIVASS\n", $1->linenum); }
| LEQ { printf("Line %d Token: LEQ\n", $1->linenum); }
| GEQ { printf("Line %d Token: GEQ\n", $1->linenum); }
| NEQ { printf("Line %d Token: NEQ\n", $1->linenum); }
| EQ { printf("Line %d Token: EQ\n", $1->linenum); }
;
%%
int main(int argc, char * argv[])
{
if(argc > 1) {
if((yyin = fopen(argv[1], "r"))) {
// file open success
} else {
printf("FILE ERROR: failed to open \'%s\'\n", argv[1]);
exit(1);
}
}
numErrors = 0;
yyparse();
return 0;
}