-
Notifications
You must be signed in to change notification settings - Fork 6
/
grammar.y
82 lines (69 loc) · 2.48 KB
/
grammar.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
%{
#include <stdio.h>
#include <stdlib.h>
#include "compiler.h"
%}
%union {
struct ast *a;
double d;
struct symbol *symbol;
struct symbolList *symbolList;
struct numList *numList;
int fn;
char typeC;
}
%token <d> NUMBER
%token <symbol> SYMBOL
%token PROGRAM VAR ARRAY OF INTEGER REAL BGN END IF THEN ELSE WHILE DO DOTS PRINT
%token <typeC> STD_TYPE
%nonassoc <fn> CMP
%right '='
%left '+' '-'
%left '*' '/'
%nonassoc '|' UMINUS
%type <a> declarationList declaration statementList statement expression
%type <symbolList> symbolList
%type <numList> numList
%start program
%%
program: PROGRAM SYMBOL '(' symbolList ')' ';' declarationList BGN statementList END '.'
{ eval($7); eval($9); treeFree($9); treeFree($7); printf("parse done.\n"); }
;
declarationList: { $$ = NULL; }
| declaration ';' declarationList { if($3 == NULL) $$ = $1; else $$ = newAst('L', $1, $3); }
;
declaration: VAR symbolList ':' STD_TYPE { $$ = newDeclaration($2, $4); }
| VAR symbolList ':' ARRAY '[' NUMBER DOTS NUMBER ']' OF STD_TYPE
{ $$ = newDeclarationArr($2, $6, $8, $11); }
;
statement: IF expression THEN '{' statementList '}' { $$ = newFlow('I', $2, $5, NULL); }
| IF expression THEN '{' statementList '}' ELSE '{' statementList '}' { $$ = newFlow('I', $2, $5, $9); }
| WHILE expression DO '{' statementList '}' { $$ = newFlow('W', $2, $5, NULL); }
| expression
;
statementList: statement { $$ = $1; }
| statementList ';' statement { $$ = newAst('L', $1, $3); }
;
expression: expression CMP expression { $$ = newCompare($2, $1, $3); }
| expression '+' expression { $$ = newAst('+', $1, $3); }
| expression '-' expression { $$ = newAst('-', $1, $3); }
| expression '*' expression { $$ = newAst('*', $1, $3); }
| expression '/' expression { $$ = newAst('/', $1, $3); }
| '|' expression { $$ = newAst('|', $2, NULL); }
| '(' expression ')' { $$ = $2; }
| '-' expression %prec UMINUS { $$ = newAst('M', $2, NULL); }
| NUMBER { $$ = newNum($1); }
| SYMBOL { $$ = newReference($1); }
| SYMBOL '[' expression ']' { $$ = newReferenceArr($1, $3); }
| SYMBOL '[' expression ']' '=' expression { $$ = newAssignArr($1, $3, $6); }
| SYMBOL '=' expression { $$ = newAssign($1, $3); }
| SYMBOL '=' '{' numList '}' { $$ = newInitialArr($1, $4); }
| PRINT '(' expression ')' { $$ = newPrint($3); }
;
numList: NUMBER { $$ = newNumList($1, NULL); }
| NUMBER ',' numList { $$ = newNumList($1, $3); }
;
symbolList: SYMBOL { $$ = newSymbolList($1, NULL); }
| SYMBOL ',' symbolList { $$ = newSymbolList($1, $3); }
;
%%