开发者

match a BEGIN and END in antlr

开发者 https://www.devze.com 2023-02-19 18:02 出处:网络
开发者_开发知识库how can I say to antlr if you see a \'BEGIN\' then at this line you must see an \'END\'?
开发者_开发知识库

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
    ;   


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消