a re for asm file???

James J. Besemer jb at cascade-sys.com
Wed Oct 2 00:02:49 CEST 2002


jubafre at brturbo.com wrote:

>i have a file.asm
>	
>	LDA 	D1	;fdsfsd
>	ADD 	D2	;gfsf
>	STA 	D3	;jhgfj
>	HLT		;jgh
>D1:	DB 	3	;7
>D2:	DB 	2	;876
>D3:	DB 	0	;jh
>
>i don´t know to define a re to put in a list like this:
>
>in x just the labels
>x=['D1', 'D2','D3', '3','2','1]
>
>IN Y JUST THE INSTRUCTIONS
>Y=['LDA', 'ADD', 'HLT', 'DB','DB', 'DB']
>
>how can i make this re to find the labels and the instructions
>for a generic file.asm like the file above?????
>
Perform the following on each input line, "line":

        matches = re.match( "(\w+:)?\s+(\w+)(\s+(\w+))?(\s+;.*)?", line )
        if matches:
                label = matches.group(1)
                opcode = matches.group(2)
                args = matches.group(4)
                comments = matches.group(5)
        else:
                print "Syntax error"

If the pattern matches then the 4 variables are set to the appropriate fields.  Else the input does not conform to the language inferred from your example.  Missing optional fields are set to None.  You would filter them out as follows:

	if args:
	        x.append( args )

However, if you included the None values then all of your lists would remain aligned per the input.

Cheers.

--jb

-- 
James J. Besemer		503-280-0838 voice
2727 NE Skidmore St.		503-280-0375 fax
Portland, Oregon 97211-6557	mailto:jb at cascade-sys.com
				http://cascade-sys.com	








More information about the Python-list mailing list