mirror of
https://github.com/RustyRaptor/compilers.git
synced 2026-12-03 15:15:17 -07:00
added lex tokens using a python program
This commit is contained in:
1 parent
e38a95e6a8
commit
a6f6e58569
7 files changed
+524
No files matched your search
@@ -0,0 +1,26 @@
|
||||
# FLAGS = -O0 -Wall -Wextra -pedantic
|
||||
|
||||
# the gcc options so I don't have to copy paste them everywhere
|
||||
# any time I make changes to them.
|
||||
FLAGS = -O0 -Wextra -pedantic
|
||||
|
||||
# Entry point target
|
||||
all: lab5
|
||||
|
||||
# Compile all the THINGS!
|
||||
lab5: lab5.l lab5.y symtable.c symtable.h
|
||||
lex lab5.l
|
||||
yacc -d lab5.y
|
||||
gcc $(FLAGS) y.tab.c lex.yy.c symtable.c -o lab5 -lm
|
||||
|
||||
# remove the binary and other generated stuff
|
||||
clean:
|
||||
rm lab5 lex.yy.c y.tab.c y.tab.h
|
||||
|
||||
# runs valgrind to look for any memory mistakes
|
||||
memcheck:
|
||||
valgrind ./lab5 -s
|
||||
|
||||
# executes our code to test it's functionality
|
||||
test:
|
||||
./lab5 < testinput.txt
|
||||
@@ -0,0 +1,15 @@
|
||||
"""
|
||||
Generates the LEX directives for the DECAF tokens
|
||||
place the raw token definition from the page in tokens.txt
|
||||
remove all the comments in parentheses
|
||||
execute this python program in the folder.
|
||||
"""
|
||||
|
||||
f = open("./tokens.txt", "r")
|
||||
|
||||
for line in f:
|
||||
token_sym = ' '.join(line.split()).split(" ")
|
||||
token = token_sym[0]
|
||||
sym = token_sym[1]
|
||||
spaces = " " * (20 - len(sym)+2)
|
||||
print('\"%s\"%s{return(%s);}' % (sym, spaces, token))
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
/* Small LEX routine which returns two formal tokens (INTEGER and VARIABLE)
|
||||
along with single string elements like '+'.
|
||||
|
||||
This LEX definition is the companion to the docalc.y YACC routine which
|
||||
is a simple calculator
|
||||
|
||||
Shaun Cooper
|
||||
January 2015
|
||||
*/
|
||||
|
||||
%{
|
||||
int mydebug=0;
|
||||
#include "y.tab.h"
|
||||
int yylex();
|
||||
|
||||
%}
|
||||
|
||||
/*
|
||||
- added an entry for parenthesis to the tokens directive
|
||||
- added int directive to return T_INT token
|
||||
- changed the directive for VARIABLE to accept multichar variables
|
||||
- added semicolon as a token
|
||||
*/
|
||||
%%
|
||||
"&&" {return(T_AND);}
|
||||
"=" {return(T_ASSIGN);}
|
||||
"bool" {return(T_BOOLTYPE);}
|
||||
"break" {return(T_BREAK);}
|
||||
"char_lit" {return(T_CHARCONSTANT);}
|
||||
"continue" {return(T_CONTINUE);}
|
||||
"." {return(T_DOT);}
|
||||
"else" {return(T_ELSE);}
|
||||
"==" {return(T_EQ);}
|
||||
"extern" {return(T_EXTERN);}
|
||||
"false" {return(T_FALSE);}
|
||||
"for" {return(T_FOR);}
|
||||
"func" {return(T_FUNC);}
|
||||
">=" {return(T_GEQ);}
|
||||
">" {return(T_GT);}
|
||||
"identifier" {return(T_ID);}
|
||||
"if" {return(T_IF);}
|
||||
"int_lit" {return(T_INTCONSTANT);}
|
||||
"int" {return(T_INTTYPE);}
|
||||
"<<" {return(T_LEFTSHIFT);}
|
||||
"<=" {return(T_LEQ);}
|
||||
"!=" {return(T_NEQ);}
|
||||
"null" {return(T_NULL);}
|
||||
"||" {return(T_OR);}
|
||||
"package" {return(T_PACKAGE);}
|
||||
"return" {return(T_RETURN);}
|
||||
">>" {return(T_RIGHTSHIFT);}
|
||||
"string_lit" {return(T_STRINGCONSTANT);}
|
||||
"string" {return(T_STRINGTYPE);}
|
||||
"true" {return(T_TRUE);}
|
||||
"var" {return(T_VAR);}
|
||||
"void" {return(T_VOID);}
|
||||
"whil" {return(T_WHILE);}
|
||||
|
||||
"int" {
|
||||
return(T_INT);
|
||||
}
|
||||
[a-zA-Z][a-zA-Z0-9]* {
|
||||
if (mydebug) fprintf(stderr,"Letter found\n");
|
||||
yylval.string=strdup(yytext);
|
||||
return(VARIABLE);
|
||||
|
||||
}
|
||||
[0-9][0-9]* {
|
||||
if (mydebug) fprintf(stderr,"Digit found\n");
|
||||
yylval.value=atoi((const char *)yytext);
|
||||
return(INTEGER);
|
||||
}
|
||||
|
||||
[ \t] {
|
||||
if (mydebug) fprintf(stderr,"Whitespace found\n");
|
||||
}
|
||||
|
||||
[=\-+*/%&|();^] {
|
||||
if (mydebug) fprintf(stderr,"return a token %c\n",*yytext);
|
||||
return (*yytext);
|
||||
}
|
||||
|
||||
\n {
|
||||
if (mydebug) fprintf(stderr,"carriage return %c\n",*yytext);
|
||||
return (*yytext);
|
||||
}
|
||||
%%
|
||||
|
||||
int yywrap(void)
|
||||
{ return 1;}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
%{
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
int yylex();
|
||||
|
||||
|
||||
void yyerror (s) /* Called by yyparse on error */
|
||||
char *s;
|
||||
{
|
||||
printf ("%s \n", s);
|
||||
}
|
||||
|
||||
|
||||
%}
|
||||
/* defines the start symbol, what values come back from LEX and how the operators are associated changed to P */
|
||||
/* changed to P */
|
||||
%start P
|
||||
|
||||
/* Defines possible types */
|
||||
%union
|
||||
{
|
||||
int value;
|
||||
char* string;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
created entry for multiplication in similar format to existing rules.
|
||||
Fixed unary minus by making it one side only as opposed to the existing two sided
|
||||
expression.
|
||||
*/
|
||||
|
||||
|
||||
%% /* end specs, begin rules */
|
||||
|
||||
|
||||
|
||||
%% /* end of rules, start of program */
|
||||
|
||||
int main(){
|
||||
yyparse();
|
||||
}
|
||||
+258
@@ -0,0 +1,258 @@
|
||||
/*
|
||||
* Ziad Arafat
|
||||
* Feb 15 2021
|
||||
* Setup and understand symbol table tool functions in c
|
||||
*
|
||||
* Changes:
|
||||
* - reformated document with clang-format
|
||||
* - Moved all the prototypes and globals into symtable.h
|
||||
* - Commented the .h file
|
||||
* - Commented the .c file
|
||||
* - Reformatted printfs to reflect what the output will look like
|
||||
* - Added description of the data structure used.
|
||||
* - Changed SymTab to use a char pointer for symbol
|
||||
* - Changed all code to handle char pointed instead of label char array
|
||||
* - Made all functions non-interactive with parameters.
|
||||
* - Adjusted main code to pass inputs to the functions
|
||||
*
|
||||
* The main data structure in this code is a linked list. It's called SymTab.
|
||||
* It has 3 main fields. The label-symbol strings, the address integer, and the
|
||||
* next pointer.
|
||||
*
|
||||
* The label and symbol are just a name and the symbol itself we are storing.
|
||||
* The address is an integer storing the address of the symbol.
|
||||
*
|
||||
* The next pointer is just a pointer that points to the next Node which is
|
||||
* another instance of SymTab so it is recursive in the sense that the struct
|
||||
* uses a pointer to an instance of itself.
|
||||
*/
|
||||
#include <math.h>
|
||||
#include "symtable.h"
|
||||
|
||||
int size = 0; // stores the current size of the symtable
|
||||
|
||||
// moved the prototypes and struct definition to symtable.h
|
||||
|
||||
struct SymbTab *first, *last; // Initialize our head and tail of the linked list
|
||||
|
||||
/*
|
||||
* The main function will display a text menu with numbers corresponding to
|
||||
* each option. It takes inputs from user and passes them to the respective
|
||||
* function and displays results.
|
||||
*/
|
||||
// int main()
|
||||
// {
|
||||
// int op, y;
|
||||
|
||||
// // removed label
|
||||
// // added symbol string with size 100
|
||||
// char sym[100];
|
||||
// int addr = 0; // added variable to store address input
|
||||
|
||||
// do {
|
||||
// printf("\n\tSYMBOL TABLE IMPLEMENTATION\n");
|
||||
|
||||
// printf("\n\t1.INSERT\n\t2.DISPLAY\n\t3.DELETE\n\t4.SEARCH\n\t5.END\n");
|
||||
|
||||
// printf("\n\tEnter your option : ");
|
||||
|
||||
// scanf("%d", &op); // Get the user input
|
||||
|
||||
// switch (op) {
|
||||
// case 1:
|
||||
// // added prompt to get symbol and address from user
|
||||
// printf("Enter a symbol to INSERT: \n");
|
||||
// scanf("%s", sym);
|
||||
|
||||
// printf("Enter an address to INSERT:\n");
|
||||
// scanf("%d", &addr);
|
||||
|
||||
// // Pass inputs to Insert();
|
||||
// Insert(sym, addr);
|
||||
// break;
|
||||
// case 2:
|
||||
// Display();
|
||||
// break;
|
||||
// case 3:
|
||||
// // added prompt to get symbol and address from user
|
||||
// printf("Enter a symbol to DELETE: \n");
|
||||
// scanf("%s", sym);
|
||||
|
||||
// // pass sym input to Delete()
|
||||
// Delete(sym);
|
||||
// break;
|
||||
// case 4:
|
||||
// printf("\n\tEnter the symbol to be SEARCHED : ");
|
||||
// scanf("%s", sym); // search sym instead of label
|
||||
|
||||
// y = Search(sym);
|
||||
|
||||
// printf("\n\tSearch Result:");
|
||||
// if (y == 1) {
|
||||
// printf("\n\tThe symbol is present in the symbol table\n");
|
||||
// } else {
|
||||
// printf("\n\tThe symbol is not present in the symbol table\n");
|
||||
// }
|
||||
// break;
|
||||
// case 5:
|
||||
// exit(0);
|
||||
// }
|
||||
// } while (op < 6);
|
||||
// }
|
||||
|
||||
/*
|
||||
* Insert()
|
||||
* Inputs:
|
||||
* char *sym: the symbol we want to add to the table
|
||||
* int address: the address for the symbol we are adding.
|
||||
*
|
||||
* Inserts a new symbol-address pair to the symbol table if the symbol isn't
|
||||
* already there.
|
||||
*/
|
||||
void Insert(char *sym, int address)
|
||||
{
|
||||
int n;
|
||||
|
||||
n = Search(sym);
|
||||
// remove prompt for input
|
||||
if (n == 1) {
|
||||
printf("\n\tThe symbol exists already in the symbol table\n\tDuplicate can't be inserted");
|
||||
} else {
|
||||
struct SymbTab *p;
|
||||
|
||||
p = malloc(sizeof(struct SymbTab));
|
||||
|
||||
// remove user input
|
||||
p->symbol = strdup(sym); // copy symbol to new node
|
||||
p->addr = address; // add address to new node
|
||||
|
||||
p->next = NULL;
|
||||
|
||||
if (size == 0) {
|
||||
first = p;
|
||||
last = p;
|
||||
} else {
|
||||
last->next = p;
|
||||
last = p;
|
||||
}
|
||||
|
||||
size++;
|
||||
}
|
||||
printf("\n\tLabel inserted\n");
|
||||
}
|
||||
|
||||
/*
|
||||
* Display()
|
||||
* Uses a cursor node to traverse the symtable and display the contents of
|
||||
* each entry.
|
||||
*/
|
||||
void Display()
|
||||
{
|
||||
struct SymbTab *p;
|
||||
p = first;
|
||||
|
||||
printf("\n\tSYMBOL\t\tADDRESS\n"); // remove LABEL in print
|
||||
for (int i = 0; i < size; i++) {
|
||||
// removed the label value from the print
|
||||
printf("\t%s\t\t%d\n", p->symbol, p->addr);
|
||||
p = p->next;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Search()
|
||||
* Input: symbol, the symbol to search for
|
||||
* Output: 1 if the symbol was found. 0 if it wasn't.
|
||||
*
|
||||
* Traverses the linked list until it finds the symbol
|
||||
*/
|
||||
int Search(char *symbol) // changed input to symbol char pointer
|
||||
{
|
||||
int i, flag = 0;
|
||||
struct SymbTab *p;
|
||||
p = first;
|
||||
for (i = 0; i < size; i++) {
|
||||
// Changed label to symbol
|
||||
if (strcmp(p->symbol, symbol) == 0) {
|
||||
flag = 1;
|
||||
}
|
||||
p = p->next;
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
// Removed Modify()
|
||||
|
||||
/*
|
||||
* Delete()
|
||||
* Input: sym, the symbol to delete from the table
|
||||
*
|
||||
* Deletes a symbol from the table using a cursom and redirecting the next
|
||||
* pointer of a node.
|
||||
*/
|
||||
void Delete(char *sym)
|
||||
{
|
||||
int a;
|
||||
// removed char array
|
||||
struct SymbTab *p, *q;
|
||||
p = first;
|
||||
|
||||
// removed print and scanf
|
||||
|
||||
a = Search(sym);
|
||||
if (a == 0)
|
||||
printf("\n\tLabel not found\n");
|
||||
else {
|
||||
// replaced label with symbol
|
||||
if (strcmp(first->symbol, sym) == 0)
|
||||
first = first->next;
|
||||
// replaced label with symbol
|
||||
else if (strcmp(last->symbol, sym) == 0) {
|
||||
q = p->next;
|
||||
// replaced label with symbol
|
||||
while (strcmp(q->symbol, sym) != 0) {
|
||||
p = p->next;
|
||||
q = q->next;
|
||||
}
|
||||
p->next = NULL;
|
||||
last = p;
|
||||
} else {
|
||||
q = p->next; // replaced label with symbol
|
||||
while (strcmp(q->symbol, sym) != 0) {
|
||||
p = p->next;
|
||||
q = q->next;
|
||||
}
|
||||
p->next = q->next;
|
||||
}
|
||||
size--;
|
||||
printf("\n\tAfter Deletion:\n");
|
||||
Display();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
FetchAddress
|
||||
|
||||
input: char variable
|
||||
output: integer address
|
||||
|
||||
Modified version of Search. Returns the address integer instead of flag.
|
||||
*/
|
||||
int FetchAddress(char *s)
|
||||
{
|
||||
int i;
|
||||
struct SymbTab *p;
|
||||
p = first;
|
||||
for (i = 0; i < size; i++) {
|
||||
// Changed label to symbol
|
||||
if (strcmp(p->symbol, s) == 0) {
|
||||
return p->addr;
|
||||
}
|
||||
p = p->next;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int powfuck(int x, int y) {
|
||||
return pow(x, y);
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
|
||||
|
||||
/*
|
||||
* CS370 - Lab 3
|
||||
* Program name: Symtable
|
||||
* Purpose of program: Implements Symbol Table
|
||||
* written by: Ziad Arafat
|
||||
* Date Written: 2020-02-15
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
#ifndef SYMTABLE_H
|
||||
#define SYMTABLE_H
|
||||
|
||||
int size;
|
||||
|
||||
/*
|
||||
* Insert()
|
||||
* Inputs:
|
||||
* char *sym: a pointer to the symbol char array we want to insert
|
||||
* int address: an integer address to pair with the symbol
|
||||
*/
|
||||
void Insert(char *sym, int address); // insert a symbol, and address
|
||||
|
||||
void Display(); // Display the symboltable
|
||||
|
||||
int FetchAddress(char *s); // added FetchAddress prototype
|
||||
|
||||
int powfuck(int x, int y);
|
||||
|
||||
/*
|
||||
* Delete()
|
||||
* Input char sym: a pointer to the symbol char array we want to remove
|
||||
*/
|
||||
void Delete(char *sym);
|
||||
|
||||
// search for a symbol in the table and return weather it was found
|
||||
int Search(char * sym); // changed input to symbol
|
||||
|
||||
// removed Modify
|
||||
|
||||
/*
|
||||
* Structure for our linked list that stores the symbol table.
|
||||
* Contains a char pointer to a symbol, an integer with the address
|
||||
* and a pointer to the next SymTab node
|
||||
*/
|
||||
struct SymbTab {
|
||||
char *symbol; // arrays storing the label and symbol data
|
||||
|
||||
int addr; // integer storing the address
|
||||
|
||||
struct SymbTab *next; // points to the next node
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,33 @@
|
||||
T_AND &&
|
||||
T_ASSIGN =
|
||||
T_BOOLTYPE bool
|
||||
T_BREAK break
|
||||
T_CHARCONSTANT char_lit
|
||||
T_CONTINUE continue
|
||||
T_DOT .
|
||||
T_ELSE else
|
||||
T_EQ ==
|
||||
T_EXTERN extern
|
||||
T_FALSE false
|
||||
T_FOR for
|
||||
T_FUNC func
|
||||
T_GEQ >=
|
||||
T_GT >
|
||||
T_ID identifier
|
||||
T_IF if
|
||||
T_INTCONSTANT int_lit
|
||||
T_INTTYPE int
|
||||
T_LEFTSHIFT <<
|
||||
T_LEQ <=
|
||||
T_NEQ !=
|
||||
T_NULL null
|
||||
T_OR ||
|
||||
T_PACKAGE package
|
||||
T_RETURN return
|
||||
T_RIGHTSHIFT >>
|
||||
T_STRINGCONSTANT string_lit
|
||||
T_STRINGTYPE string
|
||||
T_TRUE true
|
||||
T_VAR var
|
||||
T_VOID void
|
||||
T_WHILE while
|
||||
Reference in new issue
Block a user