Flex newline scanning for bison

前端 未结 2 1859
醉梦人生
醉梦人生 2021-01-16 04:02

I\'d like to use the same flex/bison scanner/parser for an interpreter and for loading a file to be interpreted. I can not get the newline parsing to work correctly in both

2条回答
  •  北恋
    北恋 (楼主)
    2021-01-16 04:36

    If pressing ENTER terminates a command then the lexer should return a token for \n. Returning 0 tells the parser the input source is complete (end of file for a file or ^D for a terminal). Add an end-of-line token to your grammar and have the lexer return that when it sees \n.

    ETA: But don't forget to handle the case of the last line not ending in ENTER. Have your lexer return an end-of-line token at the end of file unless the last character is \n.

提交回复
热议问题