在 Antlr 中定义语法

发布于 2024-10-09 15:25:36 字数 1554 浏览 0 评论 0原文

我定义了以下语法。

grammar Sample_1;


@header {
  package a;
}

@lexer::header {
  package a;
}

program
    :   
        define*
        implement*
    ;


define
    :   IDENT '=(' INTEGER',' INTEGER ')'
    ;

implement
    :IDENT '=(' (IDENT ','?)* ')'
    ;

fragment LETTER : ('a'..'z' | 'A'..'Z') ;
fragment DIGIT : '0'..'9';
INTEGER : DIGIT+ ;
IDENT : LETTER (LETTER | DIGIT)*;
WS : (' ' | '\t' | '\n' | '\r' | '\f')+ {$channel = HIDDEN;};
COMMENT : '//' .* ('\n'|'\r') {$channel = HIDDEN;};

如何检查此语法,以便当我有示例时

A=(1,1)
B=(1,2)

G=(A,B)

结果成功,但如果我编写

A=(1,1)
B=(1,2)

G=(A,E)

它会给出 E 未定义的错误 感谢

结果: 我成功了,非常感谢:

grammar Sample_1;

@members{
    int level=0;
}

@header {
  package a;
}

@lexer::header {
  package a;
}

program
    :   
        block
    ;
block   
scope {
    List symbols;
}
@init {
    $block::symbols=new ArrayList();
    level++;
}
@after { 
     System.err.println("Hello");
     level--;
 }
    : (define* implement+)
    ;

define
    :   IDENT {$block::symbols.add($IDENT.text);} '=(' INTEGER',' INTEGER ')' 
    ;

implement
    :IDENT '=(' (a=IDENT 
    {if (!$block::symbols.contains($a.text)){
    System.err.println("undefined");
    }}','?)* ')'
    ;

fragment LETTER : ('a'..'z' | 'A'..'Z') ;
fragment DIGIT : '0'..'9';
INTEGER : DIGIT+ ;
IDENT : LETTER (LETTER | DIGIT)*;
WS : (' ' | '\t' | '\n' | '\r' | '\f')+ {$channel = HIDDEN;};
COMMENT : '//' .* ('\n'|'\r') {$channel = HIDDEN;};

I have defined the following grammar.

grammar Sample_1;


@header {
  package a;
}

@lexer::header {
  package a;
}

program
    :   
        define*
        implement*
    ;


define
    :   IDENT '=(' INTEGER',' INTEGER ')'
    ;

implement
    :IDENT '=(' (IDENT ','?)* ')'
    ;

fragment LETTER : ('a'..'z' | 'A'..'Z') ;
fragment DIGIT : '0'..'9';
INTEGER : DIGIT+ ;
IDENT : LETTER (LETTER | DIGIT)*;
WS : (' ' | '\t' | '\n' | '\r' | '\f')+ {$channel = HIDDEN;};
COMMENT : '//' .* ('\n'|'\r') {$channel = HIDDEN;};

How to check in this grammar so that when I have the example

A=(1,1)
B=(1,2)

G=(A,B)

the result is successful but if I write

A=(1,1)
B=(1,2)

G=(A,E)

it gives an error that E is not defined
thanks

the result:
i got it working thanks a lot:

grammar Sample_1;

@members{
    int level=0;
}

@header {
  package a;
}

@lexer::header {
  package a;
}

program
    :   
        block
    ;
block   
scope {
    List symbols;
}
@init {
    $block::symbols=new ArrayList();
    level++;
}
@after { 
     System.err.println("Hello");
     level--;
 }
    : (define* implement+)
    ;

define
    :   IDENT {$block::symbols.add($IDENT.text);} '=(' INTEGER',' INTEGER ')' 
    ;

implement
    :IDENT '=(' (a=IDENT 
    {if (!$block::symbols.contains($a.text)){
    System.err.println("undefined");
    }}','?)* ')'
    ;

fragment LETTER : ('a'..'z' | 'A'..'Z') ;
fragment DIGIT : '0'..'9';
INTEGER : DIGIT+ ;
IDENT : LETTER (LETTER | DIGIT)*;
WS : (' ' | '\t' | '\n' | '\r' | '\f')+ {$channel = HIDDEN;};
COMMENT : '//' .* ('\n'|'\r') {$channel = HIDDEN;};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄人书 2024-10-16 15:25:36

Antlr 支持动作,即嵌入语法文件中的小代码片段。

分配的操作可以存储到映射中。右侧 IDENT 的操作可以尝试从映射中提取值,如果失败则抛出异常。

Terrence Parr 的“The Definitive ANTLR Reference”第 6 章介绍了操作。

Antlr supports actions, little snippets of code embedded in the grammar file.

An action for an assignment could store into a map. An action for a right-hand-side IDENT could try to pull a value from the map, and throw an exception if it fails.

Chapter 6 in Terrence Parr's "The Definitive ANTLR Reference" covers actions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文