3
votes

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?

1
You are talking about a grammar, not a regular expression - Travis Webb
edit your title too; a lot of people know regex, but not EBNF. I added the EBNF tag for you - Travis Webb
There is very likely a conflict with the rest of the grammar. It would be helpful if you added the rest of it, or at least the definitions of 'statement' and 'exit'. - Erez

1 Answers

3
votes

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