问题描述
这是一个演示代码
label:
var id
let id = 10
goto label
如果允许关键字作为标识符将
If allowed keyword as identifier will be
let:
var var
let var = 10
goto let
这是完全合法的代码.但是在 antlr 中似乎很难做到这一点.
This is totally legal code. But it seems very hard to do this in antlr.
AFAIK,如果 antlr 匹配令牌 let,则永远不会回退到 id 令牌.所以对于antlr,它会看到
AFAIK, If antlr match a token let, will never fallback to id token. so for antlr it will see
LET_TOKEN :
VAR_TOKEN <missing ID_TOKEN>VAR_TOKEN
LET_TOKEN <missing ID_TOKEN>VAR_TOKEN = 10
虽然 antlr 允许 predicate,但我必须控制令牌匹配和问题.语法变成这样
although antlr allowed predicate, I have to control ever token match and problematic. grammar become this
grammar Demo;
options {
language = Go;
}
@parser::members{
var _need = map[string]bool{}
func skip(name string,v bool){
_need[name] = !v
fmt.Println("SKIP",name,v)
}
func need(name string)bool{
fmt.Println("NEED",name,_need[name])
return _need[name]
}
}
proj@init{skip("inst",false)}: (line? NL)* EOF;
line
: VAR ID
| LET ID EQ? Integer
;
NL: '\n';
VAR: {need("inst")}? 'var' {skip("inst",true)};
LET: {need("inst")}? 'let' {skip("inst",true)};
EQ: '=';
ID: ([a-zA-Z] [a-zA-Z0-9]*);
Integer: [0-9]+;
WS: [ \t] -> skip;
看起来好可怕.
但这在 peg 中很容易,请在 pegjs
But this is easy in peg, test this in pegjs
Expression = (Line? _ '\n')* ;
Line
= 'var' _ ID
/ 'let' _ ID _ "=" _ Integer
Integer "integer"
= [0-9]+ { return parseInt(text(), 10); }
ID = [a-zA-Z] [a-zA-Z0-9]*
_ "whitespace"
= [ \t]*
I actually done this in peggo and javacc.
我的问题是如何在 antlr4.6 中处理这些语法,我对 antlr4.6 go 目标感到非常兴奋,但似乎我为我的语法选择了错误的工具?
My question is how to handle these grammars in antlr4.6, I was so excited about the antlr4.6 go target, but seems I choose the wrong tool for my grammar ?
推荐答案
最简单的方法是为标识符定义解析器规则:
The simplest way is to define a parser rule for identifiers:
id: ID | VAR | LET;
VAR: 'var';
LET: 'let';
ID: [a-zA-Z] [a-zA-Z0-9]*;
然后在解析器规则中使用 id
而不是 ID
.
And then use id
instead of ID
in your parser rules.
另一种方法是使用ID
作为标识符和 关键字,并使用谓词来消除歧义.但它的可读性较差,所以我会使用第一种方式.
A different way is to use ID
for identifiers and keywords, and use predicates for disambiguation. But it's less readable, so I'd use the first way instead.
这篇关于Antlr4如何建立一个语法允许关键字作为标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!