''' #Assignment 2: Syntax Analyzer 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) =...

I have at least half the code written i just need the syntax analyzer implemented correctly on my python file. my file has standard update functions and comments are clear on what each function does


''' #Assignment 2: Syntax Analyzer 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]): state = state + 1 #accept starting state temp = "" for ch in s: #stop at empty character if ch is EMPTY: temp = temp + ch #temp to keep track of tokens def writeStrings(w): with open(FILENAME2, mode = 'w') as f: pass 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() CS423 Assignment 2 All sections: Due by 4/18 (Saturday), 11:59 pm The second assignment is to write a syntax analyzer. You may use any top-down parser such as a RDP, a predictive recursive descent parser or a table driven predictive parser (100 points). Hence, your assignment consists of the following tasks: (10 points for the Documentation and 80-90 points for the code and implementation) 1. Write the Design Documentation and Specifications for your project (1-10 points) 2. Rewrite the grammar provided to remove any left recursion (Also, use left factorization if necessary) Do the arithmetic expressions first using these rules: -> + | - | -> * | / | -> ( ) | | -> id *the rule is OPTIONAL Then do the Assignment statement using these rules: -> -> = ; *using a semicolon ; at the end of the rule is OPTIONAL Then do the single Declarative statement using these rules: -> -> *using a semicolon ; at the end of the rule is OPTIONAL 3. The parser should print to an output file the tokens, lexemes and the production rules used ; That is, first, write the token and lexeme found Then, print out all productions rules used for analyzing this token Note: - a simple way to do it is to have a “print statement” at the beginning of each function that will print the production rule. - It would be a good idea to have a “switch” with the “print statement” so that you can turn it on or off. 4. Then do the While and single If statements next for extra points. 5. Integrate your code with the lexer() or functions generated in the assignment 1 to get more points. 6. Error handling: if a syntax error occurs, your parser should generate a meaningful error message, such as token, lexeme, line number, and error type etc. Then, your program may exit or you may continue for further analysis. The bottom line is that your program must be able to parse the entire program if it is syntactically correct. Use the rules as needed posted on Titanium as a guide line and Turn in your assignment in separate working iterations for maximum credit Example Assume we have the following statement ….more …. a = b + c; …. more details in class…. One possible output would be as follows: …. more…. Token: Identifier Lexeme: a -> -> = ; Token: Operator Lexeme: = Token: Identifier Lexeme: b -> -> -> Token: Operator Lexeme: + ->  -> + Token: Identifier Lexeme: c -> -> Token: Separator Lexeme: ; ->  ->  …. more….. 1. Problem Statement The program creates a parser and lexer that first tokenizes source code for the implemented programming language and then parses it using RDP top-down parsing. The grammar removes left recursion and backtracking. There are two output files: one with tokens and lexemes from the lexer() function, and one with the production rules 2. How to use your program Using the program is simple. When first running the program, a list of tokens will read from a list of tokens. This way the program can be tailored to a specific language. After successfully reading the files, it then asks the user to enter a filename without the extension. It then attempts to open the file, if failed, a status message will appear. Two
Apr 25, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here