Python Forum
Cobol code to English like language/Identify ENDIF for correspoding IF in a string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cobol code to English like language/Identify ENDIF for correspoding IF in a string
#1
HI All,

I have a requirement where I get an excel spreadsheet with small Cobol code in a cell.
Sample Code :

If studentGrade >= 90 Then
2 resultLabel.Text = "A" ' display "A"
3 Else
4 If studentGrade >= 80 Then
5 resultLabel.Text = "B" ' display "B"
6 Else
7 If studentGrade >= 70 Then
8 resultLabel.Text = "C" ' display "C"
9 Else
10 If studentGrade >= 60 Then
11 resultLabel.Text = "D" ' display "D"
12 Else
13 resultLabel.Text = "F" ' display "F"
14 End If
15 End If
16 End If
17 End If

the output should be in a plain English paragraph.
Output Should be: If student grade is greater than 90 the set text as A. If student grade is greater than 80 then set text as B and so on. Need to take care of starting if and ending if condition.
The main idea of this is that for a non-programmer to understand what exactly is happening in the code.
Could you please let me know how this can be done in python.

Thank you
Venkat
Reply
#2
This looks like VBA code... In COBOL End If would be END-IF (not case sensitive)
Anyway, we are not writing code for you. Show your code in code tags, post any traceback in error tags and ask specific question.
Reply
#3
Hi,

Here is the code which I have written to read data from excel and do some validation.

---------------------------------------------------------------------------------------------
import openpyxl as opx
import re

#Define the function. This function will replace all the cobol code to business like language
def cobol_business_english(cellvalue):
    print(cellvalue)

filename = "example.xlsx"
workbook = opx.load_workbook(filename)
sheet = workbook.get_sheet_by_name('Main')
maxrows = sheet.max_row
maxcol = sheet.max_column

for i in range(2, maxrows + 1):
    name = sheet.cell(row=i, column=1).value

    if name[0:4] >= '3000':
        a = sheet.cell(row=i, column=3).value
        c = re.sub('\s+',' ',a)
        cobol_business_english(c)
    else:
        b = sheet.cell(row=i, column=3).value
        cobol_business_english(b)
---------------------------------------------------------------------------------------------

Here is the data from one of the cell....This is a Cobol code
Output:
IF Condition-Test1 IF (Cond-Test2 OR Cond-Test3) IF (cond-test-code = '16') MOVE 'C' TO Cond-options END-IF END-IF END-IF
Using c = re.sub('\s+',' ',a) ----The above data becomes..
IF Condition-Test1 IF (Cond-Test2 OR Cond-Test3) IF (cond-test-code = '16') MOVE 'C' TO Cond-options END-IF END-IF END-IF

My Output should be:
IF Condition Test1 and Cond Test2 or Cond Test3 and cond test code is 16 then set cond options to 'C'.

Could you please guide me how to proceabove-mentionedbove mentioned output.
Reply
#4
I get a small code in the form of a string.

IF 'Physics'
'Passed in physics'
else
IF 'Chemistry'
IF 'Practicals'
'Passed in Chemistry'
else
'Failed in Chemistry'
ENDIF
else
'Passed in all'
ENDIF
ENDIF

I need to break this string into multiple values based on the IF and ENDIF. My output should be

1) IF 'Physics' then 'Passed in Physics'
- otherwise
2) IF 'Chemistry'
- If 'Practicals' then 'Passed in Chemistry'
-otherwise
'Failed in 'Chemistry'
-otherwise
'Passed in all'.

The string which I get will have nested IF, so need to identify and break it into multiple values.
Please let me know your suggestions on how to write code for this in python.

Thank you
Venkat
Reply
#5
That's not python!
no such thing as endif in python
Reply
#6
You can use a parsing module such as lrparsing
import lrparsing
from lrparsing import Keyword, Ref, THIS, Token

class FooParser(lrparsing.Grammar):
    class T(lrparsing.TokenRegistry):
        basic = Token(re=r"['][^']*[']")
        ident = Token(re="[A-Za-z_][A-Za-z_0-9]*")
        
    statement = Ref('statement')
    ifblock = (Keyword('IF') + statement + statement +
               Keyword('else') + statement + Keyword('ENDIF'))
    statement = T.basic | ifblock
    START = statement

data = """
IF 'Physics' 
'Passed in physics' 
else 
IF 'Chemistry' 
IF 'Practicals' 
'Passed in Chemistry' 
else 
'Failed in Chemistry' 
ENDIF 
else 
'Passed in all' 
ENDIF 
ENDIF
"""
parse_tree = FooParser.parse(data)
print(FooParser.repr_parse_tree(parse_tree))
Output:
(START (statement (ifblock 'IF' (statement "'Physics'") (statement "'Passed in physics'") 'else' (statement (ifblock 'IF' (statement "'Chemistry'") (statement (ifblock 'IF' (statement "'Practicals'") (statement "'Passed in Chemistry'") 'else' (statement "'Failed in Chemistry'") 'ENDIF')) 'else' (statement "'Passed in all'") 'ENDIF')) 'ENDIF')))
Reply
#7
Please, don't start new threads. Keep the discussion in the original thread
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  (python) Can i get some help fixing a English to Morse translator? Pls AlexPython 8 3,266 Dec-03-2024, 10:21 AM
Last Post: skylartyler
  using last 2 characters of a file name to identify it CAD79 5 1,858 Jul-12-2024, 02:09 PM
Last Post: deanhystad
  Need help to identify CNA for automation qatester 0 565 May-31-2024, 09:24 AM
Last Post: qatester
  identify not white pixels in bmp flash77 17 7,926 Nov-10-2023, 09:21 PM
Last Post: flash77
  write a program which prints the day in english ben1122 10 6,935 Jul-25-2021, 05:55 PM
Last Post: ben1122
  More non english characters johnboy1974 8 6,738 Apr-23-2021, 02:35 PM
Last Post: snippsat
  Need to identify only files created today. tester_V 5 7,443 Feb-18-2021, 06:32 AM
Last Post: tester_V
  logo language code into python aayushi98 2 84,972 Jan-26-2021, 09:02 PM
Last Post: Serafim
  Syntax Error : I can't identify what's wrong! caarsonr 11 8,483 Jun-10-2019, 11:18 PM
Last Post: Yoriz
  English interpretation of the following file handing snippet mortch 5 4,269 May-30-2019, 08:10 AM
Last Post: mortch

Forum Jump:

User Panel Messages

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