8000 implement ast file to project · fadel-hasan/python-compiler@bd32af2 · GitHub
[go: up one dir, main page]

Skip to content

Commit bd32af2

Browse files
committed
implement ast file to project
1 parent 94b5b21 commit bd32af2

File tree

6 files changed

+584
-113
lines changed

6 files changed

+584
-113
lines changed

ast.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "python_ast_node.hpp"

indent_dedent_rule.txt

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
%{
3+
if(isEmpty(myStack)) {
4+
push(&myStack,0);
5+
}
6+
%} */
7+
8+
/* ^[ \t]* { */
9+
/*
10+
* Handle indentation as described in Python docs linked above.
11+
* Note that this pattern treats leading spaces and leading tabs
12+
* equivalently, which could cause some unexpected behavior if
13+
* they're combined in a single line. For the purposes of this
14+
* project, that's OK.
15+
*/
16+
/* if (peek(myStack) < (int)yyleng) { */
17+
/*
18+
* If the current indentation level is greater than the
19+
* previous indentation level (stored at the top of the stack),
20+
* then emit an INDENT and push the new indentation level onto
21+
* the stack.
22+
*/
23+
24+
/* push(&myStack,yyleng);
25+
printf("INDENT\n");
26+
return INDENT;
27+
} else { */
28+
/*
29+
* If the current indentation level is less than or equal to
30+
* the previous indentation level, pop indentation levels off
31+
* the stack until the top is equal to the current indentation
32+
* level. Emit a DEDENT for each element popped from the stack.
33+
*/
34+
/* while (!isEmpty(myStack) && peek(myStack) != (int)yyleng) {
35+
pop(&myStack);
36+
printf("DEDENT\n");
37+
return DEDENT;
38+
} */
39+
40+
/*
41+
* If we popped everythin g off the stack, that means the
42+
* current indentation level didn't match any on the stack,
43+
* which is an indentation error.
44+
*/
45+
46+
/* if (isEmpty(myStack)) {
47+
printf("Error: Incorrect indentation on line %d"
48+
,yylineno);
49+
return 1;
50+
}
51+
}
52+
} */
53+
54+
/* ^[^ \t\n]+ { */
55+
/*
56+
* If we find a line that's not indented, pop all indentation
57+
* levels off the stack, and emit a DEDENT for each one. Then,
58+
* call REJECT, so the next rule matching this token is also
59+
* applied.
60+
*/
61+
/* while(peek(myStack) != 0) {
62+
pop(&myStack);
63+
printf("DEDENT\n");
64+
return DEDENT;
65+
}
66+
REJECT;
67+
}
68+
69+
\r?\n { */
70+
/* printf("NEWLINE\n");
71+
return NEWLINE;
72+
}
73+
74+
<<EOF>> { */
75+
/*
76+
* If we 8000 reach the end of the file, pop all indentation levels
77+
* off the stack, and emit a DEDENT for each one.
78+
*/
79+
/* while(peek(myStack) != 0) {
80+
pop(&myStack);
81+
printf("DEDENT\n");
82+
return DEDENT;
83+
}
84+
85+
} */

parser.y

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,17 @@ program: /*empty program*/
4141
;
4242

4343

44-
statements:
45-
statement NEWLINE {printf("one statment\n");}
46-
| statements statement NEWLINE {printf("many statment\n");}
44+
statements:
45+
statement {printf("one statment\n");}
46+
| statements statement {printf("many statment\n");}
4747
;
4848

49-
statement: compound_stmt {printf("compound stmt\n");}
50-
| simple_stmt {printf("simple stmt\n");}
49+
statement: compound_stmt NEWLINE {printf("compound stmt\n");}
50+
| simple_stmt NEWLINE {printf("simple stmt\n");}
51+
/* | NEWLINE */
5152
;
5253

54+
5355
simple_stmt: expression {printf("expression\n");}
5456
| assignment {printf("assignment\n");}
5557
| return_stmt {printf("return stmt\n");}
@@ -155,16 +157,6 @@ with_item_list: with_item
155157
with_item: IDENTIFIER '(' STRING ')' AS IDENTIFIER
156158
;
157159

158-
/* expression
159-
: expression '+' expression
160-
| expression '-' expression
161-
| expression MUL expression
162-
| expression '/' expression
163-
| '-' expression %prec UMINUS
164-
| '(' expression ')'
165-
| atom
166-
167-
; */
168160

169161

170162

@@ -194,19 +186,7 @@ elif_stmt : elif_header block
194186
;
195187
elif_header : ELIF named_expression COLON
196188
;
197-
/*
198-
if_stmt: IF named_expression COLON block elif_stmt
199-
| IF named_expression COLON block else_block
200-
;
201189

202-
elif_stmt:
203-
| ELIF named_expression COLON block else_block
204-
| elif_stmt ELIF named_expression COLON block else_block
205-
;
206-
207-
else_block:
208-
| ELSE COLON block
209-
; */
210190

211191
named_expression: assignment_expression
212192
| comparison
@@ -270,17 +250,17 @@ expression: primary_expression
270250
| '(' expression ')'
271251
;
272252

273-
for_stmt: for_header changes block
253+
for_stmt: for_header changes COLON block
274254

275255
for_header: FOR IDENTIFIER IN
276256

277257
changes: IDENTIFIER
278258
|range
279259

280-
range: RANGE LBRACKET myrange RBRACKET
281-
| RANGE LBRACKET myfunc RBRACKET
260+
range: RANGE '(' myrange ')'
261+
| RANGE '(' myfunc ')'
282262

283-
myfunc: IDENTIFIER LBRACKET RBRACKET
263+
myfunc: IDENTIFIER '(' ')'
284264

285265
myrange : NUMBER
286266
| NUMBER ',' NUMBER
@@ -353,8 +333,30 @@ dict_pattern_entry
353333
: pattern COLON pattern
354334
;
355335

336+
/* expression
337+
: expression '+' expression
338+
| expression '-' expression
339+
| expression MUL expression
340+
| expression '/' expression
341+
| '-' expression %prec UMINUS
342+
| '(' expression ')'
343+
| atom
344+
345+
; */
346+
347+
/*
348+
if_stmt: IF named_expression COLON block elif_stmt
349+
| IF named_expression COLON block else_block
350+
;
356351
352+
elif_stmt:
353+
| ELIF named_expression COLON block else_block
354+
| elif_stmt ELIF named_expression COLON block else_block
355+
;
357356
357+
else_block:
358+
| ELSE COLON block
359+
; */
358360
/* atom:
359361
IDENTIFIER
360362
| NUMBER

0 commit comments

Comments
 (0)
0