Exemplo n.º 1
0
def generate():
    code = ''
    for product in GRAMMAR:
        code += '%sbool p_%s() {\n' % (tab(), product)
        for alternative in GRAMMAR[product]:
            code += '%sif(%s) {\n' % (tab(2), Terminals(alternative[0]).cond())
            for symbol in alternative:
                if is_term(symbol):
                    code += '%saccept(%s);\n' % (tab(3), symbol)
                elif symbol.endswith('*'):
                    symbol = non_term_name(symbol)
                    code += '%swhile(%s) {\n' % (
                        tab(3), Terminals(non_term_name(symbol)).cond())
                    code += '%sp_%s();\n' % (tab(4), symbol)
                    code += '%s}\n' % tab(3)
                elif symbol.endswith('?'):
                    symbol = non_term_name(symbol)
                    code += '%sif(%s) p_%s();\n' % (tab(
                        3), Terminals(non_term_name(symbol)).cond(), symbol)
                else:
                    code += '%sp_%s();\n' % (tab(3), symbol)
                if product in ACTIONS and symbol in ACTIONS[product]:
                    code += ACTIONS[product][symbol] + '\n'
            code += '%sreturn true;\n%s}\n' % (tab(3), tab(2))
        code += '%sreturn true;\n%s}\n\n' % (tab(2), tab(1))
    print(CODE % code)
Exemplo n.º 2
0
def generate():
    code = ''
    for product in GRAMMAR:
        code += '%sbool p_%s() {\n' % (tab(), product)
        for alternative in GRAMMAR[product]:
            code += '%sif(%s) {\n' % (tab(2), Terminals(alternative[0]).cond())
            for symbol in alternative:
                if is_term(symbol):
                    code += '%saccept(%s);\n' % (tab(3), symbol)
                elif symbol.endswith('*'):
                    symbol = non_term_name(symbol)
                    code += '%swhile(%s) {\n' % (tab(3), Terminals(non_term_name(symbol)).cond())
                    code += '%sp_%s();\n' % (tab(4), symbol)
                    code += '%s}\n' % tab(3)
                elif symbol.endswith('?'):
                    symbol = non_term_name(symbol)
                    code += '%sif(%s) p_%s();\n' % (tab(3), Terminals(non_term_name(symbol)).cond(), symbol)
                else:
                    code += '%sp_%s();\n' % (tab(3), symbol)
                if product in ACTIONS and symbol in ACTIONS[product]:
                    code += ACTIONS[product][symbol] + '\n'
            code += '%sreturn true;\n%s}\n' % (tab(3), tab(2))
        code += '%sreturn true;\n%s}\n\n' % (tab(2), tab(1))
    print(CODE % code)
Exemplo n.º 3
0
    if(SemanticCheck().parse(code)) {
        FILE* out = fopen("code.s", "w");
        std::cout << CodeGen(out).parse(code) << std::endl;
        fclose(out);
        return 1;
    }

    return 0;
}

#endif'''

# appended after symbol is accepted
ACTIONS = {
    'class_def': {
        'CLASS': '%sclassName = curr().strVal();' % tab(2),
        'RBRACE': '%sclassName = "";' % tab(2)
    },
    'method': {
        'TYPE': '%smethodName = curr().strVal();' % tab(2),
        'block': '%smethodName = "";' % tab(2)
    },
    'block': {
        'LBRACE': '%sblockNo++;' % tab(2)
    }
}


def generate():
    code = ''
    for product in GRAMMAR:
Exemplo n.º 4
0
    if(SemanticCheck().parse(code)) {
        FILE* out = fopen("code.s", "w");
        std::cout << CodeGen(out).parse(code) << std::endl;
        fclose(out);
        return 1;
    }

    return 0;
}

#endif'''

# appended after symbol is accepted
ACTIONS = {
    'class_def': {
        'CLASS': '%sclassName = curr().strVal();' % tab(2),
        'RBRACE': '%sclassName = "";' % tab(2)
    },
    'method': {
        'TYPE': '%smethodName = curr().strVal();' % tab(2),
        'block': '%smethodName = "";' % tab(2)
    },
    'block': {
        'LBRACE': '%sblockNo++;' % tab(2)
    }
}

def generate():
    code = ''
    for product in GRAMMAR:
        code += '%sbool p_%s() {\n' % (tab(), product)
Exemplo n.º 5
0
def generate():
    forward_decl_code = ''
    for product in GRAMMAR:  # forward definitions
        forward_decl_code += '    virtual bool p_%s();\n' % product

    print(HEADER) % (forward_decl_code)

    for product in GRAMMAR:
        code = 'bool Parser::p_%s() {\n' % product
        #code += '    puts("%s");\n' % product
        for alternative in GRAMMAR[product]:
            code += '%sif(%s) {\n' % (tab(1), Terminals(alternative[0]).cond())
            for symbol in alternative:
                if is_term(symbol):
                    code += '%sif(!accept(%s)) return false;\n' % (tab(2),
                                                                   symbol)
                elif symbol.endswith('*'):
                    symbol = non_term_name(symbol)
                    code += '%swhile(%s) {\n' % (
                        tab(2), Terminals(non_term_name(symbol)).cond())
                    code += '%sif(!p_%s()) return false;\n' % (tab(3), symbol)
                    code += '%s}\n' % tab(2)
                elif symbol.endswith('?'):
                    symbol = non_term_name(symbol)
                    code += '%sif((%s) && !p_%s()) return false;\n' % (tab(
                        2), Terminals(non_term_name(symbol)).cond(), symbol)
                else:
                    code += '%sif(!p_%s()) return false;\n' % (tab(2), symbol)
                if product in ACTIONS and symbol in ACTIONS[product]:
                    code += ACTIONS[product][symbol] + '\n'
            code += '%sreturn true;\n%s}\n' % (tab(2), tab(1))
        code += '%sPARSER_ERR("expected %s but %%s found", next->toString());\n' % (
            tab(1), ' or '.join(Terminals(product).terms))
        code += '%sreturn false;\n}\n' % tab(1)
        print(code)
    print(FOOTER)
Exemplo n.º 6
0
    'args_tail': [['COMMA', 'rval']],
    'rval': [['rval_tail', 'arithm_tail?']],
    'alloc': [['NEW', 'TYPE']],
    'rval_tail': [['factor', 'logic_tail?']],
    'factor': [['LNOT', 'rval'], ['BNOT', 'rval'],
               ['LPARENTH', 'rval', 'RPARENTH'], ['cast'], ['const'], ['ptr'],
               ['call'], ['THIS']],
    'cast': [['CAST', 'LT', 'TYPE', 'GT', 'LPARENTH', 'rval', 'RPARENTH']],
    'logic_tail': [['logic_op', 'rval_tail']],
    'arithm_tail': [['arithm_op', 'rval']],
}

# for error reporting, appended after symbol is accepted
ACTIONS = {
    'class_def': {
        'CLASS': '%sif(peek(TYPE)) className = curr().strVal();' % tab(2),
        'RBRACE': '%sclassName = "";' % tab(2)
    },
    'method': {
        'TYPE':
        '%sif(peek(ID)) methodName = curr().strVal(); blockNo = 0;' % tab(2),
        'block':
        '%smethodName = "";' % tab(2)
    },
    'block': {
        'LBRACE': '%sblockNo++;' % tab(2)
    }
}

HEADER = '''/* autogenerated by parser.py */
#ifndef __PARSER_H__
Exemplo n.º 7
0
def generate():
    forward_decl_code = ''
    for product in GRAMMAR: # forward definitions
        forward_decl_code += '    virtual bool p_%s();\n' % product

    print(HEADER) % (forward_decl_code)

    for product in GRAMMAR:
        code = 'bool Parser::p_%s() {\n' % product
        #code += '    puts("%s");\n' % product
        for alternative in GRAMMAR[product]:
            code += '%sif(%s) {\n' % (tab(1), Terminals(alternative[0]).cond())
            for symbol in alternative:
                if is_term(symbol):
                    code += '%sif(!accept(%s)) return false;\n' % (tab(2), symbol)
                elif symbol.endswith('*'):
                    symbol = non_term_name(symbol)
                    code += '%swhile(%s) {\n' % (tab(2), Terminals(non_term_name(symbol)).cond())
                    code += '%sif(!p_%s()) return false;\n' % (tab(3), symbol)
                    code += '%s}\n' % tab(2)
                elif symbol.endswith('?'):
                    symbol = non_term_name(symbol)
                    code += '%sif((%s) && !p_%s()) return false;\n' % (tab(2), Terminals(non_term_name(symbol)).cond(), symbol)
                else:
                    code += '%sif(!p_%s()) return false;\n' % (tab(2), symbol)
                if product in ACTIONS and symbol in ACTIONS[product]:
                    code += ACTIONS[product][symbol] + '\n'
            code += '%sreturn true;\n%s}\n' % (tab(2), tab(1))
        code += '%sPARSER_ERR("expected %s but %%s found", next->toString());\n' % (tab(1), ' or '.join(Terminals(product).terms))
        code += '%sreturn false;\n}\n' % tab(1)
        print(code)
    print(FOOTER)
Exemplo n.º 8
0
    'rval': [['rval_tail', 'arithm_tail?']],
    'alloc': [['NEW', 'TYPE']],
    'rval_tail': [['factor', 'logic_tail?']],
    'factor': [['LNOT', 'rval'], ['BNOT', 'rval'],
        ['LPARENTH', 'rval', 'RPARENTH'],
        ['cast'], ['const'], ['ptr'], ['call'], ['THIS']],
    'cast': [['CAST', 'LT', 'TYPE', 'GT', 'LPARENTH', 'rval', 'RPARENTH']],
    'logic_tail': [['logic_op', 'rval_tail']],
    'arithm_tail': [['arithm_op', 'rval']],
}

# for error reporting, appended after symbol is accepted
ACTIONS = {
    'class_def': {
        'CLASS': '%sif(peek(TYPE)) className = curr().strVal();' % tab(2),
        'RBRACE': '%sclassName = "";' % tab(2)
    },
    'method': {
        'TYPE': '%sif(peek(ID)) methodName = curr().strVal(); blockNo = 0;' % tab(2),
        'block': '%smethodName = "";' % tab(2)
    },
    'block': {
        'LBRACE': '%sblockNo++;' % tab(2)
    }
}

HEADER = '''/* autogenerated by parser.py */
#ifndef __PARSER_H__
#define __PARSER_H__