Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

an ANTLR Grammar for a statement or an exit statement

I wrote down the following statement in an ANTLR grammar:

loopStatement
    : 'loop'  (statement|exit)* 'end' 'loop' ';'
    ;

If I understand correctly, (statement|exit)* means that I can have a statement or an exit statement. That is i.e statement_1 exit_1, or statement_1, or statement_1 statement_2, exit_1, right?
My parser works, besides when there's no the statement.
For example:

this works:

loop
x:=x+1;   <<< statement_1
exit when x=9; <<<<exit_1
end loop;

this works as well (no exit):

loop
x:=x+1;   <<< statement_1
          <<<<exit_1  (no exit)
end loop;

but this DOES NOT work (no statement):

loop
          <<< statement_1
exit when x=9; <<<<exit_1
end loop;

Is there anything wrong with my grammar?

like image 449
pantelis Avatar asked Nov 29 '25 15:11

pantelis


1 Answers

pantelis wrote:

If I understand correctly, (statement|exit)* means that I can have a statement or an exit statement.

To be precise, (statement|exit)* matches the empty string, or zero or more statement or exit statements (in no particular order!). So it'd match:

  • statement statement statement ...
  • exit exit exit ...
  • exit exit statement statement exit ...
  • ...

But, why not make your exit statement just a regular statement? I small demo:

loopStatement
  :  'loop'  statement* 'end' 'loop' ';'
  ;

statement
  :  'exit' 'when' expression ';' // exit statement
  |  ID ':=' expression ';'       // assignment
  ;

expression
  :  equalityExpression
  ;

equalityExpression
  :  addExpression ('=' addExpression)*
  ;

addExpression
  :  atom ('+' atom)*
  ;

atom
  :  ID
  |  Number
  |  '(' expression ')'
  ;

ID
  :  'a'..'z'+
  ;

Number
  :  '0'..'9'+
  ;

which properly parses all your 3 examples:


1

loop
  x:=x+1;
  exit when x=9;
end loop;

enter image description here


2

loop
  x:=x+1;

end loop;

enter image description here


3

loop

  exit when x=9;
end loop;

enter image description here


4

Or nothing at all:

loop

end loop;

enter image description here

like image 70
Bart Kiers Avatar answered Dec 02 '25 03:12

Bart Kiers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!