-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.y
87 lines (66 loc) · 1.91 KB
/
parser.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
86
87
%{
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "loc.h"
#include "ast.h"
#include "error.h"
#define YYLTYPE LocType
#define MAX_LINE_LENG 256
extern int line_no, col_no, opt_list;
extern char buffer[MAX_LINE_LENG];
extern FILE *yyin; /* declared by lex */
extern char *yytext; /* declared by lex */
extern int yyleng;
static Node root = NULL;
extern
#ifdef __cplusplus
"C"
#endif
int yylex(void);
static void yyerror(const char *msg);
extern int yylex_destroy(void);
%}
%locations
%token PROGRAM VAR ARRAY OF INTEGER REAL STRING FUNCTION PROCEDURE PBEGIN END IF THEN ELSE WHILE DO NOT AND OR
%token LPAREN RPAREN SEMICOLON DOT COMMA COLON LBRACE RBRACE DOTDOT ASSIGNMENT ADDOP SUBOP MULOP DIVOP LTOP GTOP EQOP GETOP LETOP NEQOP
%token IDENTIFIER REALNUMBER INTEGERNUM SCIENTIFIC LITERALSTR
%union {
int val;
char* text;
double dval;
Node node;
}
%type <Node> prog
%%
/* define your snytax here */
/* @n return the sturct LocType of "n-th node", ex: @1 return the PROGRAM node's locType
$n return the $$ result you assigned to the rule, ex: $1 */
prog : PROGRAM {
root = NULL;
/*
printf("program node is @ line: %d, column: %d\n",
@1.first_line, @1.first_column);
yylval.val, yylval.text, yylval.dval to get the data (type defined in %union) you assigned by scanner.
*/
}
;
%%
void yyerror(const char *msg) {
fprintf(stderr,
"[ERROR] line %4d:%3d %s, Unmatched token: %s\n",
line_no, col_no-(int)yyleng+1, buffer, yytext);
}
int main(int argc, const char *argv[]) {
if(argc > 2)
fprintf( stderr, "Usage: ./parser [filename]\n" ), exit(0);
FILE *fp = argc == 1 ? stdin : fopen(argv[1], "r");
if(fp == NULL)
fprintf( stderr, "Open file error\n" ), exit(-1);
yyin = fp;
yyparse();
if(root){
// do pass here
}
return 0;
}