The simplest rules inoperative

I have a vertex grammar with two rules:

grammar RCFAE rule num [0-9]+ <Num> end rule identifier [a-zA-Z] [a-zA-Z]* <ID> end end 

I am trying to parse simple strings ("A" and "5"). "5" is recognized as Num if I first set this rule and returns nil if I set this rule second. Similarly, “A” is recognized as an identifier if I set this rule first, and returns zero if I put this rule second. I cannot understand how these two rules partially overlap. It drives me crazy!

Is there something I am missing or not understanding about the tops of camels or regular expressions? Thanks in advance for your help.

+4
source share
1 answer

Treetop expects the first rule to be the "basic rule." He does not try to apply all the rules that you have defined until they match - he applies only the basic rule, and if this does not match, it fails.

To do what you want, you need to add a basic rule, which can be a number or an identifier, for example:

 grammar RCFAE rule expression num / identifier end rule num [0-9]+ <Num> end rule identifier [a-zA-Z] [a-zA-Z]* <ID> end end 
+6
source

Source: https://habr.com/ru/post/1382196/


All Articles