Skip to content

Commit

Permalink
Flatten all sub-directories, fix makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
MananSoni42 committed Oct 29, 2020
1 parent 2717927 commit ada8be1
Show file tree
Hide file tree
Showing 45 changed files with 142 additions and 198 deletions.
30 changes: 4 additions & 26 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,15 @@


all: all_dependencies machine_grammar
gcc -g -o driver.exe *.o
gcc -o driver.exe *.o

machine_grammar:
python3 generate_grammar.py -o grammar/machine_grammar.txt --grammar-struct-out final_code/grammar_structs --json_in grammar/symbols.json grammar/grammar.txt
python3 generate_grammar.py -o machine_grammar.txt --grammar-struct-out grammar_structs --json_in symbols.json grammar.txt

grammar_structs.o: machine_grammar
gcc -c final_code/grammar_structs.c

read_grammar.o: final_code/read_grammar.c
gcc -c final_code/read_grammar.c

tokenizer_structs.o: final_code/tokenizer_structs.c final_code/tokenizer_structs.h final_code/tokenizer_structs.c
gcc -c final_code/tokenizer_structs.c

tokenizer.o: final_code/tokenizer.c
gcc -c final_code/tokenizer.c

parse_tree: tokenizer_structs.o grammar_structs.o read_grammar.o tokenizer.o machine_grammar
gcc -o parse_tree.exe final_code/parse_tree.c read_grammar.o tokenizer_structs.o grammar_structs.o

parse_tree.o: final_code/parse_tree.c
gcc -c final_code/parse_tree.c

parse_tree_traverse: parse_tree.o tokenizer_structs.o grammar_structs.o read_grammar.o tokenizer.o machine_grammar
gcc -o parse_tree.exe final_code/traverse_parse.c parse_tree.o read_grammar.o tokenizer_structs.o grammar_structs.o

all_dependencies:
gcc -g -c ./data_structures/*.c ./data_structures/TypeExpressionDS/*.c final_code/*.c
all_dependencies: machine_grammar
gcc -c *.c

.PHONY: clean
clean:
rm *.o
rm *.exe
rm grammar/machine_grammar.txt
58 changes: 57 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,57 @@
# PPL_Assignment_1
# PPL Assignment-1

## Type checking and type expressions

### Requirement
According to the assignment PDF, The Type Expression Table is expected to store the following:
+ Field 1: The name of variable extracted from the declaration statement and to be used in the
assignment statement.
+ Field 2: The information about whether it is an array or not, if yes, then whether it is rectangular
array or a jagged array.
Use numbers enumerated values of 0, 1 and 2 for primitive data type, rectangular array and jagged array respectively.
The value 0 corresponds to integer, real and Boolean types. However, these primitive type details are filled in the fourth field
explicitly defining the integer, real or Boolean specifications appropriately.
+ Field 3: If the type is a rectangular array, then whether it is static or dynamic based on the typeof range parameters used in
defining the array type. If it is not a rectangular array this value is stored as “not_applicable”.
+ Field 4: type expression, which is implemented using the union data type.

### Design Overview
My design is as follows:

### type_exp_table.h
+ We will have a `TYPE_EXPRESSION_TABLE` data structure which would store the `root` node [of a `linked_list` of
`TYPE_EXPRESSION_TABLE_NODE`(s)] and the `SYMBOL_TABLE`.
+ A `TYPE_EXPRESSION_TABLE_NODE` would store the Fields(1,2,3) of a variable.
+ `SYMBOL_TABLE` would be implemented as a `HASH_MAP` with the key as `variable_name` and value as
`TYPE_EXPRESSION` of the variable.
+ `TYPE_EXPRESSION_TABLE_NODE` would store the first 3 fields as per requirement -:
* `variable_name`: `char*`
* `variable_type`: `VariableType` which is an enum field of `{PRIMITIVE_TYPE, RECT_ARRAY, JAGGED_ARRAY}`
* `declaration_type`: `DeclarationType` which is an enum field of `{NOT_APPLICABLE, STATIC, DYNAMIC}`

#### Function Prototypes
* Initialise the type expression table
+ `type_exp_table* create_type_expression_table();`
* Get type expression of a variable
+ `type_expression get_type_expression(type_exp_table* t, char* variable);`
* Add entry
+ `void add_entry_to_table(type_exp_table* t, char* variable_name, VariableType var_type,`
` declaration_type decl_type, type_expression t);`
* Remove entry
+ `void remove_entry_from_table(type_exp_table* t, char* variable_name);`

### type_expression.h
+ The `TYPE_EXPRESSION` data structure would store the type expression(Field 4) of a variable
(with a unique `variable_name`).
+ The necessary fields of `TYPE_EXPRESSION` are(fields with ** are tentative and under review):
* `is_declared`: which is set to `true` when a valid `decl_stmt` corresponding to its `variable_name` is encountered.
* `variable_type`**: could be used as a `tag` for allocating union value.
* `to_be_named`: The union data structure which would store amongst:
+ `PrimitiveDataType`
+ `RECT_ARRAY_TYPE`
+ `JAGGED_ARRAY_TYPE`
### Function Prototypes
* Get the desired String Representation of TypeExpression
+ `char* get_string_representation(type_expression tp)`;
* Set is_declared to true on encountering decl_stmt
+ `void set_declare_flag(type_expression t);`
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Group 36

#include "type_exp_table.h"
#include "type_expression.h"
#include "../../final_code/parse_tree.h"
#include "parse_tree.h"

//Return type expression for fact non-terminal
type_expression *get_type_of_fact(type_exp_table *txp_table, Parse_tree_node *p);
Expand Down
90 changes: 0 additions & 90 deletions data_structures/TypeExpressionDS/temp_driver.c

This file was deleted.

8 changes: 4 additions & 4 deletions final_code/driver.c → driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Group 36
#include "grammar.h"
#include "tokenizer_structs.h"
#include "parse_tree.h"
#include "../data_structures/TypeExpressionDS/print.h"
#include "../data_structures/TypeExpressionDS/type_exp_table.h"
#include "print.h"
#include "type_exp_table.h"

#define MAXLINELEN 2048
#define MAXRULES 100
Expand All @@ -24,12 +24,12 @@ int main(int argc, char** argv){
exit(1);
}

printf("Reading grammar from grammar/machine_grammar.txt ...\n");
printf("Reading grammar from machine_grammar.txt ...\n");
Grammar* g = (Grammar*)malloc(sizeof(Grammar));
g->num_rules = 0;
g->start_symb = toSymbol("main_program");
g->rules = (Rule*)malloc(MAXRULES*sizeof(Rule));
int gflag = readGrammar("grammar/machine_grammar.txt", g);
int gflag = readGrammar("machine_grammar.txt", g);
#ifdef DEBUG
printGrammar(g);
#endif
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion final_code/gen_utils.h → gen_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Group 36
#include "parse_tree.h"
/* #include "./type_errors.h" */
#include "grammar_structs.h"
#include "../data_structures/linked_list.h"
#include "linked_list.h"
#include <stdlib.h>
#include <math.h>

Expand Down
6 changes: 2 additions & 4 deletions generate_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,8 @@ def parse_file(
terminals.add("UNKNOWN")
terminals = sorted(terminals)
non_terminals = sorted(non_terminals)
print("Terminals :")
print(len(terminals))
print("Non_Terminals :")
print(len(non_terminals))
print("Terminals :", len(terminals))
print("Non_Terminals :", len(non_terminals))
if generate_c_structs:
output_file_structs = open(c_structs_location + ".h", "w")
output_file_structs.write(
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion data_structures/hash_map.h → hash_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Group 36

#include <stdlib.h>
#include <string.h>
#include "../final_code/gen_utils.h"
#include "gen_utils.h"

typedef struct hmnode {
char *string;
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Group 36
#ifndef JAGGED_ARRAY_TYPE_H
#define JAGGED_ARRAY_TYPE_H

#include "../../data_structures/linked_list.h"
#include "../../final_code/parse_tree.h"
#include "linked_list.h"
#include "parse_tree.h"

typedef struct ____R2_DIMENSION____ r2_dimension;

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion data_structures/linked_list.h → linked_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Group 36
#define LINKED_LIST_H

#include <stdlib.h>
#include "../final_code/gen_utils.h"
#include "gen_utils.h"
typedef struct node {
void *data;
struct node *next;
Expand Down
59 changes: 59 additions & 0 deletions machine_grammar.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
main_program program ( ) { stmts }
stmts decl_stmts assign_stmts
decl_stmts decl_stmt decl_stmts
decl_stmts decl_stmt
assign_stmts assign_stmt assign_stmts
assign_stmts assign_stmt
decl_stmt decl_non_jagged
decl_stmt decl_jagged
decl_non_jagged declare list_of_identifiers : declaration_type
decl_jagged declare list_of_identifiers : jagged_array
list_of_identifiers list of variables id_list
list_of_identifiers ID
id_list ID id_list
id_list ID ID
declaration_type primitive_type ;
declaration_type rect_array ;
primitive_type integer
primitive_type real
primitive_type boolean
rect_array array range_list of primitive_type
range_list [ var .. var ] range_list
range_list [ var .. var ]
jagged_array jagged array jagged2list of primitive_type ; jagged2init
jagged_array jagged array jagged3list of primitive_type ; jagged3init
jagged2list range_list [ ]
jagged3list range_list [ ] [ ]
jagged2init R1 [ var ] : size var : values { j2list } jagged2init
jagged2init R1 [ var ] : size var : values { j2list }
j2list value_list ; j2list
j2list value_list
j2list ; j2list
j2list ;
jagged3init R1 [ var ] : size var : values { j3list } jagged3init
jagged3init R1 [ var ] : size var : values { j3list }
j3list value_list ; j3list
j3list value_list
j3list ; j3list
j3list ;
value_list var value_list
value_list var
index_list var index_list
index_list var
var ID [ index_list ]
var CONST
var ID
assign_stmt var_lhs = expr
var_lhs ID [ index_list ]
var_lhs ID
expr arithmeticexpr ;
arithmeticexpr term + arithmeticexpr
arithmeticexpr term - arithmeticexpr
arithmeticexpr term ||| arithmeticexpr
arithmeticexpr term
term fact * term
term fact / term
term fact &&& term
term fact
fact var
fact ( arithmeticexpr )
File renamed without changes.
File renamed without changes.
File renamed without changes.
56 changes: 0 additions & 56 deletions plot_parse_tree.py

This file was deleted.

File renamed without changes.
Loading

0 comments on commit ada8be1

Please sign in to comment.