samedi 21 décembre 2019

Change way to detect end of block ply yacc

I'm doing a python to c++ translater. Right now I'm finishing my syntaxical grammar and I'm having a little problem with my if-else block. The thing is that I can't figure out to detect end of block with the tabulations. Right now I got this file to test :

print(33)
if a<3:
    print(test);
b=5

It gives me the following result:

Program
|  print
|  |  '33'
|  if
|  |  < (2)
|  |  |  'a'
|  |  |  '3'
|  |  Program
|  |  |  print
|  |  |  |  'test'
|  =
|  |  'b'
|  |  '5'

Which is correct, beause b=5 is at the same level as my if. Here are my lex tokens :

reserved_words = (
    'if',
    'print',
    'range',
    'for',
    'in',
    'while'
)

tokens = (
    'COMPARATOR',
    'IDENTIFIER',
    'ILLEGAL',
    'FLOAT',
    'INT',
    'EQU',
    'ENTER',
    'POINTS',
    'TAB',
    'END'
    ) + tuple(map(lambda s:s.upper(),reserved_words))



literals = ';():\s'

def t_ENTER(t):
    r'\n'
    return t



def t_ADD_OP(t):
    r'\+|-'
    return t

def t_POINTS(t):
    r':'
    return t

def t_EQU(t):
    r'\='
    return t    

def t_MUL_OP(t):
    r'\*|/'
    return t

def t_COMPARATOR(t):
    r'[<>]'
    return t


def t_INT(t):
    #r'\d+(?!\.)(?![a-zA-Z])'
    r'\b(?<!\.)\d+(?!\.)\b'
    try:
        t.value = t.value   
    except ValueError:
        print ("Line %d: Problem while parsing %s!" % (t.lineno,t.value))
        t.value = 0
    return t

def t_ILLEGAL(t):
    r'\d+[a-zA-z]+'
    try:
        t.value = t.value   
    except ValueError:
        print ("Line %d: Problem while parsing %s!" % (t.lineno,t.value))
        t.value = 0
    return t

def t_FLOAT(t):
    r'\d+\.{1}\d+'
    try:
        t.value = float(t.value)   
    except ValueError:
        print ("Line %d: Problem while parsing %s!" % (t.lineno,t.value))
        t.value = 0.0
    return t

def t_IDENTIFIER(t):
    r'[A-Za-z_]\w*'
    if t.value in reserved_words:
        t.type = t.value.upper()
    return t

def t_TAB(t):
    r'[ \t]{4}'
    return t

def t_newline(t):
    r'\n+'
    t.lexer.lineno += len(t.value)


def t_error(t):
    print ("Illegal character '%s'" % t.value[0])
    t.lexer.skip(1)

t_ignore = ' '

lex.lex()

And my rules :

    vars = {}

def p_programme_statement(p):
    ''' programme : statement  '''
    p[0] = AST.ProgramNode(p[1])

def p_programme_recursive(p):
    ''' programme : statement ENTER programme '''
    p[0] = AST.ProgramNode([p[1]]+p[3].children)

def p_statement(p):
    ''' statement : assignation
                        | structure '''
    p[0] = p[1]

def p_expression_num_or_var(p):
    '''expression : INT
        | FLOAT 
        | IDENTIFIER 
        '''
    p[0] = AST.TokenNode(p[1])

def p_statement_print(p):
    ''' statement : PRINT '(' expression ')' '''
    p[0] = AST.PrintNode(p[3])

def p_expression_comp(p):
    ''' expression : expression COMPARATOR expression'''
    p[0] = AST.OpNode(p[2],[p[1],p[3]])

def p_structure_if(p):
    '''structure : IF expression POINTS ENTER programme ';' '''
    p[0] = AST.IfNode([p[2],p[5]])

def p_structure_while(p):
    ''' structure : WHILE expression POINTS ENTER programme '''
    p[0] = AST.WhileNode([p[2],p[5]])

def p_expression_paren(p):
    '''expression : '(' expression ')' '''
    p[0] = p[2]

def p_assign_block(p):
    ''' assignation : TAB IDENTIFIER EQU expression'''
    p[0] = AST.AssignNode([AST.TokenNode(p[2]),p[4]])

def p_assign(p):
    ''' assignation : IDENTIFIER EQU expression '''
    p[0] = AST.AssignNode([AST.TokenNode(p[1]),p[3]])

def p_error(p):
    print ("Syntax error in line %d" % p.lineno)
    yacc.errok()

The thing is : my p_Structure_if is wrong because right now I say that end of block is when the ';' appears. How can I do to detect end of block by checking tabulations (if next line not at same tabulation level of the other, then it's not same block and then not using ';' characthers?

Aucun commentaire:

Enregistrer un commentaire