-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforloop.y
149 lines (120 loc) · 4.31 KB
/
forloop.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
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
144
145
146
147
148
149
/* This is a flex and bison (GNU yacc) code for a basic C-like language parser. Here is a brief
documentation of the code:
The first part of the code contains C preprocessor directives and declarations, including:
#include <stdio.h> and #include <stdlib.h> to include standard C libraries.
int yylex() and int yyerror() function declarations, which are used to perform lexical analysis
and error handling.
extern FILE *yyin and extern FILE *yyout declarations, which are used to read input from a file
and write output to a file.
The %token section STARTFORines the tokens used in the language. These tokens are used by the parser
to recognize the input. Tokens in this code include ID for identifiers, NUM for numbers, and
FOR, LE, GE, EQ, NE, OR, AND, CONTINUE, BREAK,EXIT and RESERVED for keywords.
The %right, %left, and %precedence sections STARTFORine the operator precedence of the language.
Operators with higher precedence are parsed first. For example, %left '+' '-' defines that the
+ and - operators have left associativity, which means that the expression a+b-c is equivalent
to (a+b)-c. The %right '=' declaration defines that the = operator has right associativity.
The %% section contains the grammar rules for the language. The grammar includes rules for statements (START), loops (LOOP),
definitions (STARTFOR), expressions for initilization (EXPR),expressions for condition (CONDITION) and expressions for increement/deecrement (EXPR2).
The yyerror function is called when an error occurs during parsing. It prints an error message
and exits the program.
The main function opens the input file specified in the command-line arguments, calls yyparse()
to begin parsing, and opens a result file for writing the output.
In summary, this code defines a grammar for a basic C-like language and uses flex and bison to
perform lexical analysis and parsing. It is meant to demonstrate how to write a simple parser
for a programming language. */
%{
// Header section: includes and declarations
#include <stdio.h>
#include <stdlib.h>
// Function declarations
int yylex();
int yyerror();
extern FILE *yyin;
extern FILE *yyout;
%}
// Token definitions
%token ID NUM FOR LE GE EQ NE OR AND CONTINUE BREAK EXIT RESERVED
// Operator precedence and associativity
%right '='
%left OR AND
%left LE GE EQ NE LT GT
%left '+' '-'
%left '*' '/'
%left '!' '%'
%left ','
// Start symbol
%%
START : LOOP {printf("FOR Syntax ACCEPTED \n"); exit(0);}
// Grammar rules
LOOP : FOR '(' EXPR ';' CONDITION ';' EXPR2')' STARTFOR
| FOR '(' ';' ';' ')' STARTFOR
| FOR '(' EXPR ';' ';' ')' STARTFOR
| FOR '(' ';'CONDITION ';' ')' STARTFOR
| FOR '(' ';' ';' EXPR2 ')' STARTFOR
|FOR '(' ';' CONDITION ';' EXPR2 ')' STARTFOR
|FOR '('EXPR ';' ';' EXPR2 ')' STARTFOR
|FOR '('EXPR ';' CONDITION ';' ')' STARTFOR
;
STARTFOR : ';'
|'{' '}'
| '{' BODY '}'
| EXPR ';'
| LOOP
| ID ID ';'
;
BODY : BODY LOOP
| BODY EXPR ';'
| LOOP
| EXPR ';'
| BREAK ';'
| CONTINUE ';'
|EXIT ';'
| ID ID ';'
|';'
| BODY BREAK ';'
| BODY CONTINUE ';'
| BODY EXIT ';'
;
EXPR : ID '=' EXPR
|EXPR ',' EXPR
|RESERVED ID '=' EXPR
| EXPR '+' EXPR
| EXPR '-' EXPR
| EXPR '*' EXPR
| EXPR '/' EXPR
| EXPR '%' EXPR
| ID
| NUM
;
CONDITION :ID '=' EXPR
| CONDITION LT CONDITION
| CONDITION GT CONDITION
| CONDITION LE CONDITION
| CONDITION GE CONDITION
| CONDITION EQ CONDITION
| CONDITION NE CONDITION
| CONDITION OR CONDITION
| CONDITION AND CONDITION
|NUM
|CONDITION ','CONDITION
|ID
;
EXPR2 :ID '=' EXPR
|EXPR2 ',' EXPR2
| ID '+' '+'
| ID '-' '-'
| '+' '+' ID
| '-' '-' ID
;
%%
// Error handling function
int yyerror(char const *s)
{
printf("\nyyerror %s\n",s);
exit(1) ;
}
// Main function
int main(int argc,char **argv) {
yyin = fopen(argv[argc-1],"r"); // Open input file for reading
yyparse(); // Call parser function
}