当前位置:网站首页>Antlr4 uses keywords as identifiers

Antlr4 uses keywords as identifiers

2022-07-06 11:15:00 Antivirus Alchemist

When defining lexical files , You need to define keywords first , Rules for redefining identifiers , For example, the following example

// Match keyword CREATE
K_CREATE: 'CREATE' ;
// Match identifier -- A string of alphanumeric underscores , But it must start with a letter or an underscore 
ID: [a-zA-Z_]+[a-zA-Z0-9_]* ;

In the grammar file , We define the syntax of a variable name , The variable name rule is the identifier

varName: ID ;

There's a problem : When using "CREATE" As varName Time cannot be recognized , Although it satisfies morphology "ID" The rules of , as a result of "CREATE" It is recognized as morphology "K_CREATE"

How to use keywords as identifiers

Just add the keywords you need to use under the corresponding rules , as follows

varName: ID | K_CREATE ;
原网站

版权声明
本文为[Antivirus Alchemist]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131633484641.html