<commit_before>import ply.lex as lex
import ply.yacc as yacc
import pprint

reserved_words = {
    'let': 'LET'
}

tokens = [
    "IDENTIFIER",
    "NUMBER",
    "OPERATOR",
    "LPAREN",
    "RPAREN"
] + list(reserved_words.values())

def t_IDENTIFIER(t):
    r'[a-zA-Z_][a-zA-Z_0-9]*'
    t.type = reserved_words.get(t.value, 'IDENTIFIER')
    return t

def t_NUMBER(t):
    r"\d+(\.\d+)*"
    try:
        t.value = int(t.value)
    except ValueError:
        t.value = float(t.value)
    return t
    
t_OPERATOR = r'[\+-\^%\*/]'
t_LPAREN = '\('
t_RPAREN = '\)'

def t_error(t):
    raise TypeError("Invalid character: {}".format(t.value[0]))

t_ignore = r' '
    
lex.lex()

def p_program(p):
    '''
    program : let_expression
            | expression
    '''
    p[0] = {
        'type': 'program',
        'value': p[1]
    }

def p_let_expression(p):
    '''
    let_expression : LPAREN LET IDENTIFIER expression RPAREN
    '''
    p[0] = {
        'type': 'let_expression',
        'identifier': p[3],
        'expression': p[4]
    }

def p_expression(p):
    '''
    expression : LPAREN expression expression OPERATOR RPAREN
               | NUMBER
               | IDENTIFIER
    '''
    if len(p) == 6:
        p[0] = {
            'type': 'expression',
            'operand1': p[2],
            'operand2': p[3],
            'operator': p[4]
        }
    elif type(p[1]) == str:
        p[0] = {
            'type': 'identifier',
            'value': p[1]
        }
    else:
        p[0] = {
            'type': 'number',
            'value': p[1]
        }

def p_error(p):
    raise TypeError("Syntax error at {}".format(p.value))

yacc.yacc()

def lex_and_parse(string_in):
    return yacc.parse(string_in)

<commit_msg>#2 - switch to prefix notation for clarity<commit_after>import ply.lex as lex
import ply.yacc as yacc
import pprint

reserved_words = {
    'let': 'LET'
}

tokens = [
    "IDENTIFIER",
    "NUMBER",
    "OPERATOR",
    "LPAREN",
    "RPAREN"
] + list(reserved_words.values())

def t_IDENTIFIER(t):
    r'[a-zA-Z_][a-zA-Z_0-9]*'
    t.type = reserved_words.get(t.value, 'IDENTIFIER')
    return t

def t_NUMBER(t):
    r"\d+(\.\d+)*"
    try:
        t.value = int(t.value)
    except ValueError:
        t.value = float(t.value)
    return t
    
t_OPERATOR = r'[\+-\^%\*/]'
t_LPAREN = '\('
t_RPAREN = '\)'

def t_error(t):
    raise TypeError("Invalid character: {}".format(t.value[0]))

t_ignore = r' '
    
lex.lex()

def p_program(p):
    '''
    program : let_expression
            | expression
    '''
    p[0] = {
        'type': 'program',
        'value': p[1]
    }

def p_let_expression(p):
    '''
    let_expression : LPAREN LET IDENTIFIER expression RPAREN
    '''
    p[0] = {
        'type': 'let_expression',
        'identifier': p[3],
        'expression': p[4]
    }

def p_expression(p):
    '''
    expression : LPAREN OPERATOR expression expression RPAREN
               | NUMBER
               | IDENTIFIER
    '''
    if len(p) == 6:
        p[0] = {
            'type': 'expression',
            'operand1': p[3],
            'operand2': p[4],
            'operator': p[2]
        }
    elif type(p[1]) == str:
        p[0] = {
            'type': 'identifier',
            'value': p[1]
        }
    else:
        p[0] = {
            'type': 'number',
            'value': p[1]
        }

def p_error(p):
    raise TypeError("Syntax error at {}".format(p.value))

yacc.yacc()

def lex_and_parse(string_in):
    return yacc.parse(string_in)


{'fraction_non_alphanumeric': 0.17022487902077996, 'fraction_numerical': 0.011670936521491603, 'mean_word_length': 1.916182572614108, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2159791', 'n_tokens_mistral': 1341, 'n_tokens_neox': 1313, 'n_words': 258}