Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: error while compiling y.tab.c file #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Part 2 - Adding the Grammar Rules/parser1.y
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include"lex.yy.c"

char * yytext;

void yyerror(const char *s);
int yylex();
Expand Down
18 changes: 9 additions & 9 deletions Part 3 - Generating the Symbol Table/parser2.y
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,7 @@
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include"lex.yy.c"

void yyerror(const char *s);
int yylex();
int yywrap();
void add(char);
void insert_type();
int search(char *);
void insert_type();

struct dataType {
char * id_name;
char * data_type;
Expand All @@ -24,6 +15,15 @@
int q;
char type[10];
extern int countn;
char * yytext;

void yyerror(const char *s);
int yylex();
int yywrap();
void add(char);
void insert_type();
int search(char *);
void insert_type();
%}

%token VOID CHARACTER PRINTFF SCANFF INT FLOAT CHAR FOR IF ELSE TRUE FALSE NUMBER FLOAT_NUM ID LE GE EQ NE GT LT AND OR STR ADD MULTIPLY DIVIDE SUBTRACT UNARY INCLUDE RETURN
Expand Down
25 changes: 13 additions & 12 deletions Part 4 - Creating the Syntax Tree/parser3.y
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,7 @@
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include"lex.yy.c"
void yyerror(const char *s);
int yylex();
int yywrap();
void add(char);
void insert_type();
int search(char *);
void insert_type();
void printtree(struct node*);
void printInorder(struct node *);
struct node* mknode(struct node *left, struct node *right, char *token);


struct dataType {
char * id_name;
char * data_type;
Expand All @@ -31,6 +20,18 @@
struct node *right;
char *token;
};
char *yytext;

void yyerror(const char *s);
int yylex();
int yywrap();
void add(char);
void insert_type();
int search(char *);
void insert_type();
void printtree(struct node*);
void printInorder(struct node *);
struct node* mknode(struct node *left, struct node *right, char *token);
%}

%union {
Expand Down
33 changes: 16 additions & 17 deletions Part 5 - Adding the Semantic Analyzer/parser4.y
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,7 @@
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include"lex.yy.c"
void yyerror(const char *s);
int yylex();
int yywrap();
void add(char);
void insert_type();
int search(char *);
void insert_type();
void print_tree(struct node*);
void print_inorder(struct node *);
void check_declaration(char *);
void check_return_type(char *);
int check_types(char *, char *);
char *get_type(char *);
struct node* mknode(struct node *left, struct node *right, char *token);


struct dataType {
char * id_name;
char * data_type;
Expand All @@ -36,13 +21,27 @@
char buff[100];
char errors[10][100];
char reserved[10][10] = {"int", "float", "char", "void", "if", "else", "for", "main", "return", "include"};

struct node {
struct node *left;
struct node *right;
char *token;
};
char *yytext;

void yyerror(const char *s);
int yylex();
int yywrap();
void add(char);
void insert_type();
int search(char *);
void insert_type();
void print_tree(struct node*);
void print_inorder(struct node *);
void check_declaration(char *);
void check_return_type(char *);
int check_types(char *, char *);
char *get_type(char *);
struct node* mknode(struct node *left, struct node *right, char *token);
%}

%union { struct var_name {
Expand Down
32 changes: 16 additions & 16 deletions Part 6 - Intermediate Code Generation/parser5.y
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,7 @@
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include"lex.yy.c"
void yyerror(const char *s);
int yylex();
int yywrap();
void add(char);
void insert_type();
int search(char *);
void insert_type();
void print_tree(struct node*);
void print_inorder(struct node *);
void check_declaration(char *);
void check_return_type(char *);
int check_types(char *, char *);
char *get_type(char *);
struct node* mknode(struct node *left, struct node *right, char *token);


struct dataType {
char * id_name;
char * data_type;
Expand All @@ -40,13 +25,28 @@
char errors[10][100];
char reserved[10][10] = {"int", "float", "char", "void", "if", "else", "for", "main", "return", "include"};
char icg[50][100];
char *yytext;

struct node {
struct node *left;
struct node *right;
char *token;
};

void yyerror(const char *s);
int yylex();
int yywrap();
void add(char);
void insert_type();
int search(char *);
void insert_type();
void print_tree(struct node*);
void print_inorder(struct node *);
void check_declaration(char *);
void check_return_type(char *);
int check_types(char *, char *);
char *get_type(char *);
struct node* mknode(struct node *left, struct node *right, char *token);
%}

%union { struct var_name {
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# Frontend Phase of a C Compiler

## Environment
* flex 2.6.4
* bison (GNU Bison) 3.5.1
* Ubuntu 20.04.5 LTS

## Using the Compiler

```
lex lexer.l
yacc -d -v parser.y
gcc -ll -w y.tab.c
yacc -d parser.y
cc lex.yy.c y.tab.c
./a.out<input1.c
```

Expand Down
36 changes: 18 additions & 18 deletions parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,6 @@
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include"lex.yy.c"
void yyerror(const char *s);
int yylex();
int yywrap();
void add(char);
void insert_type();
int search(char *);
void insert_type();
void print_tree(struct node*);
void print_tree_util(struct node*, int);
void print_inorder(struct node *);
void check_declaration(char *);
void check_return_type(char *);
int check_types(char *, char *);
char *get_type(char *);
struct node* mknode(struct node *left, struct node *right, char *token);

struct dataType {
char * id_name;
Expand All @@ -27,11 +11,12 @@
int line_no;
} symbol_table[40];

char * yytext;
struct node *head;
int count=0;
int q;
char type[10];
extern int countn;
struct node *head;
int sem_errors=0;
int ic_idx=0;
int temp_var=0;
Expand All @@ -47,7 +32,22 @@
struct node *right;
char *token;
};


void yyerror(const char *s);
int yylex();
int yywrap();
void add(char);
void insert_type();
int search(char *);
void insert_type();
void print_tree(struct node* tree);
void print_tree_util(struct node *root, int space);
void print_inorder(struct node *tree);
void check_declaration(char *);
void check_return_type(char *);
int check_types(char *, char *);
char *get_type(char *);
struct node* mknode(struct node *left, struct node *right, char *token);
%}

%union { struct var_name {
Expand Down