-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Dayof/dev
Second parser version before deadline
- Loading branch information
Showing
22 changed files
with
210 additions
and
2,092 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
FILES = parser.c lexer.c ast.c sym_tab.c main.c | ||
FILES = parser/parser.c lexer/lexer.c core/ast.c core/sym_tab.c core/main.c | ||
CFLAGS = -g -Wall -pedantic -x c | ||
CC = gcc | ||
|
||
cppython: $(FILES) ast.h sym_tab.h | ||
$(CC) $(CFLAGS) $(FILES) -o cppython -lfl | ||
cppython: $(FILES) core/ast.h core/sym_tab.h | ||
$(CC) $(CFLAGS) $(FILES) -I core -I lexer -I parser -o cppython -lfl | ||
|
||
lexer.c: cppython.lex | ||
flex cppython.lex | ||
lexer/lexer.c: lexer/cppython.lex | ||
flex lexer/cppython.lex | ||
|
||
parser.c: cppython.y | ||
bison -d -v cppython.y | ||
parser/parser.c: parser/cppython.y | ||
bison -d -v parser/cppython.y | ||
|
||
clean: | ||
rm -f *.o *~ lexer.c lexer.h parser.c parser.h parser.output cppython | ||
rm -f *.o *~ lexer/lexer.c lexer/lexer.h parser/parser.c parser/parser.h parser/parser.output cppython |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include "ast.h" | ||
|
||
ast_node* create_bin_expr(char *operator, ast_node* left, ast_node* right) { | ||
if (PARSER_VERBOSE) printf("\nCreating binary expression node: .%d. .%s. .%d.\n", | ||
left->op.integer_expr, operator, right->op.integer_expr); | ||
ast_node* expr = (ast_node*) malloc(sizeof(ast_node)); | ||
expr->tag = BINARY_TYPE; | ||
expr->op.binary_expr.operator = operator; | ||
expr->op.binary_expr.left = left; | ||
expr->op.binary_expr.right = right; | ||
return expr; | ||
} | ||
|
||
ast_node* create_int_expr(int value) { | ||
if (PARSER_VERBOSE) printf("\n\nCreating integer expression node: %d\n", value); | ||
ast_node* expr = (ast_node*) malloc(sizeof(ast_node)); | ||
expr->tag = INTEGER_TYPE; | ||
expr->op.integer_expr = value; | ||
return expr; | ||
} | ||
|
||
void create_ast(ast_node* expression) { | ||
if (PARSER_VERBOSE) printf("\nCreating AST.\n"); | ||
root = expression; | ||
return; | ||
} | ||
|
||
void create_empy_ast() { | ||
if (PARSER_VERBOSE) printf("\nCreating empty AST.\n"); | ||
root = NULL; | ||
return; | ||
} | ||
|
||
ast_node* print_exp(ast_node* node) { | ||
if (PARSER_VERBOSE) { | ||
if (node == NULL) { | ||
printf("Empty expression.\n"); | ||
} else { | ||
printf("TAG: %d\n", node->tag); | ||
} | ||
} | ||
return node; | ||
} | ||
|
||
void print_ast(ast_node* node) { | ||
if (node == NULL) { | ||
printf("Empty AST.\n"); | ||
return; | ||
} | ||
|
||
if (PARSER_VERBOSE) printf("TAG: %d\n", node->tag); | ||
|
||
for (int i=0; i < AST_LVL; ++i) printf(" "); | ||
|
||
// terminal leaf | ||
if (node->tag == INTEGER_TYPE) { | ||
printf("INT: %d\n", node->op.integer_expr); | ||
return; | ||
// terminal leaf | ||
} else if (node->tag == VAR_TYPE) { | ||
printf("VAR: %s\n", node->op.variable_expr); | ||
return; | ||
// non terminal node | ||
} else if (node->tag == BINARY_TYPE) { | ||
printf("OP: %s\n", node->op.binary_expr.operator); | ||
AST_LVL += 1; | ||
print_ast(node->op.binary_expr.right); | ||
print_ast(node->op.binary_expr.left); | ||
} else { | ||
printf("Print AST unknown error.\n"); | ||
return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.