0

I am working on a fun little language using regex matching lines in a file. Here is what I have so far:

import re

code=open("code.txt", "r").read()

outputf=r'output (.*)'
inputf=r'(.*) = input (.*)'
intf=r'int (.*) = (\d)'
floatf=r'float (.*) = (\d\.\d)'
outputq=re.match(outputf, code)
if outputq:
    print "Executing OUTPUT query"
    exec "print %s" %outputq.group(1)

inputq=re.match(inputf, code)
if inputq:
    print "Executing INPUT query"
    exec "%s=raw_input(%s)"%(inputq.group(1), inputq.group(2))

intq=re.match(intf, code)
if intq:
    exec "%s = %s"%(intq.group(1), intq.group(2))
    exec "print %s"%(intq.group(1))
else:
    print "Invalid syntax"

The code works in matching say:

int x = 1

But it will only match the first line and stop matching and ignore the rest of the code that I want to match. How can I match every line in the file to my regex definitions?

  • 3
    Your question is unclear to me, please try to focus on explaining *what* you want to achieve, not on *how* you're trying to do so. Please post the relevant code *only* (i.e. if you're problem is regex matching - post only the relevant lines that deals with the regex and the input). – Nir Alfasi Nov 15 '15 at 17:39
  • 1
    I am not sure what you are trying to do either, but if you're playing with programming languages you might want to know that regular expressions are poorly suited for that - see https://en.wikipedia.org/wiki/Chomsky_hierarchy#The_hierarchy -- you might want to consider using a grammar or at least take a look at this: http://nedbatchelder.com/text/python-parsers.html – lorenzog Nov 15 '15 at 17:43

2 Answers2

3

.read() reads as one line, use .split("\n") on the .read() code or use .readlines().

Then iterate over the lines and test for your commands. At the moment you take the whole code as one single line. You want to check all lines line by line.

EDIT:

for that, create a function

then read lines with readlines()

And finally iterate over lines, using the function on lines

Like that:

import re

outputf=r'output (.*)'
inputf=r'(.*) = input (.*)'
intf=r'int (.*) = (\d)'
floatf=r'float (.*) = (\d\.\d)'

def check_line(line):
    outputq=re.match(outputf, line)
    if outputq:
        print ("Executing OUTPUT query")
        exec ("print (%s)" % outputq.group(1))

    inputq=re.match(inputf, line)
    if inputq:
        print ("Executing INPUT query")
        exec ("%s=raw_input(%s)"%(inputq.group(1), inputq.group(2)))

    intq=re.match(intf, line)
    if intq:
        exec ("%s = %s"%(intq.group(1), intq.group(2)))
        exec ("print (%s)"%(intq.group(1)))
    else:
        print ("Invalid syntax")


code=open("code.txt", "r").readlines()

for line in code:
    check_line(line)

This code will still return an error, which has nothing to do with the issue tho, think about if you do the assigning of the variable correctly.

weidler
  • 676
  • 1
  • 6
  • 22
  • could you help out by writing a bit out? –  Nov 15 '15 at 17:42
  • return _compile(pattern, flags).match(string) TypeError: expected string or buffer –  Nov 15 '15 at 17:58
  • Sure, u have to replace the "code" var in the `match()` with line, edited – weidler Nov 15 '15 at 18:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95194/discussion-between-aaron-gill-braun-and-t-w). –  Nov 15 '15 at 23:19
1

You're using re.match() which means that your regex has to match the whole string (which in this case is the whole file). If you iterate over each line in the file, then .match() will work. Alternatively you might want to look at re.search(), re.findall() and other similar alternatives.

It looks like your code needs to iterate over the lines in the file: How to iterate over the file in python

Community
  • 1
  • 1
Harry Harrison
  • 591
  • 4
  • 12