Python Forum
Python for syntax conversion
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python for syntax conversion
#5
(Dec-20-2019, 06:59 PM)Gribouillis Wrote:
kingsman Wrote:(a) I am ten years old
I love eating apple
I am a boy

(b) Apple is my favourite fruit
I am a male
I have birthed 10 years
Here is a basic algorithmic scheme that you could follow for such a work
Output:
Description of the input language: it has 3 statements age_stm: syntax: "I am <number> years old" parameters: number: a number written in english eating_stm: syntax: "I love eating <food>" parameters: food: the name of some food gender_stm: syntax: "I am a <gender>" parameters: gender: boy or girl First step: PARSING OF THE INPUT FILE The aim of this step is to recognize the statements contained in the input file and their parameters. In the example, the result is a sequence of python objects age_stm(number = ten) eating_stm(food = apple) gender_stm(gender = boy) Second step: CODE GENERATION each parsed statement is translated in a statement of the second language according to a specific rule rule for an age_stm: convert the number in english to a number in digits emit the statement "I have birthed <digits> years" rule for a eating_stm: identify the type of the food (eg apple is a fruit) emit the statement "<food> is my favorite <food type>" rule for a gender_stm: convert the gender to the appropriate transcription (boy -> male) emit the statement "I am a <converted gender>" There may be many more rules, for example there may be rules to change the order of the emited statements, also a single input statement may induce more than one output statement, etc.

These are the codes I have create
File = open('First.txt', 'r')
thing = []
for line in File:
    thing.append(line)
first_stm = thing[0]
second_stm = thing[1]
third_stm = thing[2]
number = (first_stm.split()[2])
food = (second_stm.split()[3])
gender = (third_stm.split()[3])
File.close()


Conversion = open('First conversion', 'w+')
Conversion.write(food + ' is my favourite fruit\n')
if gender == 'boy':
    Conversion.write('I am a male')
else:
    Conversion.write('I am a female')
Conversion.close()
For the first statement, I need to convert ten to 10.
But there are various situations, the age can be 11, 13 ,46, 57, 65
So, how can I convert it from a string to digit.
For the third statement, the case is just boy or girl, It does not similar to the first statement that having so many options.

How about imitate the third statement using 'if' statement
for example
if number == 'one'
Conversion.write('I have birthed 1 years')
elif number == 'two'
Conversion.write('I have birthed 2 years')
If I use this method, I need to use 'if' for many time such as 100 time to satisfy the reality case(100 years old)
Is there any easier method to deal with it?
Reply


Messages In This Thread
Python for syntax conversion - by kingsman - Dec-20-2019, 02:52 PM
RE: Python for syntax conversion - by ichabod801 - Dec-20-2019, 03:58 PM
RE: Python for syntax conversion - by kingsman - Dec-20-2019, 05:33 PM
RE: Python for syntax conversion - by Gribouillis - Dec-20-2019, 06:59 PM
RE: Python for syntax conversion - by kingsman - Dec-21-2019, 04:49 PM
RE: Python for syntax conversion - by Gribouillis - Dec-21-2019, 05:16 PM
RE: Python for syntax conversion - by kingsman - Dec-22-2019, 10:02 AM
RE: Python for syntax conversion - by buran - Dec-21-2019, 05:47 PM
RE: Python for syntax conversion - by Gribouillis - Dec-22-2019, 10:58 AM
RE: Python for syntax conversion - by kingsman - Dec-23-2019, 04:44 PM
RE: Python for syntax conversion - by Gribouillis - Dec-23-2019, 05:13 PM
RE: Python for syntax conversion - by kingsman - Dec-24-2019, 11:55 AM
RE: Python for syntax conversion - by Gribouillis - Dec-24-2019, 01:03 PM
RE: Python for syntax conversion - by kingsman - Dec-26-2019, 12:48 PM
RE: Python for syntax conversion - by Gribouillis - Dec-26-2019, 05:50 PM
RE: Python for syntax conversion - by kingsman - Dec-27-2019, 01:07 PM
RE: Python for syntax conversion - by Gribouillis - Dec-27-2019, 01:47 PM
RE: Python for syntax conversion - by kingsman - Dec-27-2019, 02:05 PM
RE: Python for syntax conversion - by Gribouillis - Dec-27-2019, 02:15 PM
RE: Python for syntax conversion - by kingsman - Dec-27-2019, 03:49 PM
RE: Python for syntax conversion - by kingsman - Apr-27-2020, 09:26 AM
RE: Python for syntax conversion - by Gribouillis - Dec-27-2019, 04:18 PM
RE: Python for syntax conversion - by kingsman - Dec-27-2019, 04:26 PM
RE: Python for syntax conversion - by kingsman - Apr-27-2020, 03:24 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python format conversion bluefrog 2 2,790 Jul-22-2018, 03:49 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020