pantelis
pantelis

Reputation: 113

match a BEGIN and END in antlr

how can I say to antlr if you see a 'BEGIN' then at this line you must see an 'END'?

here is my code ( i only need the BEGIN/END when i have multiple statements)

whileStatement
    : 'WHILE' expression 'DO'
         'BEGIN'?
               statement
         'END'?
    ;

and my statements

statement
    :   assignmentStatement
    |   ifStatement
    |   doLoopStatement
    |   whileStatement
    |   procedureCallStatement
    ;   

Upvotes: 1

Views: 444

Answers (1)

Fred Foo
Fred Foo

Reputation: 363858

No experience with ANTLR, but generally in BNF/context-free grammars you'd express this as

whileStatement
    : 'WHILE' expr 'DO'
      statementBlock
    ;

statementBlock
    : statement
    | 'BEGIN' statement* 'END'
    ;

or add statementBlock as an alternative in the definition of statement.

Upvotes: 2

Related Questions