开发者

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
    ;   


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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜