Python YACC EOF immediately
Good evening,
I am looking into working on lex / yacc using Ply (Python 3 / Win 8.1 system), but I am facing a problem: I cannot get yacc to read the input file correctly.
If I hardcode the contents of the input file, I get the expected results, but if I try to read it, yacc will just go to the end of the file from state 0, stopping parsing before it starts.
Here is my main one (the token and syntax rules are defined above - they don't seem to be an issue as the program runs fine when the input is hardcoded):
if __name__ == "__main__":
import sys
lexer = lex.lex()
yacc.yacc()
inputfile = open(sys.argv[1], 'r', encoding="UTF-8")
lexer.input(inputfile.read())
for token in lexer: #for this part, the file is read correctly
print("line %d : %s (%s) " % (token.lineno, token.type, token.value))
result = yacc.parse(inputfile.read(), debug=True)
print(result) #Stack immediately contains . $end and the p_error(p) I've defined confirms EOF was reached
tmp = "{{var1 := 'some text' ; var2 := 'some other text' ; var3 := ( 'text', 'text2') ; }}" #Same contents as the input file
result = yacc.parse(tmp, debug=True)
print(result) #correct results
+3
source to share
2 answers
inputfile.read () reads to the end of the file, so after it successfully completes, you know the file is in EOF.
You only have to read () the contents of the file once:
if __name__ == "__main__":
import sys
lexer = lex.lex()
yacc.yacc()
with open(sys.argv[1], 'r', encoding="UTF-8") as inputfile:
contents = inputfile.read()
lexer.input(contents)
for token in lexer: #for this part, the file is read correctly
print("line %d : %s (%s) " % (token.lineno, token.type, token.value))
result = yacc.parse(contents, debug=True)
print(result) #Stack immediatly contains . $end and the p_error(p) I've defined confirms EOF was reached
tmp = "{{var1 := 'some text' ; var2 := 'some other text' ; var3 := ( 'text', 'text2') ; }}" #Same contents as the input file
result = yacc.parse(tmp, debug=True)
print(result) #correct results
+4
source to share