'time' occurence in the code

Dec 22, 2009 at 10:46 AM

Hello,

I am parsing code in which the time is expressed in the following format : HH:MM

I need to be able to identify any 'time' occurence in the code. For example:

IF $v_heure < 23:59 THEN
BEGIN
 // Do something...
 SET $v_heure TO $v_heure_ref - 23:59
END
ELSE
BEGIN
 // Do something...
END

I think I need to overload a Terminal for this purpose, but I'm not sure how to proceed.

Thank you in advance for our help,

Nicolas

Coordinator
Dec 22, 2009 at 7:09 PM

Hi

I you are going to create an AST tree, I think the simples way is to define a NonTerminal for time with custom AstNodeCreator method:

var timeLiteral = new NonTerminal("TimeLiteral", createTimeLiteral);  

ntTime.Rule = Number + ":" + Number

In node creator method create a LiteralValueNode with appropriate time value computed from components.

You can go with custom terminal, but then you need to make sure regular Number literal has lower priority than your custom time terminal, so that time term gets the first chance to recognize the input. This looks a bit inefficient as all regular numbers would have to be recognized twice - first by time terminal, and if it fails, regular Number terminal does the job.

Hope this explains it

Roman

 

Dec 23, 2009 at 8:21 AM

Hi

I already try this method but the rule conflicted with the switch statement.

for example:

SWITCH variable$
BEGIN
    CASE 5 :
  // code
 CASE  12:
 // code
END

May be I can try with RegexBasedTerminal?

thanks

Nicolas

 

 

Coordinator
Dec 23, 2009 at 9:12 PM

You have to formulate clear disambiguation rule before you proceed. Whether you do it with NonTerminal or with custom terminal, you need a formal method to distinguish a time value from Case value. You should probably go with custom terminal and add checking of previous token (available through Context.PreviousToken) - check if it is "case" keyword and make decision based on that