Question

I have a treetop grammar with only two rules:

grammar RCFAE
    rule num
        [0-9]+ <Num>
    end

    rule identifier
        [a-zA-Z] [a-zA-Z]* <ID>
    end
end

I'm trying to parse simple strings ("A" and "5"). The "5" is recognized as a Num if I put that rule first, and returns nil if i put that rule second. Similarly, "A" is recognized as an ID if I put that rule first, and returns nil if I put that rule second. I can't understand how these two rules overlap in any way. It's driving me crazy!

Is there something I'm missing or don't understand about treetop or regular expressions? Thanks in advance for your help.

Was it helpful?

Solution

Treetop expects the first rule to be the "main rule". It doesn't try to apply all the rules you defined until one matches - it only applies the main rule and if that does not match, it fails.

To do what you want, you need to add a main rule which might be a num or an identifier, like this:

grammar RCFAE
    rule expression
        num / identifier
    end

    rule num
        [0-9]+ <Num>
    end

    rule identifier
        [a-zA-Z] [a-zA-Z]* <ID>
    end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top