parsingbnfpratt-parser

How do I avoid infinite recursion when parsing a comparison in form of "expression < expression"


I want to parse a comparison in the following BNF:

expression ::= comparison | number | string | "(" expression ")"
comparison ::= ( expression ( ">" | "<" | ">=" | "<=" | "==" | "!=" ) expression ) 

Because I can't know whether there will be a comparison operator following the expression, I thought of first trying to parse a comparison. This code will call the function for parsing an expression again, which will then result in an infinite loop. How do handle this problem properly?


Solution

  • So at the end I solved it by having a function expression which calls another function expression_without_infix. Then it tests if the next operator is an infix operator and parses it accordingly.