我如何区分具有相同规则但在不同范围内的声明变量(ANTLR 4)?
How can i differentiate between the declared variables with the same rule but in different scope (ANTLR 4)?
我正在用 ANTLR 4 开发我自己的语言,这里是我的语法的一部分。
program: main EOF; // the program rule.
main: NEWLINE* study NEWLINE* ; // the main rule;
study : studyBlock (NEWLINE* assignVariableBlock)? ; // the study rule.
/* simple assign name = "name" */
simpleAssign: name = ID '=' value = (STRING | BOOLEAN | INTEGER | DOUBLE);
/* study parameters */
studyParameters: (| ( simpleAssign (',' simpleAssign)*) );
/* study block */
studyBlock: 'study' '(' studyParameters ')' NEWLINE ;
/* assign variables block */
assignVariableBlock: simpleAssign*;
在规则 studyParameters 和规则 assignVariableBlock 中使用了 simpleAssign 规则,所以如果我的 DSL 如下,我如何获取每个特定 block.For 示例中声明的变量。
study(string = "string", string2 = "string2")
x = "string3"
y = "string4"
我的听众怎么才能有
// study parameters
[string:"string",string2:"string2"] // map
// tmp variabels
[x:"string3",y:"string4"] // map
您可以检查上下文的 parent
变量:
@Override
public void enterSimpleAssign(YourParser.SimpleAssignContext ctx) {
if (ctx.parent instanceof YourParser.StudyParametersContext) {
// called from `studyParameters`
}
else {
// called from `assignVariableBlock`
}
}
我正在用 ANTLR 4 开发我自己的语言,这里是我的语法的一部分。
program: main EOF; // the program rule.
main: NEWLINE* study NEWLINE* ; // the main rule;
study : studyBlock (NEWLINE* assignVariableBlock)? ; // the study rule.
/* simple assign name = "name" */
simpleAssign: name = ID '=' value = (STRING | BOOLEAN | INTEGER | DOUBLE);
/* study parameters */
studyParameters: (| ( simpleAssign (',' simpleAssign)*) );
/* study block */
studyBlock: 'study' '(' studyParameters ')' NEWLINE ;
/* assign variables block */
assignVariableBlock: simpleAssign*;
在规则 studyParameters 和规则 assignVariableBlock 中使用了 simpleAssign 规则,所以如果我的 DSL 如下,我如何获取每个特定 block.For 示例中声明的变量。
study(string = "string", string2 = "string2")
x = "string3"
y = "string4"
我的听众怎么才能有
// study parameters
[string:"string",string2:"string2"] // map
// tmp variabels
[x:"string3",y:"string4"] // map
您可以检查上下文的 parent
变量:
@Override
public void enterSimpleAssign(YourParser.SimpleAssignContext ctx) {
if (ctx.parent instanceof YourParser.StudyParametersContext) {
// called from `studyParameters`
}
else {
// called from `assignVariableBlock`
}
}