assign2.py ''' #Assignment 2: Syntax Analyzer Author: Sergio Herrera Grammar G = (N,T,S,R) S->E$ E->TE' E'->+TE'|-TE'|e T->FT' T'->*FT'|/FT'|e F->id|num|(E) Note: e represents epsilon ''' #FIRST AND...

Need to complete Assignment 3 but is built on Assignment 2 which i already have some skeleton code for but couldnt finish implementing. Would like python file with skeleton code to be used for Assignment 3


assign2.py ''' #Assignment 2: Syntax Analyzer Author: Sergio Herrera Grammar G = (N,T,S,R) S->E$ E->TE' E'->+TE'|-TE'|e T->FT' T'->*FT'|/FT'|e F->id|num|(E) Note: e represents epsilon ''' #FIRST AND FOLLOW SETS #FIRST(S) = FIRST(F) = FIRST(T) = FIRST(E) firstS = firstF = firstT = firstE = {')', 'id', 'num'} #FOLLOW(E') = FOLLOW(E) followEprime = followE = {')', '$'} #FIRST(E') firstEprime = {'+', '-', 'e'} #FOLLOW(E') = FOLLOW(T) followTprime = followT = firstEprime.union(followE) #FIRST(T') firstTprime = {'*', '/', 'e'} #FOLLOW(F) followF = followTprime.union(firstTprime) #CONSTANTS KEYWORDS = ['int', 'float', 'bool', 'true', 'false', 'if', 'else', 'then', 'endif', 'while', 'whileend', 'do', 'doend', 'for', 'forend', 'input', 'output', 'and', 'or', 'not'] SEPARATORS = ['(', ')', '{', '}', '[', ']', ',', '.', ':', ';'] RELOPS = ['+', '-', '*', '/', '>', '<', '%',="" '="">=', '<=', '="='," '!='] COMMENT = [' !',="" '%']="" empty=' ' newline='\n' stop='$' filename1="" filename2="out.txt" #call="" read="" function="" def="" main():="" readfile()="" #read="" text="" file="" and="" send="" string="" to="" parse="" def="" readfile():="" global="" filename1="" filename1="input("Enter" read="" filename="" followed="" by="" extension:="" ")="" print("file="" 1="" is="" "="" +="" filename1)="" f="open(FILENAME1," mode='r' )="" filelines="f.readlines()" f.close()="" #close="" read="" file="" n="len(fileLines)" for="" i,line="" in="" enumerate(filelines):="" if="" i="=" (n-1):="" line="line" +="" '$'="" #add="" '$'="" to="" end="" of="" string="" to="" indicate="" stopping="" symbol="" #ignore="" lines="" with="" comment="" characters="" if="" line[0]="" not="" in="" comment="" or="" line[:1]="" not="" in="" comment:="" if="" line[0]="" !="NEWLINE:" parsestrings(line)="" #call="" to="" parse="" through="" each="" character="" in="" string="" def="" parsestrings(s):="" state="-1" #initial="" state="" #check="" if="" line="" has="" at="" least="" one="" character="" if="" len(s)=""> 0: if len(s) == 1 and s is STOP: print("s = " + s + ", end of the string") else: if isLetter(s[0]): #starting state accepted #first char is letter state = state + 1 temp = "" for ch in s: #stop at empty character if ch is EMPTY: temp = temp + ch #temp to keep track of tokens k,v = addToken(state, temp) writeStrings(k,v) def writeStrings(k,v): s = "TOKEN: " + k + "\tLexeme: " + v s = s + "-><" +="" k="" +="" "="">" with open(FILENAME2, mode = 'a') as f: f.write(s) f.close() #RETURN CORRECT TOKEN AND LEXEME def addToken(tok, lex): switcher = { 1: "IDENTIFIER", 2: "KEYWORD", 3: "OPERATOR", 4: "SEPARATOR", 5: "NUMBER", 6: "ERROR" } return switcher[tok], lex #CHECK IF CHARACTER IS A LETTER def isLetter(ch): return ch.isalpha() #CHECK IF CHARACTER IS A DIGIT def isNumber(ch): return ch.isdigit() #CHECK IF NUMBER IS A FLOAT def isFloat(f): try: float(f) return True except: return False #CHECK IF TOKEN IS KEYWORD def isKeyword(k): if k.lower in KEYWORDS: return True return False #CHECK IF SUBSTRING IS RELATIONAL OPERATOR def isRelop(r): if r in RELOPS: return True return False #CHECK IF CHARACTER IS SEPARATOR def isSeparator(s): if s in SEPARATORS: return True return False #CHECK IF LETTER OR WORD IS VALID ID def isValidID(id): size = len(id) if size == 1: return isLetter(id) else: if isLetter(id[0]): for i in range(1,size): if not isLetter(id[i]) or not isNumber(id[i]): return False return True if __name__ == "__main__": main() in.txt !this is a comment a Assignment 3 Partial Solutions.pdf 1 Assignment 3 solutions Using RDP *Semantic actions are under-lined 1) Assignment Statement A1) A -> id = E { gen_instr (POPM, get_address(id)) } A2) E -> T E’ A3) E’ -> + T { gen_intsr (ADD, nil) } E’ A4) E’ -> ε A5) T -> F T’ A6) T’ -> *F { gen_instr (MUL, nil) } T’ A7) T’ -> ε A8) F -> id { gen_instr (PUSHM, get_address(id) ) Procedure A () { If token = id then { save = token; lexer(); If token = “=” then { lexer(); E(); get_instr (POPM, get_address (save) ); } else error_message ( “= expected” ); } else error_massage ( “ id expected” ); } Procedure E (): { T (); E’(); 2 } Procedure E’(); { If token = “+” then { lexer(); T(); gen_instr (ADD, nil); E’(); } }; Procedure T(); { F(); T’(); } Procedure T’() { If token = “*” then { lexer(); F(); gen_instr(MUL, nil); T’(); } } Procedure F(); { If token = id then { gen_instr(PUSHM, get_address (token)); lexer(); } 3 else error_message(“id expected”); }; Procedure gen_instr(op, oprnd) /* instr_address shows the current insturction address is global */ { Instr_table [instr_address].address = inst_address; Instr_table [instr_address].op = op; Instr_table [instr_address].oprnd = oprnd; Instr_address++; }; Example: x = a + b*c (addresses a = 5001 , b=5002, c=5003 and x = 5004) INSTR_TABLE address Op Oprnd 1 PUSHM 5001 2 PUSHM 5002 3 PUSHM 5003 4 MUL nil 5 ADD nil 6 POPM 5004 Print from INSTR_TABLE ignoring “nil” 4 2. While Statement W1) W -> while ( C ) S whileend W2) C - > E R E W3) R -> == | ^= | > | < |=""> | =< procedure while_statement(); { if token = “while” then { addr = instr_address; procedure="" while_statement();="" {="" if="" token="“while”" then="" {="" addr="">
May 06, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here