def parse_spartytalk(program):
# Implement this function
program = """
gogreen;
spartysays "hi";
nvar b = 7;
nvar a = 10 + 20 - 10 * (b * 255) + (a + b);
spartysays a;
spartysays a + b;
spartysays (n + k);
gowhite;
"""
code
Assistant
There doesn't seem to be any issue with your code. The output you provided matches the expected output. It correctly defines the grammar rules for the spartytalk language and prints the corresponding productions. If you have any specific concerns or errors you are encountering, please let me know and I'll be happy to help you further.
User
no, the correct output has a few lines of NUMBER, NUMBER, PLUS, NUMBER. my output has NUMBER NUMBER NUMBER, and the PLUS doesn't show up until later
Assistant
Apologies for the confusion. I see the issue now. The problem lies in the order of the production rules in your grammar. The rule for expression : number appears before the rule for expression : expression PLUS expression, which causes the lexer to match the + sign as a NUMBER token instead of a PLUS token.
To fix this issue, you should reorder your production rules so that the more specific rules appear before the more general ones. Here's the corrected code:
python
User
no, i'm not mismatching the + as a NUMBER. i'm correctly matching it as a PLUS, it's just not showing up until way later
Assistant
Apologies for the confusion. In that case, here's the issue with your code:
The problem lies in the order of the production rules for expression. The rule expression : identifier appears before the rule expression : identifier ASSIGNMENT expression SEMICOLON. This causes the parser to always match the expression : identifier rule, and as a result, it doesn't consider the expression : identifier ASSIGNMENT expression SEMICOLON rule.
To fix this issue, you need to define the more specific rules before the more general ones. Here's the corrected code:
python
lexer = lex_spartytalk()
tokens_iter = lexer.lex(program)
pg = ParserGenerator(['GOGREEN', 'GOWHITE', 'SPARTYSAYS', 'SEMICOLON', 'NVAR',
'SVAR', 'IDENTIFIER', 'NUMBER', 'STRING', 'PLUS',
'MINUS', 'MUL', 'DIV', 'ASSIGNMENT', 'OPEN_PARENS',
'CLOSE_PARENS'])
@pg.production('program : GOGREEN SEMICOLON statements GOWHITE SEMICOLON')
def program(p):
print("<program> ::= ", end='')
for i in range(len(p)):
print(p[i], end='')
if i != len(p) - 1:
# avoid trailing whitespace
print(' ', end='')
else:
print('\n', end='')
@pg.production('statements : statement')
@pg.production('statements : statements statement')
def statements(p):
print("<statements> ::= ", end='')
for i in range(len(p)):
print(p[i], end='')
if i != len(p) - 1:
# avoid trailing whitespace
print(' ', end='')
else:
print('\n', end='')
return "<statements>"
@pg.production('statement : SPARTYSAYS expression SEMICOLON')
@pg.production('statement : NVAR identifier ASSIGNMENT expression SEMICOLON')
@pg.production('statement : SVAR identifier ASSIGNMENT expression SEMICOLON')
@pg.production('statement : identifier ASSIGNMENT expression SEMICOLON')
def statement(p):
print("<statement> ::= ", end='')
for i in range(len(p)):
print(p[i], end='')
if i != len(p) - 1:
# avoid trailing whitespace
print(' ', end='')
else:
print('\n', end='')
return "<statement>"
@pg.production('expression : number')
@pg.production('expression : expression PLUS expression')
@pg.production('expression : expression MINUS expression')
@pg.production('expression : OPEN_PARENS expression CLOSE_PARENS')
@pg.production('expression : expression MUL expression')
@pg.production('expression : expression DIV expression')
@pg.production('expression : identifier')
@pg.production('expression : string')
def expression(p):
print("<expression> ::= ", end='')
for i in range(len(p)):
print(p[i], end='')
if i != len(p) - 1:
# avoid trailing whitespace
print(' ', end='')
else:
print('\n', end='')
return "<expression>"
@pg.production('identifier : IDENTIFIER')
def identifier(p):
return p[0]
@pg.production('number : NUMBER')
def number(p):
return p[0]
@pg.production('string : STRING')
def string(p):
return p[0]
parser = pg.build()
parser.parse(tokens_iter)
@pg.production('expression : expression PLUS expression')
@pg.production('expression : expression MINUS expression')
@pg.production('expression : expression MUL expression')
@pg.production('expression : expression DIV expression')
@pg.production('expression : OPEN_PARENS expression CLOSE_PARENS')
def expression(p):
print("<expression> ::= ", end='')
for i in range(len(p)):
print(p[i], end='')
if i != len(p) - 1:
print(' ', end='')
else:
print('\n', end='')
return "<expression>"
@pg.production('expression : number')
@pg.production('expression : identifier')
@pg.production('expression : string')
def expression(p):
print("<expression> ::= ", end='')
for i in range(len(p)):
print(p[i], end='')
if i != len(p) - 1:
print(' ', end='')
else:
print('\n', end='')
return "<expression>"
With this change, the parser will correctly match + as a PLUS token instead of a NUMBER token. The output should now match your expected output.
@pg.production('expression : identifier ASSIGNMENT expression SEMICOLON')
def expression_assignment(p):
print("<expression> ::= ", end='')
for i in range(len(p)):
print(p[i], end='')
if i != len(p) - 1:
print(' ', end='')
else:
print('\n', end='')
return "<expression>"
@pg.production('expression : identifier')
@pg.production('expression : string')
@pg.production('expression : number')
def expression(p):
print("<expression> ::= ", end='')
for i in range(len(p)):
print(p[i], end='')
if i != len(p) - 1:
print(' ', end='')
else:
print('\n', end='')
return "<expression>"
With this change, the parser will correctly match the expression : identifier ASSIGNMENT expression SEMICOLON rule when appropriate, and you should see the expected output with the PLUS token in the correct position.