-
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
1 parent
b9e4627
commit 9a81a81
Showing
5 changed files
with
584 additions
and
0 deletions.
There are no files selected for viewing
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,244 @@ | ||
import pandas as pd | ||
|
||
|
||
grammar = open("C:\My Files\Programming\compilerPhase2Final\grammar.txt", "r") | ||
x = grammar.read() | ||
g = x.split('\n') | ||
|
||
|
||
|
||
nonTerminalList = [] | ||
rhs = [] | ||
|
||
tempList = [] | ||
|
||
for line in g: | ||
tempList.append(line.split(" ")) | ||
|
||
|
||
|
||
for t in tempList: | ||
for i in t: | ||
if( i == '->'): | ||
c = t.index(i) | ||
nonTerminalList.append(t[c-1]) | ||
|
||
for t in tempList: | ||
for i in t: | ||
if( i == '->'): | ||
c = t.index(i) | ||
rhs.append(t[c+1:]) | ||
|
||
print("non: \n", nonTerminalList) | ||
print("rhs: \n", rhs) | ||
|
||
|
||
|
||
productions = dict() | ||
|
||
print("test", len(nonTerminalList)) | ||
|
||
counter = 0 | ||
for t in tempList: | ||
for i in t: | ||
if(i == nonTerminalList[counter]): | ||
productions[nonTerminalList[counter]] = rhs[counter] | ||
counter = counter + 1 | ||
print(counter, 'time coorect') | ||
if(counter == len(nonTerminalList) - 1): | ||
break | ||
if(counter == len(nonTerminalList) - 1): | ||
break | ||
|
||
|
||
first_dict = dict() | ||
follow_dict = dict() | ||
|
||
print("productions: \n", productions) | ||
|
||
#---first--- | ||
|
||
def first(nonTerminalList, productions): | ||
c = nonTerminalList[0] | ||
print("c",c) | ||
ans = set() | ||
if c.isupper(): | ||
c = nonTerminalList | ||
print("c2", c) | ||
for st in productions[c]: | ||
print("st", st) | ||
if st == '#' : | ||
if len(nonTerminalList)!=1 : | ||
ans = ans.union( first(nonTerminalList[1:], productions) ) | ||
else : | ||
ans = ans.union('@') | ||
else : | ||
f = first(st, productions) | ||
ans = ans.union(x for x in f) | ||
print("if else", ans) | ||
else: | ||
ans = ans.union(c) | ||
print("else", ans) | ||
return ans | ||
|
||
|
||
print ('\nFirst\n') | ||
|
||
|
||
for nonTerminalList in productions: | ||
first_dict[nonTerminalList] = first(nonTerminalList, productions) | ||
|
||
|
||
print("done") | ||
|
||
for f in first_dict: | ||
print(str(f) + " : " + str(first_dict[f])) | ||
|
||
#---follow--- | ||
|
||
def follow(s, productions, ans): | ||
if len(s) != 1 : | ||
return {} | ||
|
||
for key in productions: | ||
for value in productions[key]: | ||
f = value.find(s) | ||
if f != -1: | ||
if f == (len(value)-1): | ||
if key != s: | ||
if key in ans: | ||
temp = ans[key] | ||
else: | ||
ans = follow(key, productions, ans) | ||
temp = ans[key] | ||
ans[s] = ans[s].union(temp) | ||
else: | ||
first_of_next = first(value[f+1:], productions) | ||
ans[s] = ans[s].union(first_of_next) | ||
return ans | ||
|
||
|
||
print ('\nFollow\n') | ||
|
||
start = nonTerminalList[0] | ||
|
||
for nonTerminalList in productions: | ||
follow_dict[nonTerminalList] = set() | ||
start = nonTerminalList | ||
|
||
follow_dict[start] = follow_dict[start].union('$') | ||
|
||
for nonTerminalList in productions: | ||
follow_dict = follow(nonTerminalList, productions, follow_dict) | ||
|
||
for f in follow_dict: | ||
print(str(f) + " : " + str(follow_dict[f])) | ||
|
||
|
||
#---parsingTable---- | ||
|
||
def Table(follow, productions): | ||
|
||
print ("\nParsing Table\n") | ||
|
||
table = {} | ||
for key in productions: | ||
for value in productions[key]: | ||
if value != '#': | ||
for element in first(value, productions): | ||
table[key, element] = value | ||
else: | ||
for element in follow[key]: | ||
table[key, element] = value | ||
|
||
for key,val in table.items(): | ||
print( key, "=>", val) | ||
|
||
new_table = {} | ||
for pair in table: | ||
new_table[pair[1]] = {} | ||
|
||
for pair in table: | ||
new_table[pair[1]][pair[0]] = table[pair] | ||
|
||
|
||
print ("\n") | ||
print ("\nParsing Table in matrix form\n") | ||
print (pd.DataFrame(new_table).fillna('-')) | ||
print ("\n") | ||
|
||
return table | ||
|
||
|
||
#---parser---- | ||
|
||
def parse(tokens, start_symbol, parsingTable): | ||
|
||
flag = 0 | ||
|
||
#appending dollar to end of input | ||
tokens = tokens + "$" | ||
|
||
stack = [] | ||
|
||
stack.append("$") | ||
stack.append(start_symbol) | ||
|
||
input_len = len(tokens) | ||
index = 0 | ||
|
||
|
||
while len(stack) > 0: | ||
|
||
#element at top of stack | ||
top = stack[len(stack)-1] | ||
|
||
print ("Top =>",top) | ||
|
||
#current input | ||
current_input = tokens[index] | ||
|
||
print ("Current_Input => ",current_input) | ||
|
||
if top == current_input: | ||
stack.pop() | ||
index = index + 1 | ||
else: | ||
|
||
#finding value for key in table | ||
key = top , current_input | ||
print (key) | ||
|
||
#top of stack terminal => not accepted | ||
if key not in parsingTable: | ||
flag = 1 | ||
break | ||
|
||
value = parsingTable[key] | ||
if value != '#': | ||
value = value[::-1] | ||
value = list(value) | ||
|
||
#poping top of stack | ||
stack.pop() | ||
|
||
#push value chars to stack | ||
for element in value: | ||
stack.append(element) | ||
else: | ||
stack.pop() | ||
|
||
if flag == 0: | ||
print ("String accepted!") | ||
else: | ||
print ("String not accepted!") | ||
|
||
|
||
tokensFile = open("C:\My Files\Programming\compilerPhase2Final\tokens.txt", "r") | ||
t = tokensFile.read() | ||
tokens = t.split('\n') | ||
|
||
parsingTable = Table(follow_dict, productions) | ||
|
||
parse(tokens, start, parsingTable) | ||
|
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,146 @@ | ||
import numpy as np | ||
import pandas as pd | ||
import re | ||
|
||
|
||
f = open("decaf_input3.decaf", "r") | ||
x = f.read() | ||
|
||
|
||
Keywords = [] | ||
Operators = [] | ||
Decimals = [] | ||
Symbols = [] | ||
Identifiers = [] | ||
|
||
|
||
List_Keywords = ['def', 'if', 'else', 'while', 'return', 'break', 'continue', 'int', 'bool', 'void', 'true', 'false', 'for', 'callout', 'class', 'interface', 'extands', 'implements', 'new', 'this', 'string', 'float', 'double', 'null'] | ||
List_Symbols = ['(', '{', '[', ']', '}', ')', ',', ';', '=', '+', '-', '*', '/', '%', '<', '>', '<=', '>=', '==', '!=', '&&', '||', '!'] | ||
List_Decimals = [] | ||
List_String = [] | ||
List_Hex = [] | ||
List_Id = [] | ||
|
||
print('x', x) | ||
|
||
|
||
def remove_Spaces(x): | ||
|
||
y = [] | ||
for line in x: | ||
if (line.strip() != ''): | ||
y.append(line.strip()) | ||
|
||
return y | ||
|
||
|
||
def remove_Comments(x): | ||
|
||
Multi_Comments_Removed = re.sub("/\*[^*]*\*+(?:[^/*][^*]*\*+)*/", "", x) | ||
Single_Comments_Removed = re.sub("//.*", "", Multi_Comments_Removed) | ||
Comments_removed = Single_Comments_Removed | ||
|
||
|
||
return Comments_removed | ||
|
||
|
||
|
||
Comments_removed = remove_Comments(x) | ||
z = Comments_removed.split('\n') | ||
z_ = remove_Spaces(z) | ||
|
||
print('z', z) | ||
print('z_', z_) | ||
|
||
|
||
a = '\n'.join([str(elem) for elem in z_]) | ||
scanned_Program_lines = a.split('\n') | ||
|
||
|
||
C = [] | ||
for line in scanned_Program_lines: | ||
C.append(line) | ||
|
||
za = [] | ||
for x in C: | ||
za.append(x.split()) | ||
|
||
print('za', za) | ||
|
||
# Decimal | ||
for q in za: | ||
for i in q: | ||
if i.isdecimal(): | ||
List_Decimals.append(i) | ||
print('List_Decimals', List_Decimals) | ||
|
||
# String | ||
for q in za: | ||
for i in q: | ||
if(i.startswith('"')): | ||
List_String.append(i) | ||
|
||
print(List_String) | ||
|
||
# Identifiers | ||
for q in za: | ||
for i in q: | ||
if(i.isidentifier()): | ||
List_Id.append(i) | ||
print('List_Id', List_Id) | ||
|
||
# Jadvale Namad | ||
|
||
name = List_Id + List_Keywords | ||
nemune = list(set(name)) | ||
print('nemune', nemune) | ||
|
||
Nemune = pd.DataFrame(nemune) | ||
print(Nemune) | ||
|
||
|
||
|
||
# Donbale Tokens | ||
|
||
tokens = [] | ||
|
||
for u in za: | ||
for i in u: | ||
for l in List_Keywords: | ||
if(i == l): | ||
index = nemune.index(i) | ||
token = ['KEY', index] | ||
tokens.append(token) | ||
|
||
for o in List_Id: | ||
if(i == o): | ||
index = nemune.index(i) | ||
token = ('ID', index) | ||
tokens.append(token) | ||
|
||
for m in List_Symbols: | ||
if(i == m): | ||
token = ('SYM', i) | ||
tokens.append(token) | ||
|
||
|
||
for n in List_String: | ||
if(i == n): | ||
token = ('STR', i) | ||
tokens.append(token) | ||
|
||
for p in List_Decimals: | ||
if(i == p): | ||
token = ('DEC', i) | ||
tokens.append(token) | ||
|
||
"""for q in List_Hex: | ||
if(i == q): | ||
token = ('HEX', i) | ||
tokens.append(token)""" | ||
|
||
print("-------------------------------------------------------") | ||
print(tokens) | ||
|
||
Tokens = pd.DataFrame(tokens) | ||
print(Tokens) |
Oops, something went wrong.