-
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.
- Loading branch information
0 parents
commit 6e3289e
Showing
9 changed files
with
5,071 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
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,58 @@ | ||
%{ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
%} | ||
|
||
/* Prevent the need for linking with -lfl */ | ||
%option noyywrap | ||
%option nounput | ||
%option noinput | ||
|
||
%% | ||
|
||
\n { ++yylineno;} | ||
[\ \t\b\f]+ // ignore whitespace | ||
|
||
"0"[xX][0-9AaBbCcDdEeFf]+ {char *end; int val = strtol(yytext, &end, 16); yylval.intValue=val; return INTEGER_LITERAL;} | ||
"0"[xX][0-9a-zA-Z]+ printf("Invalid hexadecimal number %s at line %d\n", yytext, yylineno); | ||
"0"[0-7]+ {char *end; int val = strtol(yytext, &end, 8); yylval.intValue=val; return INTEGER_LITERAL;} | ||
"0"[0-9a-zA-Z]+ printf("Invalid octal number %s at line %d\n", yytext, yylineno); | ||
[0-9]+ {char *end; int val = strtol(yytext, &end, 10); yylval.intValue=val; return INTEGER_LITERAL;} | ||
|
||
"const" { yylval.lineno = yylineno; return CONST; } | ||
"int" { yylval.lineno = yylineno; return INT; } | ||
"float" { yylval.lineno = yylineno; return FLOAT; } | ||
"void" { yylval.lineno = yylineno; return VOID; } | ||
"if" { yylval.lineno = yylineno; return IF; } | ||
"else" { yylval.lineno = yylineno; return ELSE; } | ||
"while" { yylval.lineno = yylineno; return WHILE; } | ||
"break" { yylval.lineno = yylineno; return BREAK; } | ||
"continue" { yylval.lineno = yylineno; return CONTINUE; } | ||
"return" { yylval.lineno = yylineno; return RETURN; } | ||
|
||
"{" { yylval.lineno = yylineno; return LBRACE; } | ||
"}" { yylval.lineno = yylineno; return RBRACE; } | ||
"[" { yylval.lineno = yylineno; return LBRACKET; } | ||
"]" { yylval.lineno = yylineno; return RBRACKET; } | ||
"(" { yylval.lineno = yylineno; return LPAREN; } | ||
")" { yylval.lineno = yylineno; return RPAREN; } | ||
"," { yylval.lineno = yylineno; return COMMA; } | ||
";" { yylval.lineno = yylineno; return SEMICOLON; } | ||
"+" { yylval.lineno = yylineno; return ADD; } | ||
"-" { yylval.lineno = yylineno; return SUB; } | ||
"*" { yylval.lineno = yylineno; return MUL; } | ||
"/" { yylval.lineno = yylineno; return DIV; } | ||
"=" { yylval.lineno = yylineno; return ASSIGN; } | ||
"==" { yylval.lineno = yylineno; return EQUAL; } | ||
"!=" { yylval.lineno = yylineno; return UNEQUAL; } | ||
"<" { yylval.lineno = yylineno; return LESS; } | ||
">" { yylval.lineno = yylineno; return GREATER; } | ||
"<=" { yylval.lineno = yylineno; return LESSEQUAL; } | ||
">=" { yylval.lineno = yylineno; return GREATEREQUAL; } | ||
|
||
[A-Za-z_][A-Za-z0-9_]* {yylval.string=yytext; printf("%s\n", yylval.string); return ID;} | ||
|
||
. printf("Invalid character %c at line %d \n", yytext[0], yylineno); | ||
%% | ||
|
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,29 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
extern int yylineno; | ||
|
||
#include "yacc.tab.c" | ||
#include "lex.yy.c" | ||
|
||
int main() | ||
{ | ||
yylineno = 1; | ||
yyin = fopen("test1.txt", "r"); | ||
root = createTreeNode("root"); | ||
|
||
if (yyin) | ||
{ | ||
yyparse(); | ||
fclose(yyin); | ||
} | ||
else | ||
{ | ||
printf("unable to open file:\n"); | ||
return 0; | ||
} | ||
|
||
traverseTree(root, 0); | ||
|
||
return 0; | ||
} |
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,15 @@ | ||
int main() | ||
{ | ||
int a; | ||
int i; | ||
int b; | ||
a=b+1; | ||
if (a==b) | ||
{ | ||
i=1; | ||
} | ||
else | ||
{ | ||
i=2; | ||
} | ||
} |
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,111 @@ | ||
#include <stddef.h> | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
|
||
typedef struct TreeNode | ||
{ | ||
char *name; | ||
int lineno; | ||
int intValue; | ||
char *stringValue; | ||
struct ChildNode *children; | ||
} TreeNode; | ||
|
||
typedef struct ChildNode | ||
{ | ||
TreeNode *child; | ||
struct ChildNode *next; | ||
} ChildNode; | ||
|
||
TreeNode *createTreeNode(char* name) | ||
{ | ||
if(name == NULL) | ||
return NULL; | ||
TreeNode *newNode = malloc(sizeof(TreeNode)); | ||
newNode->name = name; | ||
newNode->children = NULL; | ||
return newNode; | ||
} | ||
|
||
TreeNode *createTreeNodeLine(char *name, TreeNode *child) | ||
{ | ||
if (name == NULL) | ||
return NULL; | ||
TreeNode *newNode = malloc(sizeof(TreeNode)); | ||
newNode->name = name; | ||
//newNode->lineno = child->lineno; | ||
newNode->children = NULL; | ||
return newNode; | ||
} | ||
|
||
TreeNode *createTreeNodeInt(char *name, int line, int value) | ||
{ | ||
if (name == NULL) | ||
return NULL; | ||
TreeNode *newNode = malloc(sizeof(TreeNode)); | ||
newNode->name = name; | ||
newNode->lineno = line; | ||
newNode->intValue = value; | ||
newNode->children = NULL; | ||
return newNode; | ||
} | ||
|
||
TreeNode *createTreeNodeString(char *name, int line, char *value) | ||
{ | ||
printf("----%s: %s----\n", name, value); | ||
if (name == NULL) | ||
return NULL; | ||
TreeNode *newNode = malloc(sizeof(TreeNode)); | ||
newNode->name = name; | ||
newNode->lineno = line; | ||
newNode->stringValue = value; | ||
newNode->children = NULL; | ||
return newNode; | ||
} | ||
|
||
void addChild(TreeNode *parent, TreeNode *child) | ||
{ | ||
ChildNode *newChildNode = malloc(sizeof(ChildNode)); | ||
newChildNode->child = child; | ||
newChildNode->next = NULL; | ||
|
||
if (parent->children == NULL) | ||
{ | ||
parent->children = newChildNode; | ||
} | ||
else | ||
{ | ||
ChildNode *current = parent->children; | ||
while (current->next != NULL) | ||
{ | ||
current = current->next; | ||
} | ||
current->next = newChildNode; | ||
} | ||
} | ||
|
||
void traverseTree(TreeNode *root, int depth) | ||
{ | ||
if (root == NULL) | ||
return; | ||
|
||
for (int i = 0; i < depth; i++) { | ||
printf(" "); | ||
} | ||
|
||
if (root->stringValue != NULL) | ||
printf("%s: %s\n", root->name, root->stringValue); | ||
else if (root->intValue != 0) | ||
printf("%s: %d\n", root->name, root->intValue); | ||
else if (root->lineno != 0) | ||
printf("%s (%d)\n", root->name, root->lineno); | ||
else | ||
printf("%s\n", root->name); | ||
|
||
ChildNode *current = root->children; | ||
while (current != NULL) | ||
{ | ||
traverseTree(current->child, depth + 1); | ||
current = current->next; | ||
} | ||
} |
Binary file not shown.
Oops, something went wrong.