-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpressionparser.txt
More file actions
145 lines (111 loc) · 4.28 KB
/
expressionparser.txt
File metadata and controls
145 lines (111 loc) · 4.28 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
%{
#include <stdio.h>
#include <stdlib.h>
int yylex();
void yyerror(char *msg);
%}
%union{
int intValue; /* integer value */
float floatValue; /* float value */
char charValue; /* char value */
char* stringValue; /* string value*/
char* identifier; /* identifier name */
char* comment;
};
%token <intValue> INTVALUE;
%token <floatValue> FLOATVALUE;
%token <charValue> CHARVALUE;
%token <stringValue> STRINGVALUE;
%token <comment> COMMENT;
%token <identifier> IDENTIFIER;
%token CONSTANT INT FLOAT STRING CHAR BOOL IF THEN ELSE WHILE DO SWITCH CASE DEFAULT FOR AND OR EQUALEQUAL GREATERTHAN SMALLERTHAN GREATERTHANOREQUAL SMALLERTHANOREQUAL NOT NOTEQUAL VOID MAIN RETURN BOOLVALUE
%token SEMI_COLON OPENED_BRACKET CLOSED_BRACKET OPENED_BRACE CLOSED_BRACE OPENED_SQ_BRACKET CLOSED_SQ_BRACKET COMMA TWO_DOTS PLUS MINUS MULTIPLY DIVIDE REMAINDER PLUS_EQUAL MINUS_EQUAL MULTIPLY_EQUAL DIVIDE_EQUAL PLUS_PLUS MINUS_MINUS EQUAL
%token ERROR
%right MINUS
%left PLUS
%right DIVIDE
%left MULTIPLY
%right REMAINDER
%nonassoc PLUS_PLUS MINUS_MINUS
%left OR AND
%left EQUALEQUAL NOTEQUAL
%left SMALLERTHAN SMALLERTHANOREQUAL GREATERTHAN GREATERTHANOREQUAL
%right NOT
%%
/* Language BODY */
/* Comment_ Function_ Comment_ Main_ Comment_ */
Root_: Comment_ Main_ {printf("\nAccepted inshaallah\n");}
;
Comment_: COMMENT /* COMMENT Comment_ */{printf("First comment\n");}
|Comment_ COMMENT {printf("Second comment\n");}
;
Main_: VOID MAIN OPENED_BRACKET CLOSED_BRACKET OPENED_BRACE Body_ CLOSED_BRACE {printf("Main root\n");}
;
Body_: Body_ Declaration_ {printf("Body\n");}
| Declaration_ {printf("First declaration\n");}
|Body_ Assignment_ SEMI_COLON {printf("First assignment\n");}
|Assignment_ SEMI_COLON
;
Declaration_: datatype IdentifierList_ SEMI_COLON {printf("declaration normal\n ");}
| CONSTANT datatype IdentifierList_ SEMI_COLON {printf("constant declaration\n ");}
;
IdentifierList_: IDENTIFIER {printf("id \n");}
| IDENTIFIER COMMA IdentifierList_ {printf("list of identifiers \n ")}
| Assignment_ {printf("assignment \n")}
;
Assignment_: IDENTIFIER EQUAL Expr_ {printf("assignment expr \n");}
;
Val_: Number_
|STRINGVALUE {printf("val string value\n")}
| CHARVALUE {printf("val char \n")}
| BOOLVALUE {printf("val bool \n")}
;
Number_: INTVALUE {printf("number int \n")}
| FLOATVALUE {printf("number float \n")}
;
Expr_: MathExpr_
| LogExpr_
;
datatype : INT {printf("Data Type");}
| FLOAT
| STRING
| CHAR
| BOOL
;
MathExpr_: MathExpr_ PLUS Term_
|MathExpr_ MINUS Term_
|Term_
;
Term_: Term_ MULTIPLY Factor_
|Term_ DIVIDE Factor_
|Factor_
;
Factor_: OPENED_BRACKET MathExpr_ CLOSED_BRACKET
|MINUS Factor_
|Val_ {printf("logic value")}
|IDENTIFIER {printf("logic identifier")}
;
LogExpr_: MathExpr_ OR MathExpr_ {printf(" OR expression")}
| MathExpr_ AND MathExpr_ {printf("and expression")}
| NOT MathExpr_ {printf("not expression")}
| OPENED_BRACKET LogExpr_ CLOSED_BRACKET {printf("OPENED_BRACKET logical expression")}
| MathExpr_ NOTEQUAL MathExpr_ {printf("not equal math expression")}
| MathExpr_ EQUALEQUAL MathExpr_ {printf("equal math expression")}
| MathExpr_ GREATERTHAN MathExpr_ {printf("greater than expression")}
| MathExpr_ GREATERTHANOREQUAL MathExpr_ {printf("greater than or equal expression")}
| MathExpr_ SMALLERTHAN MathExpr_ {printf("smaller than expression")}
| MathExpr_ SMALLERTHANOREQUAL MathExpr_ {printf("smaller than equal expression")}
;
%%
#include"lex.yy.c"
int main(){
yyparse();
return yylex();
}
int yywrap(void){
return 1;
}
void yyerror(char *msg){
fprintf(stderr,"%s\n",msg);
exit(1);
}