I am making a basic syntax checker for a language i am trying to make. The lexical analyzer passes something to the function isIfStmt() to check if its valid like:
#IF#RPAREN...#LPAREN#NEWLINE...#VAR#ASSTOP#RPAREN...#LPAREN#NEWLINE...#END#SPACE#IF
This is the python code i wrote:
def isIfStmt(ifStmt):
ifObj = re.match(r'\#IF\#LPAREN(.*)\#RPAREN(.*)\#END\#SPACE\#IF', ifStmt)
print(ifObj)
if not isLogicExp(ifObj.group(1)):
return False
if not isStmtList(ifObj.group(2)):
return False
if ifObj==None:
return False
return True
The isLogicExp function checks if the logical expression supplied is correct. and isStmtList checks if the code inside is a valid set of simple statements that might include another if.
The problem is that this regular expression matches the last #LPAREN and I want it to match the one that is for the first left parenthesis in the if stmt line. But it matches the last one in the argument supplied.
if()
a=(10)
if i change the regex tor'\#IF\#LPAREN[^\NEWLINE]\#RPAREN(.*)\#END\#SPACE\#IF'
it doesn't work either.
If I try to use the non-greedy approach with #RPAREN, it has a problem that between the parenthesis, there can be more in the logicalExp inside. I tries using the non-greedy approach with #NEWLINE as,
r'\#IF\#LPAREN(.*)\#RPAREN?(#NEWLINE)(.*)\#END\#SPACE\#IF'
But it's not working either. I still match the last #RPAREN