I am developing a programming language and am having problems with condition statements. Here is the code in my language:
x = 4 ->
? 2 > 5 <?
x = 7 ->
?> ->
[o] <- x ->
Here is a specific part of the grammar that defines the conditions:
post_condition_evaluation_block : post_condition_evaluation_block_opening_operator compound_statement post_condition_evaluation_block_closing_operator
condition_statement : condition_specification_operator expression post_condition_evaluation_block
| condition_specification_operator expression post_condition_evaluation_block condition_extension
There is nothing wrong with the grammar; the code is working fine. The problem is that the expression 2 > 5is evaluated after the next expression x = 7, so it prints 7 instead of 4 (which is incorrect, because the expression evaluates to false). I deal with this problem by counting condition blocks:
condition_blocks = {0: True}
current_condition_block = 0
And then, when it comes to approving a condition:
def p_condition_statement(p):
"""condition_statement : condition_specification_operator expression post_condition_evaluation_block
| condition_specification_operator expression post_condition_evaluation_block condition_extension"""
global current_condition_block
current_condition_block += 1
condition_blocks[current_condition_block] = p[2]
print(condition_blocks)
It adds the value False (p 2 ) of the expression to the corresponding block index in the dictionary. The problem is that when I get the assignment:
def p_assignment(p):
"""assignment : identifier assignment_operator expression"""
if len(p) == 4 and condition_blocks[current_condition_block]:
if p[2] == '=':
identifiers[p[1]] = parse_object(p[3])
elif p[2] == "+=":
identifiers[p[1]] += parse_object(p[3])
elif p[2] == "-=":
identifiers[p[1]] -= parse_object(p[3])
elif p[2] == "*=":
identifiers[p[1]] *= parse_object(p[3])
elif p[2] == "/=":
identifiers[p[1]] /= parse_object(p[3])
p[0] = (p[1], p[2], p[3])
, , " ". / , , .
, , /YACC, , , , , , , ... , , , .