Question

This program will require you to create a basic parser for an input string that is...

This program will require you to create a basic parser for an input string that is somewhat like Python. Python has an older parser module and a newer ast module to manage pure Python syntax, but they won't be useful here.

The program will read a data string, identify the control characters, and print out the overall structure of the string. Here's an example

Input String: [{1}]

Output: Number inside a dictionary inside a list

Here are some basic rules for the program:

  • The control characters you need to be concerned with are: , { } (dictionary), () (tuple), and ' " (string). Note that all string start with a single quote and end with a double quote (to reduce complexity in your solution).
  • The program must print out the structures starting with the innermost first and ending with the outermost
  • Once you start getting structure close characters (e.g. ] } " ) you can ignore any new start character structures (e.g. [ } ' )
  • You must also identify an errors in the closing order for the structures. Once you identify an error, print an Error message and stop processing

Input String: {[(abc)}]

Output: Characters inside a tuple inside a ERROR

  • The input string may have spaces and other erroneous control characters. These can all be ignored and should not print an error message (i.e. only closing characters above that are out of place will cause an error message to be printed).
  • The innermost characters will either be reported as a "number" or as "characters" based on their value. Numbers will have only 0-9 and a-z and A-Z in the inner set makes the value "characters."
  • In addition to having erroneous characters in the input string, there will also be random digits and characters in the input string. These are not reported and can be ignored. Only the innermost nested characters matter for this exercise.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python Code:

inpstr = input('Enter the string: ') #get the string from the user
charList = [] #to store the brackets
shouldappend = True #flag to check closing brackets
ischar = False
i=-1
for char in inpstr: #iterating throught each characters
i+=1
if (ord(char) >= 65 and ord(char) <= 90) or (ord(char) >= 97 and ord(char) <= 122): #check if it is a letter
ischar = True
elif (ord(char) >= 48 and ord(char) <= 57): #check if it is a number
pass
if (i+1 < len(inpstr)) and shouldappend == True: #checking for last character
if ((ord(char) >= 48 and ord(char) <= 57) or (ord(char) >= 65 and ord(char) <= 90) or (ord(char) >= 97 and ord(char) <= 122)) and ((ord(inpstr[i+1]) < 65 or ord(inpstr[i+1]) > 90) and (ord(inpstr[i+1]) < 97 or ord(inpstr[i+1]) > 122) and (ord(inpstr[i+1]) < 48 or ord(inpstr[i+1]) > 57)): #check if it is a number
shouldappend = False
if ischar == False:
print('Numbers ', end='')
else:
print('Characters ', end='')
if (char == '(' or char == '{' or char == '[' or char == "'") and shouldappend == True: #check if it is opening bracket
charList.append(char)
elif char == ')': #check if it is closing bracket
shouldappend = False
stackchar = charList.pop(len(charList)-1) #getting the last element of the list
if stackchar != '(': #checking if there is proper opening bracket
print ('inside a ERROR ', end='') #if there isnt printing error then exiting
break
else:
print ('inside a tuple ', end='') #print the output
elif char == '}': #similarly we check for "}"
shouldappend = False
stackchar = charList.pop(len(charList)-1)
if stackchar != '{':
print ('inside a ERROR ', end='')
break
else:
print ('inside a dictionary ', end='')
elif char == ']': #similarly we check for "]"
shouldappend = False
stackchar = charList.pop(len(charList)-1)
if stackchar != '[':
print ('inside a ERROR ', end='')
break
else:
print ('inside a list ', end='')
elif char == '"': #similarly we check for "
shouldappend = False
stackchar = charList.pop(len(charList)-1)
if stackchar != "'":
print ('inside a ERROR ', end='')
break
else:
print ('inside a string ', end='')


Sample Output:

Enter the string: {[(avc)]}
Characters inside a tuple inside a list inside a dictionary

Add a comment
Know the answer?
Add Answer to:
This program will require you to create a basic parser for an input string that is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that determines if a string (passed as a command-line argument) has all unique...

    Write a program that determines if a string (passed as a command-line argument) has all unique characters. The program must print the number of times each existing character appears, and then print whether or not they are all unique. Special requirements: You are NOT allowed to modify the input string or create any additional data structures (no arrays, lists, dictionaries, or other strings). You may have only one string (the one received as the command-line argument). You may have one...

  • Part 1. Write a program that takes a string as input, strips whitespace and punctuation from...

    Part 1. Write a program that takes a string as input, strips whitespace and punctuation from the words, and converts them to lowercase. Hint: The string module provides strings named whitespace, which contains space, tab, newline, etc., and punctuation which contains the punctuation characters. Let’s see if we can make Python swear: >>> import string >>> print string.punctuation !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ Also, you might consider using the string methods strip, replace and translate (Chapter 2 of "Introducing Python" is a great reference...

  • For Phton language 3. You are to create a decoder program that takes a tweet as...

    For Phton language 3. You are to create a decoder program that takes a tweet as input and turns it into an English sentence. A tweet will contain common acronyms and abbreviations, and your program will need to have a way to decode them (dictionary). For example) “lol” => “laughing out loud”, “brb” => “be right back” Provided is a file with common tweet abbreviations and associated meanings, your program is to use this csv file (tweet_decoder.csv) to create its...

  • Write a C program to “de-vowel” an input string. Assume that the user provides input containing...

    Write a C program to “de-vowel” an input string. Assume that the user provides input containing only the characters a through z (i.e., all lowercase letters). Your program should create an output string that deletes all vowels from the input string, pushing the letters together to fill any gaps. For example, given the input “theturtleandthehare” your code should print out “thtrtlndthhr”. Your program should create an output string from the input string, before printing its output. Sample Input: Enter a...

  • Write a program that uses a recursive function to determine whether a string is a character-unit...

    Write a program that uses a recursive function to determine whether a string is a character-unit palindrome. Moreover, the options for doing case sensitive comparisons and not ignoring spaces are indicated with flags. For example "A nut for a jar of tuna" is a palindrome if spaces are ignored and not otherwise. "Step on no pets" is a palindrome whether spaces are ignored or not, but is not a palindrome if it is case sensitive. Background Palindromes are character sequences...

  • Questions 1. How to create a comment in python? 2. The way to obtain user input...

    Questions 1. How to create a comment in python? 2. The way to obtain user input from command line 3. List standard mathematical operators in python and explain them 4. List comparison operators in python 5. Explain while loop. Give example 6. Explain for loop. Give example 7. How to create infinite loop? And how to stop it? 8. Explain a built-in function ‘range’ for ‘for’ loop 9. Explain break statement 10. Explain continue statement 11. Explain pass statement 12....

  • Create a program that performs the following operations: 1. Prompt for and accept a string of...

    Create a program that performs the following operations: 1. Prompt for and accept a string of up to 80 characters from the user. • The memory buffer for this string is created by: buffer: .space 80 #create space for string input The syscall to place input into the buffer looks like: li $v0,8 # code for syscall read_string la $a0, buffer #tell syscall where the buffer is li $a1, 80 # tell syscall how big the buffer is syscall 2....

  • please use pythom Write a program that ask the user for a string as input. It...

    please use pythom Write a program that ask the user for a string as input. It should duplicate of all the characters in the string and print it back out to the user. For example: AbC123 would be printed out as AAbbCC112233 Write a program that takes two lists and displays the items that occur in both lists. For example: ["a", "b", "c"] ["c", "a", "d"] would display a and c

  • VERY URGENT*** THANK YOU IN ADVANCE: THIS IS THE CODE I HAVE GOTTEN SO FAR: PYTHON...

    VERY URGENT*** THANK YOU IN ADVANCE: THIS IS THE CODE I HAVE GOTTEN SO FAR: PYTHON This is a code that is supposed to help someone study/ practice for jeopardy. The two functions described below are required for this assignment. You may add other functions that you think are appropriate: menu() This function, which displays all the user options to the screen, prompt the user for their choice and returns their choice. This function will verify user input and ALWAYS...

  • Part I Create a Java program that inputs a grade from the user. The grade input...

    Part I Create a Java program that inputs a grade from the user. The grade input from the user will be an integer. Once the input is stored, use an if-else-if block of code to determine the letter grade of the inputted integer grade. Do not use a bunch of if statements by themselves to solve this problem. You will print an error message for inputs greater than 100 and for inputs less than 0. Both errors must be handled...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT