Skip to content

Commit

Permalink
Merge pull request #1 from Dayof/dev
Browse files Browse the repository at this point in the history
Initial parser version
  • Loading branch information
Dayof authored Oct 17, 2020
2 parents 131d6cd + 3e60d7f commit 241531a
Show file tree
Hide file tree
Showing 25 changed files with 2,814 additions and 407 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
cppython
cppython
*.tab.*
*.yy.*
Binary file modified cppython.pdf
Binary file not shown.
15 changes: 15 additions & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FILES = parser.c lexer.c ast.c sym_tab.c main.c
CFLAGS = -g -Wall -pedantic -x c
CC = gcc

cppython: $(FILES) ast.h sym_tab.h
$(CC) $(CFLAGS) $(FILES) -o cppython -lfl

lexer.c: cppython.lex
flex cppython.lex

parser.c: cppython.y
bison -d -v cppython.y

clean:
rm -f *.o *~ lexer.c lexer.h parser.c parser.h parser.output cppython
51 changes: 34 additions & 17 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,63 @@
## Requirements

- Flex 2.6.4
- Bison 3.5.1
- GCC 9.3.0

## Usage

```bash
$ chmod +x build.sh
$ ./build.sh
$ ./cppython tests/valid_1.ppy # for valid test
$ ./cppython tests/incorrect_1.ppy # for invalid test
$ make
$ ./cppython tests/parser/valid_1.ppy # for valid test
$ ./cppython tests/parser/invalid_1.ppy # for invalid test
```

## Output examples

- Valid input 1:
- Commands: ./build.sh && ./cppython tests/valid_1.ppy
- Commands: ./cppython tests/parser/valid_1.ppy
- Output:

```bash
CPPython interpreter:
Welcome to CPPython interpreter:
Lexer/parser:

line 1. <keyword, 'def'> <id, 1> <delimiter, '('> <id, 2> <delimiter, ','> <id, 3> <delimiter, ')'> <delimiter, ':'>
line 2. <id, 4> <delimiter, '='> <integer, '1'>
line 3. <id, 5> <operator, '+'> <delimiter, '='> <integer, '1'>
line 1. Token: <integer, '1'>

Creating integer expression node: 1
Token: <add, '+'>
Assign expression.
Token: <integer, '1'>

Creating integer expression node: 1

line 2.
Assign expression.

Creating binary expression node: 1 + 1
1

AST created.

Lexer and parser finished.
```

- Invalid input 2:
- Commands: ./build.sh && ./cppython tests/invalid_1.ppy
- Commands: ./cppython tests/parser/invalid_1.ppy
- Output:

```bash
CPPython interpreter:
Welcome to CPPython interpreter:
Lexer/parser:
line 1. Token: <integer, '1'>
Creating integer expression node: 1
line 1. <keyword, 'def'> <id, 1> <delimiter, '('> <delimiter, ')'> <delimiter, ':'>
line 2. <id, 2> <delimiter, '='> <integer, '1'>
line 3. <id, 3> <operator, '+'> <delimiter, '='> <integer, '1'>
line 4. <id, 4> <delimiter, '='>
LexerError: line 4, column 13, token '@' is not recognized
LexerError: line 1, column 2, token '^' is not recognized
```

## Author

Name: Dayanne Fernandes da Cunha
University ID: 130107191
University ID: 130107191
32 changes: 32 additions & 0 deletions src/ast.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <stdio.h>
#include <stdlib.h>
#include "ast.h"

ast_node* create_bin_expr(char *operator, ast_node* left, ast_node* right) {
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->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) {
printf("\n\nCreating integer expression node: %d\n", value);
ast_node* expr = (ast_node*) malloc(sizeof(ast_node));
expr->op.integer_expr = value;
return expr;
}

void create_ast(ast_node* expression) {
ast* ast_obj = (ast*) malloc(sizeof(ast));
ast_obj->head = expression;
ast_obj->next = NULL;
printf("\nAST created.\n");
}

ast_node* show(ast_node* expression) {
printf("\nAssign expression.\n");
return expression;
}
31 changes: 31 additions & 0 deletions src/ast.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef __AST_H__
#define __AST_H__

int line, column;

typedef struct exp {
union {
int integer_expr;
char *string_expr;
char *variable_expr;
struct {
char *operator;
struct exp* left;
struct exp* right;
} binary_expr;
} op;
} ast_node;

typedef struct expr_list {
ast_node* head;
struct expr_list* next;
} ast;

void create_ast(ast_node* expression);
ast_node* show(ast_node* expression);
ast_node* create_int_expr(int value);
ast_node* create_bin_expr(char *operator, ast_node* left, ast_node* right);

void handle_token(int token);

#endif // __AST_H__
5 changes: 0 additions & 5 deletions src/build.sh

This file was deleted.

Loading

0 comments on commit 241531a

Please sign in to comment.