Python Forum

Full Version: Cobol code to English like language/Identify ENDIF for correspoding IF in a string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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.
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.
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
That's not python!
no such thing as endif in python
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')))
Please, don't start new threads. Keep the discussion in the original thread